file_name
stringlengths
5
52
name
stringlengths
4
95
original_source_type
stringlengths
0
23k
source_type
stringlengths
9
23k
source_definition
stringlengths
9
57.9k
source
dict
source_range
dict
file_context
stringlengths
0
721k
dependencies
dict
opens_and_abbrevs
listlengths
2
94
vconfig
dict
interleaved
bool
1 class
verbose_type
stringlengths
1
7.42k
effect
stringclasses
118 values
effect_flags
sequencelengths
0
2
mutual_with
sequencelengths
0
11
ideal_premises
sequencelengths
0
236
proof_features
sequencelengths
0
1
is_simple_lemma
bool
2 classes
is_div
bool
2 classes
is_proof
bool
2 classes
is_simply_typed
bool
2 classes
is_type
bool
2 classes
partial_definition
stringlengths
5
3.99k
completed_definiton
stringlengths
1
1.63M
isa_cross_project_example
bool
1 class
FStar.OrdSet.fst
FStar.OrdSet.all_means_not_any_not
val all_means_not_any_not (#a:eqtype) (#f:cmp a) (s: ordset a f) (c: condition a) : Lemma (all s c = not (any s (inv c)))
val all_means_not_any_not (#a:eqtype) (#f:cmp a) (s: ordset a f) (c: condition a) : Lemma (all s c = not (any s (inv c)))
let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 97, "end_line": 530, "start_col": 0, "start_line": 530 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.OrdSet.ordset a f -> c: FStar.OrdSet.condition a -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.all s c = Prims.op_Negation (FStar.OrdSet.any s (FStar.OrdSet.inv c)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.condition", "FStar.OrdSet.simple_induction", "Prims.b2t", "Prims.op_Equality", "Prims.bool", "FStar.OrdSet.all", "Prims.op_Negation", "FStar.OrdSet.any", "FStar.OrdSet.inv", "Prims.unit" ]
[]
false
false
true
false
false
let all_means_not_any_not #a #f s c =
simple_induction (fun p -> all p c = not (any p (inv c))) s
false
FStar.OrdSet.fst
FStar.OrdSet.find_first_is_some_iff_any
val find_first_is_some_iff_any (#a:eqtype) (#f:cmp a) (s:ordset a f) (c: condition a) : Lemma (Some? (find_first s c) = any s c)
val find_first_is_some_iff_any (#a:eqtype) (#f:cmp a) (s:ordset a f) (c: condition a) : Lemma (Some? (find_first s c) = any s c)
let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 105, "end_line": 536, "start_col": 0, "start_line": 536 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.OrdSet.ordset a f -> c: FStar.OrdSet.condition a -> FStar.Pervasives.Lemma (ensures Some? (FStar.OrdSet.find_first s c) = FStar.OrdSet.any s c)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.condition", "FStar.OrdSet.simple_induction", "Prims.b2t", "Prims.op_Equality", "Prims.bool", "FStar.Pervasives.Native.uu___is_Some", "FStar.OrdSet.find_first", "FStar.OrdSet.any", "Prims.unit" ]
[]
false
false
true
false
false
let find_first_is_some_iff_any #_ #_ s c =
simple_induction (fun p -> Some? (find_first p c) = any p c) s
false
FStar.OrdSet.fst
FStar.OrdSet.liat_size
val liat_size (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) : Lemma (size (liat s) = ((size s)-1))
val liat_size (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) : Lemma (size (liat s) = ((size s)-1))
let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 80, "end_line": 543, "start_col": 0, "start_line": 542 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.OrdSet.ordset a f {s <> FStar.OrdSet.empty} -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.size (FStar.OrdSet.liat s) = FStar.OrdSet.size s - 1)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "Prims.b2t", "Prims.op_disEquality", "Prims.list", "Prims.Nil", "FStar.OrdSet.base_induction", "Prims.op_Equality", "Prims.int", "FStar.OrdSet.size", "FStar.OrdSet.liat", "Prims.op_Subtraction", "Prims.bool", "Prims.unit", "Prims.l_True", "Prims.squash", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let liat_size #a #f (s: ordset a f {s <> []}) : Lemma (size (liat s) = size s - 1) =
base_induction (fun p -> if p <> [] then size (liat p) = size p - 1 else true) s
false
FStar.OrdSet.fst
FStar.OrdSet.find_last
val find_last (#a:eqtype) (#f:cmp a) (s: ordset a f) (c: condition a) : (z:option a{ match z with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)) })
val find_last (#a:eqtype) (#f:cmp a) (s: ordset a f) (c: condition a) : (z:option a{ match z with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)) })
let find_last #a #f s c = find_last_props s c; find_last' s c
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 16, "end_line": 572, "start_col": 0, "start_line": 570 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.OrdSet.ordset a f -> c: FStar.OrdSet.condition a -> z: FStar.Pervasives.Native.option a { (match z with | FStar.Pervasives.Native.None #_ -> Prims.op_Negation (FStar.OrdSet.any s c) | FStar.Pervasives.Native.Some #_ v -> FStar.OrdSet.any s c /\ (forall (x: a{FStar.OrdSet.mem x s && c x}). f x v)) <: Type0 }
Prims.Tot
[ "total" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.condition", "FStar.OrdSet.find_last'", "Prims.unit", "FStar.OrdSet.find_last_props", "FStar.Pervasives.Native.option", "Prims.b2t", "Prims.op_Negation", "FStar.OrdSet.any", "Prims.l_and", "Prims.l_Forall", "Prims.op_AmpAmp", "FStar.OrdSet.mem" ]
[]
false
false
false
false
false
let find_last #a #f s c =
find_last_props s c; find_last' s c
false
FStar.OrdSet.fst
FStar.OrdSet.find_last_props
val find_last_props (#a #f: _) (s: ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x: a{mem x s && c x}). f x v)))) (decreases size s)
val find_last_props (#a #f: _) (s: ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x: a{mem x s && c x}). f x v)))) (decreases size s)
let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 56, "end_line": 568, "start_col": 0, "start_line": 560 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.OrdSet.ordset a f -> c: FStar.OrdSet.condition a -> FStar.Pervasives.Lemma (ensures ((match FStar.OrdSet.find_last' s c with | FStar.Pervasives.Native.None #_ -> Prims.op_Negation (FStar.OrdSet.any s c) | FStar.Pervasives.Native.Some #_ v -> FStar.OrdSet.any s c /\ (forall (x: a{FStar.OrdSet.mem x s && c x}). f x v)) <: Type0)) (decreases FStar.OrdSet.size s)
FStar.Pervasives.Lemma
[ "lemma", "" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.condition", "Prims.op_GreaterThan", "FStar.OrdSet.size", "FStar.OrdSet.any_if_mem", "Prims.bool", "FStar.OrdSet.any_liat", "Prims.unit", "FStar.OrdSet.find_last_props", "FStar.OrdSet.liat_size", "FStar.Pervasives.Native.tuple2", "Prims.b2t", "Prims.op_Equality", "FStar.Pervasives.Native.Mktuple2", "FStar.OrdSet.liat", "FStar.OrdSet.last", "FStar.OrdSet.unsnoc", "Prims.l_True", "Prims.squash", "FStar.OrdSet.find_last'", "Prims.op_Negation", "FStar.OrdSet.any", "Prims.l_and", "Prims.l_Forall", "Prims.op_AmpAmp", "FStar.OrdSet.mem", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec find_last_props #a #f (s: ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x: a{mem x s && c x}). f x v)))) (decreases size s) =
if size s > 0 then let liat, last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c
false
FStar.OrdSet.fst
FStar.OrdSet.union_mem_forall
val union_mem_forall (#a #f: _) (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2))
val union_mem_forall (#a #f: _) (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2))
let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 51, "end_line": 610, "start_col": 0, "start_line": 607 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (ensures forall (x: a). FStar.OrdSet.mem x (FStar.OrdSet.union s1 s2) = (FStar.OrdSet.mem x s1 || FStar.OrdSet.mem x s2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.Classical.forall_intro", "Prims.b2t", "Prims.op_Equality", "Prims.bool", "FStar.OrdSet.mem", "FStar.OrdSet.union", "Prims.op_BarBar", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern", "FStar.OrdSet.mem_union", "Prims.l_Forall" ]
[]
false
false
true
false
false
let union_mem_forall #a #f (s1: ordset a f) (s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) =
let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux
false
FStar.OrdSet.fst
FStar.OrdSet.find_first_precedes_any_other
val find_first_precedes_any_other (#a:eqtype) (#f:cmp a) (s:ordset a f) (c: condition a) (x:a{mem x s && c x}) : Lemma (Some? (find_first s c) && f (Some?.v (find_first s c)) x)
val find_first_precedes_any_other (#a:eqtype) (#f:cmp a) (s:ordset a f) (c: condition a) (x:a{mem x s && c x}) : Lemma (Some? (find_first s c) && f (Some?.v (find_first s c)) x)
let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 13, "end_line": 540, "start_col": 0, "start_line": 538 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.OrdSet.ordset a f -> c: FStar.OrdSet.condition a -> x: a{FStar.OrdSet.mem x s && c x} -> FStar.Pervasives.Lemma (ensures Some? (FStar.OrdSet.find_first s c) && f (Some?.v (FStar.OrdSet.find_first s c)) x)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.condition", "Prims.b2t", "Prims.op_AmpAmp", "FStar.OrdSet.mem", "FStar.OrdSet.set_props", "Prims.unit", "Prims.op_disEquality", "FStar.OrdSet.head", "FStar.OrdSet.find_first_precedes_any_other", "FStar.OrdSet.tail", "Prims.bool" ]
[ "recursion" ]
false
false
true
false
false
let rec find_first_precedes_any_other #a #f s c x =
if head s <> x then find_first_precedes_any_other (tail s) c x; set_props s
false
FStar.OrdSet.fst
FStar.OrdSet.find_last'
val find_last' (#a #f: _) (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s)
val find_last' (#a #f: _) (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s)
let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 26, "end_line": 558, "start_col": 0, "start_line": 553 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.OrdSet.ordset a f -> c: FStar.OrdSet.condition a -> Prims.Tot (FStar.Pervasives.Native.option a)
Prims.Tot
[ "total", "" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.condition", "Prims.op_Equality", "FStar.OrdSet.empty", "FStar.Pervasives.Native.None", "Prims.bool", "FStar.Pervasives.Native.Some", "FStar.OrdSet.find_last'", "FStar.Pervasives.Native.option", "Prims.unit", "FStar.OrdSet.liat_size", "FStar.Pervasives.Native.tuple2", "Prims.b2t", "FStar.Pervasives.Native.Mktuple2", "FStar.OrdSet.liat", "FStar.OrdSet.last", "FStar.OrdSet.unsnoc" ]
[ "recursion" ]
false
false
false
false
false
let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) =
if s = empty then None else let liat, last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c
false
FStar.OrdSet.fst
FStar.OrdSet.find_last_is_some_iff_any
val find_last_is_some_iff_any (#a:eqtype) (#f:cmp a) (s:ordset a f) (c: condition a) : Lemma (Some? (find_last s c) = any s c)
val find_last_is_some_iff_any (#a:eqtype) (#f:cmp a) (s:ordset a f) (c: condition a) : Lemma (Some? (find_last s c) = any s c)
let find_last_is_some_iff_any #a #f s c = find_last_props s c
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 61, "end_line": 574, "start_col": 0, "start_line": 574 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.OrdSet.ordset a f -> c: FStar.OrdSet.condition a -> FStar.Pervasives.Lemma (ensures Some? (FStar.OrdSet.find_last s c) = FStar.OrdSet.any s c)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.condition", "FStar.OrdSet.find_last_props", "Prims.unit" ]
[]
true
false
true
false
false
let find_last_is_some_iff_any #a #f s c =
find_last_props s c
false
FStar.OrdSet.fst
FStar.OrdSet.find_last_follows_any_other
val find_last_follows_any_other (#a:eqtype) (#f:cmp a) (s:ordset a f) (c: condition a) (x:a{mem x s && c x}) : Lemma (Some? (find_last s c) && f x (Some?.v (find_last s c)))
val find_last_follows_any_other (#a:eqtype) (#f:cmp a) (s:ordset a f) (c: condition a) (x:a{mem x s && c x}) : Lemma (Some? (find_last s c) && f x (Some?.v (find_last s c)))
let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 21, "end_line": 579, "start_col": 0, "start_line": 576 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.OrdSet.ordset a f -> c: FStar.OrdSet.condition a -> x: a{FStar.OrdSet.mem x s && c x} -> FStar.Pervasives.Lemma (ensures Some? (FStar.OrdSet.find_last s c) && f x (Some?.v (FStar.OrdSet.find_last s c)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.condition", "Prims.b2t", "Prims.op_AmpAmp", "FStar.OrdSet.mem", "FStar.OrdSet.find_last_props", "Prims.unit", "FStar.OrdSet.find_last_is_some_iff_any", "FStar.OrdSet.any_if_mem" ]
[]
true
false
true
false
false
let find_last_follows_any_other #a #f s c x =
any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c
false
FStar.OrdSet.fst
FStar.OrdSet.union_with_empty
val union_with_empty (#a #f: _) (s: ordset a f) : Lemma (union s empty = s)
val union_with_empty (#a #f: _) (s: ordset a f) : Lemma (union s empty = s)
let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 58, "end_line": 613, "start_col": 0, "start_line": 612 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.union s FStar.OrdSet.empty = s)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.eq_lemma", "FStar.OrdSet.union", "FStar.OrdSet.empty", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.b2t", "Prims.op_Equality", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) =
eq_lemma (union s empty) s
false
FStar.OrdSet.fst
FStar.OrdSet.size_of_intersect
val size_of_intersect (#a:eqtype) (#f:cmp a) (s1 s2: ordset a f) : Lemma (ensures size (intersect s1 s2) = count s1 (mem_of s2) /\ size (intersect s1 s2) = count s2 (mem_of s1))
val size_of_intersect (#a:eqtype) (#f:cmp a) (s1 s2: ordset a f) : Lemma (ensures size (intersect s1 s2) = count s1 (mem_of s2) /\ size (intersect s1 s2) = count s2 (mem_of s1))
let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1)
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 39, "end_line": 605, "start_col": 0, "start_line": 600 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.size (FStar.OrdSet.intersect s1 s2) = FStar.OrdSet.count s1 (FStar.OrdSet.mem_of s2) /\ FStar.OrdSet.size (FStar.OrdSet.intersect s1 s2) = FStar.OrdSet.count s2 (FStar.OrdSet.mem_of s1))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.count_is_size_of_where", "FStar.OrdSet.mem_of", "Prims.unit", "FStar.OrdSet.intersect_is_symmetric", "FStar.OrdSet.intersect_eq_where" ]
[]
true
false
true
false
false
let size_of_intersect #_ #_ s1 s2 =
intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1)
false
FStar.OrdSet.fst
FStar.OrdSet.intersect_eq_where
val intersect_eq_where (#a:eqtype) (#f:cmp a) (s1 s2:ordset a f) : Lemma (intersect s1 s2 = where s1 (mem_of s2))
val intersect_eq_where (#a:eqtype) (#f:cmp a) (s1 s2:ordset a f) : Lemma (intersect s1 s2 = where s1 (mem_of s2))
let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2)
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 64, "end_line": 592, "start_col": 0, "start_line": 591 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.intersect s1 s2 = FStar.OrdSet.where s1 (FStar.OrdSet.mem_of s2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.same_members_means_eq", "FStar.OrdSet.where", "FStar.OrdSet.mem_of", "FStar.OrdSet.intersect", "Prims.unit" ]
[]
true
false
true
false
false
let intersect_eq_where #_ #_ s1 s2 =
same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2)
false
FStar.OrdSet.fst
FStar.OrdSet.count_is_size_of_where
val count_is_size_of_where (#a:eqtype) (#f:cmp a) (s: ordset a f) (c: condition a) : Lemma (count s c = size (where s c))
val count_is_size_of_where (#a:eqtype) (#f:cmp a) (s: ordset a f) (c: condition a) : Lemma (count s c = size (where s c))
let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 62, "end_line": 598, "start_col": 0, "start_line": 597 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.OrdSet.ordset a f -> c: FStar.OrdSet.condition a -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.count s c = FStar.OrdSet.size (FStar.OrdSet.where s c))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.condition", "FStar.OrdSet.simple_induction", "Prims.b2t", "Prims.op_Equality", "Prims.nat", "FStar.OrdSet.count", "FStar.OrdSet.size", "FStar.OrdSet.where", "Prims.unit" ]
[]
false
false
true
false
false
let count_is_size_of_where #_ #_ s c =
simple_induction (fun p -> count p c = size (where p c)) s
false
FStar.OrdSet.fst
FStar.OrdSet.any_liat
val any_liat (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) (c:condition a) : Lemma (any s c = (any (liat s) c || c (last s)))
val any_liat (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) (c:condition a) : Lemma (any s c = (any (liat s) c || c (last s)))
let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 56, "end_line": 551, "start_col": 0, "start_line": 548 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.OrdSet.ordset a f {s <> FStar.OrdSet.empty} -> c: FStar.OrdSet.condition a -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.any s c = (FStar.OrdSet.any (FStar.OrdSet.liat s) c || c (FStar.OrdSet.last s)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "Prims.b2t", "Prims.op_disEquality", "Prims.list", "Prims.Nil", "FStar.OrdSet.condition", "Prims.op_GreaterThan", "FStar.OrdSet.size", "FStar.OrdSet.any_liat", "Prims.bool", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.op_Equality", "FStar.OrdSet.any", "Prims.op_BarBar", "FStar.OrdSet.liat", "FStar.OrdSet.last", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec any_liat #a #f (s: ordset a f {s <> []}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) =
match s with | [x] -> () | h :: (t: ordset a f) -> if size t > 0 then any_liat t c
false
FStar.OrdSet.fst
FStar.OrdSet.minus_eq_where
val minus_eq_where (#a:eqtype) (#f:cmp a) (s1 s2: ordset a f) : Lemma (minus s1 s2 = where s1 (inv (mem_of s2)))
val minus_eq_where (#a:eqtype) (#f:cmp a) (s1 s2: ordset a f) : Lemma (minus s1 s2 = where s1 (inv (mem_of s2)))
let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2)
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 66, "end_line": 595, "start_col": 0, "start_line": 594 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.minus s1 s2 = FStar.OrdSet.where s1 (FStar.OrdSet.inv (FStar.OrdSet.mem_of s2)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.same_members_means_eq", "FStar.OrdSet.where", "FStar.OrdSet.inv", "FStar.OrdSet.mem_of", "FStar.OrdSet.minus", "Prims.unit" ]
[]
true
false
true
false
false
let minus_eq_where #_ #_ s1 s2 =
same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2)
false
FStar.OrdSet.fst
FStar.OrdSet.union_sort_lemma
val union_sort_lemma (#a: eqtype) (#f: _) (h: a) (t1 t2: ordset a f) : Lemma (requires sorted f (h :: t1) /\ sorted f (h :: t2)) (ensures sorted f (h :: (union t1 t2)))
val union_sort_lemma (#a: eqtype) (#f: _) (h: a) (t1 t2: ordset a f) : Lemma (requires sorted f (h :: t1) /\ sorted f (h :: t2)) (ensures sorted f (h :: (union t1 t2)))
let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 5, "end_line": 642, "start_col": 0, "start_line": 632 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
h: a -> t1: FStar.OrdSet.ordset a f -> t2: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (requires FStar.OrdSet.sorted f (h :: t1) /\ FStar.OrdSet.sorted f (h :: t2)) (ensures FStar.OrdSet.sorted f (h :: FStar.OrdSet.union t1 t2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "Prims.op_Equality", "Prims.int", "FStar.OrdSet.size", "FStar.OrdSet.union_with_empty", "Prims.bool", "FStar.OrdSet.set_props", "FStar.OrdSet.union", "Prims.unit", "FStar.OrdSet.union_mem_forall", "Prims.l_and", "Prims.b2t", "FStar.OrdSet.sorted", "Prims.Cons", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let union_sort_lemma (#a: eqtype) #f (h: a) (t1: ordset a f) (t2: ordset a f) : Lemma (requires sorted f (h :: t1) /\ sorted f (h :: t2)) (ensures sorted f (h :: (union t1 t2))) =
if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else (union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2))
false
FStar.OrdSet.fst
FStar.OrdSet.union_with_prefix
val union_with_prefix (#a: eqtype) (#f: _) (h: a) (t1 t2: (z: ordset a f {sorted f (h :: z)})) : Lemma (union #a #f (h :: t1) (h :: t2) = h :: (union t1 t2))
val union_with_prefix (#a: eqtype) (#f: _) (h: a) (t1 t2: (z: ordset a f {sorted f (h :: z)})) : Lemma (union #a #f (h :: t1) (h :: t2) = h :: (union t1 t2))
let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2))
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 72, "end_line": 648, "start_col": 0, "start_line": 644 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
h: a -> t1: z: FStar.OrdSet.ordset a f {FStar.OrdSet.sorted f (h :: z)} -> t2: z: FStar.OrdSet.ordset a f {FStar.OrdSet.sorted f (h :: z)} -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.union (h :: t1) (h :: t2) = h :: FStar.OrdSet.union t1 t2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "Prims.b2t", "FStar.OrdSet.sorted", "Prims.Cons", "FStar.OrdSet.same_members_means_eq", "FStar.OrdSet.union", "Prims.unit", "FStar.OrdSet.union_sort_lemma", "FStar.OrdSet.union_mem_forall", "Prims.l_True", "Prims.squash", "Prims.op_Equality", "Prims.list", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let union_with_prefix (#a: eqtype) #f (h: a) (t1: (z: ordset a f {sorted f (h :: z)})) (t2: (z: ordset a f {sorted f (h :: z)})) : Lemma (union #a #f (h :: t1) (h :: t2) = h :: (union t1 t2)) =
union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h :: t1) (h :: t2)) (h :: (union t1 t2))
false
FStar.OrdSet.fst
FStar.OrdSet.count_dichotomy
val count_dichotomy (#a:eqtype) (#f:cmp a) (s: ordset a f) (c: condition a) : Lemma (size s = count s c + count s (inv c))
val count_dichotomy (#a:eqtype) (#f:cmp a) (s: ordset a f) (c: condition a) : Lemma (size s = count s c + count s (inv c))
let rec count_dichotomy #_ #_ s c = if s<>[] then count_dichotomy (tail s) c
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 76, "end_line": 691, "start_col": 0, "start_line": 691 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1) let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2) let size_of_union_aux_2 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1 let rec size_of_union #a #f s1 s2 = let size = size #a #f in match s1,s2 with | [], _ -> same_members_means_eq s2 (union s1 s2) | _, [] -> same_members_means_eq s1 (union s1 s2) | h1::(t1:ordset a f), h2::(t2:ordset a f) -> size_of_union t1 s2; size_of_union s1 t2; if h1 = h2 then union_with_prefix h1 t1 t2 else if f h1 h2 then size_of_union_aux_1 s1 s2 else size_of_union_aux_2 s1 s2
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.OrdSet.ordset a f -> c: FStar.OrdSet.condition a -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.size s = FStar.OrdSet.count s c + FStar.OrdSet.count s (FStar.OrdSet.inv c))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.condition", "Prims.op_disEquality", "Prims.list", "Prims.Nil", "FStar.OrdSet.count_dichotomy", "FStar.OrdSet.tail", "Prims.bool", "Prims.unit" ]
[ "recursion" ]
false
false
true
false
false
let rec count_dichotomy #_ #_ s c =
if s <> [] then count_dichotomy (tail s) c
false
FStar.OrdSet.fst
FStar.OrdSet.union_is_symmetric
val union_is_symmetric (#a #f: _) (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1)
val union_is_symmetric (#a #f: _) (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1)
let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1)
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 51, "end_line": 660, "start_col": 0, "start_line": 659 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.union s1 s2 = FStar.OrdSet.union s2 s1)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.same_members_means_eq", "FStar.OrdSet.union", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.b2t", "Prims.op_Equality", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let union_is_symmetric #a #f (s1: ordset a f) (s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) =
same_members_means_eq (union s1 s2) (union s2 s1)
false
FStar.OrdSet.fst
FStar.OrdSet.size_of_minus
val size_of_minus (#a:eqtype) (#f:cmp a) (s1 s2: ordset a f) : Lemma (size (minus s1 s2) = size s1 - size (intersect s1 s2))
val size_of_minus (#a:eqtype) (#f:cmp a) (s1 s2: ordset a f) : Lemma (size (minus s1 s2) = size s1 - size (intersect s1 s2))
let size_of_minus #_ #_ s1 s2 = minus_eq_where s1 s2; intersect_eq_where s1 s2; count_dichotomy s1 (mem_of s2); count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s1 (inv (mem_of s2))
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 45, "end_line": 698, "start_col": 0, "start_line": 693 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1) let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2) let size_of_union_aux_2 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1 let rec size_of_union #a #f s1 s2 = let size = size #a #f in match s1,s2 with | [], _ -> same_members_means_eq s2 (union s1 s2) | _, [] -> same_members_means_eq s1 (union s1 s2) | h1::(t1:ordset a f), h2::(t2:ordset a f) -> size_of_union t1 s2; size_of_union s1 t2; if h1 = h2 then union_with_prefix h1 t1 t2 else if f h1 h2 then size_of_union_aux_1 s1 s2 else size_of_union_aux_2 s1 s2 let rec count_dichotomy #_ #_ s c = if s<>[] then count_dichotomy (tail s) c
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.size (FStar.OrdSet.minus s1 s2) = FStar.OrdSet.size s1 - FStar.OrdSet.size (FStar.OrdSet.intersect s1 s2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.count_is_size_of_where", "FStar.OrdSet.inv", "FStar.OrdSet.mem_of", "Prims.unit", "FStar.OrdSet.count_dichotomy", "FStar.OrdSet.intersect_eq_where", "FStar.OrdSet.minus_eq_where" ]
[]
true
false
true
false
false
let size_of_minus #_ #_ s1 s2 =
minus_eq_where s1 s2; intersect_eq_where s1 s2; count_dichotomy s1 (mem_of s2); count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s1 (inv (mem_of s2))
false
FStar.OrdSet.fst
FStar.OrdSet.union_head_lemma
val union_head_lemma (#a #f: _) (s1 s2: ordset a f) : Lemma (match s1, s2 with | [], [] -> (union s1 s2 = []) | [], h :: t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h :: t, [] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1 :: t1, h2 :: t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)))
val union_head_lemma (#a #f: _) (s1 s2: ordset a f) : Lemma (match s1, s2 with | [], [] -> (union s1 s2 = []) | [], h :: t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h :: t, [] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1 :: t1, h2 :: t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)))
let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2)
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 42, "end_line": 630, "start_col": 0, "start_line": 615 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (ensures ((match s1, s2 with | FStar.Pervasives.Native.Mktuple2 #_ #_ (Prims.Nil #_) (Prims.Nil #_) -> FStar.OrdSet.union s1 s2 = [] | FStar.Pervasives.Native.Mktuple2 #_ #_ (Prims.Nil #_) (Prims.Cons #_ h _) -> FStar.OrdSet.size (FStar.OrdSet.union s1 s2) > 0 && Cons?.hd (FStar.OrdSet.union s1 s2) = h | FStar.Pervasives.Native.Mktuple2 #_ #_ (Prims.Cons #_ h _) (Prims.Nil #_) -> FStar.OrdSet.size (FStar.OrdSet.union s1 s2) > 0 && Cons?.hd (FStar.OrdSet.union s1 s2) = h | FStar.Pervasives.Native.Mktuple2 #_ #_ (Prims.Cons #_ h1 _) (Prims.Cons #_ h2 _) -> FStar.OrdSet.size (FStar.OrdSet.union s1 s2) > 0 && Cons?.hd (FStar.OrdSet.union s1 s2) = (match f h1 h2 with | true -> h1 | _ -> h2)) <: Type0))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.Pervasives.Native.Mktuple2", "Prims.list", "FStar.OrdSet.union_with_empty", "FStar.OrdSet.set_props", "FStar.OrdSet.union", "Prims.unit", "FStar.OrdSet.union_mem_forall", "Prims.l_True", "Prims.squash", "Prims.b2t", "Prims.op_Equality", "Prims.Nil", "Prims.op_AmpAmp", "Prims.op_GreaterThan", "FStar.OrdSet.size", "Prims.__proj__Cons__item__hd", "Prims.bool", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let union_head_lemma #a #f (s1: ordset a f) (s2: ordset a f) : Lemma (match s1, s2 with | [], [] -> (union s1 s2 = []) | [], h :: t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h :: t, [] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1 :: t1, h2 :: t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2))) =
match s1, s2 with | [], [] -> () | [], h :: t -> () | h :: t, [] -> union_with_empty s1 | h1 :: t1, h2 :: t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2)
false
FStar.OrdSet.fst
FStar.OrdSet.where
val where (#a:eqtype) (#f:cmp a) (s:ordset a f) (c: condition a) : Pure (ordset a f) (requires True) (ensures fun (z:ordset a f) -> (as_list #a z == FStar.List.Tot.Base.filter c (as_list s)) /\ (forall x. mem x z = (mem x s && c x)) /\ (if size z > 0 && size s > 0 then f (head s) (head z) else true))
val where (#a:eqtype) (#f:cmp a) (s:ordset a f) (c: condition a) : Pure (ordset a f) (requires True) (ensures fun (z:ordset a f) -> (as_list #a z == FStar.List.Tot.Base.filter c (as_list s)) /\ (forall x. mem x z = (mem x s && c x)) /\ (if size z > 0 && size s > 0 then f (head s) (head z) else true))
let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 66, "end_line": 589, "start_col": 0, "start_line": 585 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.OrdSet.ordset a f -> c: FStar.OrdSet.condition a -> Prims.Pure (FStar.OrdSet.ordset a f)
Prims.Pure
[]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.condition", "Prims.Nil", "Prims.Cons", "Prims.bool", "Prims.list", "FStar.OrdSet.where" ]
[ "recursion" ]
false
false
false
false
false
let rec where #a #f s c =
match s with | [] -> [] | h :: [] -> if c h then [h] else [] | h :: (t: ordset a f) -> if c h then h :: (where t c) else where t c
false
FStar.OrdSet.fst
FStar.OrdSet.lemma_strict_subset_minus_size
val lemma_strict_subset_minus_size (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f) (s:ordset a f) : Lemma (requires (strict_subset s1 s2 /\ subset s1 s /\ subset s2 s)) (ensures (size (minus s s2) < size (minus s s1))) [SMTPat (strict_subset s1 s2); SMTPat (subset s1 s); SMTPat (subset s2 s)]
val lemma_strict_subset_minus_size (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f) (s:ordset a f) : Lemma (requires (strict_subset s1 s2 /\ subset s1 s /\ subset s2 s)) (ensures (size (minus s s2) < size (minus s s1))) [SMTPat (strict_subset s1 s2); SMTPat (subset s1 s); SMTPat (subset s2 s)]
let lemma_strict_subset_minus_size #_ #_ s1 s2 s = let size_diff : pos = size s2 - size s1 in size_of_minus s s2; size_of_minus s s1; intersect_with_subset s2 s; intersect_is_symmetric s2 s; intersect_with_subset s1 s; intersect_is_symmetric s1 s
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 29, "end_line": 709, "start_col": 0, "start_line": 702 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1) let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2) let size_of_union_aux_2 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1 let rec size_of_union #a #f s1 s2 = let size = size #a #f in match s1,s2 with | [], _ -> same_members_means_eq s2 (union s1 s2) | _, [] -> same_members_means_eq s1 (union s1 s2) | h1::(t1:ordset a f), h2::(t2:ordset a f) -> size_of_union t1 s2; size_of_union s1 t2; if h1 = h2 then union_with_prefix h1 t1 t2 else if f h1 h2 then size_of_union_aux_1 s1 s2 else size_of_union_aux_2 s1 s2 let rec count_dichotomy #_ #_ s c = if s<>[] then count_dichotomy (tail s) c let size_of_minus #_ #_ s1 s2 = minus_eq_where s1 s2; intersect_eq_where s1 s2; count_dichotomy s1 (mem_of s2); count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s1 (inv (mem_of s2)) let intersect_with_subset #_ #_ s1 s2 = same_members_means_eq (intersect s1 s2) s1
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> s: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (requires FStar.OrdSet.strict_subset s1 s2 /\ FStar.OrdSet.subset s1 s /\ FStar.OrdSet.subset s2 s) (ensures FStar.OrdSet.size (FStar.OrdSet.minus s s2) < FStar.OrdSet.size (FStar.OrdSet.minus s s1)) [ SMTPat (FStar.OrdSet.strict_subset s1 s2); SMTPat (FStar.OrdSet.subset s1 s); SMTPat (FStar.OrdSet.subset s2 s) ]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.intersect_is_symmetric", "Prims.unit", "FStar.OrdSet.intersect_with_subset", "FStar.OrdSet.size_of_minus", "Prims.pos", "Prims.op_Subtraction", "FStar.OrdSet.size" ]
[]
true
false
true
false
false
let lemma_strict_subset_minus_size #_ #_ s1 s2 s =
let size_diff:pos = size s2 - size s1 in size_of_minus s s2; size_of_minus s s1; intersect_with_subset s2 s; intersect_is_symmetric s2 s; intersect_with_subset s1 s; intersect_is_symmetric s1 s
false
Steel.Channel.Simplex.fst
Steel.Channel.Simplex.gather_r
val gather_r (#p: sprot) (r: ref chan_val) (v: chan_val) : SteelT unit ((pts_to r half v) `star` (in_state r p)) (fun _ -> (pts_to r full_perm v) `star` (in_state_slprop p v))
val gather_r (#p: sprot) (r: ref chan_val) (v: chan_val) : SteelT unit ((pts_to r half v) `star` (in_state r p)) (fun _ -> (pts_to r full_perm v) `star` (in_state_slprop p v))
let gather_r (#p:sprot) (r:ref chan_val) (v:chan_val) : SteelT unit (pts_to r half v `star` in_state r p) (fun _ -> pts_to r full_perm v `star` in_state_slprop p v) = let v' = witness_exists () in H.higher_ref_pts_to_injective_eq #_ #_ #_ #_ #v #_ r; H.gather #_ #_ #half #half #v #v r; rewrite_slprop (pts_to r (sum_perm half half) v) (pts_to r full_perm v) (fun _ -> ()); rewrite_slprop (in_state_slprop p v') (in_state_slprop p v) (fun _ -> ())
{ "file_name": "lib/steel/Steel.Channel.Simplex.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 77, "end_line": 281, "start_col": 0, "start_line": 273 }
(* Copyright 2020 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module Steel.Channel.Simplex module P = Steel.Channel.Protocol open Steel.SpinLock open Steel.Memory open Steel.Effect.Atomic open Steel.Effect open Steel.HigherReference open Steel.FractionalPermission module MRef = Steel.MonotonicHigherReference module H = Steel.HigherReference let sprot = p:prot { more p } noeq type chan_val = { chan_prot : sprot; chan_msg : msg_t chan_prot; chan_ctr : nat } let mref a p = MRef.ref a p let trace_ref (p:prot) = mref (partial_trace_of p) extended_to noeq type chan_t (p:prot) = { send: ref chan_val; recv: ref chan_val; trace: trace_ref p; } let half : perm = half_perm full_perm let step (s:sprot) (x:msg_t s) = step s x let chan_inv_step_p (vrecv vsend:chan_val) : prop = (vsend.chan_prot == step vrecv.chan_prot vrecv.chan_msg /\ vsend.chan_ctr == vrecv.chan_ctr + 1) let chan_inv_step (vrecv vsend:chan_val) : vprop = pure (chan_inv_step_p vrecv vsend) let chan_inv_cond (vsend:chan_val) (vrecv:chan_val) : vprop = if vsend.chan_ctr = vrecv.chan_ctr then pure (vsend == vrecv) else chan_inv_step vrecv vsend let trace_until_prop #p (r:trace_ref p) (vr:chan_val) (tr: partial_trace_of p) : vprop = MRef.pts_to r full_perm tr `star` pure (until tr == step vr.chan_prot vr.chan_msg) let trace_until #p (r:trace_ref p) (vr:chan_val) = h_exists (trace_until_prop r vr) let chan_inv_recv #p (c:chan_t p) (vsend:chan_val) = h_exists (fun (vrecv:chan_val) -> pts_to c.recv half vrecv `star` trace_until c.trace vrecv `star` chan_inv_cond vsend vrecv) let chan_inv #p (c:chan_t p) : vprop = h_exists (fun (vsend:chan_val) -> pts_to c.send half vsend `star` chan_inv_recv c vsend) let intro_chan_inv_cond_eqT (vs vr:chan_val) : Steel unit emp (fun _ -> chan_inv_cond vs vr) (requires fun _ -> vs == vr) (ensures fun _ _ _ -> True) = intro_pure (vs == vs); rewrite_slprop (chan_inv_cond vs vs) (chan_inv_cond vs vr) (fun _ -> ()) let intro_chan_inv_cond_stepT (vs vr:chan_val) : SteelT unit (chan_inv_step vr vs) (fun _ -> chan_inv_cond vs vr) = Steel.Utils.extract_pure (chan_inv_step_p vr vs); rewrite_slprop (chan_inv_step vr vs) (chan_inv_cond vs vr) (fun _ -> ()) let intro_chan_inv_auxT #p (#vs : chan_val) (#vr : chan_val) (c:chan_t p) : SteelT unit (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_cond vs vr) (fun _ -> chan_inv c) = intro_exists _ (fun (vr:chan_val) -> pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_cond vs vr); intro_exists _ (fun (vs:chan_val) -> pts_to c.send half vs `star` chan_inv_recv c vs) let intro_chan_inv_stepT #p (c:chan_t p) (vs vr:chan_val) : SteelT unit (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_step vr vs) (fun _ -> chan_inv c) = intro_chan_inv_cond_stepT vs vr; intro_chan_inv_auxT c let intro_chan_inv_eqT #p (c:chan_t p) (vs vr:chan_val) : Steel unit (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr) (fun _ -> chan_inv c) (requires fun _ -> vs == vr) (ensures fun _ _ _ -> True) = intro_chan_inv_cond_eqT vs vr; intro_chan_inv_auxT c noeq type chan p = { chan_chan : chan_t p; chan_lock : lock (chan_inv chan_chan) } let in_state_prop (p:prot) (vsend:chan_val) : prop = p == step vsend.chan_prot vsend.chan_msg irreducible let next_chan_val (#p:sprot) (x:msg_t p) (vs0:chan_val { in_state_prop p vs0 }) : Tot (vs:chan_val{in_state_prop (step p x) vs /\ chan_inv_step_p vs0 vs}) = { chan_prot = (step vs0.chan_prot vs0.chan_msg); chan_msg = x; chan_ctr = vs0.chan_ctr + 1 } [@@__reduce__] let in_state_slprop (p:prot) (vsend:chan_val) : vprop = pure (in_state_prop p vsend) let in_state (r:ref chan_val) (p:prot) = h_exists (fun (vsend:chan_val) -> pts_to r half vsend `star` in_state_slprop p vsend) let sender #q (c:chan q) (p:prot) = in_state c.chan_chan.send p let receiver #q (c:chan q) (p:prot) = in_state c.chan_chan.recv p let intro_chan_inv #p (c:chan_t p) (v:chan_val) : SteelT unit (pts_to c.send half v `star` pts_to c.recv half v `star` trace_until c.trace v) (fun _ -> chan_inv c) = intro_chan_inv_eqT c v v let chan_val_p (p:prot) = (vs0:chan_val { in_state_prop p vs0 }) let intro_in_state (r:ref chan_val) (p:prot) (v:chan_val_p p) : SteelT unit (pts_to r half v) (fun _ -> in_state r p) = intro_pure (in_state_prop p v); intro_exists v (fun (v:chan_val) -> pts_to r half v `star` in_state_slprop p v) let msg t p = Msg Send unit (fun _ -> p) let init_chan_val (p:prot) = v:chan_val {v.chan_prot == msg unit p} let initial_trace (p:prot) : (q:partial_trace_of p {until q == p}) = { to = p; tr=Waiting p} let intro_trace_until #q (r:trace_ref q) (tr:partial_trace_of q) (v:chan_val) : Steel unit (MRef.pts_to r full_perm tr) (fun _ -> trace_until r v) (requires fun _ -> until tr == step v.chan_prot v.chan_msg) (ensures fun _ _ _ -> True) = intro_pure (until tr == step v.chan_prot v.chan_msg); intro_exists tr (fun (tr:partial_trace_of q) -> MRef.pts_to r full_perm tr `star` pure (until tr == (step v.chan_prot v.chan_msg))); () let chan_t_sr (p:prot) (send recv:ref chan_val) = (c:chan_t p{c.send == send /\ c.recv == recv}) let intro_trace_until_init #p (c:chan_t p) (v:init_chan_val p) : SteelT unit (MRef.pts_to c.trace full_perm (initial_trace p)) (fun _ -> trace_until c.trace v) = intro_pure (until (initial_trace p) == step v.chan_prot v.chan_msg); //TODO: Not sure why I need this rewrite rewrite_slprop (MRef.pts_to c.trace full_perm (initial_trace p) `star` pure (until (initial_trace p) == step v.chan_prot v.chan_msg)) (MRef.pts_to c.trace full_perm (initial_trace p) `star` pure (until (initial_trace p) == step v.chan_prot v.chan_msg)) (fun _ -> ()); intro_exists (initial_trace p) (trace_until_prop c.trace v) let mk_chan (#p:prot) (send recv:ref chan_val) (v:init_chan_val p) : SteelT (chan_t_sr p send recv) (pts_to send half v `star` pts_to recv half v) (fun c -> chan_inv c) = let tr: trace_ref p = MRef.alloc (extended_to #p) (initial_trace p) in let c = Mkchan_t send recv tr in rewrite_slprop (MRef.pts_to tr full_perm (initial_trace p)) (MRef.pts_to c.trace full_perm (initial_trace p)) (fun _ -> ()); intro_trace_until_init c v; rewrite_slprop (pts_to send half v `star` pts_to recv half v) (pts_to c.send half v `star` pts_to c.recv half v) (fun _ -> ()); intro_chan_inv #p c v; let c' : chan_t_sr p send recv = c in rewrite_slprop (chan_inv c) (chan_inv c') (fun _ -> ()); return c' let new_chan (p:prot) : SteelT (chan p) emp (fun c -> sender c p `star` receiver c p) = let q = msg unit p in let v : chan_val = { chan_prot = q; chan_msg = (); chan_ctr = 0 } in let vp : init_chan_val p = v in let send = H.alloc v in let recv = H.alloc v in H.share recv; H.share send; (* TODO: use smt_fallback *) rewrite_slprop (pts_to send (half_perm full_perm) v `star` pts_to send (half_perm full_perm) v `star` pts_to recv (half_perm full_perm) v `star` pts_to recv (half_perm full_perm) v) (pts_to send half vp `star` pts_to send half vp `star` pts_to recv half vp `star` pts_to recv half vp) (fun _ -> ()); let c = mk_chan send recv vp in intro_in_state send p vp; intro_in_state recv p vp; let l = Steel.SpinLock.new_lock (chan_inv c) in let ch = { chan_chan = c; chan_lock = l } in rewrite_slprop (in_state send p) (sender ch p) (fun _ -> ()); rewrite_slprop (in_state recv p) (receiver ch p) (fun _ -> ()); return ch [@@__reduce__] let send_recv_in_sync (r:ref chan_val) (p:prot{more p}) #q (c:chan_t q) (vs vr:chan_val) : vprop = (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` pure (vs == vr) `star` in_state r p) [@@__reduce__] let sender_ahead (r:ref chan_val) (p:prot{more p}) #q (c:chan_t q) (vs vr:chan_val) : vprop = (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_step vr vs `star` in_state r p) let update_channel (#p:sprot) #q (c:chan_t q) (x:msg_t p) (vs:chan_val) (r:ref chan_val) : SteelT chan_val (pts_to r full_perm vs `star` in_state_slprop p vs) (fun vs' -> pts_to r full_perm vs' `star` (in_state_slprop (step p x) vs' `star` chan_inv_step vs vs')) = elim_pure (in_state_prop p vs); let vs' = next_chan_val x vs in H.write r vs'; intro_pure (in_state_prop (step p x) vs'); intro_pure (chan_inv_step_p vs vs'); return vs' [@@__reduce__] let send_pre_available (p:sprot) #q (c:chan_t q) (vs vr:chan_val) = send_recv_in_sync c.send p c vs vr
{ "checked_file": "/", "dependencies": [ "Steel.Utils.fst.checked", "Steel.SpinLock.fsti.checked", "Steel.MonotonicHigherReference.fsti.checked", "Steel.Memory.fsti.checked", "Steel.HigherReference.fsti.checked", "Steel.FractionalPermission.fst.checked", "Steel.Effect.Atomic.fsti.checked", "Steel.Effect.fsti.checked", "Steel.Channel.Protocol.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "Steel.Channel.Simplex.fst" }
[ { "abbrev": true, "full_module": "Steel.HigherReference", "short_module": "H" }, { "abbrev": true, "full_module": "Steel.MonotonicHigherReference", "short_module": "MRef" }, { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.HigherReference", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Atomic", "short_module": null }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.SpinLock", "short_module": null }, { "abbrev": true, "full_module": "Steel.Channel.Protocol", "short_module": "P" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Channel.Protocol", "short_module": null }, { "abbrev": false, "full_module": "Steel.Channel", "short_module": null }, { "abbrev": false, "full_module": "Steel.Channel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
r: Steel.HigherReference.ref Steel.Channel.Simplex.chan_val -> v: Steel.Channel.Simplex.chan_val -> Steel.Effect.SteelT Prims.unit
Steel.Effect.SteelT
[]
[]
[ "Steel.Channel.Simplex.sprot", "Steel.HigherReference.ref", "Steel.Channel.Simplex.chan_val", "Steel.Effect.Atomic.rewrite_slprop", "FStar.Ghost.hide", "FStar.Set.set", "Steel.Memory.iname", "FStar.Set.empty", "Steel.Channel.Simplex.in_state_slprop", "FStar.Ghost.reveal", "Steel.Memory.mem", "Prims.unit", "Steel.HigherReference.pts_to", "Steel.FractionalPermission.sum_perm", "Steel.Channel.Simplex.half", "Steel.FractionalPermission.full_perm", "Steel.HigherReference.gather", "Steel.HigherReference.higher_ref_pts_to_injective_eq", "FStar.Ghost.erased", "Steel.Effect.Atomic.witness_exists", "Steel.Effect.Common.star", "Steel.Effect.Common.vprop", "Steel.Channel.Simplex.in_state" ]
[]
false
true
false
false
false
let gather_r (#p: sprot) (r: ref chan_val) (v: chan_val) : SteelT unit ((pts_to r half v) `star` (in_state r p)) (fun _ -> (pts_to r full_perm v) `star` (in_state_slprop p v)) =
let v' = witness_exists () in H.higher_ref_pts_to_injective_eq #_ #_ #_ #_ #v #_ r; H.gather #_ #_ #half #half #v #v r; rewrite_slprop (pts_to r (sum_perm half half) v) (pts_to r full_perm v) (fun _ -> ()); rewrite_slprop (in_state_slprop p v') (in_state_slprop p v) (fun _ -> ())
false
FStar.OrdSet.fst
FStar.OrdSet.intersect_with_subset
val intersect_with_subset (#a:eqtype) (#f:cmp a) (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures intersect s1 s2 = s1)
val intersect_with_subset (#a:eqtype) (#f:cmp a) (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures intersect s1 s2 = s1)
let intersect_with_subset #_ #_ s1 s2 = same_members_means_eq (intersect s1 s2) s1
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 82, "end_line": 700, "start_col": 0, "start_line": 700 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1) let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2) let size_of_union_aux_2 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1 let rec size_of_union #a #f s1 s2 = let size = size #a #f in match s1,s2 with | [], _ -> same_members_means_eq s2 (union s1 s2) | _, [] -> same_members_means_eq s1 (union s1 s2) | h1::(t1:ordset a f), h2::(t2:ordset a f) -> size_of_union t1 s2; size_of_union s1 t2; if h1 = h2 then union_with_prefix h1 t1 t2 else if f h1 h2 then size_of_union_aux_1 s1 s2 else size_of_union_aux_2 s1 s2 let rec count_dichotomy #_ #_ s c = if s<>[] then count_dichotomy (tail s) c let size_of_minus #_ #_ s1 s2 = minus_eq_where s1 s2; intersect_eq_where s1 s2; count_dichotomy s1 (mem_of s2); count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s1 (inv (mem_of s2))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (requires FStar.OrdSet.subset s1 s2) (ensures FStar.OrdSet.intersect s1 s2 = s1)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.same_members_means_eq", "FStar.OrdSet.intersect", "Prims.unit" ]
[]
true
false
true
false
false
let intersect_with_subset #_ #_ s1 s2 =
same_members_means_eq (intersect s1 s2) s1
false
FStar.OrdSet.fst
FStar.OrdSet.lemma_intersect_symmetric
val lemma_intersect_symmetric (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f) : Lemma (requires True) (ensures (intersect s1 s2 == intersect s2 s1)) [SMTPatOr [[SMTPat (intersect s1 s2)]; [SMTPat (intersect s2 s1)]]]
val lemma_intersect_symmetric (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f) : Lemma (requires True) (ensures (intersect s1 s2 == intersect s2 s1)) [SMTPatOr [[SMTPat (intersect s1 s2)]; [SMTPat (intersect s2 s1)]]]
let lemma_intersect_symmetric = intersect_is_symmetric
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 54, "end_line": 717, "start_col": 0, "start_line": 717 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1) let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2) let size_of_union_aux_2 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1 let rec size_of_union #a #f s1 s2 = let size = size #a #f in match s1,s2 with | [], _ -> same_members_means_eq s2 (union s1 s2) | _, [] -> same_members_means_eq s1 (union s1 s2) | h1::(t1:ordset a f), h2::(t2:ordset a f) -> size_of_union t1 s2; size_of_union s1 t2; if h1 = h2 then union_with_prefix h1 t1 t2 else if f h1 h2 then size_of_union_aux_1 s1 s2 else size_of_union_aux_2 s1 s2 let rec count_dichotomy #_ #_ s c = if s<>[] then count_dichotomy (tail s) c let size_of_minus #_ #_ s1 s2 = minus_eq_where s1 s2; intersect_eq_where s1 s2; count_dichotomy s1 (mem_of s2); count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s1 (inv (mem_of s2)) let intersect_with_subset #_ #_ s1 s2 = same_members_means_eq (intersect s1 s2) s1 let lemma_strict_subset_minus_size #_ #_ s1 s2 s = let size_diff : pos = size s2 - size s1 in size_of_minus s s2; size_of_minus s s1; intersect_with_subset s2 s; intersect_is_symmetric s2 s; intersect_with_subset s1 s; intersect_is_symmetric s1 s let lemma_disjoint_union_subset #_ #_ s1 s2 = size_of_union s1 s2 let lemma_subset_union #_ #_ _ _ _ = () let lemma_strict_subset_transitive #_ #_ _ _ _ = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.intersect s1 s2 == FStar.OrdSet.intersect s2 s1) [SMTPatOr [[SMTPat (FStar.OrdSet.intersect s1 s2)]; [SMTPat (FStar.OrdSet.intersect s2 s1)]]]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.OrdSet.intersect_is_symmetric" ]
[]
true
false
true
false
false
let lemma_intersect_symmetric =
intersect_is_symmetric
false
FStar.OrdSet.fst
FStar.OrdSet.lemma_union_symmetric
val lemma_union_symmetric (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f) : Lemma (union s1 s2 == union s2 s1) [SMTPat (union s1 s2)]
val lemma_union_symmetric (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f) : Lemma (union s1 s2 == union s2 s1) [SMTPat (union s1 s2)]
let lemma_union_symmetric #a #f s1 s2 = eq_lemma (union s1 s2) (union s2 s1)
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 76, "end_line": 737, "start_col": 0, "start_line": 737 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1) let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2) let size_of_union_aux_2 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1 let rec size_of_union #a #f s1 s2 = let size = size #a #f in match s1,s2 with | [], _ -> same_members_means_eq s2 (union s1 s2) | _, [] -> same_members_means_eq s1 (union s1 s2) | h1::(t1:ordset a f), h2::(t2:ordset a f) -> size_of_union t1 s2; size_of_union s1 t2; if h1 = h2 then union_with_prefix h1 t1 t2 else if f h1 h2 then size_of_union_aux_1 s1 s2 else size_of_union_aux_2 s1 s2 let rec count_dichotomy #_ #_ s c = if s<>[] then count_dichotomy (tail s) c let size_of_minus #_ #_ s1 s2 = minus_eq_where s1 s2; intersect_eq_where s1 s2; count_dichotomy s1 (mem_of s2); count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s1 (inv (mem_of s2)) let intersect_with_subset #_ #_ s1 s2 = same_members_means_eq (intersect s1 s2) s1 let lemma_strict_subset_minus_size #_ #_ s1 s2 s = let size_diff : pos = size s2 - size s1 in size_of_minus s s2; size_of_minus s s1; intersect_with_subset s2 s; intersect_is_symmetric s2 s; intersect_with_subset s1 s; intersect_is_symmetric s1 s let lemma_disjoint_union_subset #_ #_ s1 s2 = size_of_union s1 s2 let lemma_subset_union #_ #_ _ _ _ = () let lemma_strict_subset_transitive #_ #_ _ _ _ = () let lemma_intersect_symmetric = intersect_is_symmetric let lemma_intersect_union_empty_aux1 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) (ensures (intersect (union s1 s2) s3 = empty)) = eq_lemma empty (intersect (union s1 s2) s3) let lemma_intersect_union_empty_aux2 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect (union s1 s2) s3 = empty)) (ensures (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) = eq_lemma empty (intersect s1 s3); eq_lemma empty (intersect s2 s3) let lemma_intersect_union_empty #a #f (s1 s2 s3: ordset a f) : Lemma ((intersect (union s1 s2) s3 = empty) = (intersect s1 s3 = empty && intersect s2 s3 = empty)) [SMTPat (intersect (union s1 s2) s3)] = Classical.move_requires_3 (lemma_intersect_union_empty_aux1 #a #f) s1 s2 s3; Classical.move_requires_3 (lemma_intersect_union_empty_aux2 #a #f) s1 s2 s3
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.union s1 s2 == FStar.OrdSet.union s2 s1) [SMTPat (FStar.OrdSet.union s1 s2)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.eq_lemma", "FStar.OrdSet.union", "Prims.unit" ]
[]
true
false
true
false
false
let lemma_union_symmetric #a #f s1 s2 =
eq_lemma (union s1 s2) (union s2 s1)
false
FStar.OrdSet.fst
FStar.OrdSet.union_of_tails_size
val union_of_tails_size (#a: eqtype) (#f: _) (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2))
val union_of_tails_size (#a: eqtype) (#f: _) (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2))
let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2)
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 59, "end_line": 657, "start_col": 0, "start_line": 650 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (requires FStar.OrdSet.size s1 > 0 && FStar.OrdSet.size s2 > 0 && Cons?.hd s1 <> Cons?.hd s2 && f (Cons?.hd s1) (Cons?.hd s2)) (ensures FStar.OrdSet.size (FStar.OrdSet.union s1 s2) = 1 + FStar.OrdSet.size (FStar.OrdSet.union (Cons?.tl s1) s2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "Prims.list", "FStar.OrdSet.same_members_means_eq", "Prims.Cons", "FStar.OrdSet.union", "Prims.unit", "FStar.OrdSet.set_props", "FStar.OrdSet.union_mem_forall", "Prims.b2t", "Prims.op_AmpAmp", "Prims.op_GreaterThan", "FStar.OrdSet.size", "Prims.op_disEquality", "Prims.__proj__Cons__item__hd", "Prims.squash", "Prims.op_Equality", "Prims.int", "Prims.op_Addition", "Prims.__proj__Cons__item__tl", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let union_of_tails_size (#a: eqtype) #f (s1: ordset a f) (s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) =
match s1 with | h1 :: (t1: ordset a f) -> match s2 with | h2 :: (t2: ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1 :: (union t1 s2)) (union s1 s2)
false
FStar.OrdSet.fst
FStar.OrdSet.lemma_disjoint_union_subset
val lemma_disjoint_union_subset (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f) : Lemma (requires (~ (s1 == empty) /\ ~ (s2 == empty) /\ intersect s1 s2 == empty)) (ensures (strict_subset s1 (union s1 s2) /\ strict_subset s2 (union s1 s2))) [SMTPatOr [[SMTPat (strict_subset s1 (union s1 s2))]; [SMTPat (strict_subset s2 (union s1 s2))]]]
val lemma_disjoint_union_subset (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f) : Lemma (requires (~ (s1 == empty) /\ ~ (s2 == empty) /\ intersect s1 s2 == empty)) (ensures (strict_subset s1 (union s1 s2) /\ strict_subset s2 (union s1 s2))) [SMTPatOr [[SMTPat (strict_subset s1 (union s1 s2))]; [SMTPat (strict_subset s2 (union s1 s2))]]]
let lemma_disjoint_union_subset #_ #_ s1 s2 = size_of_union s1 s2
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 65, "end_line": 711, "start_col": 0, "start_line": 711 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1) let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2) let size_of_union_aux_2 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1 let rec size_of_union #a #f s1 s2 = let size = size #a #f in match s1,s2 with | [], _ -> same_members_means_eq s2 (union s1 s2) | _, [] -> same_members_means_eq s1 (union s1 s2) | h1::(t1:ordset a f), h2::(t2:ordset a f) -> size_of_union t1 s2; size_of_union s1 t2; if h1 = h2 then union_with_prefix h1 t1 t2 else if f h1 h2 then size_of_union_aux_1 s1 s2 else size_of_union_aux_2 s1 s2 let rec count_dichotomy #_ #_ s c = if s<>[] then count_dichotomy (tail s) c let size_of_minus #_ #_ s1 s2 = minus_eq_where s1 s2; intersect_eq_where s1 s2; count_dichotomy s1 (mem_of s2); count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s1 (inv (mem_of s2)) let intersect_with_subset #_ #_ s1 s2 = same_members_means_eq (intersect s1 s2) s1 let lemma_strict_subset_minus_size #_ #_ s1 s2 s = let size_diff : pos = size s2 - size s1 in size_of_minus s s2; size_of_minus s s1; intersect_with_subset s2 s; intersect_is_symmetric s2 s; intersect_with_subset s1 s; intersect_is_symmetric s1 s
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (requires ~(s1 == FStar.OrdSet.empty) /\ ~(s2 == FStar.OrdSet.empty) /\ FStar.OrdSet.intersect s1 s2 == FStar.OrdSet.empty) (ensures FStar.OrdSet.strict_subset s1 (FStar.OrdSet.union s1 s2) /\ FStar.OrdSet.strict_subset s2 (FStar.OrdSet.union s1 s2)) [ SMTPatOr [ [SMTPat (FStar.OrdSet.strict_subset s1 (FStar.OrdSet.union s1 s2))]; [SMTPat (FStar.OrdSet.strict_subset s2 (FStar.OrdSet.union s1 s2))] ] ]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.size_of_union", "Prims.unit" ]
[]
true
false
true
false
false
let lemma_disjoint_union_subset #_ #_ s1 s2 =
size_of_union s1 s2
false
FStar.OrdSet.fst
FStar.OrdSet.size_of_union_aux_2
val size_of_union_aux_2 (#a #f: _) (s1 s2: (z: ordset a f {z <> empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2)))
val size_of_union_aux_2 (#a #f: _) (s1 s2: (z: ordset a f {z <> empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2)))
let size_of_union_aux_2 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 29, "end_line": 677, "start_col": 0, "start_line": 670 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1) let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: z: FStar.OrdSet.ordset a f {z <> FStar.OrdSet.empty} -> s2: z: FStar.OrdSet.ordset a f {z <> FStar.OrdSet.empty} -> FStar.Pervasives.Lemma (requires FStar.OrdSet.head s1 <> FStar.OrdSet.head s2 && Prims.op_Negation (f (FStar.OrdSet.head s1) (FStar.OrdSet.head s2)) && FStar.OrdSet.size (FStar.OrdSet.union s1 (FStar.OrdSet.tail s2)) = FStar.OrdSet.size s1 + FStar.OrdSet.size (FStar.OrdSet.tail s2) - FStar.OrdSet.size (FStar.OrdSet.intersect s1 (FStar.OrdSet.tail s2))) (ensures FStar.OrdSet.size (FStar.OrdSet.union s1 s2) = FStar.OrdSet.size s1 + FStar.OrdSet.size s2 - FStar.OrdSet.size (FStar.OrdSet.intersect s1 s2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "Prims.b2t", "Prims.op_disEquality", "FStar.OrdSet.empty", "FStar.OrdSet.size_of_union_aux_1", "Prims.unit", "FStar.Classical.forall_intro_2", "Prims.op_Equality", "FStar.OrdSet.intersect", "FStar.OrdSet.intersect_is_symmetric", "FStar.OrdSet.union", "FStar.OrdSet.union_is_symmetric", "Prims.op_AmpAmp", "FStar.OrdSet.head", "Prims.op_Negation", "Prims.int", "FStar.OrdSet.size", "FStar.OrdSet.tail", "Prims.op_Subtraction", "Prims.op_Addition", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let size_of_union_aux_2 #a #f (s1: (z: ordset a f {z <> empty})) (s2: (z: ordset a f {z <> empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) =
Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1
false
FStar.OrdSet.fst
FStar.OrdSet.as_set
val as_set : #a:eqtype -> #f:cmp a -> ordset a f -> Tot (S.set a)
val as_set : #a:eqtype -> #f:cmp a -> ordset a f -> Tot (S.set a)
let rec as_set #a #f s = match s with | [] -> S.empty | hd::tl -> S.union (S.singleton hd) (as_set #a #f tl)
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 56, "end_line": 750, "start_col": 0, "start_line": 747 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1) let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2) let size_of_union_aux_2 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1 let rec size_of_union #a #f s1 s2 = let size = size #a #f in match s1,s2 with | [], _ -> same_members_means_eq s2 (union s1 s2) | _, [] -> same_members_means_eq s1 (union s1 s2) | h1::(t1:ordset a f), h2::(t2:ordset a f) -> size_of_union t1 s2; size_of_union s1 t2; if h1 = h2 then union_with_prefix h1 t1 t2 else if f h1 h2 then size_of_union_aux_1 s1 s2 else size_of_union_aux_2 s1 s2 let rec count_dichotomy #_ #_ s c = if s<>[] then count_dichotomy (tail s) c let size_of_minus #_ #_ s1 s2 = minus_eq_where s1 s2; intersect_eq_where s1 s2; count_dichotomy s1 (mem_of s2); count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s1 (inv (mem_of s2)) let intersect_with_subset #_ #_ s1 s2 = same_members_means_eq (intersect s1 s2) s1 let lemma_strict_subset_minus_size #_ #_ s1 s2 s = let size_diff : pos = size s2 - size s1 in size_of_minus s s2; size_of_minus s s1; intersect_with_subset s2 s; intersect_is_symmetric s2 s; intersect_with_subset s1 s; intersect_is_symmetric s1 s let lemma_disjoint_union_subset #_ #_ s1 s2 = size_of_union s1 s2 let lemma_subset_union #_ #_ _ _ _ = () let lemma_strict_subset_transitive #_ #_ _ _ _ = () let lemma_intersect_symmetric = intersect_is_symmetric let lemma_intersect_union_empty_aux1 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) (ensures (intersect (union s1 s2) s3 = empty)) = eq_lemma empty (intersect (union s1 s2) s3) let lemma_intersect_union_empty_aux2 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect (union s1 s2) s3 = empty)) (ensures (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) = eq_lemma empty (intersect s1 s3); eq_lemma empty (intersect s2 s3) let lemma_intersect_union_empty #a #f (s1 s2 s3: ordset a f) : Lemma ((intersect (union s1 s2) s3 = empty) = (intersect s1 s3 = empty && intersect s2 s3 = empty)) [SMTPat (intersect (union s1 s2) s3)] = Classical.move_requires_3 (lemma_intersect_union_empty_aux1 #a #f) s1 s2 s3; Classical.move_requires_3 (lemma_intersect_union_empty_aux2 #a #f) s1 s2 s3 let lemma_union_symmetric #a #f s1 s2 = eq_lemma (union s1 s2) (union s2 s1) let union_of_disjoint #a #f s1 s2 = eq_lemma (minus (union s1 s2) s1) s2 let distinct_is_idempotent #a #f s = eq_lemma (distinct f s) s (* Conversion from OrdSet to Set *) module S = FStar.Set
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "abbrev": true, "full_module": "FStar.Set", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Set", "short_module": "S" }, { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.OrdSet.ordset a f -> FStar.Set.set a
Prims.Tot
[ "total" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.Set.empty", "Prims.list", "FStar.Set.union", "FStar.Set.singleton", "FStar.OrdSet.as_set", "FStar.Set.set" ]
[ "recursion" ]
false
false
false
false
false
let rec as_set #a #f s =
match s with | [] -> S.empty | hd :: tl -> S.union (S.singleton hd) (as_set #a #f tl)
false
FStar.OrdSet.fst
FStar.OrdSet.lemma_intersect_union_empty
val lemma_intersect_union_empty (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f) (s3:ordset a f) : Lemma ((intersect (union s1 s2) s3 = empty) = (intersect s1 s3 = empty && intersect s2 s3 = empty)) [SMTPat (intersect (union s1 s2) s3)]
val lemma_intersect_union_empty (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f) (s3:ordset a f) : Lemma ((intersect (union s1 s2) s3 = empty) = (intersect s1 s3 = empty && intersect s2 s3 = empty)) [SMTPat (intersect (union s1 s2) s3)]
let lemma_intersect_union_empty #a #f (s1 s2 s3: ordset a f) : Lemma ((intersect (union s1 s2) s3 = empty) = (intersect s1 s3 = empty && intersect s2 s3 = empty)) [SMTPat (intersect (union s1 s2) s3)] = Classical.move_requires_3 (lemma_intersect_union_empty_aux1 #a #f) s1 s2 s3; Classical.move_requires_3 (lemma_intersect_union_empty_aux2 #a #f) s1 s2 s3
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 77, "end_line": 735, "start_col": 0, "start_line": 730 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1) let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2) let size_of_union_aux_2 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1 let rec size_of_union #a #f s1 s2 = let size = size #a #f in match s1,s2 with | [], _ -> same_members_means_eq s2 (union s1 s2) | _, [] -> same_members_means_eq s1 (union s1 s2) | h1::(t1:ordset a f), h2::(t2:ordset a f) -> size_of_union t1 s2; size_of_union s1 t2; if h1 = h2 then union_with_prefix h1 t1 t2 else if f h1 h2 then size_of_union_aux_1 s1 s2 else size_of_union_aux_2 s1 s2 let rec count_dichotomy #_ #_ s c = if s<>[] then count_dichotomy (tail s) c let size_of_minus #_ #_ s1 s2 = minus_eq_where s1 s2; intersect_eq_where s1 s2; count_dichotomy s1 (mem_of s2); count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s1 (inv (mem_of s2)) let intersect_with_subset #_ #_ s1 s2 = same_members_means_eq (intersect s1 s2) s1 let lemma_strict_subset_minus_size #_ #_ s1 s2 s = let size_diff : pos = size s2 - size s1 in size_of_minus s s2; size_of_minus s s1; intersect_with_subset s2 s; intersect_is_symmetric s2 s; intersect_with_subset s1 s; intersect_is_symmetric s1 s let lemma_disjoint_union_subset #_ #_ s1 s2 = size_of_union s1 s2 let lemma_subset_union #_ #_ _ _ _ = () let lemma_strict_subset_transitive #_ #_ _ _ _ = () let lemma_intersect_symmetric = intersect_is_symmetric let lemma_intersect_union_empty_aux1 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) (ensures (intersect (union s1 s2) s3 = empty)) = eq_lemma empty (intersect (union s1 s2) s3) let lemma_intersect_union_empty_aux2 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect (union s1 s2) s3 = empty)) (ensures (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) = eq_lemma empty (intersect s1 s3); eq_lemma empty (intersect s2 s3)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> s3: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.intersect (FStar.OrdSet.union s1 s2) s3 = FStar.OrdSet.empty = (FStar.OrdSet.intersect s1 s3 = FStar.OrdSet.empty && FStar.OrdSet.intersect s2 s3 = FStar.OrdSet.empty)) [SMTPat (FStar.OrdSet.intersect (FStar.OrdSet.union s1 s2) s3)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.Classical.move_requires_3", "Prims.b2t", "Prims.op_Equality", "FStar.OrdSet.intersect", "FStar.OrdSet.union", "FStar.OrdSet.empty", "Prims.l_and", "Prims.eq2", "FStar.OrdSet.lemma_intersect_union_empty_aux2", "Prims.unit", "FStar.OrdSet.lemma_intersect_union_empty_aux1", "Prims.l_True", "Prims.squash", "Prims.bool", "Prims.op_AmpAmp", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat", "Prims.Nil" ]
[]
false
false
true
false
false
let lemma_intersect_union_empty #a #f (s1: ordset a f) (s2: ordset a f) (s3: ordset a f) : Lemma ((intersect (union s1 s2) s3 = empty) = (intersect s1 s3 = empty && intersect s2 s3 = empty)) [SMTPat (intersect (union s1 s2) s3)] =
Classical.move_requires_3 (lemma_intersect_union_empty_aux1 #a #f) s1 s2 s3; Classical.move_requires_3 (lemma_intersect_union_empty_aux2 #a #f) s1 s2 s3
false
FStar.OrdSet.fst
FStar.OrdSet.distinct_is_idempotent
val distinct_is_idempotent (#a:eqtype) (#f: cmp a) (s: ordset a f) : Lemma (distinct f (as_list s) = s)
val distinct_is_idempotent (#a:eqtype) (#f: cmp a) (s: ordset a f) : Lemma (distinct f (as_list s) = s)
let distinct_is_idempotent #a #f s = eq_lemma (distinct f s) s
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 62, "end_line": 741, "start_col": 0, "start_line": 741 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1) let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2) let size_of_union_aux_2 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1 let rec size_of_union #a #f s1 s2 = let size = size #a #f in match s1,s2 with | [], _ -> same_members_means_eq s2 (union s1 s2) | _, [] -> same_members_means_eq s1 (union s1 s2) | h1::(t1:ordset a f), h2::(t2:ordset a f) -> size_of_union t1 s2; size_of_union s1 t2; if h1 = h2 then union_with_prefix h1 t1 t2 else if f h1 h2 then size_of_union_aux_1 s1 s2 else size_of_union_aux_2 s1 s2 let rec count_dichotomy #_ #_ s c = if s<>[] then count_dichotomy (tail s) c let size_of_minus #_ #_ s1 s2 = minus_eq_where s1 s2; intersect_eq_where s1 s2; count_dichotomy s1 (mem_of s2); count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s1 (inv (mem_of s2)) let intersect_with_subset #_ #_ s1 s2 = same_members_means_eq (intersect s1 s2) s1 let lemma_strict_subset_minus_size #_ #_ s1 s2 s = let size_diff : pos = size s2 - size s1 in size_of_minus s s2; size_of_minus s s1; intersect_with_subset s2 s; intersect_is_symmetric s2 s; intersect_with_subset s1 s; intersect_is_symmetric s1 s let lemma_disjoint_union_subset #_ #_ s1 s2 = size_of_union s1 s2 let lemma_subset_union #_ #_ _ _ _ = () let lemma_strict_subset_transitive #_ #_ _ _ _ = () let lemma_intersect_symmetric = intersect_is_symmetric let lemma_intersect_union_empty_aux1 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) (ensures (intersect (union s1 s2) s3 = empty)) = eq_lemma empty (intersect (union s1 s2) s3) let lemma_intersect_union_empty_aux2 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect (union s1 s2) s3 = empty)) (ensures (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) = eq_lemma empty (intersect s1 s3); eq_lemma empty (intersect s2 s3) let lemma_intersect_union_empty #a #f (s1 s2 s3: ordset a f) : Lemma ((intersect (union s1 s2) s3 = empty) = (intersect s1 s3 = empty && intersect s2 s3 = empty)) [SMTPat (intersect (union s1 s2) s3)] = Classical.move_requires_3 (lemma_intersect_union_empty_aux1 #a #f) s1 s2 s3; Classical.move_requires_3 (lemma_intersect_union_empty_aux2 #a #f) s1 s2 s3 let lemma_union_symmetric #a #f s1 s2 = eq_lemma (union s1 s2) (union s2 s1) let union_of_disjoint #a #f s1 s2 = eq_lemma (minus (union s1 s2) s1) s2
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.distinct f (FStar.OrdSet.as_list s) = s)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.eq_lemma", "FStar.OrdSet.distinct", "Prims.unit" ]
[]
true
false
true
false
false
let distinct_is_idempotent #a #f s =
eq_lemma (distinct f s) s
false
FStar.OrdSet.fst
FStar.OrdSet.union_of_disjoint
val union_of_disjoint (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f) : Lemma (requires (disjoint s1 s2)) (ensures (minus (union s1 s2) s1 == s2)) [SMTPat (union s1 s2); SMTPat (disjoint s1 s2)]
val union_of_disjoint (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f) : Lemma (requires (disjoint s1 s2)) (ensures (minus (union s1 s2) s1 == s2)) [SMTPat (union s1 s2); SMTPat (disjoint s1 s2)]
let union_of_disjoint #a #f s1 s2 = eq_lemma (minus (union s1 s2) s1) s2
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 72, "end_line": 739, "start_col": 0, "start_line": 739 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1) let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2) let size_of_union_aux_2 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1 let rec size_of_union #a #f s1 s2 = let size = size #a #f in match s1,s2 with | [], _ -> same_members_means_eq s2 (union s1 s2) | _, [] -> same_members_means_eq s1 (union s1 s2) | h1::(t1:ordset a f), h2::(t2:ordset a f) -> size_of_union t1 s2; size_of_union s1 t2; if h1 = h2 then union_with_prefix h1 t1 t2 else if f h1 h2 then size_of_union_aux_1 s1 s2 else size_of_union_aux_2 s1 s2 let rec count_dichotomy #_ #_ s c = if s<>[] then count_dichotomy (tail s) c let size_of_minus #_ #_ s1 s2 = minus_eq_where s1 s2; intersect_eq_where s1 s2; count_dichotomy s1 (mem_of s2); count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s1 (inv (mem_of s2)) let intersect_with_subset #_ #_ s1 s2 = same_members_means_eq (intersect s1 s2) s1 let lemma_strict_subset_minus_size #_ #_ s1 s2 s = let size_diff : pos = size s2 - size s1 in size_of_minus s s2; size_of_minus s s1; intersect_with_subset s2 s; intersect_is_symmetric s2 s; intersect_with_subset s1 s; intersect_is_symmetric s1 s let lemma_disjoint_union_subset #_ #_ s1 s2 = size_of_union s1 s2 let lemma_subset_union #_ #_ _ _ _ = () let lemma_strict_subset_transitive #_ #_ _ _ _ = () let lemma_intersect_symmetric = intersect_is_symmetric let lemma_intersect_union_empty_aux1 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) (ensures (intersect (union s1 s2) s3 = empty)) = eq_lemma empty (intersect (union s1 s2) s3) let lemma_intersect_union_empty_aux2 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect (union s1 s2) s3 = empty)) (ensures (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) = eq_lemma empty (intersect s1 s3); eq_lemma empty (intersect s2 s3) let lemma_intersect_union_empty #a #f (s1 s2 s3: ordset a f) : Lemma ((intersect (union s1 s2) s3 = empty) = (intersect s1 s3 = empty && intersect s2 s3 = empty)) [SMTPat (intersect (union s1 s2) s3)] = Classical.move_requires_3 (lemma_intersect_union_empty_aux1 #a #f) s1 s2 s3; Classical.move_requires_3 (lemma_intersect_union_empty_aux2 #a #f) s1 s2 s3 let lemma_union_symmetric #a #f s1 s2 = eq_lemma (union s1 s2) (union s2 s1)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (requires FStar.OrdSet.disjoint s1 s2) (ensures FStar.OrdSet.minus (FStar.OrdSet.union s1 s2) s1 == s2) [SMTPat (FStar.OrdSet.union s1 s2); SMTPat (FStar.OrdSet.disjoint s1 s2)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.eq_lemma", "FStar.OrdSet.minus", "FStar.OrdSet.union", "Prims.unit" ]
[]
true
false
true
false
false
let union_of_disjoint #a #f s1 s2 =
eq_lemma (minus (union s1 s2) s1) s2
false
FStar.OrdSet.fst
FStar.OrdSet.size_of_union
val size_of_union (#a:eqtype) (#f:cmp a) (s1 s2: ordset a f) : Lemma (size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2)))
val size_of_union (#a:eqtype) (#f:cmp a) (s1 s2: ordset a f) : Lemma (size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2)))
let rec size_of_union #a #f s1 s2 = let size = size #a #f in match s1,s2 with | [], _ -> same_members_means_eq s2 (union s1 s2) | _, [] -> same_members_means_eq s1 (union s1 s2) | h1::(t1:ordset a f), h2::(t2:ordset a f) -> size_of_union t1 s2; size_of_union s1 t2; if h1 = h2 then union_with_prefix h1 t1 t2 else if f h1 h2 then size_of_union_aux_1 s1 s2 else size_of_union_aux_2 s1 s2
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 42, "end_line": 689, "start_col": 0, "start_line": 679 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1) let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2) let size_of_union_aux_2 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.size (FStar.OrdSet.union s1 s2) = FStar.OrdSet.size s1 + FStar.OrdSet.size s2 - FStar.OrdSet.size (FStar.OrdSet.intersect s1 s2) )
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.Pervasives.Native.Mktuple2", "Prims.list", "FStar.OrdSet.same_members_means_eq", "FStar.OrdSet.union", "Prims.op_Equality", "FStar.OrdSet.union_with_prefix", "Prims.bool", "FStar.OrdSet.size_of_union_aux_1", "FStar.OrdSet.size_of_union_aux_2", "Prims.unit", "FStar.OrdSet.size_of_union", "Prims.nat", "FStar.OrdSet.size" ]
[ "recursion" ]
false
false
true
false
false
let rec size_of_union #a #f s1 s2 =
let size = size #a #f in match s1, s2 with | [], _ -> same_members_means_eq s2 (union s1 s2) | _, [] -> same_members_means_eq s1 (union s1 s2) | h1 :: (t1: ordset a f), h2 :: (t2: ordset a f) -> size_of_union t1 s2; size_of_union s1 t2; if h1 = h2 then union_with_prefix h1 t1 t2 else if f h1 h2 then size_of_union_aux_1 s1 s2 else size_of_union_aux_2 s1 s2
false
FStar.OrdSet.fst
FStar.OrdSet.lemma_intersect_union_empty_aux2
val lemma_intersect_union_empty_aux2 (#a #f: _) (s1 s2 s3: ordset a f) : Lemma (requires (intersect (union s1 s2) s3 = empty)) (ensures (intersect s1 s3 == empty /\ intersect s2 s3 == empty))
val lemma_intersect_union_empty_aux2 (#a #f: _) (s1 s2 s3: ordset a f) : Lemma (requires (intersect (union s1 s2) s3 = empty)) (ensures (intersect s1 s3 == empty /\ intersect s2 s3 == empty))
let lemma_intersect_union_empty_aux2 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect (union s1 s2) s3 = empty)) (ensures (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) = eq_lemma empty (intersect s1 s3); eq_lemma empty (intersect s2 s3)
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 36, "end_line": 728, "start_col": 0, "start_line": 724 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1) let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2) let size_of_union_aux_2 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1 let rec size_of_union #a #f s1 s2 = let size = size #a #f in match s1,s2 with | [], _ -> same_members_means_eq s2 (union s1 s2) | _, [] -> same_members_means_eq s1 (union s1 s2) | h1::(t1:ordset a f), h2::(t2:ordset a f) -> size_of_union t1 s2; size_of_union s1 t2; if h1 = h2 then union_with_prefix h1 t1 t2 else if f h1 h2 then size_of_union_aux_1 s1 s2 else size_of_union_aux_2 s1 s2 let rec count_dichotomy #_ #_ s c = if s<>[] then count_dichotomy (tail s) c let size_of_minus #_ #_ s1 s2 = minus_eq_where s1 s2; intersect_eq_where s1 s2; count_dichotomy s1 (mem_of s2); count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s1 (inv (mem_of s2)) let intersect_with_subset #_ #_ s1 s2 = same_members_means_eq (intersect s1 s2) s1 let lemma_strict_subset_minus_size #_ #_ s1 s2 s = let size_diff : pos = size s2 - size s1 in size_of_minus s s2; size_of_minus s s1; intersect_with_subset s2 s; intersect_is_symmetric s2 s; intersect_with_subset s1 s; intersect_is_symmetric s1 s let lemma_disjoint_union_subset #_ #_ s1 s2 = size_of_union s1 s2 let lemma_subset_union #_ #_ _ _ _ = () let lemma_strict_subset_transitive #_ #_ _ _ _ = () let lemma_intersect_symmetric = intersect_is_symmetric let lemma_intersect_union_empty_aux1 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) (ensures (intersect (union s1 s2) s3 = empty)) = eq_lemma empty (intersect (union s1 s2) s3)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> s3: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (requires FStar.OrdSet.intersect (FStar.OrdSet.union s1 s2) s3 = FStar.OrdSet.empty) (ensures FStar.OrdSet.intersect s1 s3 == FStar.OrdSet.empty /\ FStar.OrdSet.intersect s2 s3 == FStar.OrdSet.empty)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.eq_lemma", "FStar.OrdSet.empty", "FStar.OrdSet.intersect", "Prims.unit", "Prims.b2t", "Prims.op_Equality", "FStar.OrdSet.union", "Prims.squash", "Prims.l_and", "Prims.eq2", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let lemma_intersect_union_empty_aux2 #a #f (s1: ordset a f) (s2: ordset a f) (s3: ordset a f) : Lemma (requires (intersect (union s1 s2) s3 = empty)) (ensures (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) =
eq_lemma empty (intersect s1 s3); eq_lemma empty (intersect s2 s3)
false
FStar.OrdSet.fst
FStar.OrdSet.lemma_as_set_mem
val lemma_as_set_mem (#a:eqtype) (#f:cmp a) (s:ordset a f) (x:a) : Lemma (mem x s <==> S.mem x (as_set s)) [SMTPat (mem x s); SMTPat (S.mem x (as_set s))]
val lemma_as_set_mem (#a:eqtype) (#f:cmp a) (s:ordset a f) (x:a) : Lemma (mem x s <==> S.mem x (as_set s)) [SMTPat (mem x s); SMTPat (S.mem x (as_set s))]
let rec lemma_as_set_mem #a #f s x = match s with | [] -> () | hd::tl -> if x = hd then () else lemma_as_set_mem #a #f tl x
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 40, "end_line": 758, "start_col": 0, "start_line": 752 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1) let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2) let size_of_union_aux_2 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1 let rec size_of_union #a #f s1 s2 = let size = size #a #f in match s1,s2 with | [], _ -> same_members_means_eq s2 (union s1 s2) | _, [] -> same_members_means_eq s1 (union s1 s2) | h1::(t1:ordset a f), h2::(t2:ordset a f) -> size_of_union t1 s2; size_of_union s1 t2; if h1 = h2 then union_with_prefix h1 t1 t2 else if f h1 h2 then size_of_union_aux_1 s1 s2 else size_of_union_aux_2 s1 s2 let rec count_dichotomy #_ #_ s c = if s<>[] then count_dichotomy (tail s) c let size_of_minus #_ #_ s1 s2 = minus_eq_where s1 s2; intersect_eq_where s1 s2; count_dichotomy s1 (mem_of s2); count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s1 (inv (mem_of s2)) let intersect_with_subset #_ #_ s1 s2 = same_members_means_eq (intersect s1 s2) s1 let lemma_strict_subset_minus_size #_ #_ s1 s2 s = let size_diff : pos = size s2 - size s1 in size_of_minus s s2; size_of_minus s s1; intersect_with_subset s2 s; intersect_is_symmetric s2 s; intersect_with_subset s1 s; intersect_is_symmetric s1 s let lemma_disjoint_union_subset #_ #_ s1 s2 = size_of_union s1 s2 let lemma_subset_union #_ #_ _ _ _ = () let lemma_strict_subset_transitive #_ #_ _ _ _ = () let lemma_intersect_symmetric = intersect_is_symmetric let lemma_intersect_union_empty_aux1 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) (ensures (intersect (union s1 s2) s3 = empty)) = eq_lemma empty (intersect (union s1 s2) s3) let lemma_intersect_union_empty_aux2 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect (union s1 s2) s3 = empty)) (ensures (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) = eq_lemma empty (intersect s1 s3); eq_lemma empty (intersect s2 s3) let lemma_intersect_union_empty #a #f (s1 s2 s3: ordset a f) : Lemma ((intersect (union s1 s2) s3 = empty) = (intersect s1 s3 = empty && intersect s2 s3 = empty)) [SMTPat (intersect (union s1 s2) s3)] = Classical.move_requires_3 (lemma_intersect_union_empty_aux1 #a #f) s1 s2 s3; Classical.move_requires_3 (lemma_intersect_union_empty_aux2 #a #f) s1 s2 s3 let lemma_union_symmetric #a #f s1 s2 = eq_lemma (union s1 s2) (union s2 s1) let union_of_disjoint #a #f s1 s2 = eq_lemma (minus (union s1 s2) s1) s2 let distinct_is_idempotent #a #f s = eq_lemma (distinct f s) s (* Conversion from OrdSet to Set *) module S = FStar.Set let rec as_set #a #f s = match s with | [] -> S.empty | hd::tl -> S.union (S.singleton hd) (as_set #a #f tl)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "abbrev": true, "full_module": "FStar.Set", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Set", "short_module": "S" }, { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.OrdSet.ordset a f -> x: a -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.mem x s <==> FStar.Set.mem x (FStar.OrdSet.as_set s)) [SMTPat (FStar.OrdSet.mem x s); SMTPat (FStar.Set.mem x (FStar.OrdSet.as_set s))]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "Prims.list", "Prims.op_Equality", "Prims.bool", "FStar.OrdSet.lemma_as_set_mem", "Prims.unit" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_as_set_mem #a #f s x =
match s with | [] -> () | hd :: tl -> if x = hd then () else lemma_as_set_mem #a #f tl x
false
FStar.OrdSet.fst
FStar.OrdSet.size_of_union_aux_1
val size_of_union_aux_1 (#a #f: _) (s1 s2: (z: ordset a f {z <> empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2)))
val size_of_union_aux_1 (#a #f: _) (s1 s2: (z: ordset a f {z <> empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2)))
let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2)
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 68, "end_line": 668, "start_col": 0, "start_line": 662 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: z: FStar.OrdSet.ordset a f {z <> FStar.OrdSet.empty} -> s2: z: FStar.OrdSet.ordset a f {z <> FStar.OrdSet.empty} -> FStar.Pervasives.Lemma (requires FStar.OrdSet.head s1 <> FStar.OrdSet.head s2 && f (FStar.OrdSet.head s1) (FStar.OrdSet.head s2) && FStar.OrdSet.size (FStar.OrdSet.union (FStar.OrdSet.tail s1) s2) = FStar.OrdSet.size (FStar.OrdSet.tail s1) + FStar.OrdSet.size s2 - FStar.OrdSet.size (FStar.OrdSet.intersect (FStar.OrdSet.tail s1) s2)) (ensures FStar.OrdSet.size (FStar.OrdSet.union s1 s2) = FStar.OrdSet.size s1 + FStar.OrdSet.size s2 - FStar.OrdSet.size (FStar.OrdSet.intersect s1 s2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "Prims.b2t", "Prims.op_disEquality", "FStar.OrdSet.empty", "FStar.OrdSet.same_members_means_eq", "FStar.OrdSet.intersect", "FStar.OrdSet.tail", "Prims.unit", "FStar.OrdSet.union_of_tails_size", "Prims.op_AmpAmp", "FStar.OrdSet.head", "Prims.op_Equality", "Prims.int", "FStar.OrdSet.size", "FStar.OrdSet.union", "Prims.op_Subtraction", "Prims.op_Addition", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let size_of_union_aux_1 #a #f (s1: (z: ordset a f {z <> empty})) (s2: (z: ordset a f {z <> empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) =
union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2)
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.term_eq
val term_eq : _: FStar.Stubs.Reflection.Types.term -> _: FStar.Stubs.Reflection.Types.term -> FStar.Tactics.Effect.Tac Prims.bool
let term_eq = FStar.Tactics.term_eq_old
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 39, "end_line": 12, "start_col": 0, "start_line": 12 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: FStar.Stubs.Reflection.Types.term -> _: FStar.Stubs.Reflection.Types.term -> FStar.Tactics.Effect.Tac Prims.bool
FStar.Tactics.Effect.Tac
[]
[]
[ "FStar.Stubs.Tactics.V1.Builtins.term_eq_old" ]
[]
false
true
false
false
false
let term_eq =
FStar.Tactics.term_eq_old
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.cast_info_to_string
val cast_info_to_string : cast_info -> Tac string
val cast_info_to_string : cast_info -> Tac string
let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")"
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 56, "end_line": 35, "start_col": 0, "start_line": 32 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
info: FStar.InteractiveHelpers.Effectful.cast_info -> FStar.Tactics.Effect.Tac Prims.string
FStar.Tactics.Effect.Tac
[]
[]
[ "FStar.InteractiveHelpers.Effectful.cast_info", "Prims.op_Hat", "Prims.string", "FStar.InteractiveHelpers.Base.option_to_string", "FStar.InteractiveHelpers.ExploreTerm.type_info", "FStar.InteractiveHelpers.ExploreTerm.type_info_to_string", "FStar.InteractiveHelpers.Effectful.__proj__Mkcast_info__item__exp_ty", "FStar.InteractiveHelpers.Effectful.__proj__Mkcast_info__item__p_ty", "FStar.Stubs.Tactics.V1.Builtins.term_to_string", "FStar.InteractiveHelpers.Effectful.__proj__Mkcast_info__item__term" ]
[]
false
true
false
false
false
let cast_info_to_string info =
"Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")"
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.mk_effect_info
val mk_effect_info : ei_type: FStar.InteractiveHelpers.ExploreTerm.effect_type -> ei_ret_type: FStar.InteractiveHelpers.ExploreTerm.type_info -> ei_pre: FStar.Pervasives.Native.option FStar.Stubs.Reflection.Types.term -> ei_post: FStar.Pervasives.Native.option FStar.Stubs.Reflection.Types.term -> FStar.InteractiveHelpers.Effectful.effect_info
let mk_effect_info = Mkeffect_info
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 34, "end_line": 45, "start_col": 0, "start_line": 45 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ei_type: FStar.InteractiveHelpers.ExploreTerm.effect_type -> ei_ret_type: FStar.InteractiveHelpers.ExploreTerm.type_info -> ei_pre: FStar.Pervasives.Native.option FStar.Stubs.Reflection.Types.term -> ei_post: FStar.Pervasives.Native.option FStar.Stubs.Reflection.Types.term -> FStar.InteractiveHelpers.Effectful.effect_info
Prims.Tot
[ "total" ]
[]
[ "FStar.InteractiveHelpers.Effectful.Mkeffect_info" ]
[]
false
false
false
true
false
let mk_effect_info =
Mkeffect_info
false
FStar.OrdSet.fst
FStar.OrdSet.lemma_intersect_union_empty_aux1
val lemma_intersect_union_empty_aux1 (#a #f: _) (s1 s2 s3: ordset a f) : Lemma (requires (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) (ensures (intersect (union s1 s2) s3 = empty))
val lemma_intersect_union_empty_aux1 (#a #f: _) (s1 s2 s3: ordset a f) : Lemma (requires (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) (ensures (intersect (union s1 s2) s3 = empty))
let lemma_intersect_union_empty_aux1 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) (ensures (intersect (union s1 s2) s3 = empty)) = eq_lemma empty (intersect (union s1 s2) s3)
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 47, "end_line": 722, "start_col": 0, "start_line": 719 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1) let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2) let size_of_union_aux_2 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1 let rec size_of_union #a #f s1 s2 = let size = size #a #f in match s1,s2 with | [], _ -> same_members_means_eq s2 (union s1 s2) | _, [] -> same_members_means_eq s1 (union s1 s2) | h1::(t1:ordset a f), h2::(t2:ordset a f) -> size_of_union t1 s2; size_of_union s1 t2; if h1 = h2 then union_with_prefix h1 t1 t2 else if f h1 h2 then size_of_union_aux_1 s1 s2 else size_of_union_aux_2 s1 s2 let rec count_dichotomy #_ #_ s c = if s<>[] then count_dichotomy (tail s) c let size_of_minus #_ #_ s1 s2 = minus_eq_where s1 s2; intersect_eq_where s1 s2; count_dichotomy s1 (mem_of s2); count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s1 (inv (mem_of s2)) let intersect_with_subset #_ #_ s1 s2 = same_members_means_eq (intersect s1 s2) s1 let lemma_strict_subset_minus_size #_ #_ s1 s2 s = let size_diff : pos = size s2 - size s1 in size_of_minus s s2; size_of_minus s s1; intersect_with_subset s2 s; intersect_is_symmetric s2 s; intersect_with_subset s1 s; intersect_is_symmetric s1 s let lemma_disjoint_union_subset #_ #_ s1 s2 = size_of_union s1 s2 let lemma_subset_union #_ #_ _ _ _ = () let lemma_strict_subset_transitive #_ #_ _ _ _ = () let lemma_intersect_symmetric = intersect_is_symmetric
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> s3: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (requires FStar.OrdSet.intersect s1 s3 == FStar.OrdSet.empty /\ FStar.OrdSet.intersect s2 s3 == FStar.OrdSet.empty) (ensures FStar.OrdSet.intersect (FStar.OrdSet.union s1 s2) s3 = FStar.OrdSet.empty)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.OrdSet.eq_lemma", "FStar.OrdSet.empty", "FStar.OrdSet.intersect", "FStar.OrdSet.union", "Prims.unit", "Prims.l_and", "Prims.eq2", "Prims.squash", "Prims.b2t", "Prims.op_Equality", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let lemma_intersect_union_empty_aux1 #a #f (s1: ordset a f) (s2: ordset a f) (s3: ordset a f) : Lemma (requires (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) (ensures (intersect (union s1 s2) s3 = empty)) =
eq_lemma empty (intersect (union s1 s2) s3)
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.mk_eterm_info
val mk_eterm_info : einfo: FStar.InteractiveHelpers.Effectful.effect_info -> head: FStar.Stubs.Reflection.Types.term -> parameters: Prims.list FStar.InteractiveHelpers.Effectful.cast_info -> FStar.InteractiveHelpers.Effectful.eterm_info
let mk_eterm_info = Mketerm_info
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 32, "end_line": 73, "start_col": 0, "start_line": 73 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]"
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
einfo: FStar.InteractiveHelpers.Effectful.effect_info -> head: FStar.Stubs.Reflection.Types.term -> parameters: Prims.list FStar.InteractiveHelpers.Effectful.cast_info -> FStar.InteractiveHelpers.Effectful.eterm_info
Prims.Tot
[ "total" ]
[]
[ "FStar.InteractiveHelpers.Effectful.Mketerm_info" ]
[]
false
false
false
true
false
let mk_eterm_info =
Mketerm_info
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.effect_info_to_string
val effect_info_to_string : effect_info -> Tac string
val effect_info_to_string : effect_info -> Tac string
let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")"
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 49, "end_line": 54, "start_col": 0, "start_line": 49 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: FStar.InteractiveHelpers.Effectful.effect_info -> FStar.Tactics.Effect.Tac Prims.string
FStar.Tactics.Effect.Tac
[]
[]
[ "FStar.InteractiveHelpers.Effectful.effect_info", "Prims.op_Hat", "Prims.string", "FStar.InteractiveHelpers.ExploreTerm.effect_type_to_string", "FStar.InteractiveHelpers.Effectful.__proj__Mkeffect_info__item__ei_type", "FStar.InteractiveHelpers.Base.option_to_string", "FStar.Stubs.Reflection.Types.term", "FStar.Stubs.Tactics.V1.Builtins.term_to_string", "FStar.InteractiveHelpers.Effectful.__proj__Mkeffect_info__item__ei_post", "FStar.InteractiveHelpers.ExploreTerm.type_info_to_string", "FStar.InteractiveHelpers.Effectful.__proj__Mkeffect_info__item__ei_ret_type", "FStar.InteractiveHelpers.Effectful.__proj__Mkeffect_info__item__ei_pre" ]
[]
false
true
false
false
false
let effect_info_to_string c =
"Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")"
false
FStar.OrdSet.fst
FStar.OrdSet.lemma_as_set_disjoint_left
val lemma_as_set_disjoint_left (#a #f: _) (s1 s2: ordset a f) : Lemma (requires S.disjoint (as_set s1) (as_set s2)) (ensures intersect s1 s2 = empty)
val lemma_as_set_disjoint_left (#a #f: _) (s1 s2: ordset a f) : Lemma (requires S.disjoint (as_set s1) (as_set s2)) (ensures intersect s1 s2 = empty)
let lemma_as_set_disjoint_left #a #f (s1 s2: ordset a f) : Lemma (requires S.disjoint (as_set s1) (as_set s2)) (ensures intersect s1 s2 = empty) = let mem_eq p q : Lemma (S.mem p (as_set q) <==> mem #a #f p q) = () in Classical.forall_intro_2 mem_eq
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 33, "end_line": 768, "start_col": 0, "start_line": 764 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1) let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2) let size_of_union_aux_2 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1 let rec size_of_union #a #f s1 s2 = let size = size #a #f in match s1,s2 with | [], _ -> same_members_means_eq s2 (union s1 s2) | _, [] -> same_members_means_eq s1 (union s1 s2) | h1::(t1:ordset a f), h2::(t2:ordset a f) -> size_of_union t1 s2; size_of_union s1 t2; if h1 = h2 then union_with_prefix h1 t1 t2 else if f h1 h2 then size_of_union_aux_1 s1 s2 else size_of_union_aux_2 s1 s2 let rec count_dichotomy #_ #_ s c = if s<>[] then count_dichotomy (tail s) c let size_of_minus #_ #_ s1 s2 = minus_eq_where s1 s2; intersect_eq_where s1 s2; count_dichotomy s1 (mem_of s2); count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s1 (inv (mem_of s2)) let intersect_with_subset #_ #_ s1 s2 = same_members_means_eq (intersect s1 s2) s1 let lemma_strict_subset_minus_size #_ #_ s1 s2 s = let size_diff : pos = size s2 - size s1 in size_of_minus s s2; size_of_minus s s1; intersect_with_subset s2 s; intersect_is_symmetric s2 s; intersect_with_subset s1 s; intersect_is_symmetric s1 s let lemma_disjoint_union_subset #_ #_ s1 s2 = size_of_union s1 s2 let lemma_subset_union #_ #_ _ _ _ = () let lemma_strict_subset_transitive #_ #_ _ _ _ = () let lemma_intersect_symmetric = intersect_is_symmetric let lemma_intersect_union_empty_aux1 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) (ensures (intersect (union s1 s2) s3 = empty)) = eq_lemma empty (intersect (union s1 s2) s3) let lemma_intersect_union_empty_aux2 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect (union s1 s2) s3 = empty)) (ensures (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) = eq_lemma empty (intersect s1 s3); eq_lemma empty (intersect s2 s3) let lemma_intersect_union_empty #a #f (s1 s2 s3: ordset a f) : Lemma ((intersect (union s1 s2) s3 = empty) = (intersect s1 s3 = empty && intersect s2 s3 = empty)) [SMTPat (intersect (union s1 s2) s3)] = Classical.move_requires_3 (lemma_intersect_union_empty_aux1 #a #f) s1 s2 s3; Classical.move_requires_3 (lemma_intersect_union_empty_aux2 #a #f) s1 s2 s3 let lemma_union_symmetric #a #f s1 s2 = eq_lemma (union s1 s2) (union s2 s1) let union_of_disjoint #a #f s1 s2 = eq_lemma (minus (union s1 s2) s1) s2 let distinct_is_idempotent #a #f s = eq_lemma (distinct f s) s (* Conversion from OrdSet to Set *) module S = FStar.Set let rec as_set #a #f s = match s with | [] -> S.empty | hd::tl -> S.union (S.singleton hd) (as_set #a #f tl) let rec lemma_as_set_mem #a #f s x = match s with | [] -> () | hd::tl -> if x = hd then () else lemma_as_set_mem #a #f tl x let lemma_as_set_disjoint_right #a #f (s1 s2: ordset a f) : Lemma (requires intersect s1 s2 = empty) (ensures S.disjoint (as_set s1) (as_set s2)) = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "abbrev": true, "full_module": "FStar.Set", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Set", "short_module": "S" }, { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (requires FStar.Set.disjoint (FStar.OrdSet.as_set s1) (FStar.OrdSet.as_set s2)) (ensures FStar.OrdSet.intersect s1 s2 = FStar.OrdSet.empty)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.Classical.forall_intro_2", "Prims.l_iff", "Prims.b2t", "FStar.Set.mem", "FStar.OrdSet.as_set", "FStar.OrdSet.mem", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern", "FStar.Set.disjoint", "Prims.op_Equality", "FStar.OrdSet.intersect", "FStar.OrdSet.empty" ]
[]
false
false
true
false
false
let lemma_as_set_disjoint_left #a #f (s1: ordset a f) (s2: ordset a f) : Lemma (requires S.disjoint (as_set s1) (as_set s2)) (ensures intersect s1 s2 = empty) =
let mem_eq p q : Lemma (S.mem p (as_set q) <==> mem #a #f p q) = () in Classical.forall_intro_2 mem_eq
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.eterm_info_to_string
val eterm_info_to_string : eterm_info -> Tac string
val eterm_info_to_string : eterm_info -> Tac string
let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]"
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 18, "end_line": 71, "start_col": 0, "start_line": 65 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
info: FStar.InteractiveHelpers.Effectful.eterm_info -> FStar.Tactics.Effect.Tac Prims.string
FStar.Tactics.Effect.Tac
[]
[]
[ "FStar.InteractiveHelpers.Effectful.eterm_info", "Prims.op_Hat", "Prims.string", "FStar.Stubs.Tactics.V1.Builtins.term_to_string", "FStar.InteractiveHelpers.Effectful.__proj__Mketerm_info__item__head", "FStar.InteractiveHelpers.Effectful.effect_info_to_string", "FStar.InteractiveHelpers.Effectful.__proj__Mketerm_info__item__einfo", "FStar.List.Tot.Base.fold_left", "Prims.list", "FStar.Tactics.Util.map", "FStar.InteractiveHelpers.Effectful.cast_info", "FStar.InteractiveHelpers.Effectful.cast_info_to_string", "FStar.InteractiveHelpers.Effectful.__proj__Mketerm_info__item__parameters" ]
[]
false
true
false
false
false
let eterm_info_to_string info =
let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]"
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.comp_to_effect_info
val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info)
val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info)
let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 33, "end_line": 148, "start_col": 0, "start_line": 146 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> c: FStar.Stubs.Reflection.Types.comp -> FStar.Tactics.Effect.Tac (FStar.Pervasives.Native.option FStar.InteractiveHelpers.Effectful.effect_info)
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.Stubs.Reflection.Types.comp", "FStar.InteractiveHelpers.Effectful.comp_view_to_effect_info", "FStar.Pervasives.Native.option", "FStar.InteractiveHelpers.Effectful.effect_info", "FStar.Stubs.Reflection.V1.Data.comp_view", "FStar.Stubs.Reflection.V1.Builtins.inspect_comp" ]
[]
false
true
false
false
false
let comp_to_effect_info dbg c =
let cv:comp_view = inspect_comp c in comp_view_to_effect_info dbg cv
false
FStar.OrdSet.fst
FStar.OrdSet.lemma_as_set_disjoint
val lemma_as_set_disjoint (#a:eqtype) (#f:cmp a) (s1 s2:ordset a f) : Lemma (intersect s1 s2 = empty <==> S.disjoint (as_set s1) (as_set s2)) [SMTPat (S.disjoint (as_set s1) (as_set s2))]
val lemma_as_set_disjoint (#a:eqtype) (#f:cmp a) (s1 s2:ordset a f) : Lemma (intersect s1 s2 = empty <==> S.disjoint (as_set s1) (as_set s2)) [SMTPat (S.disjoint (as_set s1) (as_set s2))]
let lemma_as_set_disjoint #a #f s1 s2 = Classical.move_requires_2 (lemma_as_set_disjoint_right #a #f) s1 s2; Classical.move_requires_2 (lemma_as_set_disjoint_left #a #f) s1 s2
{ "file_name": "ulib/experimental/FStar.OrdSet.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 68, "end_line": 772, "start_col": 0, "start_line": 770 }
(* Copyright 2008-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. *) module FStar.OrdSet type ordset a f = l:(list a){sorted f l} let hasEq_ordset _ _ = () let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) = match x with | [] -> () | ph::pt -> simple_induction p pt; assert (p (Cons?.tl (ph::pt))) let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f) : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l)) (ensures p x) (decreases List.Tot.Base.length x) = if List.Tot.Base.length x < 2 then () else match x with | ph::pt -> base_induction p pt; assert (p (Cons?.tl (ph::pt))) let empty #_ #_ = [] let tail #a #f s = Cons?.tl s <: ordset a f let head #_ #_ s = Cons?.hd s let mem #_ #_ x s = List.Tot.mem x s (* In case snoc-based List implementation is optimized, we use library ones, but we additionally supply them with relevant postconditions that come from s being an ordset. *) let rec last_direct #a #f (s: ordset a f{s <> empty}) : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) = match s with | [x] -> x | h::g::t -> last_direct (tail s) let last_lib #a #f (s: ordset a f{s <> empty}) = snd (List.Tot.Base.unsnoc s) let last_eq #a #f (s: ordset a f{s <> empty}) : Lemma (last_direct s = last_lib s) = simple_induction (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s let last #a #f s = last_eq s; last_lib s let rec liat_direct #a #f (s: ordset a f{s <> empty}) : (l:ordset a f{ (forall x. mem x l = (mem x s && (x <> last s))) /\ (if tail s <> empty then head s = head l else true) }) = match s with | [x] -> [] | h::g::t -> h::(liat_direct #a #f (g::t)) let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) let liat_eq #a #f (s:ordset a f {s<>empty}) : Lemma (liat_direct s = liat_lib s) = simple_induction (fun p -> if p<>[] then liat_direct p = liat_lib p else true) s let liat #a #f s = liat_eq s; liat_lib s let unsnoc #a #f s = liat_eq s; last_eq s; let l = List.Tot.Base.unsnoc s in (fst l, snd l) let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s val insert': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){let s = as_list s in let l = as_list l in (Cons? l /\ (head #a #f l = x \/ (Cons? s /\ head #a #f l = head #a #f s)))}) let rec insert' #_ #f x s = match s with | [] -> [x] | hd::tl -> if x = hd then hd::tl else if f x hd then x::hd::tl else hd::(insert' #_ #f x tl) let rec distinct' #a f l : Tot (ordset a f) = match l with | [] -> [] | x::t -> insert' x (distinct' f t) let rec insert_mem (#a:eqtype) #f (x:a) (s:ordset a f) : Lemma (mem x (insert' x s)) = if s<>empty then insert_mem #a #f x (tail s) let insert_sub (#a:eqtype) #f x (s:ordset a f) test : Lemma (mem test (insert' x s) = (mem test s || test = x)) = simple_induction (fun p -> mem test (insert' x p) = (mem test p || test = x)) s let rec distinct_props #a (f:cmp a) (l: list a) : Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) = match l with | [] -> () | x::t -> distinct_props f t; Classical.forall_intro (insert_sub x (distinct' f t)) let distinct #a f l = distinct_props f l; distinct' f l let rec union #_ #_ s1 s2 = match s1 with | [] -> s2 | hd::tl -> union tl (insert' hd s2) val remove': #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f -> Tot (l:(ordset a f){ ((Nil? s ==> Nil? l) /\ (Cons? s ==> head s = x ==> l = tail s) /\ (Cons? s ==> head s =!= x ==> (Cons? l /\ head l = Cons?.hd s)))}) let rec remove' #a #f x s = match s with | [] -> [] | hd::(tl: ordset a f) -> if x = hd then tl else hd::(remove' x tl) let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s let liat_length #a #f (s:ordset a f{s<>empty}) : Lemma (size' (liat s) = ((size' s) - 1)) = simple_induction (fun p -> if p<>empty then size' (liat p) = ((size' p)-1) else true) s let rec not_mem_aux (#a:eqtype) (#f:cmp a) (x:a) (s:ordset a f) : Lemma (requires (size' s > 0) && (head s <> x) && (f x (head s))) (ensures not (mem x s)) = if tail s <> [] then not_mem_aux x (tail s) let rec subset' #a #f (s1 s2: ordset a f) = match s1, s2 with | [], _ -> true | hd::tl, hd'::tl' -> if f hd hd' && hd = hd' then subset' #a #f tl tl' else if f hd hd' && not (hd = hd') then false else subset' #a #f s1 tl' | _, _ -> false let tail_is_subset #a #f (s:ordset a f{size' s > 0}) : Lemma (Cons?.tl s `subset'` s) = simple_induction (fun (s:ordset a f) -> size' s=0 || subset' (Cons?.tl s) s) s let self_is_subset #a #f (s:ordset a f) : Lemma (subset' s s) = simple_induction (fun (s:ordset a f) -> subset' s s) s (* returns a pair of (fst z) = (everything from s that goes after x) and (snd z) = (true if x was found in s, false otherwise) *) let rec remove_until_greater_than #a #f x (s: ordset a f) : z:(ordset a f * bool) { (size' (fst z) <= size' s) && (not(mem x (fst z))) && (subset' (fst z) s) && (snd z = mem x s) && (match (fst z) with | [] -> true | h::t -> (sorted f (x::(fst z)))) } = match s with | [] -> ([], false) | h::(t:ordset a f) -> if h=x then begin if size' t > 0 then not_mem_aux x t; tail_is_subset s; (t, true) end else if f x h then begin not_mem_aux x s; self_is_subset s; (s, false) end else remove_until_greater_than x t let rec remove_until_gt_prop #a #f (s: ordset a f) (x:a) (test:a) : Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) = match s with | [] -> () | h::(t:ordset a f) -> let aux (test:a) : Lemma (requires f test x && h<>test) (ensures not (mem test (fst (remove_until_greater_than x s)))) = remove_until_gt_prop #a #f t x test in Classical.move_requires aux test; if h <> x then remove_until_gt_prop t x test let rec remove_until_gt_mem #a #f (s: ordset a f) (x:a) (test:a) : Lemma (mem test (fst (remove_until_greater_than x s)) = ( mem test s && f x test && (x<>test) )) = if size' s > 0 then remove_until_gt_mem (tail s) x test let mem_implies_f #a #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures f (Cons?.hd s) x) = simple_induction (fun s -> mem x s ==> f (head s) x) s (* Smart intersect is the set intersect that accounts for the ordering of both lists, eliminating some checks by trimming leading elements of one of the input lists that are guaranteeed to not belong to the other list. E.g. smart_intersect [1;2;3] [3;4;5] can safely trim [1;2;3] to just [3] upon inspecting the head of [3;4;5]. *) let rec smart_intersect #a #f (s1 s2: ordset a f) : Tot (z:ordset a f{ (forall x. mem x z = (mem x s1 && mem x s2)) /\ (forall (x:a{sorted f (x::s1)}). sorted f (x::z)) /\ (forall (x:a{sorted f (x::s2)}). sorted f (x::z)) }) (decreases size' s1 + size' s2) = match s1 with | [] -> [] | h1::(t1:ordset a f) -> match s2 with | [] -> [] | h2::(t2:ordset a f) -> if h1=h2 then h1::smart_intersect t1 t2 else begin if f h1 h2 then ( let skip1, found = remove_until_greater_than #a #f h2 t1 in let subresult : ordset a f = smart_intersect skip1 t2 in Classical.forall_intro (remove_until_gt_mem t1 h2); Classical.forall_intro (Classical.move_requires (mem_implies_f s2)); if found then h2::subresult else subresult ) else ( let skip2, found = remove_until_greater_than #a #f h1 t2 in let subresult = smart_intersect #a #f t1 skip2 in Classical.forall_intro (remove_until_gt_mem t2 h1); Classical.forall_intro (Classical.move_requires (mem_implies_f s1)); if found then h1::subresult else subresult ) end let intersect #a #f s1 s2 = smart_intersect s1 s2 let choose #a #f s = match s with | [] -> None | x::_ -> Some x let remove #a #f x s = remove' #_ #f x s let size #a #f s = size' s let subset #a #f s1 s2 = subset' s1 s2 let singleton (#a:eqtype) #f x = [x] let mem_of_empty #a #f (s: ordset a f{size s = 0}) (x: a) : Lemma (not (mem x s)) = () let mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma ((mem #a #f x (Cons?.tl s) || (x = Cons?.hd s)) = mem x s) = () let not_mem_of_tail #a #f (s: ordset a f{size s > 0}) (x:a) : Lemma (not (mem x (tail s)) = not (mem x s) || x = head s) = simple_induction (fun s -> mem x s ==> f (head s) x) s let rec set_props #a #f (s:ordset a f) : Lemma (if size s > 0 then ((forall x. mem x (tail s) ==> f (head s) x /\ head s <> x)) else forall x. not (mem x s)) = if (size s > 1) then set_props (tail s) let rec same_members_means_eq #a #f (s1 s2: ordset a f) : Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) = match s1 with | [] -> if size s2>0 then assert (mem (head s2) s2) | h1::t1 -> set_props s1; set_props s2; match s2 with | h2::t2 -> same_members_means_eq #a #f t1 t2 let intersect_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (intersect s1 s2 = intersect s2 s1) = same_members_means_eq (intersect s1 s2) (intersect s2 s1) let remove_until_gt_exclusion #a #f (s:ordset a f) (x:a) (test:a) : Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s))))) (ensures x=test || not (mem test s)) = remove_until_gt_mem s x test let rec mem_implies_subset #a #f (s1 s2: ordset a f) : Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) = match s1 with | [] -> () | h1::(t1:ordset a f) -> set_props s1; set_props s2; mem_implies_subset t1 s2; if (size s2 > 0 && f (head s2) h1) then mem_implies_subset s1 (tail s2) let rec subset_implies_mem #a #f (p q: ordset a f) : Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) = if Cons? p && Cons? q then if head p = head q then subset_implies_mem (tail p) (tail q) else subset_implies_mem p (tail q) let subset_transitivity #a #f (p q r: ordset a f) : Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) = subset_implies_mem p q; subset_implies_mem q r; mem_implies_subset p r let head_is_never_in_tail #a #f (s:ordset a f{size s > 0}) : Lemma (not (mem (head s) (tail s))) = set_props s let rec smart_minus #a #f (p q: ordset a f) : z:ordset a f { ( forall x. mem x z = (mem x p && (not (mem x q)))) /\ (match p,z with | ph::pt, zh::zt -> f ph zh | ph::pt, [] -> subset p q | [], _ -> z = []) } = match p with | [] -> [] | ph::(pt:ordset a f) -> match q with | [] -> p | qh::(qt:ordset a f) -> let q_after_ph, found = remove_until_greater_than ph q in Classical.forall_intro (remove_until_gt_mem q ph); Classical.forall_intro (Classical.move_requires (mem_implies_f p)); if found then begin let result = smart_minus pt q_after_ph in set_props p; if result = [] then begin subset_transitivity pt q_after_ph q; subset_implies_mem pt q; mem_implies_subset p q end; result end else ph::(smart_minus pt q_after_ph) let empty_minus_means_subset #a #f (p q: ordset a f) : Lemma (requires size (smart_minus p q) = 0) (ensures subset p q) = () // a little test versus integers :) let ncmp (x y:nat) = x <= y let _ = assert (smart_minus #nat #ncmp [1;2;3;4] [3] == [1;2;4]) let minus #a #f s1 s2 = smart_minus s1 s2 let strict_subset #a #f s1 s2 = s1 <> s2 && subset s1 s2 let eq_lemma #a #f s1 s2 = same_members_means_eq s1 s2 let mem_empty #_ #_ _ = () let mem_singleton #_ #_ _ _ = () let mem_insert (#a:eqtype) #f (el:a) (s: ordset a f) (x:a) : Lemma (mem x (insert' el s) = (x=el || mem x s)) = simple_induction (fun p -> mem x (insert' el p) = (x=el || mem x p)) s let rec mem_union #_ #_ s1 s2 x = if size s1 > 0 then match s1 with | hd::tl -> mem_union tl (insert' hd s2) x; mem_insert hd s2 x let mem_intersect #_ #f s1 s2 x = () let mem_subset (#a:eqtype) #f s1 s2 = subset_implies_mem s1 s2; mem_implies_subset s1 s2 let choose_empty (#a:eqtype) #f = () let choose_s (#a:eqtype) #f s = () let rec mem_remove (#a:eqtype) #f x y s = if size s > 0 then (set_props s; mem_remove x y (tail s)) let eq_remove (#a:eqtype) #f x s = simple_induction (fun p -> not (mem x p) ==> p = remove x p) s let size_empty (#a:eqtype) #f s = () let rec size_remove (#a:eqtype) #f x s = match s with | hd::tl -> if x<>hd then size_remove #_ #f x tl let size_singleton (#a:eqtype) #f x = () let rec subset_size (#a:eqtype) #f x y = match x, y with | [], _ -> () | hd::tl, hd'::(tl':ordset a f) -> if f hd hd' && hd = hd' then subset_size tl tl' else subset_size x tl' let insert_when_already_exists (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (requires mem x s) (ensures insert' x s == s) = simple_induction (fun p -> mem x p <==> insert' x p = p) s let size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) >= size s) = simple_induction (fun p -> size (insert' x p) >= size p) s let rec precise_size_insert (#a:eqtype) #f (s: ordset a f) (x:a) : Lemma (size (insert' x s) = (if mem x s then size s else (size s) + 1)) = if size s > 0 then precise_size_insert (tail s) x let rec size_of_union_left (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s2) = match s1 with | [] -> () | hd::tl -> size_of_union_left tl (insert' hd s2); precise_size_insert s2 hd let size_of_union_right (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (ensures size (union s1 s2) >= size s1) = eq_lemma (union s1 s2) (union s2 s1); size_of_union_left s2 s1 let size_union #a #f s1 s2 = size_of_union_left s1 s2; size_of_union_right s1 s2 let fold #a #acc #f g init s = List.Tot.fold_left g init s private let rec map_internal (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa) : Pure (ordset b fb) (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures (fun sb -> Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))) = match sa with | [] -> [] | x :: xs -> let y = g x in let ys = map_internal #a #b #fa #fb g xs in if not (Cons? ys) || Cons?.hd ys <> y then y :: ys else ys let rec map_size (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures size (map_internal #a #b #fa #fb g sa) <= size sa) = if size sa > 0 then map_size #a #b #fa #fb g (tail sa) let rec map_as_list (#a #b:eqtype) (#fa:cmp a) (#fb: cmp b) (g: a->b) (sa:ordset a fa) : Lemma (requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y))) (ensures as_list (map_internal #a #b #fa #fb g sa) == FStar.List.Tot.map g (as_list sa)) = match sa with | [] -> () | h::(t:ordset a fa) -> map_as_list #a #b #fa #fb g t let map #a #b #fa #fb g sa = map_size #a #b #fa #fb g sa; map_as_list #a #b #fa #fb g sa; map_internal #a #b #fa #fb g sa let lemma_strict_subset_size #a #f s1 s2 = let eql (p q: ordset a f) : Lemma (requires forall x. mem x p = mem x q) (ensures p=q) = eq_lemma p q in Classical.move_requires_2 eql s1 s2; eliminate exists x. mem x s2 && not (mem x s1) returns size s2 > size s1 with _. begin Classical.forall_intro (mem_insert x s1); precise_size_insert s1 x; assert (subset (insert' x s1) s2) end let lemma_minus_mem #a #f s1 s2 x = () let rec strict_subset_implies_diff_element #a #f (s1 s2: ordset a f) : Lemma (requires strict_subset s1 s2) (ensures exists x. (mem x s2 /\ not (mem x s1))) = match s1,s2 with | [], h::t -> () | h1::t1, h2::t2 -> Classical.move_requires (mem_implies_f s1) h2; if h1=h2 then begin strict_subset_implies_diff_element #a #f t1 t2; set_props s2 end let diff_element_implies_strict_subset #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2 /\ (exists x. (mem x s2 /\ not (mem x s1)))) (ensures strict_subset s1 s2) = () let lemma_strict_subset_exists_diff #a #f (s1 s2: ordset a f) : Lemma (requires subset s1 s2) (ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) = Classical.move_requires_2 strict_subset_implies_diff_element s1 s2 let rec count #a #f s c : nat = match s with | [] -> 0 | h::t -> if c h then 1 + count #a #f t c else count #a #f t c let count_of_empty #_ #_ _ _ = () let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (tail s) c let rec all #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> true | h::t -> c h && all #a #f t c let rec any #a #f (s: ordset a f) (c: condition a) : Tot bool = match s with | [] -> false | h::t -> c h || any #a #f t c let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x let any_if_mem #a #f (s:ordset a f) (c: condition a) x : Lemma (requires mem x s && c x) (ensures any s c) = simple_induction (fun p -> mem x p && c x ==> any p c) s let all_means_not_any_not #a #f s c = simple_induction (fun p -> all p c = not (any p (inv c))) s let rec find_first #a #f s c = match s with | [] -> None | h::(t:ordset a f) -> if c h then Some h else find_first t c let find_first_is_some_iff_any #_ #_ s c = simple_induction (fun p -> Some? (find_first p c) = any p c) s let rec find_first_precedes_any_other #a #f s c x = if head s<>x then find_first_precedes_any_other (tail s) c x; set_props s let liat_size #a #f (s:ordset a f{s<>[]}) : Lemma (size (liat s) = size s - 1) = base_induction (fun p -> if p<>[] then size (liat p) = size p - 1 else true) s let mem_liat #a #f (s:ordset a f{s<>[]}) (x:a) : Lemma (mem x s = (mem x (liat s) || x = last s)) = () let rec any_liat #a #f (s:ordset a f{s<>[]}) (c: condition a) : Lemma (any s c = (any (liat s) c || c (last s))) = match s with | [x] -> () | h::(t:ordset a f) -> if size t > 0 then any_liat t c let rec find_last' #a #f (s: ordset a f) (c: condition a) : Tot (option a) (decreases size s) = if s=empty then None else let liat,last = unsnoc s in liat_size s; if c last then Some last else find_last' liat c let rec find_last_props #a #f (s:ordset a f) (c: condition a) : Lemma (ensures (match find_last' s c with | None -> not (any s c) | Some v -> (any s c /\ (forall (x:a{mem x s && c x}). f x v)))) (decreases size s) = if size s > 0 then let liat,last = unsnoc s in liat_size s; find_last_props liat c; if c last then any_if_mem s c last else any_liat s c let find_last #a #f s c = find_last_props s c; find_last' s c let find_last_is_some_iff_any #a #f s c = find_last_props s c let find_last_follows_any_other #a #f s c x = any_if_mem s c x; find_last_is_some_iff_any s c; find_last_props s c let size_of_tail #a #f s = () let count_of_tail #_ #_ _ _ = () let rec where #a #f s c = match s with | [] -> [] | h::[] -> if c h then [h] else [] | h::(t:ordset a f) -> if c h then h::(where t c) else where t c let intersect_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (mem_of s2)) (intersect s1 s2) let minus_eq_where #_ #_ s1 s2 = same_members_means_eq (where s1 (inv (mem_of s2))) (minus s1 s2) let count_is_size_of_where #_ #_ s c = simple_induction (fun p -> count p c = size (where p c)) s let size_of_intersect #_ #_ s1 s2 = intersect_eq_where s1 s2; intersect_eq_where s2 s1; intersect_is_symmetric s1 s2; count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s2 (mem_of s1) let union_mem_forall #a #f (s1 s2: ordset a f) : Lemma (forall x. (mem x (union s1 s2)) = (mem x s1 || mem x s2)) = let aux x : Lemma (mem x (union s1 s2) = (mem x s1 || mem x s2)) = mem_union s1 s2 x in Classical.forall_intro aux let union_with_empty #a #f (s: ordset a f) : Lemma (union s empty = s) = eq_lemma (union s empty) s let union_head_lemma #a #f (s1 s2: ordset a f) : Lemma (match s1, s2 with | [],[] -> (union s1 s2 = []) | [],h::t -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h::t,[] -> size (union s1 s2) > 0 && Cons?.hd (union s1 s2) = h | h1::t1, h2::t2 -> size (union s1 s2) > 0 && (Cons?.hd (union s1 s2) = (if f h1 h2 then h1 else h2)) ) = match s1,s2 with | [],[] -> () | [],h::t -> () | h::t,[] -> union_with_empty s1 | h1::t1, h2::t2 -> union_mem_forall s1 s2; set_props s1; set_props s2; set_props (union s1 s2) let union_sort_lemma (#a:eqtype) #f (h:a) (t1 t2: ordset a f) : Lemma (requires sorted f (h::t1) /\ sorted f (h::t2)) (ensures sorted f (h::(union t1 t2))) = if size t1 = 0 then union_with_empty t2 else if size t2 = 0 then union_with_empty t1 else begin union_mem_forall t1 t2; set_props t1; set_props t2; set_props (union t1 t2) end let union_with_prefix (#a:eqtype) #f (h:a) (t1 t2: (z:ordset a f{sorted f (h::z)})) : Lemma (union #a #f (h::t1) (h::t2) = h::(union t1 t2)) = union_mem_forall t1 t2; union_sort_lemma h t1 t2; same_members_means_eq (union #a #f (h::t1) (h::t2)) (h::(union t1 t2)) let union_of_tails_size (#a:eqtype) #f (s1 s2: ordset a f) : Lemma (requires size s1 > 0 && size s2 > 0 && (Cons?.hd s1 <> Cons?.hd s2) && f (Cons?.hd s1) (Cons?.hd s2)) (ensures size (union s1 s2) = 1 + size (union #a #f (Cons?.tl s1) s2)) = match s1 with | h1::(t1:ordset a f) -> match s2 with | h2::(t2:ordset a f) -> union_mem_forall t1 s2; set_props s1; set_props s2; same_members_means_eq (h1::(union t1 s2)) (union s1 s2) let union_is_symmetric #a #f (s1 s2: ordset a f) : Lemma (union s1 s2 = union s2 s1) = same_members_means_eq (union s1 s2) (union s2 s1) let size_of_union_aux_1 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && (f (head s1) (head s2)) && (size (union (tail s1) s2) = size (tail s1) + size s2 - size (intersect (tail s1) s2))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = union_of_tails_size s1 s2; same_members_means_eq (intersect (tail s1) s2) (intersect s1 s2) let size_of_union_aux_2 #a #f (s1 s2: (z:ordset a f{z<>empty})) : Lemma (requires (head s1) <> (head s2) && not (f (head s1) (head s2)) && (size (union s1 (tail s2)) = size s1 + size (tail s2) - size (intersect s1 (tail s2)))) (ensures size (union s1 s2) = (size s1 + size s2 - size (intersect s1 s2))) = Classical.forall_intro_2 (union_is_symmetric #a #f); Classical.forall_intro_2 (intersect_is_symmetric #a #f); size_of_union_aux_1 s2 s1 let rec size_of_union #a #f s1 s2 = let size = size #a #f in match s1,s2 with | [], _ -> same_members_means_eq s2 (union s1 s2) | _, [] -> same_members_means_eq s1 (union s1 s2) | h1::(t1:ordset a f), h2::(t2:ordset a f) -> size_of_union t1 s2; size_of_union s1 t2; if h1 = h2 then union_with_prefix h1 t1 t2 else if f h1 h2 then size_of_union_aux_1 s1 s2 else size_of_union_aux_2 s1 s2 let rec count_dichotomy #_ #_ s c = if s<>[] then count_dichotomy (tail s) c let size_of_minus #_ #_ s1 s2 = minus_eq_where s1 s2; intersect_eq_where s1 s2; count_dichotomy s1 (mem_of s2); count_is_size_of_where s1 (mem_of s2); count_is_size_of_where s1 (inv (mem_of s2)) let intersect_with_subset #_ #_ s1 s2 = same_members_means_eq (intersect s1 s2) s1 let lemma_strict_subset_minus_size #_ #_ s1 s2 s = let size_diff : pos = size s2 - size s1 in size_of_minus s s2; size_of_minus s s1; intersect_with_subset s2 s; intersect_is_symmetric s2 s; intersect_with_subset s1 s; intersect_is_symmetric s1 s let lemma_disjoint_union_subset #_ #_ s1 s2 = size_of_union s1 s2 let lemma_subset_union #_ #_ _ _ _ = () let lemma_strict_subset_transitive #_ #_ _ _ _ = () let lemma_intersect_symmetric = intersect_is_symmetric let lemma_intersect_union_empty_aux1 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) (ensures (intersect (union s1 s2) s3 = empty)) = eq_lemma empty (intersect (union s1 s2) s3) let lemma_intersect_union_empty_aux2 #a #f (s1 s2 s3: ordset a f) : Lemma (requires (intersect (union s1 s2) s3 = empty)) (ensures (intersect s1 s3 == empty /\ intersect s2 s3 == empty)) = eq_lemma empty (intersect s1 s3); eq_lemma empty (intersect s2 s3) let lemma_intersect_union_empty #a #f (s1 s2 s3: ordset a f) : Lemma ((intersect (union s1 s2) s3 = empty) = (intersect s1 s3 = empty && intersect s2 s3 = empty)) [SMTPat (intersect (union s1 s2) s3)] = Classical.move_requires_3 (lemma_intersect_union_empty_aux1 #a #f) s1 s2 s3; Classical.move_requires_3 (lemma_intersect_union_empty_aux2 #a #f) s1 s2 s3 let lemma_union_symmetric #a #f s1 s2 = eq_lemma (union s1 s2) (union s2 s1) let union_of_disjoint #a #f s1 s2 = eq_lemma (minus (union s1 s2) s1) s2 let distinct_is_idempotent #a #f s = eq_lemma (distinct f s) s (* Conversion from OrdSet to Set *) module S = FStar.Set let rec as_set #a #f s = match s with | [] -> S.empty | hd::tl -> S.union (S.singleton hd) (as_set #a #f tl) let rec lemma_as_set_mem #a #f s x = match s with | [] -> () | hd::tl -> if x = hd then () else lemma_as_set_mem #a #f tl x let lemma_as_set_disjoint_right #a #f (s1 s2: ordset a f) : Lemma (requires intersect s1 s2 = empty) (ensures S.disjoint (as_set s1) (as_set s2)) = () let lemma_as_set_disjoint_left #a #f (s1 s2: ordset a f) : Lemma (requires S.disjoint (as_set s1) (as_set s2)) (ensures intersect s1 s2 = empty) = let mem_eq p q : Lemma (S.mem p (as_set q) <==> mem #a #f p q) = () in Classical.forall_intro_2 mem_eq
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Base.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "FStar.OrdSet.fst" }
[ { "abbrev": true, "full_module": "FStar.Set", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Set", "short_module": "S" }, { "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> FStar.Pervasives.Lemma (ensures FStar.OrdSet.intersect s1 s2 = FStar.OrdSet.empty <==> FStar.Set.disjoint (FStar.OrdSet.as_set s1) (FStar.OrdSet.as_set s2)) [SMTPat (FStar.Set.disjoint (FStar.OrdSet.as_set s1) (FStar.OrdSet.as_set s2))]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.OrdSet.cmp", "FStar.OrdSet.ordset", "FStar.Classical.move_requires_2", "FStar.Set.disjoint", "FStar.OrdSet.as_set", "Prims.b2t", "Prims.op_Equality", "FStar.OrdSet.intersect", "FStar.OrdSet.empty", "FStar.OrdSet.lemma_as_set_disjoint_left", "Prims.unit", "FStar.OrdSet.lemma_as_set_disjoint_right" ]
[]
false
false
true
false
false
let lemma_as_set_disjoint #a #f s1 s2 =
Classical.move_requires_2 (lemma_as_set_disjoint_right #a #f) s1 s2; Classical.move_requires_2 (lemma_as_set_disjoint_left #a #f) s1 s2
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.tcc_no_lift
val tcc_no_lift : env -> term -> Tac comp
val tcc_no_lift : env -> term -> Tac comp
let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 11, "end_line": 199, "start_col": 0, "start_line": 191 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
e: FStar.Stubs.Reflection.Types.env -> t: FStar.Stubs.Reflection.Types.term -> FStar.Tactics.Effect.Tac FStar.Stubs.Reflection.Types.comp
FStar.Tactics.Effect.Tac
[]
[]
[ "FStar.Stubs.Reflection.Types.env", "FStar.Stubs.Reflection.Types.term", "FStar.Stubs.Reflection.V1.Data.argv", "Prims.list", "FStar.InteractiveHelpers.ExploreTerm.inst_comp", "FStar.List.Tot.Base.map", "FStar.Pervasives.Native.fst", "FStar.Stubs.Reflection.V1.Data.aqualv", "FStar.Stubs.Reflection.Types.comp", "FStar.Stubs.Tactics.V1.Builtins.tcc", "FStar.Pervasives.Native.tuple2", "FStar.Tactics.V1.SyntaxHelpers.collect_app", "FStar.Stubs.Reflection.V1.Data.term_view", "FStar.Stubs.Tactics.V1.Builtins.inspect" ]
[]
false
true
false
false
false
let tcc_no_lift e t =
match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> tcc e t
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.has_refinement
val has_refinement (ty: type_info) : Tot bool
val has_refinement (ty: type_info) : Tot bool
let has_refinement (ty:type_info) : Tot bool = Some? ty.refin
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 16, "end_line": 227, "start_col": 0, "start_line": 226 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ty: FStar.InteractiveHelpers.ExploreTerm.type_info -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "FStar.InteractiveHelpers.ExploreTerm.type_info", "FStar.Pervasives.Native.uu___is_Some", "FStar.Stubs.Reflection.Types.term", "FStar.InteractiveHelpers.ExploreTerm.__proj__Mktype_info__item__refin", "Prims.bool" ]
[]
false
false
false
true
false
let has_refinement (ty: type_info) : Tot bool =
Some? ty.refin
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.get_rawest_type
val get_rawest_type (ty: type_info) : Tot typ
val get_rawest_type (ty: type_info) : Tot typ
let get_rawest_type (ty:type_info) : Tot typ = ty.ty
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 7, "end_line": 236, "start_col": 0, "start_line": 235 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ty: FStar.InteractiveHelpers.ExploreTerm.type_info -> FStar.Stubs.Reflection.Types.typ
Prims.Tot
[ "total" ]
[]
[ "FStar.InteractiveHelpers.ExploreTerm.type_info", "FStar.InteractiveHelpers.ExploreTerm.__proj__Mktype_info__item__ty", "FStar.Stubs.Reflection.Types.typ" ]
[]
false
false
false
true
false
let get_rawest_type (ty: type_info) : Tot typ =
ty.ty
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.get_opt_refinment
val get_opt_refinment (ty: type_info) : Tot (option term)
val get_opt_refinment (ty: type_info) : Tot (option term)
let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 10, "end_line": 233, "start_col": 0, "start_line": 232 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ty: FStar.InteractiveHelpers.ExploreTerm.type_info -> FStar.Pervasives.Native.option FStar.Stubs.Reflection.Types.term
Prims.Tot
[ "total" ]
[]
[ "FStar.InteractiveHelpers.ExploreTerm.type_info", "FStar.InteractiveHelpers.ExploreTerm.__proj__Mktype_info__item__refin", "FStar.Pervasives.Native.option", "FStar.Stubs.Reflection.Types.term" ]
[]
false
false
false
true
false
let get_opt_refinment (ty: type_info) : Tot (option term) =
ty.refin
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.cast_info_list_to_propositions
val cast_info_list_to_propositions : bool -> genv -> list cast_info -> Tac (list proposition)
val cast_info_list_to_propositions : bool -> genv -> list cast_info -> Tac (list proposition)
let cast_info_list_to_propositions dbg ge ls = let lsl = map (cast_info_to_propositions dbg ge) ls in flatten lsl
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 13, "end_line": 349, "start_col": 0, "start_line": 347 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown (*/// Retrieve the list of types from the parameters stored in ``typ_or_comp``. val typ_or_comp_to_param_types : typ_or_comp -> Tot (list typ) let typ_or_comp_to_param_types c = let pl = params_of_typ_or_comp c in List.Tot.map type_of_binder pl *) (**** Step 2 *) /// The retrieved type refinements and post-conditions are not instantiated (they /// are lambda functions): instantiate them to get propositions. /// Generate a term of the form [has_type x ty] val mk_has_type : term -> typ -> Tac term let mk_has_type t ty = let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params // TODO: I don't understand why I need ifuel 2 here #push-options "--ifuel 2" /// Generate the propositions equivalent to a correct type cast. /// Note that the type refinements need to be instantiated. val cast_info_to_propositions : bool -> genv -> cast_info -> Tac (list proposition) let cast_info_to_propositions dbg ge ci = print_dbg dbg ("[> cast_info_to_propositions:\n" ^ cast_info_to_string ci); match compare_cast_types dbg ci with | Refines -> print_dbg dbg ("-> Comparison result: Refines"); [] | Same_raw_type -> print_dbg dbg ("-> Comparison result: Same_raw_type"); let refin = get_refinement (Some?.v ci.exp_ty) in let inst_refin = mk_app_norm ge.env refin [ci.term] in [inst_refin] | Unknown -> print_dbg dbg ("-> Comparison result: Unknown"); match ci.p_ty, ci.exp_ty with | Some p_ty, Some e_ty -> let p_rty = get_rawest_type p_ty in let e_rty = get_rawest_type e_ty in (* For the type cast, we generate an assertion of the form: * [> has_type (p <: type_of_p) expected_type * The reason is that we want the user to know which parameter is * concerned (hence the ``has_type``), and which types should be * related (hence the ascription). *) let ascr_term = pack (Tv_AscribedT ci.term p_rty None false) in let has_type_params = [(p_rty, Q_Implicit); (ascr_term, Q_Explicit); (e_rty, Q_Explicit)] in let type_cast = mk_app (`has_type) has_type_params in (* Expected type's refinement *) let inst_opt_refin = opt_mk_app_norm ge.env e_ty.refin [ci.term] in opt_cons inst_opt_refin [type_cast] | _ -> [] #pop-options /// Generates a list of propositions from a list of ``cast_info``. Note that /// the user should revert the list before printing the propositions.
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> ge: FStar.InteractiveHelpers.Base.genv -> ls: Prims.list FStar.InteractiveHelpers.Effectful.cast_info -> FStar.Tactics.Effect.Tac (Prims.list FStar.InteractiveHelpers.Propositions.proposition)
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.InteractiveHelpers.Base.genv", "Prims.list", "FStar.InteractiveHelpers.Effectful.cast_info", "FStar.List.Tot.Base.flatten", "FStar.InteractiveHelpers.Propositions.proposition", "FStar.Tactics.Util.map", "FStar.InteractiveHelpers.Effectful.cast_info_to_propositions" ]
[]
false
true
false
false
false
let cast_info_list_to_propositions dbg ge ls =
let lsl = map (cast_info_to_propositions dbg ge) ls in flatten lsl
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.mk_has_type
val mk_has_type : term -> typ -> Tac term
val mk_has_type : term -> typ -> Tac term
let mk_has_type t ty = let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 27, "end_line": 304, "start_col": 0, "start_line": 302 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown (*/// Retrieve the list of types from the parameters stored in ``typ_or_comp``. val typ_or_comp_to_param_types : typ_or_comp -> Tot (list typ) let typ_or_comp_to_param_types c = let pl = params_of_typ_or_comp c in List.Tot.map type_of_binder pl *) (**** Step 2 *) /// The retrieved type refinements and post-conditions are not instantiated (they /// are lambda functions): instantiate them to get propositions. /// Generate a term of the form [has_type x ty]
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
t: FStar.Stubs.Reflection.Types.term -> ty: FStar.Stubs.Reflection.Types.typ -> FStar.Tactics.Effect.Tac FStar.Stubs.Reflection.Types.term
FStar.Tactics.Effect.Tac
[]
[]
[ "FStar.Stubs.Reflection.Types.term", "FStar.Stubs.Reflection.Types.typ", "FStar.Reflection.V1.Derived.mk_app", "Prims.list", "FStar.Stubs.Reflection.V1.Data.argv", "Prims.Cons", "FStar.Pervasives.Native.Mktuple2", "FStar.Stubs.Reflection.V1.Data.aqualv", "FStar.Stubs.Reflection.V1.Data.Q_Implicit", "FStar.Stubs.Reflection.V1.Data.Q_Explicit", "Prims.Nil" ]
[]
false
true
false
false
false
let mk_has_type t ty =
let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.opt_remove_refin
val opt_remove_refin : typ -> Tac typ
val opt_remove_refin : typ -> Tac typ
let opt_remove_refin ty = match inspect ty with | Tv_Refine _ sort _ -> sort | _ -> ty
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 11, "end_line": 391, "start_col": 0, "start_line": 388 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown (*/// Retrieve the list of types from the parameters stored in ``typ_or_comp``. val typ_or_comp_to_param_types : typ_or_comp -> Tot (list typ) let typ_or_comp_to_param_types c = let pl = params_of_typ_or_comp c in List.Tot.map type_of_binder pl *) (**** Step 2 *) /// The retrieved type refinements and post-conditions are not instantiated (they /// are lambda functions): instantiate them to get propositions. /// Generate a term of the form [has_type x ty] val mk_has_type : term -> typ -> Tac term let mk_has_type t ty = let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params // TODO: I don't understand why I need ifuel 2 here #push-options "--ifuel 2" /// Generate the propositions equivalent to a correct type cast. /// Note that the type refinements need to be instantiated. val cast_info_to_propositions : bool -> genv -> cast_info -> Tac (list proposition) let cast_info_to_propositions dbg ge ci = print_dbg dbg ("[> cast_info_to_propositions:\n" ^ cast_info_to_string ci); match compare_cast_types dbg ci with | Refines -> print_dbg dbg ("-> Comparison result: Refines"); [] | Same_raw_type -> print_dbg dbg ("-> Comparison result: Same_raw_type"); let refin = get_refinement (Some?.v ci.exp_ty) in let inst_refin = mk_app_norm ge.env refin [ci.term] in [inst_refin] | Unknown -> print_dbg dbg ("-> Comparison result: Unknown"); match ci.p_ty, ci.exp_ty with | Some p_ty, Some e_ty -> let p_rty = get_rawest_type p_ty in let e_rty = get_rawest_type e_ty in (* For the type cast, we generate an assertion of the form: * [> has_type (p <: type_of_p) expected_type * The reason is that we want the user to know which parameter is * concerned (hence the ``has_type``), and which types should be * related (hence the ascription). *) let ascr_term = pack (Tv_AscribedT ci.term p_rty None false) in let has_type_params = [(p_rty, Q_Implicit); (ascr_term, Q_Explicit); (e_rty, Q_Explicit)] in let type_cast = mk_app (`has_type) has_type_params in (* Expected type's refinement *) let inst_opt_refin = opt_mk_app_norm ge.env e_ty.refin [ci.term] in opt_cons inst_opt_refin [type_cast] | _ -> [] #pop-options /// Generates a list of propositions from a list of ``cast_info``. Note that /// the user should revert the list before printing the propositions. val cast_info_list_to_propositions : bool -> genv -> list cast_info -> Tac (list proposition) let cast_info_list_to_propositions dbg ge ls = let lsl = map (cast_info_to_propositions dbg ge) ls in flatten lsl /// When dealing with unknown effects, we try to guess what the pre and post-conditions /// are. The effects are indeed very likely to have a pre and a post-condition, /// and to be parameterized with an internal effect state, so that the pre and posts /// have the following shapes: /// - pre : STATE -> Type0 /// - post : STATE -> RET -> STATE -> Type0 /// Or (no state/pure): /// - pre : Type0 /// - post : RET -> Type0 /// We try to detect that with the following functions: noeq type pre_post_type = | PP_Unknown | PP_Pure | PP_State : (state_type:term) -> pre_post_type val compute_pre_type : bool -> env -> term -> Tac pre_post_type let compute_pre_type dbg e pre = print_dbg dbg "[> compute_pre_type"; match safe_tc e pre with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [], true -> print_dbg dbg "PP_Pure"; PP_Pure | [b], true -> print_dbg dbg ("PP_State " ^ (term_to_string (type_of_binder b))); PP_State (type_of_binder b) | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ty: FStar.Stubs.Reflection.Types.typ -> FStar.Tactics.Effect.Tac FStar.Stubs.Reflection.Types.typ
FStar.Tactics.Effect.Tac
[]
[]
[ "FStar.Stubs.Reflection.Types.typ", "FStar.Stubs.Reflection.Types.bv", "FStar.Stubs.Reflection.Types.term", "FStar.Stubs.Reflection.V1.Data.term_view", "FStar.Stubs.Tactics.V1.Builtins.inspect" ]
[]
false
true
false
false
false
let opt_remove_refin ty =
match inspect ty with | Tv_Refine _ sort _ -> sort | _ -> ty
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.check_pre_post_type
val check_pre_post_type : bool -> env -> term -> term -> term -> Tac pre_post_type
val check_pre_post_type : bool -> env -> term -> term -> term -> Tac pre_post_type
let check_pre_post_type dbg e pre ret_type post = print_dbg dbg "[> check_pre_post_type"; match compute_pre_type dbg e pre, compute_post_type dbg e ret_type post with | PP_Pure, PP_Pure -> print_dbg dbg "PP_Pure, PP_Pure"; PP_Pure | PP_State ty1, PP_State ty2 -> print_dbg dbg "PP_State, PP_State"; if term_eq ty1 ty2 then PP_State ty1 else PP_Unknown | _ -> print_dbg dbg "_, _"; PP_Unknown
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 14, "end_line": 457, "start_col": 0, "start_line": 446 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown (*/// Retrieve the list of types from the parameters stored in ``typ_or_comp``. val typ_or_comp_to_param_types : typ_or_comp -> Tot (list typ) let typ_or_comp_to_param_types c = let pl = params_of_typ_or_comp c in List.Tot.map type_of_binder pl *) (**** Step 2 *) /// The retrieved type refinements and post-conditions are not instantiated (they /// are lambda functions): instantiate them to get propositions. /// Generate a term of the form [has_type x ty] val mk_has_type : term -> typ -> Tac term let mk_has_type t ty = let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params // TODO: I don't understand why I need ifuel 2 here #push-options "--ifuel 2" /// Generate the propositions equivalent to a correct type cast. /// Note that the type refinements need to be instantiated. val cast_info_to_propositions : bool -> genv -> cast_info -> Tac (list proposition) let cast_info_to_propositions dbg ge ci = print_dbg dbg ("[> cast_info_to_propositions:\n" ^ cast_info_to_string ci); match compare_cast_types dbg ci with | Refines -> print_dbg dbg ("-> Comparison result: Refines"); [] | Same_raw_type -> print_dbg dbg ("-> Comparison result: Same_raw_type"); let refin = get_refinement (Some?.v ci.exp_ty) in let inst_refin = mk_app_norm ge.env refin [ci.term] in [inst_refin] | Unknown -> print_dbg dbg ("-> Comparison result: Unknown"); match ci.p_ty, ci.exp_ty with | Some p_ty, Some e_ty -> let p_rty = get_rawest_type p_ty in let e_rty = get_rawest_type e_ty in (* For the type cast, we generate an assertion of the form: * [> has_type (p <: type_of_p) expected_type * The reason is that we want the user to know which parameter is * concerned (hence the ``has_type``), and which types should be * related (hence the ascription). *) let ascr_term = pack (Tv_AscribedT ci.term p_rty None false) in let has_type_params = [(p_rty, Q_Implicit); (ascr_term, Q_Explicit); (e_rty, Q_Explicit)] in let type_cast = mk_app (`has_type) has_type_params in (* Expected type's refinement *) let inst_opt_refin = opt_mk_app_norm ge.env e_ty.refin [ci.term] in opt_cons inst_opt_refin [type_cast] | _ -> [] #pop-options /// Generates a list of propositions from a list of ``cast_info``. Note that /// the user should revert the list before printing the propositions. val cast_info_list_to_propositions : bool -> genv -> list cast_info -> Tac (list proposition) let cast_info_list_to_propositions dbg ge ls = let lsl = map (cast_info_to_propositions dbg ge) ls in flatten lsl /// When dealing with unknown effects, we try to guess what the pre and post-conditions /// are. The effects are indeed very likely to have a pre and a post-condition, /// and to be parameterized with an internal effect state, so that the pre and posts /// have the following shapes: /// - pre : STATE -> Type0 /// - post : STATE -> RET -> STATE -> Type0 /// Or (no state/pure): /// - pre : Type0 /// - post : RET -> Type0 /// We try to detect that with the following functions: noeq type pre_post_type = | PP_Unknown | PP_Pure | PP_State : (state_type:term) -> pre_post_type val compute_pre_type : bool -> env -> term -> Tac pre_post_type let compute_pre_type dbg e pre = print_dbg dbg "[> compute_pre_type"; match safe_tc e pre with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [], true -> print_dbg dbg "PP_Pure"; PP_Pure | [b], true -> print_dbg dbg ("PP_State " ^ (term_to_string (type_of_binder b))); PP_State (type_of_binder b) | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val opt_remove_refin : typ -> Tac typ let opt_remove_refin ty = match inspect ty with | Tv_Refine _ sort _ -> sort | _ -> ty val compute_post_type : bool -> env -> term -> term -> Tac pre_post_type let compute_post_type dbg e ret_type post = print_dbg dbg "[> compute_post_type"; let get_tot_ret_type c : Tac (option term_view) = match get_total_or_gtotal_ret_type c with | Some ret_ty -> Some (inspect ret_ty) | None -> None in match safe_tc e post with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [r], true -> (* Pure *) print_dbg dbg "PP_Pure"; PP_Pure | [s1; r; s2], true -> (* Stateful: check that the state types are coherent *) let r_ty = type_of_binder r in let s1_ty = type_of_binder s1 in (* After testing with Stack: the final state seems to have a refinement * (which gives the postcondition) so we need to remove it to match * it against the initial state *) let s2_ty = opt_remove_refin (type_of_binder s2) in let ret_type_eq = term_eq ret_type r_ty in let state_type_eq = term_eq s1_ty s2_ty in print_dbg dbg ("- ret type:\n-- target: " ^ term_to_string ret_type ^ "\n-- binder: " ^ term_to_string r_ty); print_dbg dbg ("- state types:\n-- binder1: " ^ term_to_string s1_ty ^ "\n-- binder2: " ^ term_to_string s2_ty); print_dbg dbg ("- ret type equality: " ^ string_of_bool ret_type_eq); print_dbg dbg ("- state types equality: " ^ string_of_bool state_type_eq); if ret_type_eq && state_type_eq then begin print_dbg dbg ("PP_State" ^ term_to_string (type_of_binder s1)); PP_State (type_of_binder s1) end else begin print_dbg dbg "PP_Unknown"; PP_Unknown end | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> e: FStar.Stubs.Reflection.Types.env -> pre: FStar.Stubs.Reflection.Types.term -> ret_type: FStar.Stubs.Reflection.Types.term -> post: FStar.Stubs.Reflection.Types.term -> FStar.Tactics.Effect.Tac FStar.InteractiveHelpers.Effectful.pre_post_type
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.Stubs.Reflection.Types.env", "FStar.Stubs.Reflection.Types.term", "FStar.InteractiveHelpers.Effectful.PP_Pure", "FStar.InteractiveHelpers.Effectful.pre_post_type", "Prims.unit", "FStar.InteractiveHelpers.Base.print_dbg", "FStar.InteractiveHelpers.Effectful.PP_State", "FStar.InteractiveHelpers.Effectful.PP_Unknown", "FStar.InteractiveHelpers.Effectful.term_eq", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "FStar.InteractiveHelpers.Effectful.compute_post_type", "FStar.InteractiveHelpers.Effectful.compute_pre_type" ]
[]
false
true
false
false
false
let check_pre_post_type dbg e pre ret_type post =
print_dbg dbg "[> check_pre_post_type"; match compute_pre_type dbg e pre, compute_post_type dbg e ret_type post with | PP_Pure, PP_Pure -> print_dbg dbg "PP_Pure, PP_Pure"; PP_Pure | PP_State ty1, PP_State ty2 -> print_dbg dbg "PP_State, PP_State"; if term_eq ty1 ty2 then PP_State ty1 else PP_Unknown | _ -> print_dbg dbg "_, _"; PP_Unknown
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.is_let_st_get
val is_let_st_get : dbg: Prims.bool -> t: FStar.Stubs.Reflection.V1.Data.term_view -> FStar.Tactics.Effect.Tac (FStar.Pervasives.Native.option (FStar.Stubs.Reflection.Types.bv * FStar.Stubs.Reflection.Types.typ))
let is_let_st_get dbg (t : term_view) = print_dbg dbg ("[> is_let_st_get:\n" ^ term_to_string t); match t with | Tv_Let recf attrs bv ty def body -> print_dbg dbg "The term is a let expression"; if is_st_get dbg def then Some (bv, ty) else None | _ -> print_dbg dbg "The term is not a let expression"; None
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 8, "end_line": 535, "start_col": 0, "start_line": 527 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown (*/// Retrieve the list of types from the parameters stored in ``typ_or_comp``. val typ_or_comp_to_param_types : typ_or_comp -> Tot (list typ) let typ_or_comp_to_param_types c = let pl = params_of_typ_or_comp c in List.Tot.map type_of_binder pl *) (**** Step 2 *) /// The retrieved type refinements and post-conditions are not instantiated (they /// are lambda functions): instantiate them to get propositions. /// Generate a term of the form [has_type x ty] val mk_has_type : term -> typ -> Tac term let mk_has_type t ty = let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params // TODO: I don't understand why I need ifuel 2 here #push-options "--ifuel 2" /// Generate the propositions equivalent to a correct type cast. /// Note that the type refinements need to be instantiated. val cast_info_to_propositions : bool -> genv -> cast_info -> Tac (list proposition) let cast_info_to_propositions dbg ge ci = print_dbg dbg ("[> cast_info_to_propositions:\n" ^ cast_info_to_string ci); match compare_cast_types dbg ci with | Refines -> print_dbg dbg ("-> Comparison result: Refines"); [] | Same_raw_type -> print_dbg dbg ("-> Comparison result: Same_raw_type"); let refin = get_refinement (Some?.v ci.exp_ty) in let inst_refin = mk_app_norm ge.env refin [ci.term] in [inst_refin] | Unknown -> print_dbg dbg ("-> Comparison result: Unknown"); match ci.p_ty, ci.exp_ty with | Some p_ty, Some e_ty -> let p_rty = get_rawest_type p_ty in let e_rty = get_rawest_type e_ty in (* For the type cast, we generate an assertion of the form: * [> has_type (p <: type_of_p) expected_type * The reason is that we want the user to know which parameter is * concerned (hence the ``has_type``), and which types should be * related (hence the ascription). *) let ascr_term = pack (Tv_AscribedT ci.term p_rty None false) in let has_type_params = [(p_rty, Q_Implicit); (ascr_term, Q_Explicit); (e_rty, Q_Explicit)] in let type_cast = mk_app (`has_type) has_type_params in (* Expected type's refinement *) let inst_opt_refin = opt_mk_app_norm ge.env e_ty.refin [ci.term] in opt_cons inst_opt_refin [type_cast] | _ -> [] #pop-options /// Generates a list of propositions from a list of ``cast_info``. Note that /// the user should revert the list before printing the propositions. val cast_info_list_to_propositions : bool -> genv -> list cast_info -> Tac (list proposition) let cast_info_list_to_propositions dbg ge ls = let lsl = map (cast_info_to_propositions dbg ge) ls in flatten lsl /// When dealing with unknown effects, we try to guess what the pre and post-conditions /// are. The effects are indeed very likely to have a pre and a post-condition, /// and to be parameterized with an internal effect state, so that the pre and posts /// have the following shapes: /// - pre : STATE -> Type0 /// - post : STATE -> RET -> STATE -> Type0 /// Or (no state/pure): /// - pre : Type0 /// - post : RET -> Type0 /// We try to detect that with the following functions: noeq type pre_post_type = | PP_Unknown | PP_Pure | PP_State : (state_type:term) -> pre_post_type val compute_pre_type : bool -> env -> term -> Tac pre_post_type let compute_pre_type dbg e pre = print_dbg dbg "[> compute_pre_type"; match safe_tc e pre with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [], true -> print_dbg dbg "PP_Pure"; PP_Pure | [b], true -> print_dbg dbg ("PP_State " ^ (term_to_string (type_of_binder b))); PP_State (type_of_binder b) | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val opt_remove_refin : typ -> Tac typ let opt_remove_refin ty = match inspect ty with | Tv_Refine _ sort _ -> sort | _ -> ty val compute_post_type : bool -> env -> term -> term -> Tac pre_post_type let compute_post_type dbg e ret_type post = print_dbg dbg "[> compute_post_type"; let get_tot_ret_type c : Tac (option term_view) = match get_total_or_gtotal_ret_type c with | Some ret_ty -> Some (inspect ret_ty) | None -> None in match safe_tc e post with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [r], true -> (* Pure *) print_dbg dbg "PP_Pure"; PP_Pure | [s1; r; s2], true -> (* Stateful: check that the state types are coherent *) let r_ty = type_of_binder r in let s1_ty = type_of_binder s1 in (* After testing with Stack: the final state seems to have a refinement * (which gives the postcondition) so we need to remove it to match * it against the initial state *) let s2_ty = opt_remove_refin (type_of_binder s2) in let ret_type_eq = term_eq ret_type r_ty in let state_type_eq = term_eq s1_ty s2_ty in print_dbg dbg ("- ret type:\n-- target: " ^ term_to_string ret_type ^ "\n-- binder: " ^ term_to_string r_ty); print_dbg dbg ("- state types:\n-- binder1: " ^ term_to_string s1_ty ^ "\n-- binder2: " ^ term_to_string s2_ty); print_dbg dbg ("- ret type equality: " ^ string_of_bool ret_type_eq); print_dbg dbg ("- state types equality: " ^ string_of_bool state_type_eq); if ret_type_eq && state_type_eq then begin print_dbg dbg ("PP_State" ^ term_to_string (type_of_binder s1)); PP_State (type_of_binder s1) end else begin print_dbg dbg "PP_Unknown"; PP_Unknown end | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val check_pre_post_type : bool -> env -> term -> term -> term -> Tac pre_post_type let check_pre_post_type dbg e pre ret_type post = print_dbg dbg "[> check_pre_post_type"; match compute_pre_type dbg e pre, compute_post_type dbg e ret_type post with | PP_Pure, PP_Pure -> print_dbg dbg "PP_Pure, PP_Pure"; PP_Pure | PP_State ty1, PP_State ty2 -> print_dbg dbg "PP_State, PP_State"; if term_eq ty1 ty2 then PP_State ty1 else PP_Unknown | _ -> print_dbg dbg "_, _"; PP_Unknown val check_opt_pre_post_type : bool -> env -> option term -> term -> option term -> Tac (option pre_post_type) let check_opt_pre_post_type dbg e opt_pre ret_type opt_post = print_dbg dbg "[> check_opt_pre_post_type"; match opt_pre, opt_post with | Some pre, Some post -> print_dbg dbg "Some pre, Some post"; Some (check_pre_post_type dbg e pre ret_type post) | Some pre, None -> print_dbg dbg "Some pre, None"; Some (compute_pre_type dbg e pre) | None, Some post -> print_dbg dbg "None, Some post"; Some (compute_post_type dbg e ret_type post) | None, None -> print_dbg dbg "None, None"; None val _introduce_variables_for_abs : genv -> typ -> Tac (list term & list binder & genv) let rec _introduce_variables_for_abs ge ty = match inspect ty with | Tv_Arrow b c -> let ge1, b1 = genv_push_fresh_binder ge ("__" ^ name_of_binder b) (type_of_binder b) in let bv1 = bv_of_binder b1 in let v1 = pack (Tv_Var bv1) in begin match get_total_or_gtotal_ret_type c with | Some ty1 -> let vl, bl, ge2 = _introduce_variables_for_abs ge1 ty1 in v1 :: vl, b1 :: bl, ge2 | None -> [v1], [b1], ge1 end | _ -> [], [], ge val introduce_variables_for_abs : genv -> term -> Tac (list term & list binder & genv) let introduce_variables_for_abs ge tm = match safe_tc ge.env tm with | Some ty -> _introduce_variables_for_abs ge ty | None -> [], [], ge val introduce_variables_for_opt_abs : genv -> option term -> Tac (list term & list binder & genv) let introduce_variables_for_opt_abs ge opt_tm = match opt_tm with | Some tm -> introduce_variables_for_abs ge tm | None -> [], [], ge val effect_type_is_stateful : effect_type -> Tot bool let effect_type_is_stateful etype = match etype with | E_Total | E_GTotal | E_Lemma | E_PURE | E_Pure -> false | E_Stack | E_ST | E_Unknown -> true let is_st_get dbg t : Tac bool = print_dbg dbg ("[> is_st_get:\n" ^ term_to_string t); match inspect t with | Tv_App hd (a, qual) -> print_dbg dbg "-> Is Tv_App"; begin match inspect hd with | Tv_FVar fv -> print_dbg dbg ("-> Head is Tv_FVar: " ^ fv_to_string fv); fv_eq_name fv ["FStar"; "HyperStack"; "ST"; "get"] | _ -> print_dbg dbg "-> Head is not Tv_FVar"; false end | _ -> print_dbg dbg "-> Is not Tv_App"; false
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> t: FStar.Stubs.Reflection.V1.Data.term_view -> FStar.Tactics.Effect.Tac (FStar.Pervasives.Native.option (FStar.Stubs.Reflection.Types.bv * FStar.Stubs.Reflection.Types.typ))
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.Stubs.Reflection.V1.Data.term_view", "Prims.list", "FStar.Stubs.Reflection.Types.term", "FStar.Stubs.Reflection.Types.bv", "FStar.Stubs.Reflection.Types.typ", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.option", "FStar.InteractiveHelpers.Effectful.is_st_get", "Prims.unit", "FStar.InteractiveHelpers.Base.print_dbg", "Prims.string", "Prims.op_Hat", "FStar.Stubs.Tactics.V1.Builtins.term_to_string", "FStar.Stubs.Tactics.V1.Builtins.pack" ]
[]
false
true
false
false
false
let is_let_st_get dbg (t: term_view) =
print_dbg dbg ("[> is_let_st_get:\n" ^ term_to_string t); match t with | Tv_Let recf attrs bv ty def body -> print_dbg dbg "The term is a let expression"; if is_st_get dbg def then Some (bv, ty) else None | _ -> print_dbg dbg "The term is not a let expression"; None
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.compute_effect_info
val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info)
val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info)
let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 16, "end_line": 155, "start_col": 0, "start_line": 152 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> e: FStar.Stubs.Reflection.Types.env -> tm: FStar.Stubs.Reflection.Types.term -> FStar.Tactics.Effect.Tac (FStar.Pervasives.Native.option FStar.InteractiveHelpers.Effectful.effect_info)
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.Stubs.Reflection.Types.env", "FStar.Stubs.Reflection.Types.term", "FStar.Stubs.Reflection.Types.comp", "FStar.InteractiveHelpers.Effectful.comp_to_effect_info", "FStar.Pervasives.Native.option", "FStar.InteractiveHelpers.Effectful.effect_info", "FStar.Pervasives.Native.None", "FStar.InteractiveHelpers.ExploreTerm.safe_tcc" ]
[]
false
true
false
false
false
let compute_effect_info dbg e tm =
match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.typ_or_comp_to_effect_info
val typ_or_comp_to_effect_info (dbg: bool) (ge: genv) (c: typ_or_comp) : Tac effect_info
val typ_or_comp_to_effect_info (dbg: bool) (ge: genv) (c: typ_or_comp) : Tac effect_info
let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 25, "end_line": 179, "start_col": 0, "start_line": 159 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> ge: FStar.InteractiveHelpers.Base.genv -> c: FStar.InteractiveHelpers.ExploreTerm.typ_or_comp -> FStar.Tactics.Effect.Tac FStar.InteractiveHelpers.Effectful.effect_info
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.InteractiveHelpers.Base.genv", "FStar.InteractiveHelpers.ExploreTerm.typ_or_comp", "FStar.Stubs.Reflection.Types.typ", "Prims.list", "FStar.Stubs.Reflection.Types.binder", "Prims.nat", "FStar.InteractiveHelpers.Effectful.mk_effect_info", "FStar.InteractiveHelpers.ExploreTerm.E_Total", "FStar.Pervasives.Native.None", "FStar.Stubs.Reflection.Types.term", "FStar.InteractiveHelpers.Effectful.effect_info", "FStar.InteractiveHelpers.ExploreTerm.type_info", "FStar.InteractiveHelpers.ExploreTerm.get_type_info_from_type", "FStar.Stubs.Reflection.Types.comp", "FStar.InteractiveHelpers.Base.mfail", "Prims.string", "Prims.op_Hat", "FStar.InteractiveHelpers.Base.acomp_to_string", "FStar.Pervasives.Native.option", "FStar.InteractiveHelpers.Effectful.comp_to_effect_info", "Prims.b2t", "Prims.op_Equality", "Prims.int", "FStar.InteractiveHelpers.ExploreTerm.num_unflushed_of_typ_or_comp", "FStar.InteractiveHelpers.ExploreTerm.flush_typ_or_comp", "FStar.InteractiveHelpers.Base.__proj__Mkgenv__item__env" ]
[]
false
true
false
false
false
let typ_or_comp_to_effect_info (dbg: bool) (ge: genv) (c: typ_or_comp) : Tac effect_info =
let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.type_comparison_to_string
val type_comparison_to_string : c: FStar.InteractiveHelpers.Effectful.type_comparison -> Prims.string
let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown"
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 24, "end_line": 246, "start_col": 0, "start_line": 242 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: FStar.InteractiveHelpers.Effectful.type_comparison -> Prims.string
Prims.Tot
[ "total" ]
[]
[ "FStar.InteractiveHelpers.Effectful.type_comparison", "Prims.string" ]
[]
false
false
false
true
false
let type_comparison_to_string c =
match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown"
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.get_refinement
val get_refinement (ty: type_info{Some? ty.refin}) : Tot term
val get_refinement (ty: type_info{Some? ty.refin}) : Tot term
let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 18, "end_line": 230, "start_col": 0, "start_line": 229 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ty: FStar.InteractiveHelpers.ExploreTerm.type_info{Some? (Mktype_info?.refin ty)} -> FStar.Stubs.Reflection.Types.term
Prims.Tot
[ "total" ]
[]
[ "FStar.InteractiveHelpers.ExploreTerm.type_info", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "FStar.Stubs.Reflection.Types.term", "FStar.InteractiveHelpers.ExploreTerm.__proj__Mktype_info__item__refin", "FStar.Pervasives.Native.__proj__Some__item__v" ]
[]
false
false
false
false
false
let get_refinement (ty: type_info{Some? ty.refin}) : Tot term =
Some?.v ty.refin
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.cast_info_to_propositions
val cast_info_to_propositions : bool -> genv -> cast_info -> Tac (list proposition)
val cast_info_to_propositions : bool -> genv -> cast_info -> Tac (list proposition)
let cast_info_to_propositions dbg ge ci = print_dbg dbg ("[> cast_info_to_propositions:\n" ^ cast_info_to_string ci); match compare_cast_types dbg ci with | Refines -> print_dbg dbg ("-> Comparison result: Refines"); [] | Same_raw_type -> print_dbg dbg ("-> Comparison result: Same_raw_type"); let refin = get_refinement (Some?.v ci.exp_ty) in let inst_refin = mk_app_norm ge.env refin [ci.term] in [inst_refin] | Unknown -> print_dbg dbg ("-> Comparison result: Unknown"); match ci.p_ty, ci.exp_ty with | Some p_ty, Some e_ty -> let p_rty = get_rawest_type p_ty in let e_rty = get_rawest_type e_ty in (* For the type cast, we generate an assertion of the form: * [> has_type (p <: type_of_p) expected_type * The reason is that we want the user to know which parameter is * concerned (hence the ``has_type``), and which types should be * related (hence the ascription). *) let ascr_term = pack (Tv_AscribedT ci.term p_rty None false) in let has_type_params = [(p_rty, Q_Implicit); (ascr_term, Q_Explicit); (e_rty, Q_Explicit)] in let type_cast = mk_app (`has_type) has_type_params in (* Expected type's refinement *) let inst_opt_refin = opt_mk_app_norm ge.env e_ty.refin [ci.term] in opt_cons inst_opt_refin [type_cast] | _ -> []
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 13, "end_line": 341, "start_col": 0, "start_line": 312 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown (*/// Retrieve the list of types from the parameters stored in ``typ_or_comp``. val typ_or_comp_to_param_types : typ_or_comp -> Tot (list typ) let typ_or_comp_to_param_types c = let pl = params_of_typ_or_comp c in List.Tot.map type_of_binder pl *) (**** Step 2 *) /// The retrieved type refinements and post-conditions are not instantiated (they /// are lambda functions): instantiate them to get propositions. /// Generate a term of the form [has_type x ty] val mk_has_type : term -> typ -> Tac term let mk_has_type t ty = let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params // TODO: I don't understand why I need ifuel 2 here #push-options "--ifuel 2" /// Generate the propositions equivalent to a correct type cast. /// Note that the type refinements need to be instantiated.
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> ge: FStar.InteractiveHelpers.Base.genv -> ci: FStar.InteractiveHelpers.Effectful.cast_info -> FStar.Tactics.Effect.Tac (Prims.list FStar.InteractiveHelpers.Propositions.proposition)
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.InteractiveHelpers.Base.genv", "FStar.InteractiveHelpers.Effectful.cast_info", "Prims.Nil", "FStar.InteractiveHelpers.Propositions.proposition", "Prims.list", "Prims.unit", "FStar.InteractiveHelpers.Base.print_dbg", "Prims.Cons", "FStar.Stubs.Reflection.Types.term", "FStar.InteractiveHelpers.Base.mk_app_norm", "FStar.InteractiveHelpers.Base.__proj__Mkgenv__item__env", "FStar.InteractiveHelpers.Effectful.__proj__Mkcast_info__item__term", "FStar.InteractiveHelpers.Effectful.get_refinement", "FStar.Pervasives.Native.__proj__Some__item__v", "FStar.InteractiveHelpers.ExploreTerm.type_info", "FStar.InteractiveHelpers.Effectful.__proj__Mkcast_info__item__exp_ty", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.option", "FStar.InteractiveHelpers.Effectful.__proj__Mkcast_info__item__p_ty", "FStar.InteractiveHelpers.Base.opt_cons", "FStar.InteractiveHelpers.Base.opt_mk_app_norm", "FStar.InteractiveHelpers.ExploreTerm.__proj__Mktype_info__item__refin", "FStar.Reflection.V1.Derived.mk_app", "FStar.Stubs.Reflection.V1.Data.argv", "FStar.Stubs.Reflection.V1.Data.aqualv", "FStar.Stubs.Reflection.V1.Data.Q_Implicit", "FStar.Stubs.Reflection.V1.Data.Q_Explicit", "FStar.Stubs.Tactics.V1.Builtins.pack", "FStar.Stubs.Reflection.V1.Data.Tv_AscribedT", "FStar.Pervasives.Native.None", "FStar.Stubs.Reflection.Types.typ", "FStar.InteractiveHelpers.Effectful.get_rawest_type", "FStar.Pervasives.Native.tuple2", "FStar.InteractiveHelpers.Effectful.type_comparison", "Prims.l_and", "Prims.l_imp", "Prims.l_or", "Prims.b2t", "Prims.op_Equality", "FStar.InteractiveHelpers.Effectful.Refines", "FStar.InteractiveHelpers.Effectful.Same_raw_type", "FStar.Pervasives.Native.uu___is_Some", "FStar.InteractiveHelpers.Effectful.has_refinement", "FStar.InteractiveHelpers.Effectful.compare_cast_types", "Prims.string", "Prims.op_Hat", "FStar.InteractiveHelpers.Effectful.cast_info_to_string" ]
[]
false
true
false
false
false
let cast_info_to_propositions dbg ge ci =
print_dbg dbg ("[> cast_info_to_propositions:\n" ^ cast_info_to_string ci); match compare_cast_types dbg ci with | Refines -> print_dbg dbg ("-> Comparison result: Refines"); [] | Same_raw_type -> print_dbg dbg ("-> Comparison result: Same_raw_type"); let refin = get_refinement (Some?.v ci.exp_ty) in let inst_refin = mk_app_norm ge.env refin [ci.term] in [inst_refin] | Unknown -> print_dbg dbg ("-> Comparison result: Unknown"); match ci.p_ty, ci.exp_ty with | Some p_ty, Some e_ty -> let p_rty = get_rawest_type p_ty in let e_rty = get_rawest_type e_ty in let ascr_term = pack (Tv_AscribedT ci.term p_rty None false) in let has_type_params = [(p_rty, Q_Implicit); (ascr_term, Q_Explicit); (e_rty, Q_Explicit)] in let type_cast = mk_app (`has_type) has_type_params in let inst_opt_refin = opt_mk_app_norm ge.env e_ty.refin [ci.term] in opt_cons inst_opt_refin [type_cast] | _ -> []
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.comp_view_to_effect_info
val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info)
val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info)
let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 7, "end_line": 142, "start_col": 0, "start_line": 111 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> cv: FStar.Stubs.Reflection.V1.Data.comp_view -> FStar.Tactics.Effect.Tac (FStar.Pervasives.Native.option FStar.InteractiveHelpers.Effectful.effect_info)
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.Stubs.Reflection.V1.Data.comp_view", "FStar.Stubs.Reflection.Types.typ", "FStar.Pervasives.Native.Some", "FStar.InteractiveHelpers.Effectful.effect_info", "FStar.InteractiveHelpers.Effectful.mk_effect_info", "FStar.InteractiveHelpers.ExploreTerm.E_Total", "FStar.Pervasives.Native.None", "FStar.Stubs.Reflection.Types.term", "FStar.Pervasives.Native.option", "FStar.InteractiveHelpers.ExploreTerm.type_info", "FStar.InteractiveHelpers.ExploreTerm.get_type_info_from_type", "FStar.InteractiveHelpers.ExploreTerm.E_Lemma", "FStar.InteractiveHelpers.ExploreTerm.unit_type_info", "FStar.InteractiveHelpers.Base.prettify_term", "FStar.Stubs.Reflection.V1.Data.universes", "FStar.Stubs.Reflection.Types.name", "Prims.list", "FStar.Stubs.Reflection.V1.Data.argv", "FStar.Pervasives.Native.Mktuple2", "FStar.InteractiveHelpers.ExploreTerm.effect_type", "FStar.Pervasives.Native.tuple2", "FStar.Stubs.Reflection.V1.Data.aqualv", "FStar.Tactics.Util.map", "FStar.InteractiveHelpers.ExploreTerm.effect_name_to_type", "Prims.unit", "FStar.InteractiveHelpers.Base.print_dbg", "Prims.op_Hat", "FStar.Reflection.V1.Derived.flatten_name" ]
[]
false
true
false
false
false
let comp_view_to_effect_info dbg cv =
match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x, a) -> (prettify_term dbg x, a)) eff_args in match etype, eff_args with | E_PURE, [pre, _] -> Some (mk_res (Some pre) None) | E_Pure, [pre, _ ; post, _] | E_Stack, [pre, _ ; post, _] | E_ST, [pre, _ ; post, _] -> Some (mk_res (Some pre) (Some post)) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [pre, _] -> Some (mk_res (Some pre) None) | E_Unknown, [pre, _ ; post, _] -> Some (mk_res (Some pre) (Some post)) | _ -> None
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.compute_eterm_info
val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info
val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info
let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 16, "end_line": 220, "start_col": 0, "start_line": 205 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> e: FStar.Stubs.Reflection.Types.env -> t: FStar.Stubs.Reflection.Types.term -> FStar.Tactics.Effect.Tac FStar.InteractiveHelpers.Effectful.eterm_info
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.Stubs.Reflection.Types.env", "FStar.Stubs.Reflection.Types.term", "Prims.list", "FStar.InteractiveHelpers.Effectful.cast_info", "FStar.Tactics.V1.Derived.try_with", "FStar.InteractiveHelpers.Effectful.eterm_info", "Prims.unit", "FStar.InteractiveHelpers.Base.mfail", "Prims.string", "Prims.op_Hat", "FStar.Stubs.Tactics.V1.Builtins.term_to_string", "FStar.InteractiveHelpers.Effectful.effect_info", "FStar.InteractiveHelpers.Effectful.mk_eterm_info", "FStar.Pervasives.Native.option", "FStar.InteractiveHelpers.Effectful.comp_to_effect_info", "FStar.Stubs.Reflection.Types.comp", "FStar.InteractiveHelpers.Effectful.tcc_no_lift", "Prims.exn", "FStar.Tactics.Effect.raise", "FStar.Pervasives.Native.tuple2", "FStar.InteractiveHelpers.Effectful.decompose_application" ]
[]
false
true
false
false
false
let compute_eterm_info (dbg: bool) (e: env) (t: term) =
let hd, parameters = decompose_application e t in try let c:comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.decompose_application_aux
val decompose_application_aux (e: env) (t: term) : Tac (term & list cast_info)
val decompose_application_aux (e: env) (t: term) : Tac (term & list cast_info)
let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, []
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 14, "end_line": 101, "start_col": 0, "start_line": 81 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
e: FStar.Stubs.Reflection.Types.env -> t: FStar.Stubs.Reflection.Types.term -> FStar.Tactics.Effect.Tac (FStar.Stubs.Reflection.Types.term * Prims.list FStar.InteractiveHelpers.Effectful.cast_info)
FStar.Tactics.Effect.Tac
[]
[]
[ "FStar.Stubs.Reflection.Types.env", "FStar.Stubs.Reflection.Types.term", "FStar.Stubs.Reflection.V1.Data.aqualv", "Prims.list", "FStar.InteractiveHelpers.Effectful.cast_info", "FStar.Pervasives.Native.Mktuple2", "Prims.Cons", "FStar.InteractiveHelpers.Effectful.mk_cast_info", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.option", "FStar.InteractiveHelpers.ExploreTerm.type_info", "FStar.Pervasives.Native.None", "FStar.Stubs.Reflection.Types.binder", "FStar.Stubs.Reflection.Types.comp", "FStar.Pervasives.Native.Some", "FStar.InteractiveHelpers.ExploreTerm.get_type_info_from_type", "FStar.Stubs.Reflection.Types.typ", "FStar.Tactics.V1.Derived.binder_sort", "FStar.Stubs.Reflection.V1.Data.term_view", "FStar.Stubs.Tactics.V1.Builtins.inspect", "FStar.InteractiveHelpers.ExploreTerm.safe_tc", "FStar.InteractiveHelpers.ExploreTerm.get_type_info", "FStar.InteractiveHelpers.Effectful.decompose_application_aux", "Prims.Nil" ]
[ "recursion" ]
false
true
false
false
false
let rec decompose_application_aux (e: env) (t: term) : Tac (term & list cast_info) =
match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in let a_type = get_type_info e a in let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, []
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.compute_pre_type
val compute_pre_type : bool -> env -> term -> Tac pre_post_type
val compute_pre_type : bool -> env -> term -> Tac pre_post_type
let compute_pre_type dbg e pre = print_dbg dbg "[> compute_pre_type"; match safe_tc e pre with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [], true -> print_dbg dbg "PP_Pure"; PP_Pure | [b], true -> print_dbg dbg ("PP_State " ^ (term_to_string (type_of_binder b))); PP_State (type_of_binder b) | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 16, "end_line": 385, "start_col": 0, "start_line": 366 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown (*/// Retrieve the list of types from the parameters stored in ``typ_or_comp``. val typ_or_comp_to_param_types : typ_or_comp -> Tot (list typ) let typ_or_comp_to_param_types c = let pl = params_of_typ_or_comp c in List.Tot.map type_of_binder pl *) (**** Step 2 *) /// The retrieved type refinements and post-conditions are not instantiated (they /// are lambda functions): instantiate them to get propositions. /// Generate a term of the form [has_type x ty] val mk_has_type : term -> typ -> Tac term let mk_has_type t ty = let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params // TODO: I don't understand why I need ifuel 2 here #push-options "--ifuel 2" /// Generate the propositions equivalent to a correct type cast. /// Note that the type refinements need to be instantiated. val cast_info_to_propositions : bool -> genv -> cast_info -> Tac (list proposition) let cast_info_to_propositions dbg ge ci = print_dbg dbg ("[> cast_info_to_propositions:\n" ^ cast_info_to_string ci); match compare_cast_types dbg ci with | Refines -> print_dbg dbg ("-> Comparison result: Refines"); [] | Same_raw_type -> print_dbg dbg ("-> Comparison result: Same_raw_type"); let refin = get_refinement (Some?.v ci.exp_ty) in let inst_refin = mk_app_norm ge.env refin [ci.term] in [inst_refin] | Unknown -> print_dbg dbg ("-> Comparison result: Unknown"); match ci.p_ty, ci.exp_ty with | Some p_ty, Some e_ty -> let p_rty = get_rawest_type p_ty in let e_rty = get_rawest_type e_ty in (* For the type cast, we generate an assertion of the form: * [> has_type (p <: type_of_p) expected_type * The reason is that we want the user to know which parameter is * concerned (hence the ``has_type``), and which types should be * related (hence the ascription). *) let ascr_term = pack (Tv_AscribedT ci.term p_rty None false) in let has_type_params = [(p_rty, Q_Implicit); (ascr_term, Q_Explicit); (e_rty, Q_Explicit)] in let type_cast = mk_app (`has_type) has_type_params in (* Expected type's refinement *) let inst_opt_refin = opt_mk_app_norm ge.env e_ty.refin [ci.term] in opt_cons inst_opt_refin [type_cast] | _ -> [] #pop-options /// Generates a list of propositions from a list of ``cast_info``. Note that /// the user should revert the list before printing the propositions. val cast_info_list_to_propositions : bool -> genv -> list cast_info -> Tac (list proposition) let cast_info_list_to_propositions dbg ge ls = let lsl = map (cast_info_to_propositions dbg ge) ls in flatten lsl /// When dealing with unknown effects, we try to guess what the pre and post-conditions /// are. The effects are indeed very likely to have a pre and a post-condition, /// and to be parameterized with an internal effect state, so that the pre and posts /// have the following shapes: /// - pre : STATE -> Type0 /// - post : STATE -> RET -> STATE -> Type0 /// Or (no state/pure): /// - pre : Type0 /// - post : RET -> Type0 /// We try to detect that with the following functions: noeq type pre_post_type = | PP_Unknown | PP_Pure | PP_State : (state_type:term) -> pre_post_type
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> e: FStar.Stubs.Reflection.Types.env -> pre: FStar.Stubs.Reflection.Types.term -> FStar.Tactics.Effect.Tac FStar.InteractiveHelpers.Effectful.pre_post_type
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.Stubs.Reflection.Types.env", "FStar.Stubs.Reflection.Types.term", "FStar.InteractiveHelpers.Effectful.PP_Unknown", "FStar.InteractiveHelpers.Effectful.pre_post_type", "Prims.unit", "FStar.InteractiveHelpers.Base.print_dbg", "Prims.list", "FStar.Stubs.Reflection.Types.binder", "FStar.Stubs.Reflection.Types.comp", "FStar.Pervasives.Native.Mktuple2", "FStar.InteractiveHelpers.ExploreTerm.is_total_or_gtotal", "FStar.InteractiveHelpers.Effectful.PP_Pure", "FStar.InteractiveHelpers.Effectful.PP_State", "FStar.Reflection.V1.Derived.type_of_binder", "Prims.string", "Prims.op_Hat", "FStar.Stubs.Tactics.V1.Builtins.term_to_string", "FStar.Pervasives.Native.tuple2", "Prims.string_of_int", "FStar.List.Tot.Base.length", "FStar.Tactics.V1.SyntaxHelpers.collect_arr_bs", "FStar.Pervasives.Native.option", "FStar.InteractiveHelpers.ExploreTerm.safe_tc" ]
[]
false
true
false
false
false
let compute_pre_type dbg e pre =
print_dbg dbg "[> compute_pre_type"; match safe_tc e pre with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [], true -> print_dbg dbg "PP_Pure"; PP_Pure | [b], true -> print_dbg dbg ("PP_State " ^ (term_to_string (type_of_binder b))); PP_State (type_of_binder b) | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.effect_type_is_stateful
val effect_type_is_stateful : effect_type -> Tot bool
val effect_type_is_stateful : effect_type -> Tot bool
let effect_type_is_stateful etype = match etype with | E_Total | E_GTotal | E_Lemma | E_PURE | E_Pure -> false | E_Stack | E_ST | E_Unknown -> true
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 38, "end_line": 508, "start_col": 0, "start_line": 505 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown (*/// Retrieve the list of types from the parameters stored in ``typ_or_comp``. val typ_or_comp_to_param_types : typ_or_comp -> Tot (list typ) let typ_or_comp_to_param_types c = let pl = params_of_typ_or_comp c in List.Tot.map type_of_binder pl *) (**** Step 2 *) /// The retrieved type refinements and post-conditions are not instantiated (they /// are lambda functions): instantiate them to get propositions. /// Generate a term of the form [has_type x ty] val mk_has_type : term -> typ -> Tac term let mk_has_type t ty = let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params // TODO: I don't understand why I need ifuel 2 here #push-options "--ifuel 2" /// Generate the propositions equivalent to a correct type cast. /// Note that the type refinements need to be instantiated. val cast_info_to_propositions : bool -> genv -> cast_info -> Tac (list proposition) let cast_info_to_propositions dbg ge ci = print_dbg dbg ("[> cast_info_to_propositions:\n" ^ cast_info_to_string ci); match compare_cast_types dbg ci with | Refines -> print_dbg dbg ("-> Comparison result: Refines"); [] | Same_raw_type -> print_dbg dbg ("-> Comparison result: Same_raw_type"); let refin = get_refinement (Some?.v ci.exp_ty) in let inst_refin = mk_app_norm ge.env refin [ci.term] in [inst_refin] | Unknown -> print_dbg dbg ("-> Comparison result: Unknown"); match ci.p_ty, ci.exp_ty with | Some p_ty, Some e_ty -> let p_rty = get_rawest_type p_ty in let e_rty = get_rawest_type e_ty in (* For the type cast, we generate an assertion of the form: * [> has_type (p <: type_of_p) expected_type * The reason is that we want the user to know which parameter is * concerned (hence the ``has_type``), and which types should be * related (hence the ascription). *) let ascr_term = pack (Tv_AscribedT ci.term p_rty None false) in let has_type_params = [(p_rty, Q_Implicit); (ascr_term, Q_Explicit); (e_rty, Q_Explicit)] in let type_cast = mk_app (`has_type) has_type_params in (* Expected type's refinement *) let inst_opt_refin = opt_mk_app_norm ge.env e_ty.refin [ci.term] in opt_cons inst_opt_refin [type_cast] | _ -> [] #pop-options /// Generates a list of propositions from a list of ``cast_info``. Note that /// the user should revert the list before printing the propositions. val cast_info_list_to_propositions : bool -> genv -> list cast_info -> Tac (list proposition) let cast_info_list_to_propositions dbg ge ls = let lsl = map (cast_info_to_propositions dbg ge) ls in flatten lsl /// When dealing with unknown effects, we try to guess what the pre and post-conditions /// are. The effects are indeed very likely to have a pre and a post-condition, /// and to be parameterized with an internal effect state, so that the pre and posts /// have the following shapes: /// - pre : STATE -> Type0 /// - post : STATE -> RET -> STATE -> Type0 /// Or (no state/pure): /// - pre : Type0 /// - post : RET -> Type0 /// We try to detect that with the following functions: noeq type pre_post_type = | PP_Unknown | PP_Pure | PP_State : (state_type:term) -> pre_post_type val compute_pre_type : bool -> env -> term -> Tac pre_post_type let compute_pre_type dbg e pre = print_dbg dbg "[> compute_pre_type"; match safe_tc e pre with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [], true -> print_dbg dbg "PP_Pure"; PP_Pure | [b], true -> print_dbg dbg ("PP_State " ^ (term_to_string (type_of_binder b))); PP_State (type_of_binder b) | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val opt_remove_refin : typ -> Tac typ let opt_remove_refin ty = match inspect ty with | Tv_Refine _ sort _ -> sort | _ -> ty val compute_post_type : bool -> env -> term -> term -> Tac pre_post_type let compute_post_type dbg e ret_type post = print_dbg dbg "[> compute_post_type"; let get_tot_ret_type c : Tac (option term_view) = match get_total_or_gtotal_ret_type c with | Some ret_ty -> Some (inspect ret_ty) | None -> None in match safe_tc e post with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [r], true -> (* Pure *) print_dbg dbg "PP_Pure"; PP_Pure | [s1; r; s2], true -> (* Stateful: check that the state types are coherent *) let r_ty = type_of_binder r in let s1_ty = type_of_binder s1 in (* After testing with Stack: the final state seems to have a refinement * (which gives the postcondition) so we need to remove it to match * it against the initial state *) let s2_ty = opt_remove_refin (type_of_binder s2) in let ret_type_eq = term_eq ret_type r_ty in let state_type_eq = term_eq s1_ty s2_ty in print_dbg dbg ("- ret type:\n-- target: " ^ term_to_string ret_type ^ "\n-- binder: " ^ term_to_string r_ty); print_dbg dbg ("- state types:\n-- binder1: " ^ term_to_string s1_ty ^ "\n-- binder2: " ^ term_to_string s2_ty); print_dbg dbg ("- ret type equality: " ^ string_of_bool ret_type_eq); print_dbg dbg ("- state types equality: " ^ string_of_bool state_type_eq); if ret_type_eq && state_type_eq then begin print_dbg dbg ("PP_State" ^ term_to_string (type_of_binder s1)); PP_State (type_of_binder s1) end else begin print_dbg dbg "PP_Unknown"; PP_Unknown end | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val check_pre_post_type : bool -> env -> term -> term -> term -> Tac pre_post_type let check_pre_post_type dbg e pre ret_type post = print_dbg dbg "[> check_pre_post_type"; match compute_pre_type dbg e pre, compute_post_type dbg e ret_type post with | PP_Pure, PP_Pure -> print_dbg dbg "PP_Pure, PP_Pure"; PP_Pure | PP_State ty1, PP_State ty2 -> print_dbg dbg "PP_State, PP_State"; if term_eq ty1 ty2 then PP_State ty1 else PP_Unknown | _ -> print_dbg dbg "_, _"; PP_Unknown val check_opt_pre_post_type : bool -> env -> option term -> term -> option term -> Tac (option pre_post_type) let check_opt_pre_post_type dbg e opt_pre ret_type opt_post = print_dbg dbg "[> check_opt_pre_post_type"; match opt_pre, opt_post with | Some pre, Some post -> print_dbg dbg "Some pre, Some post"; Some (check_pre_post_type dbg e pre ret_type post) | Some pre, None -> print_dbg dbg "Some pre, None"; Some (compute_pre_type dbg e pre) | None, Some post -> print_dbg dbg "None, Some post"; Some (compute_post_type dbg e ret_type post) | None, None -> print_dbg dbg "None, None"; None val _introduce_variables_for_abs : genv -> typ -> Tac (list term & list binder & genv) let rec _introduce_variables_for_abs ge ty = match inspect ty with | Tv_Arrow b c -> let ge1, b1 = genv_push_fresh_binder ge ("__" ^ name_of_binder b) (type_of_binder b) in let bv1 = bv_of_binder b1 in let v1 = pack (Tv_Var bv1) in begin match get_total_or_gtotal_ret_type c with | Some ty1 -> let vl, bl, ge2 = _introduce_variables_for_abs ge1 ty1 in v1 :: vl, b1 :: bl, ge2 | None -> [v1], [b1], ge1 end | _ -> [], [], ge val introduce_variables_for_abs : genv -> term -> Tac (list term & list binder & genv) let introduce_variables_for_abs ge tm = match safe_tc ge.env tm with | Some ty -> _introduce_variables_for_abs ge ty | None -> [], [], ge val introduce_variables_for_opt_abs : genv -> option term -> Tac (list term & list binder & genv) let introduce_variables_for_opt_abs ge opt_tm = match opt_tm with | Some tm -> introduce_variables_for_abs ge tm | None -> [], [], ge
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
etype: FStar.InteractiveHelpers.ExploreTerm.effect_type -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "FStar.InteractiveHelpers.ExploreTerm.effect_type", "Prims.bool" ]
[]
false
false
false
true
false
let effect_type_is_stateful etype =
match etype with | E_Total | E_GTotal | E_Lemma | E_PURE | E_Pure -> false | E_Stack | E_ST | E_Unknown -> true
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.find_mem_in_children
val find_mem_in_children: dbg:bool -> ge:genv -> child:term -> Tac (genv & option bv)
val find_mem_in_children: dbg:bool -> ge:genv -> child:term -> Tac (genv & option bv)
let rec find_mem_in_children dbg ge child = (* We stop whenever we find an expression which is not a let-binding *) match inspect child with | Tv_Let recf attrs bv ty def body -> if is_st_get dbg def then ge, Some bv else if term_has_effectful_comp dbg ge.env def <> Some false then ge, None else let ge1 = genv_push_bv ge bv ty false None in find_mem_in_children dbg ge1 body | _ -> ge, None
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 17, "end_line": 646, "start_col": 0, "start_line": 637 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown (*/// Retrieve the list of types from the parameters stored in ``typ_or_comp``. val typ_or_comp_to_param_types : typ_or_comp -> Tot (list typ) let typ_or_comp_to_param_types c = let pl = params_of_typ_or_comp c in List.Tot.map type_of_binder pl *) (**** Step 2 *) /// The retrieved type refinements and post-conditions are not instantiated (they /// are lambda functions): instantiate them to get propositions. /// Generate a term of the form [has_type x ty] val mk_has_type : term -> typ -> Tac term let mk_has_type t ty = let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params // TODO: I don't understand why I need ifuel 2 here #push-options "--ifuel 2" /// Generate the propositions equivalent to a correct type cast. /// Note that the type refinements need to be instantiated. val cast_info_to_propositions : bool -> genv -> cast_info -> Tac (list proposition) let cast_info_to_propositions dbg ge ci = print_dbg dbg ("[> cast_info_to_propositions:\n" ^ cast_info_to_string ci); match compare_cast_types dbg ci with | Refines -> print_dbg dbg ("-> Comparison result: Refines"); [] | Same_raw_type -> print_dbg dbg ("-> Comparison result: Same_raw_type"); let refin = get_refinement (Some?.v ci.exp_ty) in let inst_refin = mk_app_norm ge.env refin [ci.term] in [inst_refin] | Unknown -> print_dbg dbg ("-> Comparison result: Unknown"); match ci.p_ty, ci.exp_ty with | Some p_ty, Some e_ty -> let p_rty = get_rawest_type p_ty in let e_rty = get_rawest_type e_ty in (* For the type cast, we generate an assertion of the form: * [> has_type (p <: type_of_p) expected_type * The reason is that we want the user to know which parameter is * concerned (hence the ``has_type``), and which types should be * related (hence the ascription). *) let ascr_term = pack (Tv_AscribedT ci.term p_rty None false) in let has_type_params = [(p_rty, Q_Implicit); (ascr_term, Q_Explicit); (e_rty, Q_Explicit)] in let type_cast = mk_app (`has_type) has_type_params in (* Expected type's refinement *) let inst_opt_refin = opt_mk_app_norm ge.env e_ty.refin [ci.term] in opt_cons inst_opt_refin [type_cast] | _ -> [] #pop-options /// Generates a list of propositions from a list of ``cast_info``. Note that /// the user should revert the list before printing the propositions. val cast_info_list_to_propositions : bool -> genv -> list cast_info -> Tac (list proposition) let cast_info_list_to_propositions dbg ge ls = let lsl = map (cast_info_to_propositions dbg ge) ls in flatten lsl /// When dealing with unknown effects, we try to guess what the pre and post-conditions /// are. The effects are indeed very likely to have a pre and a post-condition, /// and to be parameterized with an internal effect state, so that the pre and posts /// have the following shapes: /// - pre : STATE -> Type0 /// - post : STATE -> RET -> STATE -> Type0 /// Or (no state/pure): /// - pre : Type0 /// - post : RET -> Type0 /// We try to detect that with the following functions: noeq type pre_post_type = | PP_Unknown | PP_Pure | PP_State : (state_type:term) -> pre_post_type val compute_pre_type : bool -> env -> term -> Tac pre_post_type let compute_pre_type dbg e pre = print_dbg dbg "[> compute_pre_type"; match safe_tc e pre with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [], true -> print_dbg dbg "PP_Pure"; PP_Pure | [b], true -> print_dbg dbg ("PP_State " ^ (term_to_string (type_of_binder b))); PP_State (type_of_binder b) | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val opt_remove_refin : typ -> Tac typ let opt_remove_refin ty = match inspect ty with | Tv_Refine _ sort _ -> sort | _ -> ty val compute_post_type : bool -> env -> term -> term -> Tac pre_post_type let compute_post_type dbg e ret_type post = print_dbg dbg "[> compute_post_type"; let get_tot_ret_type c : Tac (option term_view) = match get_total_or_gtotal_ret_type c with | Some ret_ty -> Some (inspect ret_ty) | None -> None in match safe_tc e post with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [r], true -> (* Pure *) print_dbg dbg "PP_Pure"; PP_Pure | [s1; r; s2], true -> (* Stateful: check that the state types are coherent *) let r_ty = type_of_binder r in let s1_ty = type_of_binder s1 in (* After testing with Stack: the final state seems to have a refinement * (which gives the postcondition) so we need to remove it to match * it against the initial state *) let s2_ty = opt_remove_refin (type_of_binder s2) in let ret_type_eq = term_eq ret_type r_ty in let state_type_eq = term_eq s1_ty s2_ty in print_dbg dbg ("- ret type:\n-- target: " ^ term_to_string ret_type ^ "\n-- binder: " ^ term_to_string r_ty); print_dbg dbg ("- state types:\n-- binder1: " ^ term_to_string s1_ty ^ "\n-- binder2: " ^ term_to_string s2_ty); print_dbg dbg ("- ret type equality: " ^ string_of_bool ret_type_eq); print_dbg dbg ("- state types equality: " ^ string_of_bool state_type_eq); if ret_type_eq && state_type_eq then begin print_dbg dbg ("PP_State" ^ term_to_string (type_of_binder s1)); PP_State (type_of_binder s1) end else begin print_dbg dbg "PP_Unknown"; PP_Unknown end | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val check_pre_post_type : bool -> env -> term -> term -> term -> Tac pre_post_type let check_pre_post_type dbg e pre ret_type post = print_dbg dbg "[> check_pre_post_type"; match compute_pre_type dbg e pre, compute_post_type dbg e ret_type post with | PP_Pure, PP_Pure -> print_dbg dbg "PP_Pure, PP_Pure"; PP_Pure | PP_State ty1, PP_State ty2 -> print_dbg dbg "PP_State, PP_State"; if term_eq ty1 ty2 then PP_State ty1 else PP_Unknown | _ -> print_dbg dbg "_, _"; PP_Unknown val check_opt_pre_post_type : bool -> env -> option term -> term -> option term -> Tac (option pre_post_type) let check_opt_pre_post_type dbg e opt_pre ret_type opt_post = print_dbg dbg "[> check_opt_pre_post_type"; match opt_pre, opt_post with | Some pre, Some post -> print_dbg dbg "Some pre, Some post"; Some (check_pre_post_type dbg e pre ret_type post) | Some pre, None -> print_dbg dbg "Some pre, None"; Some (compute_pre_type dbg e pre) | None, Some post -> print_dbg dbg "None, Some post"; Some (compute_post_type dbg e ret_type post) | None, None -> print_dbg dbg "None, None"; None val _introduce_variables_for_abs : genv -> typ -> Tac (list term & list binder & genv) let rec _introduce_variables_for_abs ge ty = match inspect ty with | Tv_Arrow b c -> let ge1, b1 = genv_push_fresh_binder ge ("__" ^ name_of_binder b) (type_of_binder b) in let bv1 = bv_of_binder b1 in let v1 = pack (Tv_Var bv1) in begin match get_total_or_gtotal_ret_type c with | Some ty1 -> let vl, bl, ge2 = _introduce_variables_for_abs ge1 ty1 in v1 :: vl, b1 :: bl, ge2 | None -> [v1], [b1], ge1 end | _ -> [], [], ge val introduce_variables_for_abs : genv -> term -> Tac (list term & list binder & genv) let introduce_variables_for_abs ge tm = match safe_tc ge.env tm with | Some ty -> _introduce_variables_for_abs ge ty | None -> [], [], ge val introduce_variables_for_opt_abs : genv -> option term -> Tac (list term & list binder & genv) let introduce_variables_for_opt_abs ge opt_tm = match opt_tm with | Some tm -> introduce_variables_for_abs ge tm | None -> [], [], ge val effect_type_is_stateful : effect_type -> Tot bool let effect_type_is_stateful etype = match etype with | E_Total | E_GTotal | E_Lemma | E_PURE | E_Pure -> false | E_Stack | E_ST | E_Unknown -> true let is_st_get dbg t : Tac bool = print_dbg dbg ("[> is_st_get:\n" ^ term_to_string t); match inspect t with | Tv_App hd (a, qual) -> print_dbg dbg "-> Is Tv_App"; begin match inspect hd with | Tv_FVar fv -> print_dbg dbg ("-> Head is Tv_FVar: " ^ fv_to_string fv); fv_eq_name fv ["FStar"; "HyperStack"; "ST"; "get"] | _ -> print_dbg dbg "-> Head is not Tv_FVar"; false end | _ -> print_dbg dbg "-> Is not Tv_App"; false let is_let_st_get dbg (t : term_view) = print_dbg dbg ("[> is_let_st_get:\n" ^ term_to_string t); match t with | Tv_Let recf attrs bv ty def body -> print_dbg dbg "The term is a let expression"; if is_st_get dbg def then Some (bv, ty) else None | _ -> print_dbg dbg "The term is not a let expression"; None // TODO: Define relation between parents and children in and use it in explore_term // app: head or arg // let : bv or def or child // match: scrutinee or branch // ascribed: e or ty /// Check if a term's computation is effectful. The return type is option /// because we may not be able to retrieve the term computation. val term_has_effectful_comp : bool -> env -> term -> Tac (option bool) let term_has_effectful_comp dbg e tm = print_dbg dbg "[> term_has_effectful_comp"; let einfo_opt = compute_effect_info dbg e tm in match einfo_opt with | Some einfo -> print_dbg dbg ("Effect type: " ^ effect_type_to_string einfo.ei_type); Some (not (effect_type_is_pure einfo.ei_type)) | None -> print_dbg dbg "Could not compute effect info"; None /// Check if a related term is effectful. This is used to look for instances of /// ``HS.mem`` to instantiate pre/postconditions, which means that the term should /// be a parent/child term of the term under study, as generated by ``explore_term`` /// (otherwise the way we check that a term is effectful doesn't make sense). /// The computation is an overapproximation: it may happen that, for instance, we /// can't compute a term computation. In this case, we consider that the term is /// effectful. There are also situations in which we may not be sure which term to /// consider. let related_term_is_effectul dbg ge tv : Tac bool = let is_effectful tm = term_has_effectful_comp dbg ge.env tm <> Some false in match tv with | Tv_Var _ | Tv_BVar _ | Tv_FVar _ -> false | Tv_App hd (a, qual) -> (* The term under focus should be the app itself or an argument *) false | Tv_Abs br body -> false | Tv_Arrow br c0 -> false | Tv_Type _ -> false | Tv_Refine bv sort ref -> false | Tv_Const _ -> false | Tv_Uvar _ _ -> false | Tv_Let recf attrs bv ty def body -> is_effectful def | Tv_Match scrutinee _ret_opt branches -> (* TODO: we need to keep track of the relation between parents and children *) (* We assume the term under focus is in one the branches of the match - this * assumption is safe: in the worst case, we won't be able to find a mem to use. * Besides, in practice it is uncommon (impossible?) to use an effectful term * as the scrutinee of a match *) is_effectful scrutinee | Tv_AscribedT e ty tac _ -> false (* The focused term should be inside the ascription *) | Tv_AscribedC e c tac _ -> false (* The focused term should be inside the ascription *) | _ -> (* Unknown: keep things safe *) true /// Look for a term of the form ``let h = ST.get ()`` in a list of parent/children terms /// and return the let-bound bv. Abort the search if we find a non-effectful term. /// The typical usages of this function are the following: /// - look for a state variable to instantiate the precondition of the term under focus /// - look for state variables for the pre/postconditions of a term defined before /// the term under focus. val find_mem_in_related: dbg:bool -> ge:genv -> tms:list term_view -> Tac (option (bv & typ)) let rec find_mem_in_related dbg ge tms = match tms with | [] -> None | tv :: tms' -> print_dbg dbg ("[> find_mem_in_related:\n" ^ term_to_string tv); match is_let_st_get dbg tv with | Some bvt -> print_dbg dbg "Term is of the form `let x = FStar.HyperStack.ST.get ()`: success"; Some bvt | None -> print_dbg dbg "Term is not of the form `let x = FStar.HyperStack.ST.get ()`: continuing"; if related_term_is_effectul dbg ge tv then begin print_dbg dbg "Term is effectful: stopping here"; None end else begin print_dbg dbg "Term is not effectful: continuing"; find_mem_in_related dbg ge tms' end // TODO: not used for now /// Look for a term of the form ``let h = ST.get ()`` in a child term (the /// focused term is supposed to be a subterm of the definition of a let-construct). val find_mem_in_children: dbg:bool -> ge:genv -> child:term -> Tac (genv & option bv)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> ge: FStar.InteractiveHelpers.Base.genv -> child: FStar.Stubs.Reflection.Types.term -> FStar.Tactics.Effect.Tac (FStar.InteractiveHelpers.Base.genv * FStar.Pervasives.Native.option FStar.Stubs.Reflection.Types.bv)
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.InteractiveHelpers.Base.genv", "FStar.Stubs.Reflection.Types.term", "Prims.list", "FStar.Stubs.Reflection.Types.bv", "FStar.Stubs.Reflection.Types.typ", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.None", "FStar.InteractiveHelpers.Effectful.find_mem_in_children", "FStar.InteractiveHelpers.Base.genv_push_bv", "Prims.op_disEquality", "FStar.InteractiveHelpers.Effectful.term_has_effectful_comp", "FStar.InteractiveHelpers.Base.__proj__Mkgenv__item__env", "FStar.InteractiveHelpers.Effectful.is_st_get", "FStar.Stubs.Reflection.V1.Data.term_view", "FStar.Stubs.Tactics.V1.Builtins.inspect" ]
[ "recursion" ]
false
true
false
false
false
let rec find_mem_in_children dbg ge child =
match inspect child with | Tv_Let recf attrs bv ty def body -> if is_st_get dbg def then ge, Some bv else if term_has_effectful_comp dbg ge.env def <> Some false then ge, None else let ge1 = genv_push_bv ge bv ty false None in find_mem_in_children dbg ge1 body | _ -> ge, None
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.check_opt_pre_post_type
val check_opt_pre_post_type : bool -> env -> option term -> term -> option term -> Tac (option pre_post_type)
val check_opt_pre_post_type : bool -> env -> option term -> term -> option term -> Tac (option pre_post_type)
let check_opt_pre_post_type dbg e opt_pre ret_type opt_post = print_dbg dbg "[> check_opt_pre_post_type"; match opt_pre, opt_post with | Some pre, Some post -> print_dbg dbg "Some pre, Some post"; Some (check_pre_post_type dbg e pre ret_type post) | Some pre, None -> print_dbg dbg "Some pre, None"; Some (compute_pre_type dbg e pre) | None, Some post -> print_dbg dbg "None, Some post"; Some (compute_post_type dbg e ret_type post) | None, None -> print_dbg dbg "None, None"; None
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 8, "end_line": 474, "start_col": 0, "start_line": 460 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown (*/// Retrieve the list of types from the parameters stored in ``typ_or_comp``. val typ_or_comp_to_param_types : typ_or_comp -> Tot (list typ) let typ_or_comp_to_param_types c = let pl = params_of_typ_or_comp c in List.Tot.map type_of_binder pl *) (**** Step 2 *) /// The retrieved type refinements and post-conditions are not instantiated (they /// are lambda functions): instantiate them to get propositions. /// Generate a term of the form [has_type x ty] val mk_has_type : term -> typ -> Tac term let mk_has_type t ty = let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params // TODO: I don't understand why I need ifuel 2 here #push-options "--ifuel 2" /// Generate the propositions equivalent to a correct type cast. /// Note that the type refinements need to be instantiated. val cast_info_to_propositions : bool -> genv -> cast_info -> Tac (list proposition) let cast_info_to_propositions dbg ge ci = print_dbg dbg ("[> cast_info_to_propositions:\n" ^ cast_info_to_string ci); match compare_cast_types dbg ci with | Refines -> print_dbg dbg ("-> Comparison result: Refines"); [] | Same_raw_type -> print_dbg dbg ("-> Comparison result: Same_raw_type"); let refin = get_refinement (Some?.v ci.exp_ty) in let inst_refin = mk_app_norm ge.env refin [ci.term] in [inst_refin] | Unknown -> print_dbg dbg ("-> Comparison result: Unknown"); match ci.p_ty, ci.exp_ty with | Some p_ty, Some e_ty -> let p_rty = get_rawest_type p_ty in let e_rty = get_rawest_type e_ty in (* For the type cast, we generate an assertion of the form: * [> has_type (p <: type_of_p) expected_type * The reason is that we want the user to know which parameter is * concerned (hence the ``has_type``), and which types should be * related (hence the ascription). *) let ascr_term = pack (Tv_AscribedT ci.term p_rty None false) in let has_type_params = [(p_rty, Q_Implicit); (ascr_term, Q_Explicit); (e_rty, Q_Explicit)] in let type_cast = mk_app (`has_type) has_type_params in (* Expected type's refinement *) let inst_opt_refin = opt_mk_app_norm ge.env e_ty.refin [ci.term] in opt_cons inst_opt_refin [type_cast] | _ -> [] #pop-options /// Generates a list of propositions from a list of ``cast_info``. Note that /// the user should revert the list before printing the propositions. val cast_info_list_to_propositions : bool -> genv -> list cast_info -> Tac (list proposition) let cast_info_list_to_propositions dbg ge ls = let lsl = map (cast_info_to_propositions dbg ge) ls in flatten lsl /// When dealing with unknown effects, we try to guess what the pre and post-conditions /// are. The effects are indeed very likely to have a pre and a post-condition, /// and to be parameterized with an internal effect state, so that the pre and posts /// have the following shapes: /// - pre : STATE -> Type0 /// - post : STATE -> RET -> STATE -> Type0 /// Or (no state/pure): /// - pre : Type0 /// - post : RET -> Type0 /// We try to detect that with the following functions: noeq type pre_post_type = | PP_Unknown | PP_Pure | PP_State : (state_type:term) -> pre_post_type val compute_pre_type : bool -> env -> term -> Tac pre_post_type let compute_pre_type dbg e pre = print_dbg dbg "[> compute_pre_type"; match safe_tc e pre with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [], true -> print_dbg dbg "PP_Pure"; PP_Pure | [b], true -> print_dbg dbg ("PP_State " ^ (term_to_string (type_of_binder b))); PP_State (type_of_binder b) | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val opt_remove_refin : typ -> Tac typ let opt_remove_refin ty = match inspect ty with | Tv_Refine _ sort _ -> sort | _ -> ty val compute_post_type : bool -> env -> term -> term -> Tac pre_post_type let compute_post_type dbg e ret_type post = print_dbg dbg "[> compute_post_type"; let get_tot_ret_type c : Tac (option term_view) = match get_total_or_gtotal_ret_type c with | Some ret_ty -> Some (inspect ret_ty) | None -> None in match safe_tc e post with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [r], true -> (* Pure *) print_dbg dbg "PP_Pure"; PP_Pure | [s1; r; s2], true -> (* Stateful: check that the state types are coherent *) let r_ty = type_of_binder r in let s1_ty = type_of_binder s1 in (* After testing with Stack: the final state seems to have a refinement * (which gives the postcondition) so we need to remove it to match * it against the initial state *) let s2_ty = opt_remove_refin (type_of_binder s2) in let ret_type_eq = term_eq ret_type r_ty in let state_type_eq = term_eq s1_ty s2_ty in print_dbg dbg ("- ret type:\n-- target: " ^ term_to_string ret_type ^ "\n-- binder: " ^ term_to_string r_ty); print_dbg dbg ("- state types:\n-- binder1: " ^ term_to_string s1_ty ^ "\n-- binder2: " ^ term_to_string s2_ty); print_dbg dbg ("- ret type equality: " ^ string_of_bool ret_type_eq); print_dbg dbg ("- state types equality: " ^ string_of_bool state_type_eq); if ret_type_eq && state_type_eq then begin print_dbg dbg ("PP_State" ^ term_to_string (type_of_binder s1)); PP_State (type_of_binder s1) end else begin print_dbg dbg "PP_Unknown"; PP_Unknown end | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val check_pre_post_type : bool -> env -> term -> term -> term -> Tac pre_post_type let check_pre_post_type dbg e pre ret_type post = print_dbg dbg "[> check_pre_post_type"; match compute_pre_type dbg e pre, compute_post_type dbg e ret_type post with | PP_Pure, PP_Pure -> print_dbg dbg "PP_Pure, PP_Pure"; PP_Pure | PP_State ty1, PP_State ty2 -> print_dbg dbg "PP_State, PP_State"; if term_eq ty1 ty2 then PP_State ty1 else PP_Unknown | _ -> print_dbg dbg "_, _"; PP_Unknown
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> e: FStar.Stubs.Reflection.Types.env -> opt_pre: FStar.Pervasives.Native.option FStar.Stubs.Reflection.Types.term -> ret_type: FStar.Stubs.Reflection.Types.term -> opt_post: FStar.Pervasives.Native.option FStar.Stubs.Reflection.Types.term -> FStar.Tactics.Effect.Tac (FStar.Pervasives.Native.option FStar.InteractiveHelpers.Effectful.pre_post_type)
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.Stubs.Reflection.Types.env", "FStar.Pervasives.Native.option", "FStar.Stubs.Reflection.Types.term", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.Some", "FStar.InteractiveHelpers.Effectful.pre_post_type", "FStar.InteractiveHelpers.Effectful.check_pre_post_type", "Prims.unit", "FStar.InteractiveHelpers.Base.print_dbg", "FStar.InteractiveHelpers.Effectful.compute_pre_type", "FStar.InteractiveHelpers.Effectful.compute_post_type", "FStar.Pervasives.Native.None" ]
[]
false
true
false
false
false
let check_opt_pre_post_type dbg e opt_pre ret_type opt_post =
print_dbg dbg "[> check_opt_pre_post_type"; match opt_pre, opt_post with | Some pre, Some post -> print_dbg dbg "Some pre, Some post"; Some (check_pre_post_type dbg e pre ret_type post) | Some pre, None -> print_dbg dbg "Some pre, None"; Some (compute_pre_type dbg e pre) | None, Some post -> print_dbg dbg "None, Some post"; Some (compute_post_type dbg e ret_type post) | None, None -> print_dbg dbg "None, None"; None
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.introduce_variables_for_opt_abs
val introduce_variables_for_opt_abs : genv -> option term -> Tac (list term & list binder & genv)
val introduce_variables_for_opt_abs : genv -> option term -> Tac (list term & list binder & genv)
let introduce_variables_for_opt_abs ge opt_tm = match opt_tm with | Some tm -> introduce_variables_for_abs ge tm | None -> [], [], ge
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 22, "end_line": 501, "start_col": 0, "start_line": 498 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown (*/// Retrieve the list of types from the parameters stored in ``typ_or_comp``. val typ_or_comp_to_param_types : typ_or_comp -> Tot (list typ) let typ_or_comp_to_param_types c = let pl = params_of_typ_or_comp c in List.Tot.map type_of_binder pl *) (**** Step 2 *) /// The retrieved type refinements and post-conditions are not instantiated (they /// are lambda functions): instantiate them to get propositions. /// Generate a term of the form [has_type x ty] val mk_has_type : term -> typ -> Tac term let mk_has_type t ty = let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params // TODO: I don't understand why I need ifuel 2 here #push-options "--ifuel 2" /// Generate the propositions equivalent to a correct type cast. /// Note that the type refinements need to be instantiated. val cast_info_to_propositions : bool -> genv -> cast_info -> Tac (list proposition) let cast_info_to_propositions dbg ge ci = print_dbg dbg ("[> cast_info_to_propositions:\n" ^ cast_info_to_string ci); match compare_cast_types dbg ci with | Refines -> print_dbg dbg ("-> Comparison result: Refines"); [] | Same_raw_type -> print_dbg dbg ("-> Comparison result: Same_raw_type"); let refin = get_refinement (Some?.v ci.exp_ty) in let inst_refin = mk_app_norm ge.env refin [ci.term] in [inst_refin] | Unknown -> print_dbg dbg ("-> Comparison result: Unknown"); match ci.p_ty, ci.exp_ty with | Some p_ty, Some e_ty -> let p_rty = get_rawest_type p_ty in let e_rty = get_rawest_type e_ty in (* For the type cast, we generate an assertion of the form: * [> has_type (p <: type_of_p) expected_type * The reason is that we want the user to know which parameter is * concerned (hence the ``has_type``), and which types should be * related (hence the ascription). *) let ascr_term = pack (Tv_AscribedT ci.term p_rty None false) in let has_type_params = [(p_rty, Q_Implicit); (ascr_term, Q_Explicit); (e_rty, Q_Explicit)] in let type_cast = mk_app (`has_type) has_type_params in (* Expected type's refinement *) let inst_opt_refin = opt_mk_app_norm ge.env e_ty.refin [ci.term] in opt_cons inst_opt_refin [type_cast] | _ -> [] #pop-options /// Generates a list of propositions from a list of ``cast_info``. Note that /// the user should revert the list before printing the propositions. val cast_info_list_to_propositions : bool -> genv -> list cast_info -> Tac (list proposition) let cast_info_list_to_propositions dbg ge ls = let lsl = map (cast_info_to_propositions dbg ge) ls in flatten lsl /// When dealing with unknown effects, we try to guess what the pre and post-conditions /// are. The effects are indeed very likely to have a pre and a post-condition, /// and to be parameterized with an internal effect state, so that the pre and posts /// have the following shapes: /// - pre : STATE -> Type0 /// - post : STATE -> RET -> STATE -> Type0 /// Or (no state/pure): /// - pre : Type0 /// - post : RET -> Type0 /// We try to detect that with the following functions: noeq type pre_post_type = | PP_Unknown | PP_Pure | PP_State : (state_type:term) -> pre_post_type val compute_pre_type : bool -> env -> term -> Tac pre_post_type let compute_pre_type dbg e pre = print_dbg dbg "[> compute_pre_type"; match safe_tc e pre with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [], true -> print_dbg dbg "PP_Pure"; PP_Pure | [b], true -> print_dbg dbg ("PP_State " ^ (term_to_string (type_of_binder b))); PP_State (type_of_binder b) | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val opt_remove_refin : typ -> Tac typ let opt_remove_refin ty = match inspect ty with | Tv_Refine _ sort _ -> sort | _ -> ty val compute_post_type : bool -> env -> term -> term -> Tac pre_post_type let compute_post_type dbg e ret_type post = print_dbg dbg "[> compute_post_type"; let get_tot_ret_type c : Tac (option term_view) = match get_total_or_gtotal_ret_type c with | Some ret_ty -> Some (inspect ret_ty) | None -> None in match safe_tc e post with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [r], true -> (* Pure *) print_dbg dbg "PP_Pure"; PP_Pure | [s1; r; s2], true -> (* Stateful: check that the state types are coherent *) let r_ty = type_of_binder r in let s1_ty = type_of_binder s1 in (* After testing with Stack: the final state seems to have a refinement * (which gives the postcondition) so we need to remove it to match * it against the initial state *) let s2_ty = opt_remove_refin (type_of_binder s2) in let ret_type_eq = term_eq ret_type r_ty in let state_type_eq = term_eq s1_ty s2_ty in print_dbg dbg ("- ret type:\n-- target: " ^ term_to_string ret_type ^ "\n-- binder: " ^ term_to_string r_ty); print_dbg dbg ("- state types:\n-- binder1: " ^ term_to_string s1_ty ^ "\n-- binder2: " ^ term_to_string s2_ty); print_dbg dbg ("- ret type equality: " ^ string_of_bool ret_type_eq); print_dbg dbg ("- state types equality: " ^ string_of_bool state_type_eq); if ret_type_eq && state_type_eq then begin print_dbg dbg ("PP_State" ^ term_to_string (type_of_binder s1)); PP_State (type_of_binder s1) end else begin print_dbg dbg "PP_Unknown"; PP_Unknown end | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val check_pre_post_type : bool -> env -> term -> term -> term -> Tac pre_post_type let check_pre_post_type dbg e pre ret_type post = print_dbg dbg "[> check_pre_post_type"; match compute_pre_type dbg e pre, compute_post_type dbg e ret_type post with | PP_Pure, PP_Pure -> print_dbg dbg "PP_Pure, PP_Pure"; PP_Pure | PP_State ty1, PP_State ty2 -> print_dbg dbg "PP_State, PP_State"; if term_eq ty1 ty2 then PP_State ty1 else PP_Unknown | _ -> print_dbg dbg "_, _"; PP_Unknown val check_opt_pre_post_type : bool -> env -> option term -> term -> option term -> Tac (option pre_post_type) let check_opt_pre_post_type dbg e opt_pre ret_type opt_post = print_dbg dbg "[> check_opt_pre_post_type"; match opt_pre, opt_post with | Some pre, Some post -> print_dbg dbg "Some pre, Some post"; Some (check_pre_post_type dbg e pre ret_type post) | Some pre, None -> print_dbg dbg "Some pre, None"; Some (compute_pre_type dbg e pre) | None, Some post -> print_dbg dbg "None, Some post"; Some (compute_post_type dbg e ret_type post) | None, None -> print_dbg dbg "None, None"; None val _introduce_variables_for_abs : genv -> typ -> Tac (list term & list binder & genv) let rec _introduce_variables_for_abs ge ty = match inspect ty with | Tv_Arrow b c -> let ge1, b1 = genv_push_fresh_binder ge ("__" ^ name_of_binder b) (type_of_binder b) in let bv1 = bv_of_binder b1 in let v1 = pack (Tv_Var bv1) in begin match get_total_or_gtotal_ret_type c with | Some ty1 -> let vl, bl, ge2 = _introduce_variables_for_abs ge1 ty1 in v1 :: vl, b1 :: bl, ge2 | None -> [v1], [b1], ge1 end | _ -> [], [], ge val introduce_variables_for_abs : genv -> term -> Tac (list term & list binder & genv) let introduce_variables_for_abs ge tm = match safe_tc ge.env tm with | Some ty -> _introduce_variables_for_abs ge ty | None -> [], [], ge
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ge: FStar.InteractiveHelpers.Base.genv -> opt_tm: FStar.Pervasives.Native.option FStar.Stubs.Reflection.Types.term -> FStar.Tactics.Effect.Tac ((Prims.list FStar.Stubs.Reflection.Types.term * Prims.list FStar.Stubs.Reflection.Types.binder) * FStar.InteractiveHelpers.Base.genv)
FStar.Tactics.Effect.Tac
[]
[]
[ "FStar.InteractiveHelpers.Base.genv", "FStar.Pervasives.Native.option", "FStar.Stubs.Reflection.Types.term", "FStar.InteractiveHelpers.Effectful.introduce_variables_for_abs", "FStar.Pervasives.Native.tuple3", "Prims.list", "FStar.Stubs.Reflection.Types.binder", "FStar.Pervasives.Native.Mktuple3", "Prims.Nil" ]
[]
false
true
false
false
false
let introduce_variables_for_opt_abs ge opt_tm =
match opt_tm with | Some tm -> introduce_variables_for_abs ge tm | None -> [], [], ge
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.compare_cast_types
val compare_cast_types (dbg: bool) (p: cast_info) : Tac (c: type_comparison { ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty)) })
val compare_cast_types (dbg: bool) (p: cast_info) : Tac (c: type_comparison { ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty)) })
let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 16, "end_line": 286, "start_col": 0, "start_line": 278 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> p: FStar.InteractiveHelpers.Effectful.cast_info -> FStar.Tactics.Effect.Tac (c: FStar.InteractiveHelpers.Effectful.type_comparison { (c = FStar.InteractiveHelpers.Effectful.Refines \/ c = FStar.InteractiveHelpers.Effectful.Same_raw_type ==> Some? (Mkcast_info?.p_ty p) /\ Some? (Mkcast_info?.exp_ty p)) /\ (c = FStar.InteractiveHelpers.Effectful.Same_raw_type ==> FStar.InteractiveHelpers.Effectful.has_refinement (Some?.v (Mkcast_info?.exp_ty p))) })
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.InteractiveHelpers.Effectful.cast_info", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.option", "FStar.InteractiveHelpers.ExploreTerm.type_info", "FStar.InteractiveHelpers.Effectful.__proj__Mkcast_info__item__p_ty", "FStar.InteractiveHelpers.Effectful.__proj__Mkcast_info__item__exp_ty", "FStar.InteractiveHelpers.Effectful.compare_types", "FStar.InteractiveHelpers.Effectful.type_comparison", "Prims.l_imp", "Prims.b2t", "Prims.op_Equality", "FStar.InteractiveHelpers.Effectful.Same_raw_type", "FStar.InteractiveHelpers.Effectful.has_refinement", "FStar.Pervasives.Native.tuple2", "FStar.InteractiveHelpers.Effectful.Unknown", "Prims.l_and", "Prims.l_or", "FStar.InteractiveHelpers.Effectful.Refines", "FStar.Pervasives.Native.uu___is_Some", "FStar.Pervasives.Native.__proj__Some__item__v", "Prims.unit", "FStar.InteractiveHelpers.Base.print_dbg" ]
[]
false
true
false
false
false
let compare_cast_types (dbg: bool) (p: cast_info) : Tac (c: type_comparison { ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty)) }) =
print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful._introduce_variables_for_abs
val _introduce_variables_for_abs : genv -> typ -> Tac (list term & list binder & genv)
val _introduce_variables_for_abs : genv -> typ -> Tac (list term & list binder & genv)
let rec _introduce_variables_for_abs ge ty = match inspect ty with | Tv_Arrow b c -> let ge1, b1 = genv_push_fresh_binder ge ("__" ^ name_of_binder b) (type_of_binder b) in let bv1 = bv_of_binder b1 in let v1 = pack (Tv_Var bv1) in begin match get_total_or_gtotal_ret_type c with | Some ty1 -> let vl, bl, ge2 = _introduce_variables_for_abs ge1 ty1 in v1 :: vl, b1 :: bl, ge2 | None -> [v1], [b1], ge1 end | _ -> [], [], ge
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 18, "end_line": 489, "start_col": 0, "start_line": 477 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown (*/// Retrieve the list of types from the parameters stored in ``typ_or_comp``. val typ_or_comp_to_param_types : typ_or_comp -> Tot (list typ) let typ_or_comp_to_param_types c = let pl = params_of_typ_or_comp c in List.Tot.map type_of_binder pl *) (**** Step 2 *) /// The retrieved type refinements and post-conditions are not instantiated (they /// are lambda functions): instantiate them to get propositions. /// Generate a term of the form [has_type x ty] val mk_has_type : term -> typ -> Tac term let mk_has_type t ty = let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params // TODO: I don't understand why I need ifuel 2 here #push-options "--ifuel 2" /// Generate the propositions equivalent to a correct type cast. /// Note that the type refinements need to be instantiated. val cast_info_to_propositions : bool -> genv -> cast_info -> Tac (list proposition) let cast_info_to_propositions dbg ge ci = print_dbg dbg ("[> cast_info_to_propositions:\n" ^ cast_info_to_string ci); match compare_cast_types dbg ci with | Refines -> print_dbg dbg ("-> Comparison result: Refines"); [] | Same_raw_type -> print_dbg dbg ("-> Comparison result: Same_raw_type"); let refin = get_refinement (Some?.v ci.exp_ty) in let inst_refin = mk_app_norm ge.env refin [ci.term] in [inst_refin] | Unknown -> print_dbg dbg ("-> Comparison result: Unknown"); match ci.p_ty, ci.exp_ty with | Some p_ty, Some e_ty -> let p_rty = get_rawest_type p_ty in let e_rty = get_rawest_type e_ty in (* For the type cast, we generate an assertion of the form: * [> has_type (p <: type_of_p) expected_type * The reason is that we want the user to know which parameter is * concerned (hence the ``has_type``), and which types should be * related (hence the ascription). *) let ascr_term = pack (Tv_AscribedT ci.term p_rty None false) in let has_type_params = [(p_rty, Q_Implicit); (ascr_term, Q_Explicit); (e_rty, Q_Explicit)] in let type_cast = mk_app (`has_type) has_type_params in (* Expected type's refinement *) let inst_opt_refin = opt_mk_app_norm ge.env e_ty.refin [ci.term] in opt_cons inst_opt_refin [type_cast] | _ -> [] #pop-options /// Generates a list of propositions from a list of ``cast_info``. Note that /// the user should revert the list before printing the propositions. val cast_info_list_to_propositions : bool -> genv -> list cast_info -> Tac (list proposition) let cast_info_list_to_propositions dbg ge ls = let lsl = map (cast_info_to_propositions dbg ge) ls in flatten lsl /// When dealing with unknown effects, we try to guess what the pre and post-conditions /// are. The effects are indeed very likely to have a pre and a post-condition, /// and to be parameterized with an internal effect state, so that the pre and posts /// have the following shapes: /// - pre : STATE -> Type0 /// - post : STATE -> RET -> STATE -> Type0 /// Or (no state/pure): /// - pre : Type0 /// - post : RET -> Type0 /// We try to detect that with the following functions: noeq type pre_post_type = | PP_Unknown | PP_Pure | PP_State : (state_type:term) -> pre_post_type val compute_pre_type : bool -> env -> term -> Tac pre_post_type let compute_pre_type dbg e pre = print_dbg dbg "[> compute_pre_type"; match safe_tc e pre with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [], true -> print_dbg dbg "PP_Pure"; PP_Pure | [b], true -> print_dbg dbg ("PP_State " ^ (term_to_string (type_of_binder b))); PP_State (type_of_binder b) | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val opt_remove_refin : typ -> Tac typ let opt_remove_refin ty = match inspect ty with | Tv_Refine _ sort _ -> sort | _ -> ty val compute_post_type : bool -> env -> term -> term -> Tac pre_post_type let compute_post_type dbg e ret_type post = print_dbg dbg "[> compute_post_type"; let get_tot_ret_type c : Tac (option term_view) = match get_total_or_gtotal_ret_type c with | Some ret_ty -> Some (inspect ret_ty) | None -> None in match safe_tc e post with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [r], true -> (* Pure *) print_dbg dbg "PP_Pure"; PP_Pure | [s1; r; s2], true -> (* Stateful: check that the state types are coherent *) let r_ty = type_of_binder r in let s1_ty = type_of_binder s1 in (* After testing with Stack: the final state seems to have a refinement * (which gives the postcondition) so we need to remove it to match * it against the initial state *) let s2_ty = opt_remove_refin (type_of_binder s2) in let ret_type_eq = term_eq ret_type r_ty in let state_type_eq = term_eq s1_ty s2_ty in print_dbg dbg ("- ret type:\n-- target: " ^ term_to_string ret_type ^ "\n-- binder: " ^ term_to_string r_ty); print_dbg dbg ("- state types:\n-- binder1: " ^ term_to_string s1_ty ^ "\n-- binder2: " ^ term_to_string s2_ty); print_dbg dbg ("- ret type equality: " ^ string_of_bool ret_type_eq); print_dbg dbg ("- state types equality: " ^ string_of_bool state_type_eq); if ret_type_eq && state_type_eq then begin print_dbg dbg ("PP_State" ^ term_to_string (type_of_binder s1)); PP_State (type_of_binder s1) end else begin print_dbg dbg "PP_Unknown"; PP_Unknown end | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val check_pre_post_type : bool -> env -> term -> term -> term -> Tac pre_post_type let check_pre_post_type dbg e pre ret_type post = print_dbg dbg "[> check_pre_post_type"; match compute_pre_type dbg e pre, compute_post_type dbg e ret_type post with | PP_Pure, PP_Pure -> print_dbg dbg "PP_Pure, PP_Pure"; PP_Pure | PP_State ty1, PP_State ty2 -> print_dbg dbg "PP_State, PP_State"; if term_eq ty1 ty2 then PP_State ty1 else PP_Unknown | _ -> print_dbg dbg "_, _"; PP_Unknown val check_opt_pre_post_type : bool -> env -> option term -> term -> option term -> Tac (option pre_post_type) let check_opt_pre_post_type dbg e opt_pre ret_type opt_post = print_dbg dbg "[> check_opt_pre_post_type"; match opt_pre, opt_post with | Some pre, Some post -> print_dbg dbg "Some pre, Some post"; Some (check_pre_post_type dbg e pre ret_type post) | Some pre, None -> print_dbg dbg "Some pre, None"; Some (compute_pre_type dbg e pre) | None, Some post -> print_dbg dbg "None, Some post"; Some (compute_post_type dbg e ret_type post) | None, None -> print_dbg dbg "None, None"; None
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ge: FStar.InteractiveHelpers.Base.genv -> ty: FStar.Stubs.Reflection.Types.typ -> FStar.Tactics.Effect.Tac ((Prims.list FStar.Stubs.Reflection.Types.term * Prims.list FStar.Stubs.Reflection.Types.binder) * FStar.InteractiveHelpers.Base.genv)
FStar.Tactics.Effect.Tac
[]
[]
[ "FStar.InteractiveHelpers.Base.genv", "FStar.Stubs.Reflection.Types.typ", "FStar.Stubs.Reflection.Types.binder", "FStar.Stubs.Reflection.Types.comp", "FStar.InteractiveHelpers.ExploreTerm.get_total_or_gtotal_ret_type", "Prims.list", "FStar.Stubs.Reflection.Types.term", "FStar.Pervasives.Native.Mktuple3", "Prims.Cons", "FStar.Pervasives.Native.tuple3", "FStar.InteractiveHelpers.Effectful._introduce_variables_for_abs", "Prims.Nil", "FStar.Stubs.Tactics.V1.Builtins.pack", "FStar.Stubs.Reflection.V1.Data.Tv_Var", "FStar.Stubs.Reflection.Types.bv", "FStar.Reflection.V1.Derived.bv_of_binder", "FStar.Pervasives.Native.tuple2", "FStar.InteractiveHelpers.Base.genv_push_fresh_binder", "FStar.Reflection.V1.Derived.type_of_binder", "Prims.string", "Prims.op_Hat", "FStar.Tactics.V1.Derived.name_of_binder", "FStar.Stubs.Reflection.V1.Data.term_view", "FStar.Stubs.Tactics.V1.Builtins.inspect" ]
[ "recursion" ]
false
true
false
false
false
let rec _introduce_variables_for_abs ge ty =
match inspect ty with | Tv_Arrow b c -> let ge1, b1 = genv_push_fresh_binder ge ("__" ^ name_of_binder b) (type_of_binder b) in let bv1 = bv_of_binder b1 in let v1 = pack (Tv_Var bv1) in (match get_total_or_gtotal_ret_type c with | Some ty1 -> let vl, bl, ge2 = _introduce_variables_for_abs ge1 ty1 in v1 :: vl, b1 :: bl, ge2 | None -> [v1], [b1], ge1) | _ -> [], [], ge
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.compare_types
val compare_types (dbg: bool) (info1 info2: type_info) : Tac (c: type_comparison{c = Same_raw_type ==> has_refinement info2})
val compare_types (dbg: bool) (info1 info2: type_info) : Tac (c: type_comparison{c = Same_raw_type ==> has_refinement info2})
let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 13, "end_line": 276, "start_col": 0, "start_line": 250 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> info1: FStar.InteractiveHelpers.ExploreTerm.type_info -> info2: FStar.InteractiveHelpers.ExploreTerm.type_info -> FStar.Tactics.Effect.Tac (c: FStar.InteractiveHelpers.Effectful.type_comparison { c = FStar.InteractiveHelpers.Effectful.Same_raw_type ==> FStar.InteractiveHelpers.Effectful.has_refinement info2 })
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.InteractiveHelpers.ExploreTerm.type_info", "FStar.InteractiveHelpers.Effectful.has_refinement", "FStar.InteractiveHelpers.Effectful.Refines", "FStar.InteractiveHelpers.Effectful.type_comparison", "Prims.l_imp", "Prims.b2t", "Prims.op_Equality", "FStar.InteractiveHelpers.Effectful.Same_raw_type", "Prims.unit", "FStar.InteractiveHelpers.Base.print_dbg", "FStar.InteractiveHelpers.Effectful.term_eq", "FStar.InteractiveHelpers.Effectful.get_refinement", "FStar.InteractiveHelpers.Effectful.Unknown", "FStar.InteractiveHelpers.ExploreTerm.__proj__Mktype_info__item__ty" ]
[]
false
true
false
false
false
let compare_types (dbg: bool) (info1 info2: type_info) : Tac (c: type_comparison{c = Same_raw_type ==> has_refinement info2}) =
print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines else let _ = print_dbg dbg "types are not equal" in Unknown
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.term_has_effectful_comp
val term_has_effectful_comp : bool -> env -> term -> Tac (option bool)
val term_has_effectful_comp : bool -> env -> term -> Tac (option bool)
let term_has_effectful_comp dbg e tm = print_dbg dbg "[> term_has_effectful_comp"; let einfo_opt = compute_effect_info dbg e tm in match einfo_opt with | Some einfo -> print_dbg dbg ("Effect type: " ^ effect_type_to_string einfo.ei_type); Some (not (effect_type_is_pure einfo.ei_type)) | None -> print_dbg dbg "Could not compute effect info"; None
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 8, "end_line": 555, "start_col": 0, "start_line": 546 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown (*/// Retrieve the list of types from the parameters stored in ``typ_or_comp``. val typ_or_comp_to_param_types : typ_or_comp -> Tot (list typ) let typ_or_comp_to_param_types c = let pl = params_of_typ_or_comp c in List.Tot.map type_of_binder pl *) (**** Step 2 *) /// The retrieved type refinements and post-conditions are not instantiated (they /// are lambda functions): instantiate them to get propositions. /// Generate a term of the form [has_type x ty] val mk_has_type : term -> typ -> Tac term let mk_has_type t ty = let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params // TODO: I don't understand why I need ifuel 2 here #push-options "--ifuel 2" /// Generate the propositions equivalent to a correct type cast. /// Note that the type refinements need to be instantiated. val cast_info_to_propositions : bool -> genv -> cast_info -> Tac (list proposition) let cast_info_to_propositions dbg ge ci = print_dbg dbg ("[> cast_info_to_propositions:\n" ^ cast_info_to_string ci); match compare_cast_types dbg ci with | Refines -> print_dbg dbg ("-> Comparison result: Refines"); [] | Same_raw_type -> print_dbg dbg ("-> Comparison result: Same_raw_type"); let refin = get_refinement (Some?.v ci.exp_ty) in let inst_refin = mk_app_norm ge.env refin [ci.term] in [inst_refin] | Unknown -> print_dbg dbg ("-> Comparison result: Unknown"); match ci.p_ty, ci.exp_ty with | Some p_ty, Some e_ty -> let p_rty = get_rawest_type p_ty in let e_rty = get_rawest_type e_ty in (* For the type cast, we generate an assertion of the form: * [> has_type (p <: type_of_p) expected_type * The reason is that we want the user to know which parameter is * concerned (hence the ``has_type``), and which types should be * related (hence the ascription). *) let ascr_term = pack (Tv_AscribedT ci.term p_rty None false) in let has_type_params = [(p_rty, Q_Implicit); (ascr_term, Q_Explicit); (e_rty, Q_Explicit)] in let type_cast = mk_app (`has_type) has_type_params in (* Expected type's refinement *) let inst_opt_refin = opt_mk_app_norm ge.env e_ty.refin [ci.term] in opt_cons inst_opt_refin [type_cast] | _ -> [] #pop-options /// Generates a list of propositions from a list of ``cast_info``. Note that /// the user should revert the list before printing the propositions. val cast_info_list_to_propositions : bool -> genv -> list cast_info -> Tac (list proposition) let cast_info_list_to_propositions dbg ge ls = let lsl = map (cast_info_to_propositions dbg ge) ls in flatten lsl /// When dealing with unknown effects, we try to guess what the pre and post-conditions /// are. The effects are indeed very likely to have a pre and a post-condition, /// and to be parameterized with an internal effect state, so that the pre and posts /// have the following shapes: /// - pre : STATE -> Type0 /// - post : STATE -> RET -> STATE -> Type0 /// Or (no state/pure): /// - pre : Type0 /// - post : RET -> Type0 /// We try to detect that with the following functions: noeq type pre_post_type = | PP_Unknown | PP_Pure | PP_State : (state_type:term) -> pre_post_type val compute_pre_type : bool -> env -> term -> Tac pre_post_type let compute_pre_type dbg e pre = print_dbg dbg "[> compute_pre_type"; match safe_tc e pre with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [], true -> print_dbg dbg "PP_Pure"; PP_Pure | [b], true -> print_dbg dbg ("PP_State " ^ (term_to_string (type_of_binder b))); PP_State (type_of_binder b) | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val opt_remove_refin : typ -> Tac typ let opt_remove_refin ty = match inspect ty with | Tv_Refine _ sort _ -> sort | _ -> ty val compute_post_type : bool -> env -> term -> term -> Tac pre_post_type let compute_post_type dbg e ret_type post = print_dbg dbg "[> compute_post_type"; let get_tot_ret_type c : Tac (option term_view) = match get_total_or_gtotal_ret_type c with | Some ret_ty -> Some (inspect ret_ty) | None -> None in match safe_tc e post with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [r], true -> (* Pure *) print_dbg dbg "PP_Pure"; PP_Pure | [s1; r; s2], true -> (* Stateful: check that the state types are coherent *) let r_ty = type_of_binder r in let s1_ty = type_of_binder s1 in (* After testing with Stack: the final state seems to have a refinement * (which gives the postcondition) so we need to remove it to match * it against the initial state *) let s2_ty = opt_remove_refin (type_of_binder s2) in let ret_type_eq = term_eq ret_type r_ty in let state_type_eq = term_eq s1_ty s2_ty in print_dbg dbg ("- ret type:\n-- target: " ^ term_to_string ret_type ^ "\n-- binder: " ^ term_to_string r_ty); print_dbg dbg ("- state types:\n-- binder1: " ^ term_to_string s1_ty ^ "\n-- binder2: " ^ term_to_string s2_ty); print_dbg dbg ("- ret type equality: " ^ string_of_bool ret_type_eq); print_dbg dbg ("- state types equality: " ^ string_of_bool state_type_eq); if ret_type_eq && state_type_eq then begin print_dbg dbg ("PP_State" ^ term_to_string (type_of_binder s1)); PP_State (type_of_binder s1) end else begin print_dbg dbg "PP_Unknown"; PP_Unknown end | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val check_pre_post_type : bool -> env -> term -> term -> term -> Tac pre_post_type let check_pre_post_type dbg e pre ret_type post = print_dbg dbg "[> check_pre_post_type"; match compute_pre_type dbg e pre, compute_post_type dbg e ret_type post with | PP_Pure, PP_Pure -> print_dbg dbg "PP_Pure, PP_Pure"; PP_Pure | PP_State ty1, PP_State ty2 -> print_dbg dbg "PP_State, PP_State"; if term_eq ty1 ty2 then PP_State ty1 else PP_Unknown | _ -> print_dbg dbg "_, _"; PP_Unknown val check_opt_pre_post_type : bool -> env -> option term -> term -> option term -> Tac (option pre_post_type) let check_opt_pre_post_type dbg e opt_pre ret_type opt_post = print_dbg dbg "[> check_opt_pre_post_type"; match opt_pre, opt_post with | Some pre, Some post -> print_dbg dbg "Some pre, Some post"; Some (check_pre_post_type dbg e pre ret_type post) | Some pre, None -> print_dbg dbg "Some pre, None"; Some (compute_pre_type dbg e pre) | None, Some post -> print_dbg dbg "None, Some post"; Some (compute_post_type dbg e ret_type post) | None, None -> print_dbg dbg "None, None"; None val _introduce_variables_for_abs : genv -> typ -> Tac (list term & list binder & genv) let rec _introduce_variables_for_abs ge ty = match inspect ty with | Tv_Arrow b c -> let ge1, b1 = genv_push_fresh_binder ge ("__" ^ name_of_binder b) (type_of_binder b) in let bv1 = bv_of_binder b1 in let v1 = pack (Tv_Var bv1) in begin match get_total_or_gtotal_ret_type c with | Some ty1 -> let vl, bl, ge2 = _introduce_variables_for_abs ge1 ty1 in v1 :: vl, b1 :: bl, ge2 | None -> [v1], [b1], ge1 end | _ -> [], [], ge val introduce_variables_for_abs : genv -> term -> Tac (list term & list binder & genv) let introduce_variables_for_abs ge tm = match safe_tc ge.env tm with | Some ty -> _introduce_variables_for_abs ge ty | None -> [], [], ge val introduce_variables_for_opt_abs : genv -> option term -> Tac (list term & list binder & genv) let introduce_variables_for_opt_abs ge opt_tm = match opt_tm with | Some tm -> introduce_variables_for_abs ge tm | None -> [], [], ge val effect_type_is_stateful : effect_type -> Tot bool let effect_type_is_stateful etype = match etype with | E_Total | E_GTotal | E_Lemma | E_PURE | E_Pure -> false | E_Stack | E_ST | E_Unknown -> true let is_st_get dbg t : Tac bool = print_dbg dbg ("[> is_st_get:\n" ^ term_to_string t); match inspect t with | Tv_App hd (a, qual) -> print_dbg dbg "-> Is Tv_App"; begin match inspect hd with | Tv_FVar fv -> print_dbg dbg ("-> Head is Tv_FVar: " ^ fv_to_string fv); fv_eq_name fv ["FStar"; "HyperStack"; "ST"; "get"] | _ -> print_dbg dbg "-> Head is not Tv_FVar"; false end | _ -> print_dbg dbg "-> Is not Tv_App"; false let is_let_st_get dbg (t : term_view) = print_dbg dbg ("[> is_let_st_get:\n" ^ term_to_string t); match t with | Tv_Let recf attrs bv ty def body -> print_dbg dbg "The term is a let expression"; if is_st_get dbg def then Some (bv, ty) else None | _ -> print_dbg dbg "The term is not a let expression"; None // TODO: Define relation between parents and children in and use it in explore_term // app: head or arg // let : bv or def or child // match: scrutinee or branch // ascribed: e or ty /// Check if a term's computation is effectful. The return type is option /// because we may not be able to retrieve the term computation.
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> e: FStar.Stubs.Reflection.Types.env -> tm: FStar.Stubs.Reflection.Types.term -> FStar.Tactics.Effect.Tac (FStar.Pervasives.Native.option Prims.bool)
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.Stubs.Reflection.Types.env", "FStar.Stubs.Reflection.Types.term", "FStar.InteractiveHelpers.Effectful.effect_info", "FStar.Pervasives.Native.Some", "Prims.op_Negation", "FStar.InteractiveHelpers.ExploreTerm.effect_type_is_pure", "FStar.InteractiveHelpers.Effectful.__proj__Mkeffect_info__item__ei_type", "FStar.Pervasives.Native.option", "Prims.unit", "FStar.InteractiveHelpers.Base.print_dbg", "Prims.op_Hat", "FStar.InteractiveHelpers.ExploreTerm.effect_type_to_string", "FStar.Pervasives.Native.None", "FStar.InteractiveHelpers.Effectful.compute_effect_info" ]
[]
false
true
false
false
false
let term_has_effectful_comp dbg e tm =
print_dbg dbg "[> term_has_effectful_comp"; let einfo_opt = compute_effect_info dbg e tm in match einfo_opt with | Some einfo -> print_dbg dbg ("Effect type: " ^ effect_type_to_string einfo.ei_type); Some (not (effect_type_is_pure einfo.ei_type)) | None -> print_dbg dbg "Could not compute effect info"; None
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.introduce_variables_for_abs
val introduce_variables_for_abs : genv -> term -> Tac (list term & list binder & genv)
val introduce_variables_for_abs : genv -> term -> Tac (list term & list binder & genv)
let introduce_variables_for_abs ge tm = match safe_tc ge.env tm with | Some ty -> _introduce_variables_for_abs ge ty | None -> [], [], ge
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 22, "end_line": 495, "start_col": 0, "start_line": 492 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown (*/// Retrieve the list of types from the parameters stored in ``typ_or_comp``. val typ_or_comp_to_param_types : typ_or_comp -> Tot (list typ) let typ_or_comp_to_param_types c = let pl = params_of_typ_or_comp c in List.Tot.map type_of_binder pl *) (**** Step 2 *) /// The retrieved type refinements and post-conditions are not instantiated (they /// are lambda functions): instantiate them to get propositions. /// Generate a term of the form [has_type x ty] val mk_has_type : term -> typ -> Tac term let mk_has_type t ty = let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params // TODO: I don't understand why I need ifuel 2 here #push-options "--ifuel 2" /// Generate the propositions equivalent to a correct type cast. /// Note that the type refinements need to be instantiated. val cast_info_to_propositions : bool -> genv -> cast_info -> Tac (list proposition) let cast_info_to_propositions dbg ge ci = print_dbg dbg ("[> cast_info_to_propositions:\n" ^ cast_info_to_string ci); match compare_cast_types dbg ci with | Refines -> print_dbg dbg ("-> Comparison result: Refines"); [] | Same_raw_type -> print_dbg dbg ("-> Comparison result: Same_raw_type"); let refin = get_refinement (Some?.v ci.exp_ty) in let inst_refin = mk_app_norm ge.env refin [ci.term] in [inst_refin] | Unknown -> print_dbg dbg ("-> Comparison result: Unknown"); match ci.p_ty, ci.exp_ty with | Some p_ty, Some e_ty -> let p_rty = get_rawest_type p_ty in let e_rty = get_rawest_type e_ty in (* For the type cast, we generate an assertion of the form: * [> has_type (p <: type_of_p) expected_type * The reason is that we want the user to know which parameter is * concerned (hence the ``has_type``), and which types should be * related (hence the ascription). *) let ascr_term = pack (Tv_AscribedT ci.term p_rty None false) in let has_type_params = [(p_rty, Q_Implicit); (ascr_term, Q_Explicit); (e_rty, Q_Explicit)] in let type_cast = mk_app (`has_type) has_type_params in (* Expected type's refinement *) let inst_opt_refin = opt_mk_app_norm ge.env e_ty.refin [ci.term] in opt_cons inst_opt_refin [type_cast] | _ -> [] #pop-options /// Generates a list of propositions from a list of ``cast_info``. Note that /// the user should revert the list before printing the propositions. val cast_info_list_to_propositions : bool -> genv -> list cast_info -> Tac (list proposition) let cast_info_list_to_propositions dbg ge ls = let lsl = map (cast_info_to_propositions dbg ge) ls in flatten lsl /// When dealing with unknown effects, we try to guess what the pre and post-conditions /// are. The effects are indeed very likely to have a pre and a post-condition, /// and to be parameterized with an internal effect state, so that the pre and posts /// have the following shapes: /// - pre : STATE -> Type0 /// - post : STATE -> RET -> STATE -> Type0 /// Or (no state/pure): /// - pre : Type0 /// - post : RET -> Type0 /// We try to detect that with the following functions: noeq type pre_post_type = | PP_Unknown | PP_Pure | PP_State : (state_type:term) -> pre_post_type val compute_pre_type : bool -> env -> term -> Tac pre_post_type let compute_pre_type dbg e pre = print_dbg dbg "[> compute_pre_type"; match safe_tc e pre with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [], true -> print_dbg dbg "PP_Pure"; PP_Pure | [b], true -> print_dbg dbg ("PP_State " ^ (term_to_string (type_of_binder b))); PP_State (type_of_binder b) | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val opt_remove_refin : typ -> Tac typ let opt_remove_refin ty = match inspect ty with | Tv_Refine _ sort _ -> sort | _ -> ty val compute_post_type : bool -> env -> term -> term -> Tac pre_post_type let compute_post_type dbg e ret_type post = print_dbg dbg "[> compute_post_type"; let get_tot_ret_type c : Tac (option term_view) = match get_total_or_gtotal_ret_type c with | Some ret_ty -> Some (inspect ret_ty) | None -> None in match safe_tc e post with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [r], true -> (* Pure *) print_dbg dbg "PP_Pure"; PP_Pure | [s1; r; s2], true -> (* Stateful: check that the state types are coherent *) let r_ty = type_of_binder r in let s1_ty = type_of_binder s1 in (* After testing with Stack: the final state seems to have a refinement * (which gives the postcondition) so we need to remove it to match * it against the initial state *) let s2_ty = opt_remove_refin (type_of_binder s2) in let ret_type_eq = term_eq ret_type r_ty in let state_type_eq = term_eq s1_ty s2_ty in print_dbg dbg ("- ret type:\n-- target: " ^ term_to_string ret_type ^ "\n-- binder: " ^ term_to_string r_ty); print_dbg dbg ("- state types:\n-- binder1: " ^ term_to_string s1_ty ^ "\n-- binder2: " ^ term_to_string s2_ty); print_dbg dbg ("- ret type equality: " ^ string_of_bool ret_type_eq); print_dbg dbg ("- state types equality: " ^ string_of_bool state_type_eq); if ret_type_eq && state_type_eq then begin print_dbg dbg ("PP_State" ^ term_to_string (type_of_binder s1)); PP_State (type_of_binder s1) end else begin print_dbg dbg "PP_Unknown"; PP_Unknown end | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val check_pre_post_type : bool -> env -> term -> term -> term -> Tac pre_post_type let check_pre_post_type dbg e pre ret_type post = print_dbg dbg "[> check_pre_post_type"; match compute_pre_type dbg e pre, compute_post_type dbg e ret_type post with | PP_Pure, PP_Pure -> print_dbg dbg "PP_Pure, PP_Pure"; PP_Pure | PP_State ty1, PP_State ty2 -> print_dbg dbg "PP_State, PP_State"; if term_eq ty1 ty2 then PP_State ty1 else PP_Unknown | _ -> print_dbg dbg "_, _"; PP_Unknown val check_opt_pre_post_type : bool -> env -> option term -> term -> option term -> Tac (option pre_post_type) let check_opt_pre_post_type dbg e opt_pre ret_type opt_post = print_dbg dbg "[> check_opt_pre_post_type"; match opt_pre, opt_post with | Some pre, Some post -> print_dbg dbg "Some pre, Some post"; Some (check_pre_post_type dbg e pre ret_type post) | Some pre, None -> print_dbg dbg "Some pre, None"; Some (compute_pre_type dbg e pre) | None, Some post -> print_dbg dbg "None, Some post"; Some (compute_post_type dbg e ret_type post) | None, None -> print_dbg dbg "None, None"; None val _introduce_variables_for_abs : genv -> typ -> Tac (list term & list binder & genv) let rec _introduce_variables_for_abs ge ty = match inspect ty with | Tv_Arrow b c -> let ge1, b1 = genv_push_fresh_binder ge ("__" ^ name_of_binder b) (type_of_binder b) in let bv1 = bv_of_binder b1 in let v1 = pack (Tv_Var bv1) in begin match get_total_or_gtotal_ret_type c with | Some ty1 -> let vl, bl, ge2 = _introduce_variables_for_abs ge1 ty1 in v1 :: vl, b1 :: bl, ge2 | None -> [v1], [b1], ge1 end | _ -> [], [], ge
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ge: FStar.InteractiveHelpers.Base.genv -> tm: FStar.Stubs.Reflection.Types.term -> FStar.Tactics.Effect.Tac ((Prims.list FStar.Stubs.Reflection.Types.term * Prims.list FStar.Stubs.Reflection.Types.binder) * FStar.InteractiveHelpers.Base.genv)
FStar.Tactics.Effect.Tac
[]
[]
[ "FStar.InteractiveHelpers.Base.genv", "FStar.Stubs.Reflection.Types.term", "FStar.InteractiveHelpers.Effectful._introduce_variables_for_abs", "FStar.Pervasives.Native.tuple3", "Prims.list", "FStar.Stubs.Reflection.Types.binder", "FStar.Pervasives.Native.Mktuple3", "Prims.Nil", "FStar.Pervasives.Native.option", "FStar.InteractiveHelpers.ExploreTerm.safe_tc", "FStar.InteractiveHelpers.Base.__proj__Mkgenv__item__env" ]
[]
false
true
false
false
false
let introduce_variables_for_abs ge tm =
match safe_tc ge.env tm with | Some ty -> _introduce_variables_for_abs ge ty | None -> [], [], ge
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.find_mem_in_related
val find_mem_in_related: dbg:bool -> ge:genv -> tms:list term_view -> Tac (option (bv & typ))
val find_mem_in_related: dbg:bool -> ge:genv -> tms:list term_view -> Tac (option (bv & typ))
let rec find_mem_in_related dbg ge tms = match tms with | [] -> None | tv :: tms' -> print_dbg dbg ("[> find_mem_in_related:\n" ^ term_to_string tv); match is_let_st_get dbg tv with | Some bvt -> print_dbg dbg "Term is of the form `let x = FStar.HyperStack.ST.get ()`: success"; Some bvt | None -> print_dbg dbg "Term is not of the form `let x = FStar.HyperStack.ST.get ()`: continuing"; if related_term_is_effectul dbg ge tv then begin print_dbg dbg "Term is effectful: stopping here"; None end else begin print_dbg dbg "Term is not effectful: continuing"; find_mem_in_related dbg ge tms' end
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 11, "end_line": 626, "start_col": 0, "start_line": 605 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown (*/// Retrieve the list of types from the parameters stored in ``typ_or_comp``. val typ_or_comp_to_param_types : typ_or_comp -> Tot (list typ) let typ_or_comp_to_param_types c = let pl = params_of_typ_or_comp c in List.Tot.map type_of_binder pl *) (**** Step 2 *) /// The retrieved type refinements and post-conditions are not instantiated (they /// are lambda functions): instantiate them to get propositions. /// Generate a term of the form [has_type x ty] val mk_has_type : term -> typ -> Tac term let mk_has_type t ty = let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params // TODO: I don't understand why I need ifuel 2 here #push-options "--ifuel 2" /// Generate the propositions equivalent to a correct type cast. /// Note that the type refinements need to be instantiated. val cast_info_to_propositions : bool -> genv -> cast_info -> Tac (list proposition) let cast_info_to_propositions dbg ge ci = print_dbg dbg ("[> cast_info_to_propositions:\n" ^ cast_info_to_string ci); match compare_cast_types dbg ci with | Refines -> print_dbg dbg ("-> Comparison result: Refines"); [] | Same_raw_type -> print_dbg dbg ("-> Comparison result: Same_raw_type"); let refin = get_refinement (Some?.v ci.exp_ty) in let inst_refin = mk_app_norm ge.env refin [ci.term] in [inst_refin] | Unknown -> print_dbg dbg ("-> Comparison result: Unknown"); match ci.p_ty, ci.exp_ty with | Some p_ty, Some e_ty -> let p_rty = get_rawest_type p_ty in let e_rty = get_rawest_type e_ty in (* For the type cast, we generate an assertion of the form: * [> has_type (p <: type_of_p) expected_type * The reason is that we want the user to know which parameter is * concerned (hence the ``has_type``), and which types should be * related (hence the ascription). *) let ascr_term = pack (Tv_AscribedT ci.term p_rty None false) in let has_type_params = [(p_rty, Q_Implicit); (ascr_term, Q_Explicit); (e_rty, Q_Explicit)] in let type_cast = mk_app (`has_type) has_type_params in (* Expected type's refinement *) let inst_opt_refin = opt_mk_app_norm ge.env e_ty.refin [ci.term] in opt_cons inst_opt_refin [type_cast] | _ -> [] #pop-options /// Generates a list of propositions from a list of ``cast_info``. Note that /// the user should revert the list before printing the propositions. val cast_info_list_to_propositions : bool -> genv -> list cast_info -> Tac (list proposition) let cast_info_list_to_propositions dbg ge ls = let lsl = map (cast_info_to_propositions dbg ge) ls in flatten lsl /// When dealing with unknown effects, we try to guess what the pre and post-conditions /// are. The effects are indeed very likely to have a pre and a post-condition, /// and to be parameterized with an internal effect state, so that the pre and posts /// have the following shapes: /// - pre : STATE -> Type0 /// - post : STATE -> RET -> STATE -> Type0 /// Or (no state/pure): /// - pre : Type0 /// - post : RET -> Type0 /// We try to detect that with the following functions: noeq type pre_post_type = | PP_Unknown | PP_Pure | PP_State : (state_type:term) -> pre_post_type val compute_pre_type : bool -> env -> term -> Tac pre_post_type let compute_pre_type dbg e pre = print_dbg dbg "[> compute_pre_type"; match safe_tc e pre with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [], true -> print_dbg dbg "PP_Pure"; PP_Pure | [b], true -> print_dbg dbg ("PP_State " ^ (term_to_string (type_of_binder b))); PP_State (type_of_binder b) | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val opt_remove_refin : typ -> Tac typ let opt_remove_refin ty = match inspect ty with | Tv_Refine _ sort _ -> sort | _ -> ty val compute_post_type : bool -> env -> term -> term -> Tac pre_post_type let compute_post_type dbg e ret_type post = print_dbg dbg "[> compute_post_type"; let get_tot_ret_type c : Tac (option term_view) = match get_total_or_gtotal_ret_type c with | Some ret_ty -> Some (inspect ret_ty) | None -> None in match safe_tc e post with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [r], true -> (* Pure *) print_dbg dbg "PP_Pure"; PP_Pure | [s1; r; s2], true -> (* Stateful: check that the state types are coherent *) let r_ty = type_of_binder r in let s1_ty = type_of_binder s1 in (* After testing with Stack: the final state seems to have a refinement * (which gives the postcondition) so we need to remove it to match * it against the initial state *) let s2_ty = opt_remove_refin (type_of_binder s2) in let ret_type_eq = term_eq ret_type r_ty in let state_type_eq = term_eq s1_ty s2_ty in print_dbg dbg ("- ret type:\n-- target: " ^ term_to_string ret_type ^ "\n-- binder: " ^ term_to_string r_ty); print_dbg dbg ("- state types:\n-- binder1: " ^ term_to_string s1_ty ^ "\n-- binder2: " ^ term_to_string s2_ty); print_dbg dbg ("- ret type equality: " ^ string_of_bool ret_type_eq); print_dbg dbg ("- state types equality: " ^ string_of_bool state_type_eq); if ret_type_eq && state_type_eq then begin print_dbg dbg ("PP_State" ^ term_to_string (type_of_binder s1)); PP_State (type_of_binder s1) end else begin print_dbg dbg "PP_Unknown"; PP_Unknown end | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val check_pre_post_type : bool -> env -> term -> term -> term -> Tac pre_post_type let check_pre_post_type dbg e pre ret_type post = print_dbg dbg "[> check_pre_post_type"; match compute_pre_type dbg e pre, compute_post_type dbg e ret_type post with | PP_Pure, PP_Pure -> print_dbg dbg "PP_Pure, PP_Pure"; PP_Pure | PP_State ty1, PP_State ty2 -> print_dbg dbg "PP_State, PP_State"; if term_eq ty1 ty2 then PP_State ty1 else PP_Unknown | _ -> print_dbg dbg "_, _"; PP_Unknown val check_opt_pre_post_type : bool -> env -> option term -> term -> option term -> Tac (option pre_post_type) let check_opt_pre_post_type dbg e opt_pre ret_type opt_post = print_dbg dbg "[> check_opt_pre_post_type"; match opt_pre, opt_post with | Some pre, Some post -> print_dbg dbg "Some pre, Some post"; Some (check_pre_post_type dbg e pre ret_type post) | Some pre, None -> print_dbg dbg "Some pre, None"; Some (compute_pre_type dbg e pre) | None, Some post -> print_dbg dbg "None, Some post"; Some (compute_post_type dbg e ret_type post) | None, None -> print_dbg dbg "None, None"; None val _introduce_variables_for_abs : genv -> typ -> Tac (list term & list binder & genv) let rec _introduce_variables_for_abs ge ty = match inspect ty with | Tv_Arrow b c -> let ge1, b1 = genv_push_fresh_binder ge ("__" ^ name_of_binder b) (type_of_binder b) in let bv1 = bv_of_binder b1 in let v1 = pack (Tv_Var bv1) in begin match get_total_or_gtotal_ret_type c with | Some ty1 -> let vl, bl, ge2 = _introduce_variables_for_abs ge1 ty1 in v1 :: vl, b1 :: bl, ge2 | None -> [v1], [b1], ge1 end | _ -> [], [], ge val introduce_variables_for_abs : genv -> term -> Tac (list term & list binder & genv) let introduce_variables_for_abs ge tm = match safe_tc ge.env tm with | Some ty -> _introduce_variables_for_abs ge ty | None -> [], [], ge val introduce_variables_for_opt_abs : genv -> option term -> Tac (list term & list binder & genv) let introduce_variables_for_opt_abs ge opt_tm = match opt_tm with | Some tm -> introduce_variables_for_abs ge tm | None -> [], [], ge val effect_type_is_stateful : effect_type -> Tot bool let effect_type_is_stateful etype = match etype with | E_Total | E_GTotal | E_Lemma | E_PURE | E_Pure -> false | E_Stack | E_ST | E_Unknown -> true let is_st_get dbg t : Tac bool = print_dbg dbg ("[> is_st_get:\n" ^ term_to_string t); match inspect t with | Tv_App hd (a, qual) -> print_dbg dbg "-> Is Tv_App"; begin match inspect hd with | Tv_FVar fv -> print_dbg dbg ("-> Head is Tv_FVar: " ^ fv_to_string fv); fv_eq_name fv ["FStar"; "HyperStack"; "ST"; "get"] | _ -> print_dbg dbg "-> Head is not Tv_FVar"; false end | _ -> print_dbg dbg "-> Is not Tv_App"; false let is_let_st_get dbg (t : term_view) = print_dbg dbg ("[> is_let_st_get:\n" ^ term_to_string t); match t with | Tv_Let recf attrs bv ty def body -> print_dbg dbg "The term is a let expression"; if is_st_get dbg def then Some (bv, ty) else None | _ -> print_dbg dbg "The term is not a let expression"; None // TODO: Define relation between parents and children in and use it in explore_term // app: head or arg // let : bv or def or child // match: scrutinee or branch // ascribed: e or ty /// Check if a term's computation is effectful. The return type is option /// because we may not be able to retrieve the term computation. val term_has_effectful_comp : bool -> env -> term -> Tac (option bool) let term_has_effectful_comp dbg e tm = print_dbg dbg "[> term_has_effectful_comp"; let einfo_opt = compute_effect_info dbg e tm in match einfo_opt with | Some einfo -> print_dbg dbg ("Effect type: " ^ effect_type_to_string einfo.ei_type); Some (not (effect_type_is_pure einfo.ei_type)) | None -> print_dbg dbg "Could not compute effect info"; None /// Check if a related term is effectful. This is used to look for instances of /// ``HS.mem`` to instantiate pre/postconditions, which means that the term should /// be a parent/child term of the term under study, as generated by ``explore_term`` /// (otherwise the way we check that a term is effectful doesn't make sense). /// The computation is an overapproximation: it may happen that, for instance, we /// can't compute a term computation. In this case, we consider that the term is /// effectful. There are also situations in which we may not be sure which term to /// consider. let related_term_is_effectul dbg ge tv : Tac bool = let is_effectful tm = term_has_effectful_comp dbg ge.env tm <> Some false in match tv with | Tv_Var _ | Tv_BVar _ | Tv_FVar _ -> false | Tv_App hd (a, qual) -> (* The term under focus should be the app itself or an argument *) false | Tv_Abs br body -> false | Tv_Arrow br c0 -> false | Tv_Type _ -> false | Tv_Refine bv sort ref -> false | Tv_Const _ -> false | Tv_Uvar _ _ -> false | Tv_Let recf attrs bv ty def body -> is_effectful def | Tv_Match scrutinee _ret_opt branches -> (* TODO: we need to keep track of the relation between parents and children *) (* We assume the term under focus is in one the branches of the match - this * assumption is safe: in the worst case, we won't be able to find a mem to use. * Besides, in practice it is uncommon (impossible?) to use an effectful term * as the scrutinee of a match *) is_effectful scrutinee | Tv_AscribedT e ty tac _ -> false (* The focused term should be inside the ascription *) | Tv_AscribedC e c tac _ -> false (* The focused term should be inside the ascription *) | _ -> (* Unknown: keep things safe *) true /// Look for a term of the form ``let h = ST.get ()`` in a list of parent/children terms /// and return the let-bound bv. Abort the search if we find a non-effectful term. /// The typical usages of this function are the following: /// - look for a state variable to instantiate the precondition of the term under focus /// - look for state variables for the pre/postconditions of a term defined before /// the term under focus. val find_mem_in_related: dbg:bool -> ge:genv -> tms:list term_view -> Tac (option (bv & typ))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> ge: FStar.InteractiveHelpers.Base.genv -> tms: Prims.list FStar.Stubs.Reflection.V1.Data.term_view -> FStar.Tactics.Effect.Tac (FStar.Pervasives.Native.option (FStar.Stubs.Reflection.Types.bv * FStar.Stubs.Reflection.Types.typ))
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.InteractiveHelpers.Base.genv", "Prims.list", "FStar.Stubs.Reflection.V1.Data.term_view", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "FStar.Stubs.Reflection.Types.bv", "FStar.Stubs.Reflection.Types.typ", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.Some", "Prims.unit", "FStar.InteractiveHelpers.Base.print_dbg", "FStar.InteractiveHelpers.Effectful.find_mem_in_related", "FStar.InteractiveHelpers.Effectful.related_term_is_effectul", "FStar.InteractiveHelpers.Effectful.is_let_st_get", "Prims.string", "Prims.op_Hat", "FStar.Stubs.Tactics.V1.Builtins.term_to_string", "FStar.Stubs.Reflection.Types.term", "FStar.Stubs.Tactics.V1.Builtins.pack" ]
[ "recursion" ]
false
true
false
false
false
let rec find_mem_in_related dbg ge tms =
match tms with | [] -> None | tv :: tms' -> print_dbg dbg ("[> find_mem_in_related:\n" ^ term_to_string tv); match is_let_st_get dbg tv with | Some bvt -> print_dbg dbg "Term is of the form `let x = FStar.HyperStack.ST.get ()`: success"; Some bvt | None -> print_dbg dbg "Term is not of the form `let x = FStar.HyperStack.ST.get ()`: continuing"; if related_term_is_effectul dbg ge tv then (print_dbg dbg "Term is effectful: stopping here"; None) else (print_dbg dbg "Term is not effectful: continuing"; find_mem_in_related dbg ge tms')
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.compute_post_type
val compute_post_type : bool -> env -> term -> term -> Tac pre_post_type
val compute_post_type : bool -> env -> term -> term -> Tac pre_post_type
let compute_post_type dbg e ret_type post = print_dbg dbg "[> compute_post_type"; let get_tot_ret_type c : Tac (option term_view) = match get_total_or_gtotal_ret_type c with | Some ret_ty -> Some (inspect ret_ty) | None -> None in match safe_tc e post with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [r], true -> (* Pure *) print_dbg dbg "PP_Pure"; PP_Pure | [s1; r; s2], true -> (* Stateful: check that the state types are coherent *) let r_ty = type_of_binder r in let s1_ty = type_of_binder s1 in (* After testing with Stack: the final state seems to have a refinement * (which gives the postcondition) so we need to remove it to match * it against the initial state *) let s2_ty = opt_remove_refin (type_of_binder s2) in let ret_type_eq = term_eq ret_type r_ty in let state_type_eq = term_eq s1_ty s2_ty in print_dbg dbg ("- ret type:\n-- target: " ^ term_to_string ret_type ^ "\n-- binder: " ^ term_to_string r_ty); print_dbg dbg ("- state types:\n-- binder1: " ^ term_to_string s1_ty ^ "\n-- binder2: " ^ term_to_string s2_ty); print_dbg dbg ("- ret type equality: " ^ string_of_bool ret_type_eq); print_dbg dbg ("- state types equality: " ^ string_of_bool state_type_eq); if ret_type_eq && state_type_eq then begin print_dbg dbg ("PP_State" ^ term_to_string (type_of_binder s1)); PP_State (type_of_binder s1) end else begin print_dbg dbg "PP_Unknown"; PP_Unknown end | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 16, "end_line": 443, "start_col": 0, "start_line": 394 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown (*/// Retrieve the list of types from the parameters stored in ``typ_or_comp``. val typ_or_comp_to_param_types : typ_or_comp -> Tot (list typ) let typ_or_comp_to_param_types c = let pl = params_of_typ_or_comp c in List.Tot.map type_of_binder pl *) (**** Step 2 *) /// The retrieved type refinements and post-conditions are not instantiated (they /// are lambda functions): instantiate them to get propositions. /// Generate a term of the form [has_type x ty] val mk_has_type : term -> typ -> Tac term let mk_has_type t ty = let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params // TODO: I don't understand why I need ifuel 2 here #push-options "--ifuel 2" /// Generate the propositions equivalent to a correct type cast. /// Note that the type refinements need to be instantiated. val cast_info_to_propositions : bool -> genv -> cast_info -> Tac (list proposition) let cast_info_to_propositions dbg ge ci = print_dbg dbg ("[> cast_info_to_propositions:\n" ^ cast_info_to_string ci); match compare_cast_types dbg ci with | Refines -> print_dbg dbg ("-> Comparison result: Refines"); [] | Same_raw_type -> print_dbg dbg ("-> Comparison result: Same_raw_type"); let refin = get_refinement (Some?.v ci.exp_ty) in let inst_refin = mk_app_norm ge.env refin [ci.term] in [inst_refin] | Unknown -> print_dbg dbg ("-> Comparison result: Unknown"); match ci.p_ty, ci.exp_ty with | Some p_ty, Some e_ty -> let p_rty = get_rawest_type p_ty in let e_rty = get_rawest_type e_ty in (* For the type cast, we generate an assertion of the form: * [> has_type (p <: type_of_p) expected_type * The reason is that we want the user to know which parameter is * concerned (hence the ``has_type``), and which types should be * related (hence the ascription). *) let ascr_term = pack (Tv_AscribedT ci.term p_rty None false) in let has_type_params = [(p_rty, Q_Implicit); (ascr_term, Q_Explicit); (e_rty, Q_Explicit)] in let type_cast = mk_app (`has_type) has_type_params in (* Expected type's refinement *) let inst_opt_refin = opt_mk_app_norm ge.env e_ty.refin [ci.term] in opt_cons inst_opt_refin [type_cast] | _ -> [] #pop-options /// Generates a list of propositions from a list of ``cast_info``. Note that /// the user should revert the list before printing the propositions. val cast_info_list_to_propositions : bool -> genv -> list cast_info -> Tac (list proposition) let cast_info_list_to_propositions dbg ge ls = let lsl = map (cast_info_to_propositions dbg ge) ls in flatten lsl /// When dealing with unknown effects, we try to guess what the pre and post-conditions /// are. The effects are indeed very likely to have a pre and a post-condition, /// and to be parameterized with an internal effect state, so that the pre and posts /// have the following shapes: /// - pre : STATE -> Type0 /// - post : STATE -> RET -> STATE -> Type0 /// Or (no state/pure): /// - pre : Type0 /// - post : RET -> Type0 /// We try to detect that with the following functions: noeq type pre_post_type = | PP_Unknown | PP_Pure | PP_State : (state_type:term) -> pre_post_type val compute_pre_type : bool -> env -> term -> Tac pre_post_type let compute_pre_type dbg e pre = print_dbg dbg "[> compute_pre_type"; match safe_tc e pre with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [], true -> print_dbg dbg "PP_Pure"; PP_Pure | [b], true -> print_dbg dbg ("PP_State " ^ (term_to_string (type_of_binder b))); PP_State (type_of_binder b) | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val opt_remove_refin : typ -> Tac typ let opt_remove_refin ty = match inspect ty with | Tv_Refine _ sort _ -> sort | _ -> ty
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> e: FStar.Stubs.Reflection.Types.env -> ret_type: FStar.Stubs.Reflection.Types.term -> post: FStar.Stubs.Reflection.Types.term -> FStar.Tactics.Effect.Tac FStar.InteractiveHelpers.Effectful.pre_post_type
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.Stubs.Reflection.Types.env", "FStar.Stubs.Reflection.Types.term", "FStar.InteractiveHelpers.Effectful.PP_Unknown", "FStar.InteractiveHelpers.Effectful.pre_post_type", "Prims.unit", "FStar.InteractiveHelpers.Base.print_dbg", "Prims.list", "FStar.Stubs.Reflection.Types.binder", "FStar.Stubs.Reflection.Types.comp", "FStar.Pervasives.Native.Mktuple2", "FStar.InteractiveHelpers.ExploreTerm.is_total_or_gtotal", "FStar.InteractiveHelpers.Effectful.PP_Pure", "Prims.op_AmpAmp", "FStar.InteractiveHelpers.Effectful.PP_State", "FStar.Reflection.V1.Derived.type_of_binder", "Prims.string", "Prims.op_Hat", "FStar.Stubs.Tactics.V1.Builtins.term_to_string", "Prims.string_of_bool", "FStar.InteractiveHelpers.Effectful.term_eq", "FStar.Stubs.Reflection.Types.typ", "FStar.InteractiveHelpers.Effectful.opt_remove_refin", "FStar.Pervasives.Native.tuple2", "Prims.string_of_int", "FStar.List.Tot.Base.length", "FStar.Tactics.V1.SyntaxHelpers.collect_arr_bs", "FStar.Pervasives.Native.option", "FStar.InteractiveHelpers.ExploreTerm.safe_tc", "FStar.Stubs.Reflection.V1.Data.term_view", "FStar.InteractiveHelpers.ExploreTerm.get_total_or_gtotal_ret_type", "FStar.Pervasives.Native.Some", "FStar.Stubs.Tactics.V1.Builtins.inspect", "FStar.Pervasives.Native.None" ]
[]
false
true
false
false
false
let compute_post_type dbg e ret_type post =
print_dbg dbg "[> compute_post_type"; let get_tot_ret_type c : Tac (option term_view) = match get_total_or_gtotal_ret_type c with | Some ret_ty -> Some (inspect ret_ty) | None -> None in match safe_tc e post with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [r], true -> print_dbg dbg "PP_Pure"; PP_Pure | [s1 ; r ; s2], true -> let r_ty = type_of_binder r in let s1_ty = type_of_binder s1 in let s2_ty = opt_remove_refin (type_of_binder s2) in let ret_type_eq = term_eq ret_type r_ty in let state_type_eq = term_eq s1_ty s2_ty in print_dbg dbg ("- ret type:\n-- target: " ^ term_to_string ret_type ^ "\n-- binder: " ^ term_to_string r_ty); print_dbg dbg ("- state types:\n-- binder1: " ^ term_to_string s1_ty ^ "\n-- binder2: " ^ term_to_string s2_ty); print_dbg dbg ("- ret type equality: " ^ string_of_bool ret_type_eq); print_dbg dbg ("- state types equality: " ^ string_of_bool state_type_eq); if ret_type_eq && state_type_eq then (print_dbg dbg ("PP_State" ^ term_to_string (type_of_binder s1)); PP_State (type_of_binder s1)) else (print_dbg dbg "PP_Unknown"; PP_Unknown) | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown
false
Steel.Channel.Simplex.fst
Steel.Channel.Simplex.update_channel
val update_channel (#p: sprot) (#q: _) (c: chan_t q) (x: msg_t p) (vs: chan_val) (r: ref chan_val) : SteelT chan_val ((pts_to r full_perm vs) `star` (in_state_slprop p vs)) (fun vs' -> (pts_to r full_perm vs') `star` ((in_state_slprop (step p x) vs') `star` (chan_inv_step vs vs')))
val update_channel (#p: sprot) (#q: _) (c: chan_t q) (x: msg_t p) (vs: chan_val) (r: ref chan_val) : SteelT chan_val ((pts_to r full_perm vs) `star` (in_state_slprop p vs)) (fun vs' -> (pts_to r full_perm vs') `star` ((in_state_slprop (step p x) vs') `star` (chan_inv_step vs vs')))
let update_channel (#p:sprot) #q (c:chan_t q) (x:msg_t p) (vs:chan_val) (r:ref chan_val) : SteelT chan_val (pts_to r full_perm vs `star` in_state_slprop p vs) (fun vs' -> pts_to r full_perm vs' `star` (in_state_slprop (step p x) vs' `star` chan_inv_step vs vs')) = elim_pure (in_state_prop p vs); let vs' = next_chan_val x vs in H.write r vs'; intro_pure (in_state_prop (step p x) vs'); intro_pure (chan_inv_step_p vs vs'); return vs'
{ "file_name": "lib/steel/Steel.Channel.Simplex.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 14, "end_line": 268, "start_col": 0, "start_line": 259 }
(* Copyright 2020 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module Steel.Channel.Simplex module P = Steel.Channel.Protocol open Steel.SpinLock open Steel.Memory open Steel.Effect.Atomic open Steel.Effect open Steel.HigherReference open Steel.FractionalPermission module MRef = Steel.MonotonicHigherReference module H = Steel.HigherReference let sprot = p:prot { more p } noeq type chan_val = { chan_prot : sprot; chan_msg : msg_t chan_prot; chan_ctr : nat } let mref a p = MRef.ref a p let trace_ref (p:prot) = mref (partial_trace_of p) extended_to noeq type chan_t (p:prot) = { send: ref chan_val; recv: ref chan_val; trace: trace_ref p; } let half : perm = half_perm full_perm let step (s:sprot) (x:msg_t s) = step s x let chan_inv_step_p (vrecv vsend:chan_val) : prop = (vsend.chan_prot == step vrecv.chan_prot vrecv.chan_msg /\ vsend.chan_ctr == vrecv.chan_ctr + 1) let chan_inv_step (vrecv vsend:chan_val) : vprop = pure (chan_inv_step_p vrecv vsend) let chan_inv_cond (vsend:chan_val) (vrecv:chan_val) : vprop = if vsend.chan_ctr = vrecv.chan_ctr then pure (vsend == vrecv) else chan_inv_step vrecv vsend let trace_until_prop #p (r:trace_ref p) (vr:chan_val) (tr: partial_trace_of p) : vprop = MRef.pts_to r full_perm tr `star` pure (until tr == step vr.chan_prot vr.chan_msg) let trace_until #p (r:trace_ref p) (vr:chan_val) = h_exists (trace_until_prop r vr) let chan_inv_recv #p (c:chan_t p) (vsend:chan_val) = h_exists (fun (vrecv:chan_val) -> pts_to c.recv half vrecv `star` trace_until c.trace vrecv `star` chan_inv_cond vsend vrecv) let chan_inv #p (c:chan_t p) : vprop = h_exists (fun (vsend:chan_val) -> pts_to c.send half vsend `star` chan_inv_recv c vsend) let intro_chan_inv_cond_eqT (vs vr:chan_val) : Steel unit emp (fun _ -> chan_inv_cond vs vr) (requires fun _ -> vs == vr) (ensures fun _ _ _ -> True) = intro_pure (vs == vs); rewrite_slprop (chan_inv_cond vs vs) (chan_inv_cond vs vr) (fun _ -> ()) let intro_chan_inv_cond_stepT (vs vr:chan_val) : SteelT unit (chan_inv_step vr vs) (fun _ -> chan_inv_cond vs vr) = Steel.Utils.extract_pure (chan_inv_step_p vr vs); rewrite_slprop (chan_inv_step vr vs) (chan_inv_cond vs vr) (fun _ -> ()) let intro_chan_inv_auxT #p (#vs : chan_val) (#vr : chan_val) (c:chan_t p) : SteelT unit (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_cond vs vr) (fun _ -> chan_inv c) = intro_exists _ (fun (vr:chan_val) -> pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_cond vs vr); intro_exists _ (fun (vs:chan_val) -> pts_to c.send half vs `star` chan_inv_recv c vs) let intro_chan_inv_stepT #p (c:chan_t p) (vs vr:chan_val) : SteelT unit (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_step vr vs) (fun _ -> chan_inv c) = intro_chan_inv_cond_stepT vs vr; intro_chan_inv_auxT c let intro_chan_inv_eqT #p (c:chan_t p) (vs vr:chan_val) : Steel unit (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr) (fun _ -> chan_inv c) (requires fun _ -> vs == vr) (ensures fun _ _ _ -> True) = intro_chan_inv_cond_eqT vs vr; intro_chan_inv_auxT c noeq type chan p = { chan_chan : chan_t p; chan_lock : lock (chan_inv chan_chan) } let in_state_prop (p:prot) (vsend:chan_val) : prop = p == step vsend.chan_prot vsend.chan_msg irreducible let next_chan_val (#p:sprot) (x:msg_t p) (vs0:chan_val { in_state_prop p vs0 }) : Tot (vs:chan_val{in_state_prop (step p x) vs /\ chan_inv_step_p vs0 vs}) = { chan_prot = (step vs0.chan_prot vs0.chan_msg); chan_msg = x; chan_ctr = vs0.chan_ctr + 1 } [@@__reduce__] let in_state_slprop (p:prot) (vsend:chan_val) : vprop = pure (in_state_prop p vsend) let in_state (r:ref chan_val) (p:prot) = h_exists (fun (vsend:chan_val) -> pts_to r half vsend `star` in_state_slprop p vsend) let sender #q (c:chan q) (p:prot) = in_state c.chan_chan.send p let receiver #q (c:chan q) (p:prot) = in_state c.chan_chan.recv p let intro_chan_inv #p (c:chan_t p) (v:chan_val) : SteelT unit (pts_to c.send half v `star` pts_to c.recv half v `star` trace_until c.trace v) (fun _ -> chan_inv c) = intro_chan_inv_eqT c v v let chan_val_p (p:prot) = (vs0:chan_val { in_state_prop p vs0 }) let intro_in_state (r:ref chan_val) (p:prot) (v:chan_val_p p) : SteelT unit (pts_to r half v) (fun _ -> in_state r p) = intro_pure (in_state_prop p v); intro_exists v (fun (v:chan_val) -> pts_to r half v `star` in_state_slprop p v) let msg t p = Msg Send unit (fun _ -> p) let init_chan_val (p:prot) = v:chan_val {v.chan_prot == msg unit p} let initial_trace (p:prot) : (q:partial_trace_of p {until q == p}) = { to = p; tr=Waiting p} let intro_trace_until #q (r:trace_ref q) (tr:partial_trace_of q) (v:chan_val) : Steel unit (MRef.pts_to r full_perm tr) (fun _ -> trace_until r v) (requires fun _ -> until tr == step v.chan_prot v.chan_msg) (ensures fun _ _ _ -> True) = intro_pure (until tr == step v.chan_prot v.chan_msg); intro_exists tr (fun (tr:partial_trace_of q) -> MRef.pts_to r full_perm tr `star` pure (until tr == (step v.chan_prot v.chan_msg))); () let chan_t_sr (p:prot) (send recv:ref chan_val) = (c:chan_t p{c.send == send /\ c.recv == recv}) let intro_trace_until_init #p (c:chan_t p) (v:init_chan_val p) : SteelT unit (MRef.pts_to c.trace full_perm (initial_trace p)) (fun _ -> trace_until c.trace v) = intro_pure (until (initial_trace p) == step v.chan_prot v.chan_msg); //TODO: Not sure why I need this rewrite rewrite_slprop (MRef.pts_to c.trace full_perm (initial_trace p) `star` pure (until (initial_trace p) == step v.chan_prot v.chan_msg)) (MRef.pts_to c.trace full_perm (initial_trace p) `star` pure (until (initial_trace p) == step v.chan_prot v.chan_msg)) (fun _ -> ()); intro_exists (initial_trace p) (trace_until_prop c.trace v) let mk_chan (#p:prot) (send recv:ref chan_val) (v:init_chan_val p) : SteelT (chan_t_sr p send recv) (pts_to send half v `star` pts_to recv half v) (fun c -> chan_inv c) = let tr: trace_ref p = MRef.alloc (extended_to #p) (initial_trace p) in let c = Mkchan_t send recv tr in rewrite_slprop (MRef.pts_to tr full_perm (initial_trace p)) (MRef.pts_to c.trace full_perm (initial_trace p)) (fun _ -> ()); intro_trace_until_init c v; rewrite_slprop (pts_to send half v `star` pts_to recv half v) (pts_to c.send half v `star` pts_to c.recv half v) (fun _ -> ()); intro_chan_inv #p c v; let c' : chan_t_sr p send recv = c in rewrite_slprop (chan_inv c) (chan_inv c') (fun _ -> ()); return c' let new_chan (p:prot) : SteelT (chan p) emp (fun c -> sender c p `star` receiver c p) = let q = msg unit p in let v : chan_val = { chan_prot = q; chan_msg = (); chan_ctr = 0 } in let vp : init_chan_val p = v in let send = H.alloc v in let recv = H.alloc v in H.share recv; H.share send; (* TODO: use smt_fallback *) rewrite_slprop (pts_to send (half_perm full_perm) v `star` pts_to send (half_perm full_perm) v `star` pts_to recv (half_perm full_perm) v `star` pts_to recv (half_perm full_perm) v) (pts_to send half vp `star` pts_to send half vp `star` pts_to recv half vp `star` pts_to recv half vp) (fun _ -> ()); let c = mk_chan send recv vp in intro_in_state send p vp; intro_in_state recv p vp; let l = Steel.SpinLock.new_lock (chan_inv c) in let ch = { chan_chan = c; chan_lock = l } in rewrite_slprop (in_state send p) (sender ch p) (fun _ -> ()); rewrite_slprop (in_state recv p) (receiver ch p) (fun _ -> ()); return ch [@@__reduce__] let send_recv_in_sync (r:ref chan_val) (p:prot{more p}) #q (c:chan_t q) (vs vr:chan_val) : vprop = (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` pure (vs == vr) `star` in_state r p) [@@__reduce__] let sender_ahead (r:ref chan_val) (p:prot{more p}) #q (c:chan_t q) (vs vr:chan_val) : vprop = (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_step vr vs `star` in_state r p)
{ "checked_file": "/", "dependencies": [ "Steel.Utils.fst.checked", "Steel.SpinLock.fsti.checked", "Steel.MonotonicHigherReference.fsti.checked", "Steel.Memory.fsti.checked", "Steel.HigherReference.fsti.checked", "Steel.FractionalPermission.fst.checked", "Steel.Effect.Atomic.fsti.checked", "Steel.Effect.fsti.checked", "Steel.Channel.Protocol.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "Steel.Channel.Simplex.fst" }
[ { "abbrev": true, "full_module": "Steel.HigherReference", "short_module": "H" }, { "abbrev": true, "full_module": "Steel.MonotonicHigherReference", "short_module": "MRef" }, { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.HigherReference", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Atomic", "short_module": null }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.SpinLock", "short_module": null }, { "abbrev": true, "full_module": "Steel.Channel.Protocol", "short_module": "P" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Channel.Protocol", "short_module": null }, { "abbrev": false, "full_module": "Steel.Channel", "short_module": null }, { "abbrev": false, "full_module": "Steel.Channel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Steel.Channel.Simplex.chan_t q -> x: Steel.Channel.Protocol.msg_t p -> vs: Steel.Channel.Simplex.chan_val -> r: Steel.HigherReference.ref Steel.Channel.Simplex.chan_val -> Steel.Effect.SteelT Steel.Channel.Simplex.chan_val
Steel.Effect.SteelT
[]
[]
[ "Steel.Channel.Simplex.sprot", "Steel.Channel.Simplex.prot", "Steel.Channel.Simplex.chan_t", "Steel.Channel.Protocol.msg_t", "Steel.Channel.Simplex.chan_val", "Steel.HigherReference.ref", "Steel.Effect.Atomic.return", "FStar.Ghost.hide", "FStar.Set.set", "Steel.Memory.iname", "FStar.Set.empty", "Steel.Effect.Common.VStar", "Steel.Channel.Simplex.chan_inv_step", "Steel.Effect.Common.pure", "Steel.Channel.Simplex.in_state_prop", "Steel.Channel.Simplex.step", "Steel.HigherReference.pts_to", "Steel.FractionalPermission.full_perm", "Steel.Effect.Common.vprop", "Prims.unit", "Steel.Effect.Atomic.intro_pure", "Steel.Channel.Simplex.chan_inv_step_p", "Steel.HigherReference.write", "Prims.l_and", "Steel.Channel.Simplex.next_chan_val", "Steel.Effect.Atomic.elim_pure", "Steel.Effect.Common.star", "Steel.Channel.Simplex.in_state_slprop" ]
[]
false
true
false
false
false
let update_channel (#p: sprot) #q (c: chan_t q) (x: msg_t p) (vs: chan_val) (r: ref chan_val) : SteelT chan_val ((pts_to r full_perm vs) `star` (in_state_slprop p vs)) (fun vs' -> (pts_to r full_perm vs') `star` ((in_state_slprop (step p x) vs') `star` (chan_inv_step vs vs'))) =
elim_pure (in_state_prop p vs); let vs' = next_chan_val x vs in H.write r vs'; intro_pure (in_state_prop (step p x) vs'); intro_pure (chan_inv_step_p vs vs'); return vs'
false
Steel.Channel.Simplex.fst
Steel.Channel.Simplex.trace
val trace (#q:prot) (cc:chan q) : SteelT (tr:partial_trace_of q & history cc tr) emp (fun _ -> emp)
val trace (#q:prot) (cc:chan q) : SteelT (tr:partial_trace_of q & history cc tr) emp (fun _ -> emp)
let trace #q (cc:chan q) : SteelT (tr:partial_trace_of q & history cc tr) emp (fun _ -> emp) = let _ = send_receive_prelude cc in let tr = witness_trace_until cc.chan_chan.trace in intro_chan_inv_auxT cc.chan_chan; Steel.SpinLock.release cc.chan_lock; tr
{ "file_name": "lib/steel/Steel.Channel.Simplex.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 6, "end_line": 458, "start_col": 0, "start_line": 451 }
(* Copyright 2020 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module Steel.Channel.Simplex module P = Steel.Channel.Protocol open Steel.SpinLock open Steel.Memory open Steel.Effect.Atomic open Steel.Effect open Steel.HigherReference open Steel.FractionalPermission module MRef = Steel.MonotonicHigherReference module H = Steel.HigherReference let sprot = p:prot { more p } noeq type chan_val = { chan_prot : sprot; chan_msg : msg_t chan_prot; chan_ctr : nat } let mref a p = MRef.ref a p let trace_ref (p:prot) = mref (partial_trace_of p) extended_to noeq type chan_t (p:prot) = { send: ref chan_val; recv: ref chan_val; trace: trace_ref p; } let half : perm = half_perm full_perm let step (s:sprot) (x:msg_t s) = step s x let chan_inv_step_p (vrecv vsend:chan_val) : prop = (vsend.chan_prot == step vrecv.chan_prot vrecv.chan_msg /\ vsend.chan_ctr == vrecv.chan_ctr + 1) let chan_inv_step (vrecv vsend:chan_val) : vprop = pure (chan_inv_step_p vrecv vsend) let chan_inv_cond (vsend:chan_val) (vrecv:chan_val) : vprop = if vsend.chan_ctr = vrecv.chan_ctr then pure (vsend == vrecv) else chan_inv_step vrecv vsend let trace_until_prop #p (r:trace_ref p) (vr:chan_val) (tr: partial_trace_of p) : vprop = MRef.pts_to r full_perm tr `star` pure (until tr == step vr.chan_prot vr.chan_msg) let trace_until #p (r:trace_ref p) (vr:chan_val) = h_exists (trace_until_prop r vr) let chan_inv_recv #p (c:chan_t p) (vsend:chan_val) = h_exists (fun (vrecv:chan_val) -> pts_to c.recv half vrecv `star` trace_until c.trace vrecv `star` chan_inv_cond vsend vrecv) let chan_inv #p (c:chan_t p) : vprop = h_exists (fun (vsend:chan_val) -> pts_to c.send half vsend `star` chan_inv_recv c vsend) let intro_chan_inv_cond_eqT (vs vr:chan_val) : Steel unit emp (fun _ -> chan_inv_cond vs vr) (requires fun _ -> vs == vr) (ensures fun _ _ _ -> True) = intro_pure (vs == vs); rewrite_slprop (chan_inv_cond vs vs) (chan_inv_cond vs vr) (fun _ -> ()) let intro_chan_inv_cond_stepT (vs vr:chan_val) : SteelT unit (chan_inv_step vr vs) (fun _ -> chan_inv_cond vs vr) = Steel.Utils.extract_pure (chan_inv_step_p vr vs); rewrite_slprop (chan_inv_step vr vs) (chan_inv_cond vs vr) (fun _ -> ()) let intro_chan_inv_auxT #p (#vs : chan_val) (#vr : chan_val) (c:chan_t p) : SteelT unit (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_cond vs vr) (fun _ -> chan_inv c) = intro_exists _ (fun (vr:chan_val) -> pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_cond vs vr); intro_exists _ (fun (vs:chan_val) -> pts_to c.send half vs `star` chan_inv_recv c vs) let intro_chan_inv_stepT #p (c:chan_t p) (vs vr:chan_val) : SteelT unit (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_step vr vs) (fun _ -> chan_inv c) = intro_chan_inv_cond_stepT vs vr; intro_chan_inv_auxT c let intro_chan_inv_eqT #p (c:chan_t p) (vs vr:chan_val) : Steel unit (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr) (fun _ -> chan_inv c) (requires fun _ -> vs == vr) (ensures fun _ _ _ -> True) = intro_chan_inv_cond_eqT vs vr; intro_chan_inv_auxT c noeq type chan p = { chan_chan : chan_t p; chan_lock : lock (chan_inv chan_chan) } let in_state_prop (p:prot) (vsend:chan_val) : prop = p == step vsend.chan_prot vsend.chan_msg irreducible let next_chan_val (#p:sprot) (x:msg_t p) (vs0:chan_val { in_state_prop p vs0 }) : Tot (vs:chan_val{in_state_prop (step p x) vs /\ chan_inv_step_p vs0 vs}) = { chan_prot = (step vs0.chan_prot vs0.chan_msg); chan_msg = x; chan_ctr = vs0.chan_ctr + 1 } [@@__reduce__] let in_state_slprop (p:prot) (vsend:chan_val) : vprop = pure (in_state_prop p vsend) let in_state (r:ref chan_val) (p:prot) = h_exists (fun (vsend:chan_val) -> pts_to r half vsend `star` in_state_slprop p vsend) let sender #q (c:chan q) (p:prot) = in_state c.chan_chan.send p let receiver #q (c:chan q) (p:prot) = in_state c.chan_chan.recv p let intro_chan_inv #p (c:chan_t p) (v:chan_val) : SteelT unit (pts_to c.send half v `star` pts_to c.recv half v `star` trace_until c.trace v) (fun _ -> chan_inv c) = intro_chan_inv_eqT c v v let chan_val_p (p:prot) = (vs0:chan_val { in_state_prop p vs0 }) let intro_in_state (r:ref chan_val) (p:prot) (v:chan_val_p p) : SteelT unit (pts_to r half v) (fun _ -> in_state r p) = intro_pure (in_state_prop p v); intro_exists v (fun (v:chan_val) -> pts_to r half v `star` in_state_slprop p v) let msg t p = Msg Send unit (fun _ -> p) let init_chan_val (p:prot) = v:chan_val {v.chan_prot == msg unit p} let initial_trace (p:prot) : (q:partial_trace_of p {until q == p}) = { to = p; tr=Waiting p} let intro_trace_until #q (r:trace_ref q) (tr:partial_trace_of q) (v:chan_val) : Steel unit (MRef.pts_to r full_perm tr) (fun _ -> trace_until r v) (requires fun _ -> until tr == step v.chan_prot v.chan_msg) (ensures fun _ _ _ -> True) = intro_pure (until tr == step v.chan_prot v.chan_msg); intro_exists tr (fun (tr:partial_trace_of q) -> MRef.pts_to r full_perm tr `star` pure (until tr == (step v.chan_prot v.chan_msg))); () let chan_t_sr (p:prot) (send recv:ref chan_val) = (c:chan_t p{c.send == send /\ c.recv == recv}) let intro_trace_until_init #p (c:chan_t p) (v:init_chan_val p) : SteelT unit (MRef.pts_to c.trace full_perm (initial_trace p)) (fun _ -> trace_until c.trace v) = intro_pure (until (initial_trace p) == step v.chan_prot v.chan_msg); //TODO: Not sure why I need this rewrite rewrite_slprop (MRef.pts_to c.trace full_perm (initial_trace p) `star` pure (until (initial_trace p) == step v.chan_prot v.chan_msg)) (MRef.pts_to c.trace full_perm (initial_trace p) `star` pure (until (initial_trace p) == step v.chan_prot v.chan_msg)) (fun _ -> ()); intro_exists (initial_trace p) (trace_until_prop c.trace v) let mk_chan (#p:prot) (send recv:ref chan_val) (v:init_chan_val p) : SteelT (chan_t_sr p send recv) (pts_to send half v `star` pts_to recv half v) (fun c -> chan_inv c) = let tr: trace_ref p = MRef.alloc (extended_to #p) (initial_trace p) in let c = Mkchan_t send recv tr in rewrite_slprop (MRef.pts_to tr full_perm (initial_trace p)) (MRef.pts_to c.trace full_perm (initial_trace p)) (fun _ -> ()); intro_trace_until_init c v; rewrite_slprop (pts_to send half v `star` pts_to recv half v) (pts_to c.send half v `star` pts_to c.recv half v) (fun _ -> ()); intro_chan_inv #p c v; let c' : chan_t_sr p send recv = c in rewrite_slprop (chan_inv c) (chan_inv c') (fun _ -> ()); return c' let new_chan (p:prot) : SteelT (chan p) emp (fun c -> sender c p `star` receiver c p) = let q = msg unit p in let v : chan_val = { chan_prot = q; chan_msg = (); chan_ctr = 0 } in let vp : init_chan_val p = v in let send = H.alloc v in let recv = H.alloc v in H.share recv; H.share send; (* TODO: use smt_fallback *) rewrite_slprop (pts_to send (half_perm full_perm) v `star` pts_to send (half_perm full_perm) v `star` pts_to recv (half_perm full_perm) v `star` pts_to recv (half_perm full_perm) v) (pts_to send half vp `star` pts_to send half vp `star` pts_to recv half vp `star` pts_to recv half vp) (fun _ -> ()); let c = mk_chan send recv vp in intro_in_state send p vp; intro_in_state recv p vp; let l = Steel.SpinLock.new_lock (chan_inv c) in let ch = { chan_chan = c; chan_lock = l } in rewrite_slprop (in_state send p) (sender ch p) (fun _ -> ()); rewrite_slprop (in_state recv p) (receiver ch p) (fun _ -> ()); return ch [@@__reduce__] let send_recv_in_sync (r:ref chan_val) (p:prot{more p}) #q (c:chan_t q) (vs vr:chan_val) : vprop = (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` pure (vs == vr) `star` in_state r p) [@@__reduce__] let sender_ahead (r:ref chan_val) (p:prot{more p}) #q (c:chan_t q) (vs vr:chan_val) : vprop = (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_step vr vs `star` in_state r p) let update_channel (#p:sprot) #q (c:chan_t q) (x:msg_t p) (vs:chan_val) (r:ref chan_val) : SteelT chan_val (pts_to r full_perm vs `star` in_state_slprop p vs) (fun vs' -> pts_to r full_perm vs' `star` (in_state_slprop (step p x) vs' `star` chan_inv_step vs vs')) = elim_pure (in_state_prop p vs); let vs' = next_chan_val x vs in H.write r vs'; intro_pure (in_state_prop (step p x) vs'); intro_pure (chan_inv_step_p vs vs'); return vs' [@@__reduce__] let send_pre_available (p:sprot) #q (c:chan_t q) (vs vr:chan_val) = send_recv_in_sync c.send p c vs vr let gather_r (#p:sprot) (r:ref chan_val) (v:chan_val) : SteelT unit (pts_to r half v `star` in_state r p) (fun _ -> pts_to r full_perm v `star` in_state_slprop p v) = let v' = witness_exists () in H.higher_ref_pts_to_injective_eq #_ #_ #_ #_ #v #_ r; H.gather #_ #_ #half #half #v #v r; rewrite_slprop (pts_to r (sum_perm half half) v) (pts_to r full_perm v) (fun _ -> ()); rewrite_slprop (in_state_slprop p v') (in_state_slprop p v) (fun _ -> ()) let send_available (#p:sprot) #q (cc:chan q) (x:msg_t p) (vs vr:chan_val) (_:unit) : SteelT unit (send_pre_available p #q cc.chan_chan vs vr) (fun _ -> sender cc (step p x)) = Steel.Utils.extract_pure (vs == vr); Steel.Utils.rewrite #_ #(send_recv_in_sync cc.chan_chan.send p cc.chan_chan vs) vr vs; elim_pure (vs == vs); gather_r cc.chan_chan.send vs; let next_vs = update_channel cc.chan_chan x vs cc.chan_chan.send in H.share cc.chan_chan.send; intro_exists next_vs (fun (next_vs:chan_val) -> pts_to cc.chan_chan.send half next_vs `star` in_state_slprop (step p x) next_vs); intro_chan_inv_stepT cc.chan_chan next_vs vs; Steel.SpinLock.release cc.chan_lock let extensible (#p:prot) (x:partial_trace_of p) = P.more x.to let next_msg_t (#p:prot) (x:partial_trace_of p) = P.next_msg_t x.to let next_trace #p (vr:chan_val) (vs:chan_val) (tr:partial_trace_of p) (s:squash (until tr == step vr.chan_prot vr.chan_msg)) (_:squash (chan_inv_step_p vr vs)) : (ts:partial_trace_of p { until ts == step vs.chan_prot vs.chan_msg }) = let msg : next_msg_t tr = vs.chan_msg in assert (extensible tr); extend_partial_trace tr msg let next_trace_st #p (vr:chan_val) (vs:chan_val) (tr:partial_trace_of p) : Steel (extension_of tr) (chan_inv_step vr vs) (fun _ -> emp) (requires fun _ -> until tr == step vr.chan_prot vr.chan_msg) (ensures fun _ ts _ -> until ts == step vs.chan_prot vs.chan_msg) = elim_pure (chan_inv_step_p vr vs); let ts : extension_of tr = next_trace vr vs tr () () in return ts let update_trace #p (r:trace_ref p) (vr:chan_val) (vs:chan_val) : Steel unit (trace_until r vr) (fun _ -> trace_until r vs) (requires fun _ -> chan_inv_step_p vr vs) (ensures fun _ _ _ -> True) = intro_pure (chan_inv_step_p vr vs); let tr = MRef.read_refine r in elim_pure (until tr == step vr.chan_prot vr.chan_msg); let ts : extension_of tr = next_trace_st vr vs tr in MRef.write r ts; intro_pure (until ts == step vs.chan_prot vs.chan_msg); intro_exists ts (fun (ts:partial_trace_of p) -> MRef.pts_to r full_perm ts `star` pure (until ts == step vs.chan_prot vs.chan_msg)) let recv_availableT (#p:sprot) #q (cc:chan q) (vs vr:chan_val) (_:unit) : SteelT (msg_t p) (sender_ahead cc.chan_chan.recv p cc.chan_chan vs vr) (fun x -> receiver cc (step p x)) = elim_pure (chan_inv_step_p vr vs); gather_r cc.chan_chan.recv vr; elim_pure (in_state_prop p vr); H.write cc.chan_chan.recv vs; H.share cc.chan_chan.recv; assert (vs.chan_prot == p); let vs_msg : msg_t p = vs.chan_msg in intro_pure (in_state_prop (step p vs_msg) vs); intro_exists vs (fun (vs:chan_val) -> pts_to cc.chan_chan.recv half vs `star` in_state_slprop (step p vs_msg) vs); update_trace cc.chan_chan.trace vr vs; intro_chan_inv cc.chan_chan vs; Steel.SpinLock.release cc.chan_lock; vs_msg #push-options "--ide_id_info_off" let send_receive_prelude (#p:prot) (cc:chan p) : SteelT (chan_val & chan_val) emp (fun v -> pts_to cc.chan_chan.send half (fst v) `star` pts_to cc.chan_chan.recv half (snd v) `star` trace_until cc.chan_chan.trace (snd v) `star` chan_inv_cond (fst v) (snd v)) = let c = cc.chan_chan in Steel.SpinLock.acquire cc.chan_lock; let vs = read_refine (chan_inv_recv cc.chan_chan) cc.chan_chan.send in let _ = witness_exists () in let vr = H.read cc.chan_chan.recv in rewrite_slprop (trace_until _ _ `star` chan_inv_cond _ _) (trace_until cc.chan_chan.trace vr `star` chan_inv_cond vs vr) (fun _ -> ()); return (vs, vr) let rec send (#p:prot) (c:chan p) (#next:prot{more next}) (x:msg_t next) : SteelT unit (sender c next) (fun _ -> sender c (step next x)) = let v = send_receive_prelude c in //matching v as vs,vr fails if (fst v).chan_ctr = (snd v).chan_ctr then ( rewrite_slprop (chan_inv_cond (fst v) (snd v)) (pure (fst v == snd v)) (fun _ -> ()); send_available c x (fst v) (snd v) () //TODO: inlining send_availableT here fails ) else ( rewrite_slprop (chan_inv_cond (fst v) (snd v)) (chan_inv_step (snd v) (fst v)) (fun _ -> ()); intro_chan_inv_stepT c.chan_chan (fst v) (snd v); Steel.SpinLock.release c.chan_lock; send c x ) let rec recv (#p:prot) (#next:prot{more next}) (c:chan p) : SteelT (msg_t next) (receiver c next) (fun x -> receiver c (step next x)) = let v = send_receive_prelude c in if (fst v).chan_ctr = (snd v).chan_ctr then ( rewrite_slprop (chan_inv_cond (fst v) (snd v)) (pure (fst v == snd v)) (fun _ -> ()); elim_pure (fst v == snd v); intro_chan_inv_eqT c.chan_chan (fst v) (snd v); Steel.SpinLock.release c.chan_lock; recv c ) else ( rewrite_slprop (chan_inv_cond (fst v) (snd v)) (chan_inv_step (snd v) (fst v)) (fun _ -> ()); recv_availableT c (fst v) (snd v) () ) let history_p' (#p:prot) (t:partial_trace_of p) (s:partial_trace_of p) : prop = t `extended_to` s /\ True let history_p (#p:prot) (t:partial_trace_of p) : MRef.stable_property extended_to = history_p' t let history (#p:prot) (c:chan p) (t:partial_trace_of p) : Type0 = MRef.witnessed c.chan_chan.trace (history_p t) let recall_trace_ref #q (r:trace_ref q) (tr tr':partial_trace_of q) (tok:MRef.witnessed r (history_p tr)) : Steel unit (MRef.pts_to r full_perm tr') (fun _ -> MRef.pts_to r full_perm tr') (requires fun _ -> True) (ensures fun _ _ _ -> history_p tr tr') = MRef.recall (history_p tr) r tr' tok let prot_equals #q (#p:_) (#vr:chan_val) (cc:chan q) : Steel unit (pts_to cc.chan_chan.recv half vr `star` receiver cc p) (fun _ -> pts_to cc.chan_chan.recv half vr `star` receiver cc p) (requires fun _ -> True) (ensures fun _ _ _ -> step vr.chan_prot vr.chan_msg == p) = let vr' = witness_exists () in H.higher_ref_pts_to_injective_eq #_ #_ #_ #_ #vr #_ cc.chan_chan.recv; rewrite_slprop (in_state_slprop _ _) (in_state_slprop p vr) (fun _ -> ()); elim_pure _; intro_in_state _ _ vr let witness_trace_until #q (#vr:chan_val) (r:trace_ref q) : SteelT (tr:partial_trace_of q & MRef.witnessed r (history_p tr)) (trace_until r vr) (fun _ -> trace_until r vr) = let tr = MRef.read_refine r in let tok = MRef.witness r (history_p tr) tr () in elim_pure (until tr == step vr.chan_prot vr.chan_msg); intro_trace_until r tr vr; (| tr, tok |)
{ "checked_file": "/", "dependencies": [ "Steel.Utils.fst.checked", "Steel.SpinLock.fsti.checked", "Steel.MonotonicHigherReference.fsti.checked", "Steel.Memory.fsti.checked", "Steel.HigherReference.fsti.checked", "Steel.FractionalPermission.fst.checked", "Steel.Effect.Atomic.fsti.checked", "Steel.Effect.fsti.checked", "Steel.Channel.Protocol.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "Steel.Channel.Simplex.fst" }
[ { "abbrev": true, "full_module": "Steel.HigherReference", "short_module": "H" }, { "abbrev": true, "full_module": "Steel.MonotonicHigherReference", "short_module": "MRef" }, { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.HigherReference", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Atomic", "short_module": null }, { "abbrev": false, "full_module": "Steel.SpinLock", "short_module": null }, { "abbrev": true, "full_module": "Steel.Channel.Protocol", "short_module": "P" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Channel.Protocol", "short_module": null }, { "abbrev": false, "full_module": "Steel.Channel", "short_module": null }, { "abbrev": false, "full_module": "Steel.Channel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cc: Steel.Channel.Simplex.chan q -> Steel.Effect.SteelT (Prims.dtuple2 (Steel.Channel.Protocol.partial_trace_of q) (fun tr -> Steel.Channel.Simplex.history cc tr))
Steel.Effect.SteelT
[]
[]
[ "Steel.Channel.Simplex.prot", "Steel.Channel.Simplex.chan", "Prims.dtuple2", "Steel.Channel.Protocol.partial_trace_of", "Steel.Channel.Simplex.history", "Prims.unit", "Steel.SpinLock.release", "Steel.Channel.Simplex.chan_inv", "Steel.Channel.Simplex.__proj__Mkchan__item__chan_chan", "Steel.Channel.Simplex.__proj__Mkchan__item__chan_lock", "Steel.Channel.Simplex.intro_chan_inv_auxT", "Steel.Channel.Simplex.chan_val", "Steel.MonotonicHigherReference.witnessed", "Steel.Channel.Protocol.extended_to", "Steel.Channel.Simplex.__proj__Mkchan_t__item__trace", "Steel.Channel.Simplex.history_p", "Steel.Channel.Simplex.witness_trace_until", "FStar.Pervasives.Native.tuple2", "Steel.Channel.Simplex.send_receive_prelude", "Steel.Effect.Common.emp", "Steel.Effect.Common.vprop" ]
[]
false
true
false
false
false
let trace #q (cc: chan q) : SteelT (tr: partial_trace_of q & history cc tr) emp (fun _ -> emp) =
let _ = send_receive_prelude cc in let tr = witness_trace_until cc.chan_chan.trace in intro_chan_inv_auxT cc.chan_chan; Steel.SpinLock.release cc.chan_lock; tr
false
Vale.Interop.Types.fst
Vale.Interop.Types.view_n
val view_n (t: base_typ) : pos
val view_n (t: base_typ) : pos
let view_n (t:base_typ) : pos = view_n_unfold t
{ "file_name": "vale/specs/interop/Vale.Interop.Types.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 47, "end_line": 32, "start_col": 0, "start_line": 32 }
module Vale.Interop.Types open FStar.Mul include Vale.Arch.HeapTypes_s module B = LowStar.Buffer module IB = LowStar.ImmutableBuffer module MB = LowStar.Monotonic.Buffer module DV = LowStar.BufferView.Down module W = Vale.Def.Words_s module L = FStar.List.Tot [@__reduce__] let base_typ_as_type (t:base_typ) : eqtype = let open W in let open Vale.Def.Types_s in match t with | TUInt8 -> UInt8.t | TUInt16 -> UInt16.t | TUInt32 -> UInt32.t | TUInt64 -> UInt64.t | TUInt128 -> quad32 [@__reduce__] unfold // without the unfold, "% view_n t" leads to very slow Z3 performance let view_n_unfold (t:base_typ) : pos = match t with | TUInt8 -> 1 | TUInt16 -> 2 | TUInt32 -> 4 | TUInt64 -> 8 | TUInt128 -> 16
{ "checked_file": "/", "dependencies": [ "Vale.Interop.Views.fsti.checked", "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Arch.HeapTypes_s.fst.checked", "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.ImmutableBuffer.fst.checked", "LowStar.BufferView.Down.fsti.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked" ], "interface_file": false, "source_file": "Vale.Interop.Types.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "Vale.Def.Words_s", "short_module": "W" }, { "abbrev": true, "full_module": "LowStar.BufferView.Down", "short_module": "DV" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": true, "full_module": "LowStar.ImmutableBuffer", "short_module": "IB" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "Vale.Arch.HeapTypes_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
t: Vale.Arch.HeapTypes_s.base_typ -> Prims.pos
Prims.Tot
[ "total" ]
[]
[ "Vale.Arch.HeapTypes_s.base_typ", "Vale.Interop.Types.view_n_unfold", "Prims.pos" ]
[]
false
false
false
true
false
let view_n (t: base_typ) : pos =
view_n_unfold t
false
Vale.Interop.Types.fst
Vale.Interop.Types.disjoint_addr
val disjoint_addr : addr1: Prims.int -> length1: Prims.int -> addr2: Prims.int -> length2: Prims.int -> Prims.bool
let disjoint_addr addr1 length1 addr2 length2 = (* The first buffer is completely before the second, or the opposite *) addr1 + length1 < addr2 || addr2 + length2 < addr1
{ "file_name": "vale/specs/interop/Vale.Interop.Types.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 25, "end_line": 61, "start_col": 0, "start_line": 58 }
module Vale.Interop.Types open FStar.Mul include Vale.Arch.HeapTypes_s module B = LowStar.Buffer module IB = LowStar.ImmutableBuffer module MB = LowStar.Monotonic.Buffer module DV = LowStar.BufferView.Down module W = Vale.Def.Words_s module L = FStar.List.Tot [@__reduce__] let base_typ_as_type (t:base_typ) : eqtype = let open W in let open Vale.Def.Types_s in match t with | TUInt8 -> UInt8.t | TUInt16 -> UInt16.t | TUInt32 -> UInt32.t | TUInt64 -> UInt64.t | TUInt128 -> quad32 [@__reduce__] unfold // without the unfold, "% view_n t" leads to very slow Z3 performance let view_n_unfold (t:base_typ) : pos = match t with | TUInt8 -> 1 | TUInt16 -> 2 | TUInt32 -> 4 | TUInt64 -> 8 | TUInt128 -> 16 let view_n (t:base_typ) : pos = view_n_unfold t [@__reduce__] let down_view (t:base_typ) : DV.view (base_typ_as_type t) UInt8.t = match t with | TUInt8 -> Vale.Interop.Views.down_view8 | TUInt16 -> Vale.Interop.Views.down_view16 | TUInt32 -> Vale.Interop.Views.down_view32 | TUInt64 -> Vale.Interop.Views.down_view64 | TUInt128 -> Vale.Interop.Views.down_view128 let b8_preorder (writeable:bool) (a:Type0) : MB.srel a = match writeable with | true -> B.trivial_preorder a | false -> IB.immutable_preorder a [@__reduce__] noeq type b8 = | Buffer: #src:base_typ -> writeable:bool -> bsrc:MB.mbuffer (base_typ_as_type src) (b8_preorder writeable (base_typ_as_type src)) (b8_preorder writeable (base_typ_as_type src)) -> b8
{ "checked_file": "/", "dependencies": [ "Vale.Interop.Views.fsti.checked", "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Arch.HeapTypes_s.fst.checked", "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.ImmutableBuffer.fst.checked", "LowStar.BufferView.Down.fsti.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked" ], "interface_file": false, "source_file": "Vale.Interop.Types.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "Vale.Def.Words_s", "short_module": "W" }, { "abbrev": true, "full_module": "LowStar.BufferView.Down", "short_module": "DV" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": true, "full_module": "LowStar.ImmutableBuffer", "short_module": "IB" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "Vale.Arch.HeapTypes_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
addr1: Prims.int -> length1: Prims.int -> addr2: Prims.int -> length2: Prims.int -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "Prims.int", "Prims.op_BarBar", "Prims.op_LessThan", "Prims.op_Addition", "Prims.bool" ]
[]
false
false
false
true
false
let disjoint_addr addr1 length1 addr2 length2 =
addr1 + length1 < addr2 || addr2 + length2 < addr1
false
Pulse.Lib.InvList.fst
Pulse.Lib.InvList.with_invlist
val with_invlist (#a:Type0) (#pre : vprop) (#post : a -> vprop) (is : invlist) (f : unit -> stt_atomic a #Unobservable emp_inames (invlist_v is ** pre) (fun v -> invlist_v is ** post v)) : stt_atomic a #Unobservable (invlist_names is) pre (fun v -> post v)
val with_invlist (#a:Type0) (#pre : vprop) (#post : a -> vprop) (is : invlist) (f : unit -> stt_atomic a #Unobservable emp_inames (invlist_v is ** pre) (fun v -> invlist_v is ** post v)) : stt_atomic a #Unobservable (invlist_names is) pre (fun v -> post v)
let with_invlist = __with_invlist
{ "file_name": "share/steel/examples/pulse/lib/pledge/Pulse.Lib.InvList.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 33, "end_line": 68, "start_col": 0, "start_line": 68 }
module Pulse.Lib.InvList open Pulse.Lib.Pervasives module T0 = Pulse.Lib.Priv.Trade0 ```pulse unobservable fn __shift_invlist_one (#a:Type0) (p : vprop) (i : inv p) (is : invlist{not (mem_inv (invlist_names is) i)}) (#pre:vprop) (#post : a -> vprop) (f : unit -> stt_atomic a #Unobservable emp_inames (invlist_v (add_one (| p, i |) is) ** pre) (fun v -> invlist_v (add_one (| p, i |) is) ** post v)) (_:unit) requires invlist_v is ** (p ** pre) returns v:a ensures invlist_v is ** (p ** post v) { rewrite (p ** invlist_v is) as (invlist_v (add_one (|p, i|) is)); let v = f (); rewrite (invlist_v (add_one (|p, i|) is)) as (p ** invlist_v is); v } ``` let shift_invlist_one = __shift_invlist_one ```pulse unobservable fn rec __with_invlist (#a:Type0) (#pre : vprop) (#post : a -> vprop) (is : invlist) (f : unit -> stt_atomic a #Unobservable emp_inames (invlist_v is ** pre) (fun v -> invlist_v is ** post v)) requires pre returns v:a ensures post v opens (invlist_names is) decreases is { match is { Nil -> { rewrite emp as invlist_v is; let r = f (); rewrite invlist_v is as emp; r } Cons h t -> { let p = dfst h; let i : inv p = dsnd h; with_invariants (i <: inv p) { assert (p ** pre); let fw : (unit -> stt_atomic a #Unobservable emp_inames (invlist_v t ** (p ** pre)) (fun v -> invlist_v t ** (p ** post v))) = shift_invlist_one #a p i t #pre #post f; let v = __with_invlist #a #(p ** pre) #(fun v -> p ** post v) t fw; assert (p ** post v); v } } } }
{ "checked_file": "/", "dependencies": [ "Pulse.Lib.Priv.Trade0.fsti.checked", "Pulse.Lib.Pervasives.fst.checked", "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "Pulse.Lib.InvList.fst" }
[ { "abbrev": true, "full_module": "Pulse.Lib.Priv.Trade0", "short_module": "T0" }, { "abbrev": false, "full_module": "Pulse.Lib.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
is: Pulse.Lib.InvList.invlist -> f: (_: Prims.unit -> Pulse.Lib.Core.stt_atomic a Pulse.Lib.Core.emp_inames (Pulse.Lib.InvList.invlist_v is ** pre) (fun v -> Pulse.Lib.InvList.invlist_v is ** post v)) -> Pulse.Lib.Core.stt_atomic a (Pulse.Lib.InvList.invlist_names is) pre (fun v -> post v)
Prims.Tot
[ "total" ]
[]
[ "Pulse.Lib.InvList.__with_invlist" ]
[]
false
false
false
false
false
let with_invlist =
__with_invlist
false
Pulse.Lib.InvList.fst
Pulse.Lib.InvList.lift_ghost_unobservable
val lift_ghost_unobservable (#pre #post: _) (f: stt_ghost unit pre post) : stt_atomic unit #Unobservable emp_inames pre post
val lift_ghost_unobservable (#pre #post: _) (f: stt_ghost unit pre post) : stt_atomic unit #Unobservable emp_inames pre post
let lift_ghost_unobservable #pre #post (f:stt_ghost unit pre post) : stt_atomic unit #Unobservable emp_inames pre post = lift_observability #_ #_ #Unobservable (lift_ghost_neutral f unit_non_informative)
{ "file_name": "share/steel/examples/pulse/lib/pledge/Pulse.Lib.InvList.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 86, "end_line": 72, "start_col": 0, "start_line": 70 }
module Pulse.Lib.InvList open Pulse.Lib.Pervasives module T0 = Pulse.Lib.Priv.Trade0 ```pulse unobservable fn __shift_invlist_one (#a:Type0) (p : vprop) (i : inv p) (is : invlist{not (mem_inv (invlist_names is) i)}) (#pre:vprop) (#post : a -> vprop) (f : unit -> stt_atomic a #Unobservable emp_inames (invlist_v (add_one (| p, i |) is) ** pre) (fun v -> invlist_v (add_one (| p, i |) is) ** post v)) (_:unit) requires invlist_v is ** (p ** pre) returns v:a ensures invlist_v is ** (p ** post v) { rewrite (p ** invlist_v is) as (invlist_v (add_one (|p, i|) is)); let v = f (); rewrite (invlist_v (add_one (|p, i|) is)) as (p ** invlist_v is); v } ``` let shift_invlist_one = __shift_invlist_one ```pulse unobservable fn rec __with_invlist (#a:Type0) (#pre : vprop) (#post : a -> vprop) (is : invlist) (f : unit -> stt_atomic a #Unobservable emp_inames (invlist_v is ** pre) (fun v -> invlist_v is ** post v)) requires pre returns v:a ensures post v opens (invlist_names is) decreases is { match is { Nil -> { rewrite emp as invlist_v is; let r = f (); rewrite invlist_v is as emp; r } Cons h t -> { let p = dfst h; let i : inv p = dsnd h; with_invariants (i <: inv p) { assert (p ** pre); let fw : (unit -> stt_atomic a #Unobservable emp_inames (invlist_v t ** (p ** pre)) (fun v -> invlist_v t ** (p ** post v))) = shift_invlist_one #a p i t #pre #post f; let v = __with_invlist #a #(p ** pre) #(fun v -> p ** post v) t fw; assert (p ** post v); v } } } } ``` let with_invlist = __with_invlist
{ "checked_file": "/", "dependencies": [ "Pulse.Lib.Priv.Trade0.fsti.checked", "Pulse.Lib.Pervasives.fst.checked", "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "Pulse.Lib.InvList.fst" }
[ { "abbrev": true, "full_module": "Pulse.Lib.Priv.Trade0", "short_module": "T0" }, { "abbrev": false, "full_module": "Pulse.Lib.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": 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: Pulse.Lib.Core.stt_ghost Prims.unit pre post -> Pulse.Lib.Core.stt_atomic Prims.unit Pulse.Lib.Core.emp_inames pre post
Prims.Tot
[ "total" ]
[]
[ "Pulse.Lib.Core.vprop", "Prims.unit", "Pulse.Lib.Core.stt_ghost", "Pulse.Lib.Core.lift_observability", "PulseCore.Observability.Neutral", "PulseCore.Observability.Unobservable", "Pulse.Lib.Core.emp_inames", "Pulse.Lib.Core.lift_ghost_neutral", "Pulse.Lib.Core.unit_non_informative", "Pulse.Lib.Core.stt_atomic" ]
[]
false
false
false
false
false
let lift_ghost_unobservable #pre #post (f: stt_ghost unit pre post) : stt_atomic unit #Unobservable emp_inames pre post =
lift_observability #_ #_ #Unobservable (lift_ghost_neutral f unit_non_informative)
false
Pulse.Lib.InvList.fst
Pulse.Lib.InvList.with_invlist_ghost
val with_invlist_ghost (#pre : vprop) (#post : vprop) (is : invlist) (f : unit -> stt_ghost unit (invlist_v is ** pre) (fun _ -> invlist_v is ** post)) : stt_atomic unit #Unobservable (invlist_names is) pre (fun _ -> post)
val with_invlist_ghost (#pre : vprop) (#post : vprop) (is : invlist) (f : unit -> stt_ghost unit (invlist_v is ** pre) (fun _ -> invlist_v is ** post)) : stt_atomic unit #Unobservable (invlist_names is) pre (fun _ -> post)
let with_invlist_ghost = __with_invlist_ghost
{ "file_name": "share/steel/examples/pulse/lib/pledge/Pulse.Lib.InvList.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 45, "end_line": 86, "start_col": 0, "start_line": 86 }
module Pulse.Lib.InvList open Pulse.Lib.Pervasives module T0 = Pulse.Lib.Priv.Trade0 ```pulse unobservable fn __shift_invlist_one (#a:Type0) (p : vprop) (i : inv p) (is : invlist{not (mem_inv (invlist_names is) i)}) (#pre:vprop) (#post : a -> vprop) (f : unit -> stt_atomic a #Unobservable emp_inames (invlist_v (add_one (| p, i |) is) ** pre) (fun v -> invlist_v (add_one (| p, i |) is) ** post v)) (_:unit) requires invlist_v is ** (p ** pre) returns v:a ensures invlist_v is ** (p ** post v) { rewrite (p ** invlist_v is) as (invlist_v (add_one (|p, i|) is)); let v = f (); rewrite (invlist_v (add_one (|p, i|) is)) as (p ** invlist_v is); v } ``` let shift_invlist_one = __shift_invlist_one ```pulse unobservable fn rec __with_invlist (#a:Type0) (#pre : vprop) (#post : a -> vprop) (is : invlist) (f : unit -> stt_atomic a #Unobservable emp_inames (invlist_v is ** pre) (fun v -> invlist_v is ** post v)) requires pre returns v:a ensures post v opens (invlist_names is) decreases is { match is { Nil -> { rewrite emp as invlist_v is; let r = f (); rewrite invlist_v is as emp; r } Cons h t -> { let p = dfst h; let i : inv p = dsnd h; with_invariants (i <: inv p) { assert (p ** pre); let fw : (unit -> stt_atomic a #Unobservable emp_inames (invlist_v t ** (p ** pre)) (fun v -> invlist_v t ** (p ** post v))) = shift_invlist_one #a p i t #pre #post f; let v = __with_invlist #a #(p ** pre) #(fun v -> p ** post v) t fw; assert (p ** post v); v } } } } ``` let with_invlist = __with_invlist let lift_ghost_unobservable #pre #post (f:stt_ghost unit pre post) : stt_atomic unit #Unobservable emp_inames pre post = lift_observability #_ #_ #Unobservable (lift_ghost_neutral f unit_non_informative) ```pulse unobservable fn __with_invlist_ghost (#pre : vprop) (#post : vprop) (is : invlist) (f : unit -> stt_ghost unit (invlist_v is ** pre) (fun _ -> invlist_v is ** post)) requires pre ensures post opens (invlist_names is) { with_invlist is (fun () -> lift_ghost_unobservable (f ())); }
{ "checked_file": "/", "dependencies": [ "Pulse.Lib.Priv.Trade0.fsti.checked", "Pulse.Lib.Pervasives.fst.checked", "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "Pulse.Lib.InvList.fst" }
[ { "abbrev": true, "full_module": "Pulse.Lib.Priv.Trade0", "short_module": "T0" }, { "abbrev": false, "full_module": "Pulse.Lib.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
is: Pulse.Lib.InvList.invlist -> f: (_: Prims.unit -> Pulse.Lib.Core.stt_ghost Prims.unit (Pulse.Lib.InvList.invlist_v is ** pre) (fun _ -> Pulse.Lib.InvList.invlist_v is ** post)) -> Pulse.Lib.Core.stt_atomic Prims.unit (Pulse.Lib.InvList.invlist_names is) pre (fun _ -> post)
Prims.Tot
[ "total" ]
[]
[ "Pulse.Lib.InvList.__with_invlist_ghost" ]
[]
false
false
false
false
false
let with_invlist_ghost =
__with_invlist_ghost
false
Vale.Interop.Types.fst
Vale.Interop.Types.get_downview
val get_downview : b: LowStar.Monotonic.Buffer.mbuffer (Vale.Interop.Types.base_typ_as_type src) rrel rel -> Prims.GTot (LowStar.BufferView.Down.buffer FStar.UInt8.t)
let get_downview (#src:base_typ) (#rrel #rel:MB.srel (base_typ_as_type src)) (b:MB.mbuffer (base_typ_as_type src) rrel rel) = DV.mk_buffer_view b (down_view src)
{ "file_name": "vale/specs/interop/Vale.Interop.Types.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 37, "end_line": 67, "start_col": 0, "start_line": 63 }
module Vale.Interop.Types open FStar.Mul include Vale.Arch.HeapTypes_s module B = LowStar.Buffer module IB = LowStar.ImmutableBuffer module MB = LowStar.Monotonic.Buffer module DV = LowStar.BufferView.Down module W = Vale.Def.Words_s module L = FStar.List.Tot [@__reduce__] let base_typ_as_type (t:base_typ) : eqtype = let open W in let open Vale.Def.Types_s in match t with | TUInt8 -> UInt8.t | TUInt16 -> UInt16.t | TUInt32 -> UInt32.t | TUInt64 -> UInt64.t | TUInt128 -> quad32 [@__reduce__] unfold // without the unfold, "% view_n t" leads to very slow Z3 performance let view_n_unfold (t:base_typ) : pos = match t with | TUInt8 -> 1 | TUInt16 -> 2 | TUInt32 -> 4 | TUInt64 -> 8 | TUInt128 -> 16 let view_n (t:base_typ) : pos = view_n_unfold t [@__reduce__] let down_view (t:base_typ) : DV.view (base_typ_as_type t) UInt8.t = match t with | TUInt8 -> Vale.Interop.Views.down_view8 | TUInt16 -> Vale.Interop.Views.down_view16 | TUInt32 -> Vale.Interop.Views.down_view32 | TUInt64 -> Vale.Interop.Views.down_view64 | TUInt128 -> Vale.Interop.Views.down_view128 let b8_preorder (writeable:bool) (a:Type0) : MB.srel a = match writeable with | true -> B.trivial_preorder a | false -> IB.immutable_preorder a [@__reduce__] noeq type b8 = | Buffer: #src:base_typ -> writeable:bool -> bsrc:MB.mbuffer (base_typ_as_type src) (b8_preorder writeable (base_typ_as_type src)) (b8_preorder writeable (base_typ_as_type src)) -> b8 let disjoint_addr addr1 length1 addr2 length2 = (* The first buffer is completely before the second, or the opposite *) addr1 + length1 < addr2 || addr2 + length2 < addr1
{ "checked_file": "/", "dependencies": [ "Vale.Interop.Views.fsti.checked", "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Arch.HeapTypes_s.fst.checked", "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.ImmutableBuffer.fst.checked", "LowStar.BufferView.Down.fsti.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked" ], "interface_file": false, "source_file": "Vale.Interop.Types.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "Vale.Def.Words_s", "short_module": "W" }, { "abbrev": true, "full_module": "LowStar.BufferView.Down", "short_module": "DV" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": true, "full_module": "LowStar.ImmutableBuffer", "short_module": "IB" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "Vale.Arch.HeapTypes_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer (Vale.Interop.Types.base_typ_as_type src) rrel rel -> Prims.GTot (LowStar.BufferView.Down.buffer FStar.UInt8.t)
Prims.GTot
[ "sometrivial" ]
[]
[ "Vale.Arch.HeapTypes_s.base_typ", "LowStar.Monotonic.Buffer.srel", "Vale.Interop.Types.base_typ_as_type", "LowStar.Monotonic.Buffer.mbuffer", "LowStar.BufferView.Down.mk_buffer_view", "FStar.UInt8.t", "Vale.Interop.Types.down_view", "LowStar.BufferView.Down.buffer" ]
[]
false
false
false
false
false
let get_downview (#src: base_typ) (#rrel #rel: MB.srel (base_typ_as_type src)) (b: MB.mbuffer (base_typ_as_type src) rrel rel) =
DV.mk_buffer_view b (down_view src)
false
Vale.Interop.Types.fst
Vale.Interop.Types.addr_map_pred
val addr_map_pred : m: (_: Vale.Interop.Types.b8 -> Vale.Def.Words_s.nat64) -> Prims.logical
let addr_map_pred (m:b8 -> W.nat64) = (forall (buf1 buf2:b8).{:pattern (m buf1); (m buf2)} MB.disjoint buf1.bsrc buf2.bsrc ==> disjoint_addr (m buf1) (DV.length (get_downview buf1.bsrc)) (m buf2) (DV.length (get_downview buf2.bsrc))) /\ (forall (b:b8).{:pattern (m b)} m b + DV.length (get_downview b.bsrc) < W.pow2_64)
{ "file_name": "vale/specs/interop/Vale.Interop.Types.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 84, "end_line": 74, "start_col": 0, "start_line": 70 }
module Vale.Interop.Types open FStar.Mul include Vale.Arch.HeapTypes_s module B = LowStar.Buffer module IB = LowStar.ImmutableBuffer module MB = LowStar.Monotonic.Buffer module DV = LowStar.BufferView.Down module W = Vale.Def.Words_s module L = FStar.List.Tot [@__reduce__] let base_typ_as_type (t:base_typ) : eqtype = let open W in let open Vale.Def.Types_s in match t with | TUInt8 -> UInt8.t | TUInt16 -> UInt16.t | TUInt32 -> UInt32.t | TUInt64 -> UInt64.t | TUInt128 -> quad32 [@__reduce__] unfold // without the unfold, "% view_n t" leads to very slow Z3 performance let view_n_unfold (t:base_typ) : pos = match t with | TUInt8 -> 1 | TUInt16 -> 2 | TUInt32 -> 4 | TUInt64 -> 8 | TUInt128 -> 16 let view_n (t:base_typ) : pos = view_n_unfold t [@__reduce__] let down_view (t:base_typ) : DV.view (base_typ_as_type t) UInt8.t = match t with | TUInt8 -> Vale.Interop.Views.down_view8 | TUInt16 -> Vale.Interop.Views.down_view16 | TUInt32 -> Vale.Interop.Views.down_view32 | TUInt64 -> Vale.Interop.Views.down_view64 | TUInt128 -> Vale.Interop.Views.down_view128 let b8_preorder (writeable:bool) (a:Type0) : MB.srel a = match writeable with | true -> B.trivial_preorder a | false -> IB.immutable_preorder a [@__reduce__] noeq type b8 = | Buffer: #src:base_typ -> writeable:bool -> bsrc:MB.mbuffer (base_typ_as_type src) (b8_preorder writeable (base_typ_as_type src)) (b8_preorder writeable (base_typ_as_type src)) -> b8 let disjoint_addr addr1 length1 addr2 length2 = (* The first buffer is completely before the second, or the opposite *) addr1 + length1 < addr2 || addr2 + length2 < addr1 let get_downview (#src:base_typ) (#rrel #rel:MB.srel (base_typ_as_type src)) (b:MB.mbuffer (base_typ_as_type src) rrel rel) = DV.mk_buffer_view b (down_view src)
{ "checked_file": "/", "dependencies": [ "Vale.Interop.Views.fsti.checked", "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Arch.HeapTypes_s.fst.checked", "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.ImmutableBuffer.fst.checked", "LowStar.BufferView.Down.fsti.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked" ], "interface_file": false, "source_file": "Vale.Interop.Types.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "Vale.Def.Words_s", "short_module": "W" }, { "abbrev": true, "full_module": "LowStar.BufferView.Down", "short_module": "DV" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": true, "full_module": "LowStar.ImmutableBuffer", "short_module": "IB" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "Vale.Arch.HeapTypes_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
m: (_: Vale.Interop.Types.b8 -> Vale.Def.Words_s.nat64) -> Prims.logical
Prims.Tot
[ "total" ]
[]
[ "Vale.Interop.Types.b8", "Vale.Def.Words_s.nat64", "Prims.l_and", "Prims.l_Forall", "Prims.l_imp", "LowStar.Monotonic.Buffer.disjoint", "Vale.Interop.Types.base_typ_as_type", "Vale.Interop.Types.__proj__Buffer__item__src", "Vale.Interop.Types.b8_preorder", "Vale.Interop.Types.__proj__Buffer__item__writeable", "Vale.Interop.Types.__proj__Buffer__item__bsrc", "Prims.b2t", "Vale.Interop.Types.disjoint_addr", "LowStar.BufferView.Down.length", "FStar.UInt8.t", "Vale.Interop.Types.get_downview", "Prims.op_LessThan", "Prims.op_Addition", "Vale.Def.Words_s.pow2_64", "Prims.logical" ]
[]
false
false
false
true
true
let addr_map_pred (m: (b8 -> W.nat64)) =
(forall (buf1: b8) (buf2: b8). {:pattern (m buf1); (m buf2)} MB.disjoint buf1.bsrc buf2.bsrc ==> disjoint_addr (m buf1) (DV.length (get_downview buf1.bsrc)) (m buf2) (DV.length (get_downview buf2.bsrc))) /\ (forall (b: b8). {:pattern (m b)} m b + DV.length (get_downview b.bsrc) < W.pow2_64)
false
Pulse.Lib.InvList.fst
Pulse.Lib.InvList.invlist_sub_split
val invlist_sub_split (is1 is2 : invlist) : stt_ghost unit (pure (invlist_sub is1 is2) ** invlist_v is2) (fun _ -> invlist_v is1 ** Pulse.Lib.Priv.Trade0.stick (invlist_v is1) (invlist_v is2))
val invlist_sub_split (is1 is2 : invlist) : stt_ghost unit (pure (invlist_sub is1 is2) ** invlist_v is2) (fun _ -> invlist_v is1 ** Pulse.Lib.Priv.Trade0.stick (invlist_v is1) (invlist_v is2))
let invlist_sub_split = __invlist_sub_split_wrap
{ "file_name": "share/steel/examples/pulse/lib/pledge/Pulse.Lib.InvList.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 48, "end_line": 159, "start_col": 0, "start_line": 159 }
module Pulse.Lib.InvList open Pulse.Lib.Pervasives module T0 = Pulse.Lib.Priv.Trade0 ```pulse unobservable fn __shift_invlist_one (#a:Type0) (p : vprop) (i : inv p) (is : invlist{not (mem_inv (invlist_names is) i)}) (#pre:vprop) (#post : a -> vprop) (f : unit -> stt_atomic a #Unobservable emp_inames (invlist_v (add_one (| p, i |) is) ** pre) (fun v -> invlist_v (add_one (| p, i |) is) ** post v)) (_:unit) requires invlist_v is ** (p ** pre) returns v:a ensures invlist_v is ** (p ** post v) { rewrite (p ** invlist_v is) as (invlist_v (add_one (|p, i|) is)); let v = f (); rewrite (invlist_v (add_one (|p, i|) is)) as (p ** invlist_v is); v } ``` let shift_invlist_one = __shift_invlist_one ```pulse unobservable fn rec __with_invlist (#a:Type0) (#pre : vprop) (#post : a -> vprop) (is : invlist) (f : unit -> stt_atomic a #Unobservable emp_inames (invlist_v is ** pre) (fun v -> invlist_v is ** post v)) requires pre returns v:a ensures post v opens (invlist_names is) decreases is { match is { Nil -> { rewrite emp as invlist_v is; let r = f (); rewrite invlist_v is as emp; r } Cons h t -> { let p = dfst h; let i : inv p = dsnd h; with_invariants (i <: inv p) { assert (p ** pre); let fw : (unit -> stt_atomic a #Unobservable emp_inames (invlist_v t ** (p ** pre)) (fun v -> invlist_v t ** (p ** post v))) = shift_invlist_one #a p i t #pre #post f; let v = __with_invlist #a #(p ** pre) #(fun v -> p ** post v) t fw; assert (p ** post v); v } } } } ``` let with_invlist = __with_invlist let lift_ghost_unobservable #pre #post (f:stt_ghost unit pre post) : stt_atomic unit #Unobservable emp_inames pre post = lift_observability #_ #_ #Unobservable (lift_ghost_neutral f unit_non_informative) ```pulse unobservable fn __with_invlist_ghost (#pre : vprop) (#post : vprop) (is : invlist) (f : unit -> stt_ghost unit (invlist_v is ** pre) (fun _ -> invlist_v is ** post)) requires pre ensures post opens (invlist_names is) { with_invlist is (fun () -> lift_ghost_unobservable (f ())); } ``` let with_invlist_ghost = __with_invlist_ghost let invlist_reveal = admit() let iname_inj (p1 p2 : vprop) (i1 : inv p1) (i2 : inv p2) : stt_ghost unit (pure (name_of_inv i1 = name_of_inv i2)) (fun _ -> pure (p1 == p2)) = admit() // We should bubble this fact up from the memory model let invlist_mem_split (i : iname) (is : invlist) (_ : squash (Set.mem i (invlist_names is))) : squash (exists l r p (ii : inv p). name_of_inv ii == i /\ is == l @ [(| p, ii |)] @ r) = admit() ```pulse ghost fn __invlist_sub_split (is1 is2 : invlist) (_: squash (invlist_sub is1 is2)) requires invlist_v is2 ensures invlist_v is1 ** T0.stick (invlist_v is1) (invlist_v is2) { open T0; match is1 { Nil -> { ghost fn aux (_:unit) requires invlist_v is2 ** invlist_v is1 ensures invlist_v is2 { rewrite invlist_v is1 as emp; (); }; intro_stick (invlist_v is1) (invlist_v is2) (invlist_v is2) aux; rewrite emp as invlist_v is1; } Cons h t -> { let p = dfst h; let i : inv p = dsnd h; let name = name_of_inv i; (* This is the interesting case, but it's rather hard to prove right now due to some pulse limitations (#151, #163). The idea is that (p, i) must also be in is2, so we can rewrite invlist_v is2 ~> invlist_v (l @ [(|p, i|)] @ r) ~> p ** invlist_v (l @ r) and prove invlist_sub t (l@r), then we call the lemma recursively to obtain p ** invlist_v t ** stick (invlist_v t) (invlist_v (l@r)) ~> invlist_v is1 ** stick (invlist_v t) (invlist_v (l@r)) then we can 'frame' the stick with 'p' to get ~> invlist_v is1 ** stick (p ** invlist_v t) (p ** invlist_v (l@r)) ~> invlist_v is1 ** stick is1 is2 *) admit(); } } } ``` ```pulse ghost fn __invlist_sub_split_wrap (is1 is2 : invlist) requires pure (invlist_sub is1 is2) ** invlist_v is2 ensures invlist_v is1 ** Pulse.Lib.Priv.Trade0.stick (invlist_v is1) (invlist_v is2) { __invlist_sub_split is1 is2 () }
{ "checked_file": "/", "dependencies": [ "Pulse.Lib.Priv.Trade0.fsti.checked", "Pulse.Lib.Pervasives.fst.checked", "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "Pulse.Lib.InvList.fst" }
[ { "abbrev": true, "full_module": "Pulse.Lib.Priv.Trade0", "short_module": "T0" }, { "abbrev": false, "full_module": "Pulse.Lib.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
is1: Pulse.Lib.InvList.invlist -> is2: Pulse.Lib.InvList.invlist -> Pulse.Lib.Core.stt_ghost Prims.unit (Pulse.Lib.Core.pure (Pulse.Lib.InvList.invlist_sub is1 is2) ** Pulse.Lib.InvList.invlist_v is2) (fun _ -> Pulse.Lib.InvList.invlist_v is1 ** Pulse.Lib.Priv.Trade0.stick (Pulse.Lib.InvList.invlist_v is1) (Pulse.Lib.InvList.invlist_v is2))
Prims.Tot
[ "total" ]
[]
[ "Pulse.Lib.InvList.__invlist_sub_split_wrap" ]
[]
false
false
false
false
false
let invlist_sub_split =
__invlist_sub_split_wrap
false
Steel.Channel.Simplex.fst
Steel.Channel.Simplex.witness_trace_until
val witness_trace_until (#q: _) (#vr: chan_val) (r: trace_ref q) : SteelT (tr: partial_trace_of q & MRef.witnessed r (history_p tr)) (trace_until r vr) (fun _ -> trace_until r vr)
val witness_trace_until (#q: _) (#vr: chan_val) (r: trace_ref q) : SteelT (tr: partial_trace_of q & MRef.witnessed r (history_p tr)) (trace_until r vr) (fun _ -> trace_until r vr)
let witness_trace_until #q (#vr:chan_val) (r:trace_ref q) : SteelT (tr:partial_trace_of q & MRef.witnessed r (history_p tr)) (trace_until r vr) (fun _ -> trace_until r vr) = let tr = MRef.read_refine r in let tok = MRef.witness r (history_p tr) tr () in elim_pure (until tr == step vr.chan_prot vr.chan_msg); intro_trace_until r tr vr; (| tr, tok |)
{ "file_name": "lib/steel/Steel.Channel.Simplex.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 17, "end_line": 449, "start_col": 0, "start_line": 441 }
(* Copyright 2020 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module Steel.Channel.Simplex module P = Steel.Channel.Protocol open Steel.SpinLock open Steel.Memory open Steel.Effect.Atomic open Steel.Effect open Steel.HigherReference open Steel.FractionalPermission module MRef = Steel.MonotonicHigherReference module H = Steel.HigherReference let sprot = p:prot { more p } noeq type chan_val = { chan_prot : sprot; chan_msg : msg_t chan_prot; chan_ctr : nat } let mref a p = MRef.ref a p let trace_ref (p:prot) = mref (partial_trace_of p) extended_to noeq type chan_t (p:prot) = { send: ref chan_val; recv: ref chan_val; trace: trace_ref p; } let half : perm = half_perm full_perm let step (s:sprot) (x:msg_t s) = step s x let chan_inv_step_p (vrecv vsend:chan_val) : prop = (vsend.chan_prot == step vrecv.chan_prot vrecv.chan_msg /\ vsend.chan_ctr == vrecv.chan_ctr + 1) let chan_inv_step (vrecv vsend:chan_val) : vprop = pure (chan_inv_step_p vrecv vsend) let chan_inv_cond (vsend:chan_val) (vrecv:chan_val) : vprop = if vsend.chan_ctr = vrecv.chan_ctr then pure (vsend == vrecv) else chan_inv_step vrecv vsend let trace_until_prop #p (r:trace_ref p) (vr:chan_val) (tr: partial_trace_of p) : vprop = MRef.pts_to r full_perm tr `star` pure (until tr == step vr.chan_prot vr.chan_msg) let trace_until #p (r:trace_ref p) (vr:chan_val) = h_exists (trace_until_prop r vr) let chan_inv_recv #p (c:chan_t p) (vsend:chan_val) = h_exists (fun (vrecv:chan_val) -> pts_to c.recv half vrecv `star` trace_until c.trace vrecv `star` chan_inv_cond vsend vrecv) let chan_inv #p (c:chan_t p) : vprop = h_exists (fun (vsend:chan_val) -> pts_to c.send half vsend `star` chan_inv_recv c vsend) let intro_chan_inv_cond_eqT (vs vr:chan_val) : Steel unit emp (fun _ -> chan_inv_cond vs vr) (requires fun _ -> vs == vr) (ensures fun _ _ _ -> True) = intro_pure (vs == vs); rewrite_slprop (chan_inv_cond vs vs) (chan_inv_cond vs vr) (fun _ -> ()) let intro_chan_inv_cond_stepT (vs vr:chan_val) : SteelT unit (chan_inv_step vr vs) (fun _ -> chan_inv_cond vs vr) = Steel.Utils.extract_pure (chan_inv_step_p vr vs); rewrite_slprop (chan_inv_step vr vs) (chan_inv_cond vs vr) (fun _ -> ()) let intro_chan_inv_auxT #p (#vs : chan_val) (#vr : chan_val) (c:chan_t p) : SteelT unit (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_cond vs vr) (fun _ -> chan_inv c) = intro_exists _ (fun (vr:chan_val) -> pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_cond vs vr); intro_exists _ (fun (vs:chan_val) -> pts_to c.send half vs `star` chan_inv_recv c vs) let intro_chan_inv_stepT #p (c:chan_t p) (vs vr:chan_val) : SteelT unit (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_step vr vs) (fun _ -> chan_inv c) = intro_chan_inv_cond_stepT vs vr; intro_chan_inv_auxT c let intro_chan_inv_eqT #p (c:chan_t p) (vs vr:chan_val) : Steel unit (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr) (fun _ -> chan_inv c) (requires fun _ -> vs == vr) (ensures fun _ _ _ -> True) = intro_chan_inv_cond_eqT vs vr; intro_chan_inv_auxT c noeq type chan p = { chan_chan : chan_t p; chan_lock : lock (chan_inv chan_chan) } let in_state_prop (p:prot) (vsend:chan_val) : prop = p == step vsend.chan_prot vsend.chan_msg irreducible let next_chan_val (#p:sprot) (x:msg_t p) (vs0:chan_val { in_state_prop p vs0 }) : Tot (vs:chan_val{in_state_prop (step p x) vs /\ chan_inv_step_p vs0 vs}) = { chan_prot = (step vs0.chan_prot vs0.chan_msg); chan_msg = x; chan_ctr = vs0.chan_ctr + 1 } [@@__reduce__] let in_state_slprop (p:prot) (vsend:chan_val) : vprop = pure (in_state_prop p vsend) let in_state (r:ref chan_val) (p:prot) = h_exists (fun (vsend:chan_val) -> pts_to r half vsend `star` in_state_slprop p vsend) let sender #q (c:chan q) (p:prot) = in_state c.chan_chan.send p let receiver #q (c:chan q) (p:prot) = in_state c.chan_chan.recv p let intro_chan_inv #p (c:chan_t p) (v:chan_val) : SteelT unit (pts_to c.send half v `star` pts_to c.recv half v `star` trace_until c.trace v) (fun _ -> chan_inv c) = intro_chan_inv_eqT c v v let chan_val_p (p:prot) = (vs0:chan_val { in_state_prop p vs0 }) let intro_in_state (r:ref chan_val) (p:prot) (v:chan_val_p p) : SteelT unit (pts_to r half v) (fun _ -> in_state r p) = intro_pure (in_state_prop p v); intro_exists v (fun (v:chan_val) -> pts_to r half v `star` in_state_slprop p v) let msg t p = Msg Send unit (fun _ -> p) let init_chan_val (p:prot) = v:chan_val {v.chan_prot == msg unit p} let initial_trace (p:prot) : (q:partial_trace_of p {until q == p}) = { to = p; tr=Waiting p} let intro_trace_until #q (r:trace_ref q) (tr:partial_trace_of q) (v:chan_val) : Steel unit (MRef.pts_to r full_perm tr) (fun _ -> trace_until r v) (requires fun _ -> until tr == step v.chan_prot v.chan_msg) (ensures fun _ _ _ -> True) = intro_pure (until tr == step v.chan_prot v.chan_msg); intro_exists tr (fun (tr:partial_trace_of q) -> MRef.pts_to r full_perm tr `star` pure (until tr == (step v.chan_prot v.chan_msg))); () let chan_t_sr (p:prot) (send recv:ref chan_val) = (c:chan_t p{c.send == send /\ c.recv == recv}) let intro_trace_until_init #p (c:chan_t p) (v:init_chan_val p) : SteelT unit (MRef.pts_to c.trace full_perm (initial_trace p)) (fun _ -> trace_until c.trace v) = intro_pure (until (initial_trace p) == step v.chan_prot v.chan_msg); //TODO: Not sure why I need this rewrite rewrite_slprop (MRef.pts_to c.trace full_perm (initial_trace p) `star` pure (until (initial_trace p) == step v.chan_prot v.chan_msg)) (MRef.pts_to c.trace full_perm (initial_trace p) `star` pure (until (initial_trace p) == step v.chan_prot v.chan_msg)) (fun _ -> ()); intro_exists (initial_trace p) (trace_until_prop c.trace v) let mk_chan (#p:prot) (send recv:ref chan_val) (v:init_chan_val p) : SteelT (chan_t_sr p send recv) (pts_to send half v `star` pts_to recv half v) (fun c -> chan_inv c) = let tr: trace_ref p = MRef.alloc (extended_to #p) (initial_trace p) in let c = Mkchan_t send recv tr in rewrite_slprop (MRef.pts_to tr full_perm (initial_trace p)) (MRef.pts_to c.trace full_perm (initial_trace p)) (fun _ -> ()); intro_trace_until_init c v; rewrite_slprop (pts_to send half v `star` pts_to recv half v) (pts_to c.send half v `star` pts_to c.recv half v) (fun _ -> ()); intro_chan_inv #p c v; let c' : chan_t_sr p send recv = c in rewrite_slprop (chan_inv c) (chan_inv c') (fun _ -> ()); return c' let new_chan (p:prot) : SteelT (chan p) emp (fun c -> sender c p `star` receiver c p) = let q = msg unit p in let v : chan_val = { chan_prot = q; chan_msg = (); chan_ctr = 0 } in let vp : init_chan_val p = v in let send = H.alloc v in let recv = H.alloc v in H.share recv; H.share send; (* TODO: use smt_fallback *) rewrite_slprop (pts_to send (half_perm full_perm) v `star` pts_to send (half_perm full_perm) v `star` pts_to recv (half_perm full_perm) v `star` pts_to recv (half_perm full_perm) v) (pts_to send half vp `star` pts_to send half vp `star` pts_to recv half vp `star` pts_to recv half vp) (fun _ -> ()); let c = mk_chan send recv vp in intro_in_state send p vp; intro_in_state recv p vp; let l = Steel.SpinLock.new_lock (chan_inv c) in let ch = { chan_chan = c; chan_lock = l } in rewrite_slprop (in_state send p) (sender ch p) (fun _ -> ()); rewrite_slprop (in_state recv p) (receiver ch p) (fun _ -> ()); return ch [@@__reduce__] let send_recv_in_sync (r:ref chan_val) (p:prot{more p}) #q (c:chan_t q) (vs vr:chan_val) : vprop = (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` pure (vs == vr) `star` in_state r p) [@@__reduce__] let sender_ahead (r:ref chan_val) (p:prot{more p}) #q (c:chan_t q) (vs vr:chan_val) : vprop = (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_step vr vs `star` in_state r p) let update_channel (#p:sprot) #q (c:chan_t q) (x:msg_t p) (vs:chan_val) (r:ref chan_val) : SteelT chan_val (pts_to r full_perm vs `star` in_state_slprop p vs) (fun vs' -> pts_to r full_perm vs' `star` (in_state_slprop (step p x) vs' `star` chan_inv_step vs vs')) = elim_pure (in_state_prop p vs); let vs' = next_chan_val x vs in H.write r vs'; intro_pure (in_state_prop (step p x) vs'); intro_pure (chan_inv_step_p vs vs'); return vs' [@@__reduce__] let send_pre_available (p:sprot) #q (c:chan_t q) (vs vr:chan_val) = send_recv_in_sync c.send p c vs vr let gather_r (#p:sprot) (r:ref chan_val) (v:chan_val) : SteelT unit (pts_to r half v `star` in_state r p) (fun _ -> pts_to r full_perm v `star` in_state_slprop p v) = let v' = witness_exists () in H.higher_ref_pts_to_injective_eq #_ #_ #_ #_ #v #_ r; H.gather #_ #_ #half #half #v #v r; rewrite_slprop (pts_to r (sum_perm half half) v) (pts_to r full_perm v) (fun _ -> ()); rewrite_slprop (in_state_slprop p v') (in_state_slprop p v) (fun _ -> ()) let send_available (#p:sprot) #q (cc:chan q) (x:msg_t p) (vs vr:chan_val) (_:unit) : SteelT unit (send_pre_available p #q cc.chan_chan vs vr) (fun _ -> sender cc (step p x)) = Steel.Utils.extract_pure (vs == vr); Steel.Utils.rewrite #_ #(send_recv_in_sync cc.chan_chan.send p cc.chan_chan vs) vr vs; elim_pure (vs == vs); gather_r cc.chan_chan.send vs; let next_vs = update_channel cc.chan_chan x vs cc.chan_chan.send in H.share cc.chan_chan.send; intro_exists next_vs (fun (next_vs:chan_val) -> pts_to cc.chan_chan.send half next_vs `star` in_state_slprop (step p x) next_vs); intro_chan_inv_stepT cc.chan_chan next_vs vs; Steel.SpinLock.release cc.chan_lock let extensible (#p:prot) (x:partial_trace_of p) = P.more x.to let next_msg_t (#p:prot) (x:partial_trace_of p) = P.next_msg_t x.to let next_trace #p (vr:chan_val) (vs:chan_val) (tr:partial_trace_of p) (s:squash (until tr == step vr.chan_prot vr.chan_msg)) (_:squash (chan_inv_step_p vr vs)) : (ts:partial_trace_of p { until ts == step vs.chan_prot vs.chan_msg }) = let msg : next_msg_t tr = vs.chan_msg in assert (extensible tr); extend_partial_trace tr msg let next_trace_st #p (vr:chan_val) (vs:chan_val) (tr:partial_trace_of p) : Steel (extension_of tr) (chan_inv_step vr vs) (fun _ -> emp) (requires fun _ -> until tr == step vr.chan_prot vr.chan_msg) (ensures fun _ ts _ -> until ts == step vs.chan_prot vs.chan_msg) = elim_pure (chan_inv_step_p vr vs); let ts : extension_of tr = next_trace vr vs tr () () in return ts let update_trace #p (r:trace_ref p) (vr:chan_val) (vs:chan_val) : Steel unit (trace_until r vr) (fun _ -> trace_until r vs) (requires fun _ -> chan_inv_step_p vr vs) (ensures fun _ _ _ -> True) = intro_pure (chan_inv_step_p vr vs); let tr = MRef.read_refine r in elim_pure (until tr == step vr.chan_prot vr.chan_msg); let ts : extension_of tr = next_trace_st vr vs tr in MRef.write r ts; intro_pure (until ts == step vs.chan_prot vs.chan_msg); intro_exists ts (fun (ts:partial_trace_of p) -> MRef.pts_to r full_perm ts `star` pure (until ts == step vs.chan_prot vs.chan_msg)) let recv_availableT (#p:sprot) #q (cc:chan q) (vs vr:chan_val) (_:unit) : SteelT (msg_t p) (sender_ahead cc.chan_chan.recv p cc.chan_chan vs vr) (fun x -> receiver cc (step p x)) = elim_pure (chan_inv_step_p vr vs); gather_r cc.chan_chan.recv vr; elim_pure (in_state_prop p vr); H.write cc.chan_chan.recv vs; H.share cc.chan_chan.recv; assert (vs.chan_prot == p); let vs_msg : msg_t p = vs.chan_msg in intro_pure (in_state_prop (step p vs_msg) vs); intro_exists vs (fun (vs:chan_val) -> pts_to cc.chan_chan.recv half vs `star` in_state_slprop (step p vs_msg) vs); update_trace cc.chan_chan.trace vr vs; intro_chan_inv cc.chan_chan vs; Steel.SpinLock.release cc.chan_lock; vs_msg #push-options "--ide_id_info_off" let send_receive_prelude (#p:prot) (cc:chan p) : SteelT (chan_val & chan_val) emp (fun v -> pts_to cc.chan_chan.send half (fst v) `star` pts_to cc.chan_chan.recv half (snd v) `star` trace_until cc.chan_chan.trace (snd v) `star` chan_inv_cond (fst v) (snd v)) = let c = cc.chan_chan in Steel.SpinLock.acquire cc.chan_lock; let vs = read_refine (chan_inv_recv cc.chan_chan) cc.chan_chan.send in let _ = witness_exists () in let vr = H.read cc.chan_chan.recv in rewrite_slprop (trace_until _ _ `star` chan_inv_cond _ _) (trace_until cc.chan_chan.trace vr `star` chan_inv_cond vs vr) (fun _ -> ()); return (vs, vr) let rec send (#p:prot) (c:chan p) (#next:prot{more next}) (x:msg_t next) : SteelT unit (sender c next) (fun _ -> sender c (step next x)) = let v = send_receive_prelude c in //matching v as vs,vr fails if (fst v).chan_ctr = (snd v).chan_ctr then ( rewrite_slprop (chan_inv_cond (fst v) (snd v)) (pure (fst v == snd v)) (fun _ -> ()); send_available c x (fst v) (snd v) () //TODO: inlining send_availableT here fails ) else ( rewrite_slprop (chan_inv_cond (fst v) (snd v)) (chan_inv_step (snd v) (fst v)) (fun _ -> ()); intro_chan_inv_stepT c.chan_chan (fst v) (snd v); Steel.SpinLock.release c.chan_lock; send c x ) let rec recv (#p:prot) (#next:prot{more next}) (c:chan p) : SteelT (msg_t next) (receiver c next) (fun x -> receiver c (step next x)) = let v = send_receive_prelude c in if (fst v).chan_ctr = (snd v).chan_ctr then ( rewrite_slprop (chan_inv_cond (fst v) (snd v)) (pure (fst v == snd v)) (fun _ -> ()); elim_pure (fst v == snd v); intro_chan_inv_eqT c.chan_chan (fst v) (snd v); Steel.SpinLock.release c.chan_lock; recv c ) else ( rewrite_slprop (chan_inv_cond (fst v) (snd v)) (chan_inv_step (snd v) (fst v)) (fun _ -> ()); recv_availableT c (fst v) (snd v) () ) let history_p' (#p:prot) (t:partial_trace_of p) (s:partial_trace_of p) : prop = t `extended_to` s /\ True let history_p (#p:prot) (t:partial_trace_of p) : MRef.stable_property extended_to = history_p' t let history (#p:prot) (c:chan p) (t:partial_trace_of p) : Type0 = MRef.witnessed c.chan_chan.trace (history_p t) let recall_trace_ref #q (r:trace_ref q) (tr tr':partial_trace_of q) (tok:MRef.witnessed r (history_p tr)) : Steel unit (MRef.pts_to r full_perm tr') (fun _ -> MRef.pts_to r full_perm tr') (requires fun _ -> True) (ensures fun _ _ _ -> history_p tr tr') = MRef.recall (history_p tr) r tr' tok let prot_equals #q (#p:_) (#vr:chan_val) (cc:chan q) : Steel unit (pts_to cc.chan_chan.recv half vr `star` receiver cc p) (fun _ -> pts_to cc.chan_chan.recv half vr `star` receiver cc p) (requires fun _ -> True) (ensures fun _ _ _ -> step vr.chan_prot vr.chan_msg == p) = let vr' = witness_exists () in H.higher_ref_pts_to_injective_eq #_ #_ #_ #_ #vr #_ cc.chan_chan.recv; rewrite_slprop (in_state_slprop _ _) (in_state_slprop p vr) (fun _ -> ()); elim_pure _; intro_in_state _ _ vr
{ "checked_file": "/", "dependencies": [ "Steel.Utils.fst.checked", "Steel.SpinLock.fsti.checked", "Steel.MonotonicHigherReference.fsti.checked", "Steel.Memory.fsti.checked", "Steel.HigherReference.fsti.checked", "Steel.FractionalPermission.fst.checked", "Steel.Effect.Atomic.fsti.checked", "Steel.Effect.fsti.checked", "Steel.Channel.Protocol.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "Steel.Channel.Simplex.fst" }
[ { "abbrev": true, "full_module": "Steel.HigherReference", "short_module": "H" }, { "abbrev": true, "full_module": "Steel.MonotonicHigherReference", "short_module": "MRef" }, { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.HigherReference", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Atomic", "short_module": null }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.SpinLock", "short_module": null }, { "abbrev": true, "full_module": "Steel.Channel.Protocol", "short_module": "P" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Channel.Protocol", "short_module": null }, { "abbrev": false, "full_module": "Steel.Channel", "short_module": null }, { "abbrev": false, "full_module": "Steel.Channel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
r: Steel.Channel.Simplex.trace_ref q -> Steel.Effect.SteelT (Prims.dtuple2 (Steel.Channel.Protocol.partial_trace_of q) (fun tr -> Steel.MonotonicHigherReference.witnessed r (Steel.Channel.Simplex.history_p tr)))
Steel.Effect.SteelT
[]
[]
[ "Steel.Channel.Simplex.prot", "Steel.Channel.Simplex.chan_val", "Steel.Channel.Simplex.trace_ref", "Prims.Mkdtuple2", "Steel.Channel.Protocol.partial_trace_of", "Steel.MonotonicHigherReference.witnessed", "Steel.Channel.Protocol.extended_to", "Steel.Channel.Simplex.history_p", "Prims.dtuple2", "Prims.unit", "Steel.Channel.Simplex.intro_trace_until", "Steel.Effect.Atomic.elim_pure", "FStar.Ghost.hide", "FStar.Set.set", "Steel.Memory.iname", "FStar.Set.empty", "Prims.eq2", "Steel.Channel.Protocol.protocol", "Steel.Channel.Protocol.until", "Steel.Channel.Simplex.step", "Steel.Channel.Simplex.__proj__Mkchan_val__item__chan_prot", "Steel.Channel.Simplex.__proj__Mkchan_val__item__chan_msg", "FStar.Ghost.reveal", "Steel.MonotonicHigherReference.ref", "Steel.MonotonicHigherReference.witness", "Steel.FractionalPermission.full_perm", "Steel.MonotonicHigherReference.read_refine", "Steel.Effect.Common.pure", "Steel.Effect.Common.vprop", "Steel.Channel.Simplex.trace_until" ]
[]
false
true
false
false
false
let witness_trace_until #q (#vr: chan_val) (r: trace_ref q) : SteelT (tr: partial_trace_of q & MRef.witnessed r (history_p tr)) (trace_until r vr) (fun _ -> trace_until r vr) =
let tr = MRef.read_refine r in let tok = MRef.witness r (history_p tr) tr () in elim_pure (until tr == step vr.chan_prot vr.chan_msg); intro_trace_until r tr vr; (| tr, tok |)
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.pre_post_to_propositions
val pre_post_to_propositions : dbg:bool -> ge:genv -> etype:effect_type -> ret_value:term -> ret_abs_binder:option binder -> ret_type:type_info -> opt_pre:option term -> opt_post:option term -> parents:list term_view (* to look for state variables for the pre *) -> children:list term_view (* to look for state variables for the pre and post *) -> Tac (genv & option proposition & option proposition)
val pre_post_to_propositions : dbg:bool -> ge:genv -> etype:effect_type -> ret_value:term -> ret_abs_binder:option binder -> ret_type:type_info -> opt_pre:option term -> opt_post:option term -> parents:list term_view (* to look for state variables for the pre *) -> children:list term_view (* to look for state variables for the pre and post *) -> Tac (genv & option proposition & option proposition)
let pre_post_to_propositions dbg ge0 etype v ret_abs_binder ret_type opt_pre opt_post parents children = print_dbg dbg "[> pre_post_to_propositions: begin"; print_dbg dbg ("- uninstantiated pre: " ^ option_to_string term_to_string opt_pre); print_dbg dbg ("- uninstantiated post: " ^ option_to_string term_to_string opt_post); let brs = match ret_abs_binder with | None -> [] | Some b -> [b] in (* Analyze the pre and the postcondition and introduce the necessary variables *) let ge3, (pre_values, pre_binders), (post_values, post_binders) = match etype with | E_Lemma -> print_dbg dbg "E_Lemma"; ge0, ([], []), ([(`())], []) | E_Total | E_GTotal -> print_dbg dbg "E_Total/E_GTotal"; ge0, ([], []), ([], []) | E_PURE | E_Pure -> print_dbg dbg "E_PURE/E_Pure"; ge0, ([], []), ([v], brs) | E_Stack | E_ST -> print_dbg dbg "E_Stack/E_ST"; (* Look for state variables in the context *) print_dbg dbg "Looking for the initial state in the context"; let b1_opt = find_mem_in_related dbg ge0 parents in print_dbg dbg "Looking for the final state in the context"; let b2_opt = find_mem_in_related dbg ge0 children in (* Introduce state variables if necessary *) let opt_push_fresh_state opt_bvt basename ge : Tac (term & binder & genv) = match opt_bvt with | Some (bv, ty) -> pack (Tv_Var bv), mk_binder bv ty, ge | None -> genv_push_fresh_var ge basename (`HS.mem) in let h1, b1, ge1 = opt_push_fresh_state b1_opt "__h0_" ge0 in let h2, b2, ge2 = opt_push_fresh_state b2_opt "__h1_" ge1 in ge2, ([h1], [b1]), ([h1; v; h2], List.Tot.flatten ([b1]::brs::[[b2]])) | E_Unknown -> (* We don't know what the effect is and the current pre and post-conditions * are currently guesses. Introduce any necessary variable abstracted by * those parameters *) (* The pre and post-conditions are likely to have the same shape as * one of Pure or Stack (depending on whether we use or not an internal * state). We try to check that and to instantiate them accordingly *) let pp_type = check_opt_pre_post_type dbg ge0.env opt_pre ret_type.ty opt_post in begin match pp_type with | Some PP_Pure -> print_dbg dbg "PP_Pure"; (* We only need the return value *) ge0, ([], []), ([v], brs) | Some (PP_State state_type) -> print_dbg dbg "PP_State"; (* Introduce variables for the states *) let s1, b1, s2, b2, ge1 = genv_push_two_fresh_vars ge0 "__s" state_type in ge1, ([s1], [b1]), ([s1; v; s2], List.Tot.flatten ([b1]::brs::[[b2]])) | Some PP_Unknown -> print_dbg dbg "PP_Unknown"; (* Introduce variables for all the values, for the pre and the post *) let pre_values, pre_binders, ge1 = introduce_variables_for_opt_abs ge0 opt_pre in let post_values, post_binders, ge1 = introduce_variables_for_opt_abs ge1 opt_post in ge1, (pre_values, pre_binders), (post_values, post_binders) | _ -> print_dbg dbg "No pre and no post"; (* No pre and no post *) ge0, ([], []), ([], []) end in (* Generate the propositions: *) (* - from the precondition *) let pre_prop = opt_mk_app_norm ge3.env opt_pre pre_values in (* - from the postcondition - note that in the case of a global post-condition * we might try to instantiate the return variable with a variable whose * type is not correct, leading to an error. We thus catch errors below and * drop the post if there is a problem *) let post_prop = try opt_mk_app_norm ge3.env opt_post post_values with | _ -> print_dbg dbg "Dropping a postcondition because of incoherent typing"; None in (* return *) print_dbg dbg "[> pre_post_to_propositions: end"; ge3, pre_prop, post_prop
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 26, "end_line": 742, "start_col": 0, "start_line": 662 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown (*/// Retrieve the list of types from the parameters stored in ``typ_or_comp``. val typ_or_comp_to_param_types : typ_or_comp -> Tot (list typ) let typ_or_comp_to_param_types c = let pl = params_of_typ_or_comp c in List.Tot.map type_of_binder pl *) (**** Step 2 *) /// The retrieved type refinements and post-conditions are not instantiated (they /// are lambda functions): instantiate them to get propositions. /// Generate a term of the form [has_type x ty] val mk_has_type : term -> typ -> Tac term let mk_has_type t ty = let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params // TODO: I don't understand why I need ifuel 2 here #push-options "--ifuel 2" /// Generate the propositions equivalent to a correct type cast. /// Note that the type refinements need to be instantiated. val cast_info_to_propositions : bool -> genv -> cast_info -> Tac (list proposition) let cast_info_to_propositions dbg ge ci = print_dbg dbg ("[> cast_info_to_propositions:\n" ^ cast_info_to_string ci); match compare_cast_types dbg ci with | Refines -> print_dbg dbg ("-> Comparison result: Refines"); [] | Same_raw_type -> print_dbg dbg ("-> Comparison result: Same_raw_type"); let refin = get_refinement (Some?.v ci.exp_ty) in let inst_refin = mk_app_norm ge.env refin [ci.term] in [inst_refin] | Unknown -> print_dbg dbg ("-> Comparison result: Unknown"); match ci.p_ty, ci.exp_ty with | Some p_ty, Some e_ty -> let p_rty = get_rawest_type p_ty in let e_rty = get_rawest_type e_ty in (* For the type cast, we generate an assertion of the form: * [> has_type (p <: type_of_p) expected_type * The reason is that we want the user to know which parameter is * concerned (hence the ``has_type``), and which types should be * related (hence the ascription). *) let ascr_term = pack (Tv_AscribedT ci.term p_rty None false) in let has_type_params = [(p_rty, Q_Implicit); (ascr_term, Q_Explicit); (e_rty, Q_Explicit)] in let type_cast = mk_app (`has_type) has_type_params in (* Expected type's refinement *) let inst_opt_refin = opt_mk_app_norm ge.env e_ty.refin [ci.term] in opt_cons inst_opt_refin [type_cast] | _ -> [] #pop-options /// Generates a list of propositions from a list of ``cast_info``. Note that /// the user should revert the list before printing the propositions. val cast_info_list_to_propositions : bool -> genv -> list cast_info -> Tac (list proposition) let cast_info_list_to_propositions dbg ge ls = let lsl = map (cast_info_to_propositions dbg ge) ls in flatten lsl /// When dealing with unknown effects, we try to guess what the pre and post-conditions /// are. The effects are indeed very likely to have a pre and a post-condition, /// and to be parameterized with an internal effect state, so that the pre and posts /// have the following shapes: /// - pre : STATE -> Type0 /// - post : STATE -> RET -> STATE -> Type0 /// Or (no state/pure): /// - pre : Type0 /// - post : RET -> Type0 /// We try to detect that with the following functions: noeq type pre_post_type = | PP_Unknown | PP_Pure | PP_State : (state_type:term) -> pre_post_type val compute_pre_type : bool -> env -> term -> Tac pre_post_type let compute_pre_type dbg e pre = print_dbg dbg "[> compute_pre_type"; match safe_tc e pre with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [], true -> print_dbg dbg "PP_Pure"; PP_Pure | [b], true -> print_dbg dbg ("PP_State " ^ (term_to_string (type_of_binder b))); PP_State (type_of_binder b) | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val opt_remove_refin : typ -> Tac typ let opt_remove_refin ty = match inspect ty with | Tv_Refine _ sort _ -> sort | _ -> ty val compute_post_type : bool -> env -> term -> term -> Tac pre_post_type let compute_post_type dbg e ret_type post = print_dbg dbg "[> compute_post_type"; let get_tot_ret_type c : Tac (option term_view) = match get_total_or_gtotal_ret_type c with | Some ret_ty -> Some (inspect ret_ty) | None -> None in match safe_tc e post with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [r], true -> (* Pure *) print_dbg dbg "PP_Pure"; PP_Pure | [s1; r; s2], true -> (* Stateful: check that the state types are coherent *) let r_ty = type_of_binder r in let s1_ty = type_of_binder s1 in (* After testing with Stack: the final state seems to have a refinement * (which gives the postcondition) so we need to remove it to match * it against the initial state *) let s2_ty = opt_remove_refin (type_of_binder s2) in let ret_type_eq = term_eq ret_type r_ty in let state_type_eq = term_eq s1_ty s2_ty in print_dbg dbg ("- ret type:\n-- target: " ^ term_to_string ret_type ^ "\n-- binder: " ^ term_to_string r_ty); print_dbg dbg ("- state types:\n-- binder1: " ^ term_to_string s1_ty ^ "\n-- binder2: " ^ term_to_string s2_ty); print_dbg dbg ("- ret type equality: " ^ string_of_bool ret_type_eq); print_dbg dbg ("- state types equality: " ^ string_of_bool state_type_eq); if ret_type_eq && state_type_eq then begin print_dbg dbg ("PP_State" ^ term_to_string (type_of_binder s1)); PP_State (type_of_binder s1) end else begin print_dbg dbg "PP_Unknown"; PP_Unknown end | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val check_pre_post_type : bool -> env -> term -> term -> term -> Tac pre_post_type let check_pre_post_type dbg e pre ret_type post = print_dbg dbg "[> check_pre_post_type"; match compute_pre_type dbg e pre, compute_post_type dbg e ret_type post with | PP_Pure, PP_Pure -> print_dbg dbg "PP_Pure, PP_Pure"; PP_Pure | PP_State ty1, PP_State ty2 -> print_dbg dbg "PP_State, PP_State"; if term_eq ty1 ty2 then PP_State ty1 else PP_Unknown | _ -> print_dbg dbg "_, _"; PP_Unknown val check_opt_pre_post_type : bool -> env -> option term -> term -> option term -> Tac (option pre_post_type) let check_opt_pre_post_type dbg e opt_pre ret_type opt_post = print_dbg dbg "[> check_opt_pre_post_type"; match opt_pre, opt_post with | Some pre, Some post -> print_dbg dbg "Some pre, Some post"; Some (check_pre_post_type dbg e pre ret_type post) | Some pre, None -> print_dbg dbg "Some pre, None"; Some (compute_pre_type dbg e pre) | None, Some post -> print_dbg dbg "None, Some post"; Some (compute_post_type dbg e ret_type post) | None, None -> print_dbg dbg "None, None"; None val _introduce_variables_for_abs : genv -> typ -> Tac (list term & list binder & genv) let rec _introduce_variables_for_abs ge ty = match inspect ty with | Tv_Arrow b c -> let ge1, b1 = genv_push_fresh_binder ge ("__" ^ name_of_binder b) (type_of_binder b) in let bv1 = bv_of_binder b1 in let v1 = pack (Tv_Var bv1) in begin match get_total_or_gtotal_ret_type c with | Some ty1 -> let vl, bl, ge2 = _introduce_variables_for_abs ge1 ty1 in v1 :: vl, b1 :: bl, ge2 | None -> [v1], [b1], ge1 end | _ -> [], [], ge val introduce_variables_for_abs : genv -> term -> Tac (list term & list binder & genv) let introduce_variables_for_abs ge tm = match safe_tc ge.env tm with | Some ty -> _introduce_variables_for_abs ge ty | None -> [], [], ge val introduce_variables_for_opt_abs : genv -> option term -> Tac (list term & list binder & genv) let introduce_variables_for_opt_abs ge opt_tm = match opt_tm with | Some tm -> introduce_variables_for_abs ge tm | None -> [], [], ge val effect_type_is_stateful : effect_type -> Tot bool let effect_type_is_stateful etype = match etype with | E_Total | E_GTotal | E_Lemma | E_PURE | E_Pure -> false | E_Stack | E_ST | E_Unknown -> true let is_st_get dbg t : Tac bool = print_dbg dbg ("[> is_st_get:\n" ^ term_to_string t); match inspect t with | Tv_App hd (a, qual) -> print_dbg dbg "-> Is Tv_App"; begin match inspect hd with | Tv_FVar fv -> print_dbg dbg ("-> Head is Tv_FVar: " ^ fv_to_string fv); fv_eq_name fv ["FStar"; "HyperStack"; "ST"; "get"] | _ -> print_dbg dbg "-> Head is not Tv_FVar"; false end | _ -> print_dbg dbg "-> Is not Tv_App"; false let is_let_st_get dbg (t : term_view) = print_dbg dbg ("[> is_let_st_get:\n" ^ term_to_string t); match t with | Tv_Let recf attrs bv ty def body -> print_dbg dbg "The term is a let expression"; if is_st_get dbg def then Some (bv, ty) else None | _ -> print_dbg dbg "The term is not a let expression"; None // TODO: Define relation between parents and children in and use it in explore_term // app: head or arg // let : bv or def or child // match: scrutinee or branch // ascribed: e or ty /// Check if a term's computation is effectful. The return type is option /// because we may not be able to retrieve the term computation. val term_has_effectful_comp : bool -> env -> term -> Tac (option bool) let term_has_effectful_comp dbg e tm = print_dbg dbg "[> term_has_effectful_comp"; let einfo_opt = compute_effect_info dbg e tm in match einfo_opt with | Some einfo -> print_dbg dbg ("Effect type: " ^ effect_type_to_string einfo.ei_type); Some (not (effect_type_is_pure einfo.ei_type)) | None -> print_dbg dbg "Could not compute effect info"; None /// Check if a related term is effectful. This is used to look for instances of /// ``HS.mem`` to instantiate pre/postconditions, which means that the term should /// be a parent/child term of the term under study, as generated by ``explore_term`` /// (otherwise the way we check that a term is effectful doesn't make sense). /// The computation is an overapproximation: it may happen that, for instance, we /// can't compute a term computation. In this case, we consider that the term is /// effectful. There are also situations in which we may not be sure which term to /// consider. let related_term_is_effectul dbg ge tv : Tac bool = let is_effectful tm = term_has_effectful_comp dbg ge.env tm <> Some false in match tv with | Tv_Var _ | Tv_BVar _ | Tv_FVar _ -> false | Tv_App hd (a, qual) -> (* The term under focus should be the app itself or an argument *) false | Tv_Abs br body -> false | Tv_Arrow br c0 -> false | Tv_Type _ -> false | Tv_Refine bv sort ref -> false | Tv_Const _ -> false | Tv_Uvar _ _ -> false | Tv_Let recf attrs bv ty def body -> is_effectful def | Tv_Match scrutinee _ret_opt branches -> (* TODO: we need to keep track of the relation between parents and children *) (* We assume the term under focus is in one the branches of the match - this * assumption is safe: in the worst case, we won't be able to find a mem to use. * Besides, in practice it is uncommon (impossible?) to use an effectful term * as the scrutinee of a match *) is_effectful scrutinee | Tv_AscribedT e ty tac _ -> false (* The focused term should be inside the ascription *) | Tv_AscribedC e c tac _ -> false (* The focused term should be inside the ascription *) | _ -> (* Unknown: keep things safe *) true /// Look for a term of the form ``let h = ST.get ()`` in a list of parent/children terms /// and return the let-bound bv. Abort the search if we find a non-effectful term. /// The typical usages of this function are the following: /// - look for a state variable to instantiate the precondition of the term under focus /// - look for state variables for the pre/postconditions of a term defined before /// the term under focus. val find_mem_in_related: dbg:bool -> ge:genv -> tms:list term_view -> Tac (option (bv & typ)) let rec find_mem_in_related dbg ge tms = match tms with | [] -> None | tv :: tms' -> print_dbg dbg ("[> find_mem_in_related:\n" ^ term_to_string tv); match is_let_st_get dbg tv with | Some bvt -> print_dbg dbg "Term is of the form `let x = FStar.HyperStack.ST.get ()`: success"; Some bvt | None -> print_dbg dbg "Term is not of the form `let x = FStar.HyperStack.ST.get ()`: continuing"; if related_term_is_effectul dbg ge tv then begin print_dbg dbg "Term is effectful: stopping here"; None end else begin print_dbg dbg "Term is not effectful: continuing"; find_mem_in_related dbg ge tms' end // TODO: not used for now /// Look for a term of the form ``let h = ST.get ()`` in a child term (the /// focused term is supposed to be a subterm of the definition of a let-construct). val find_mem_in_children: dbg:bool -> ge:genv -> child:term -> Tac (genv & option bv) let rec find_mem_in_children dbg ge child = (* We stop whenever we find an expression which is not a let-binding *) match inspect child with | Tv_Let recf attrs bv ty def body -> if is_st_get dbg def then ge, Some bv else if term_has_effectful_comp dbg ge.env def <> Some false then ge, None else let ge1 = genv_push_bv ge bv ty false None in find_mem_in_children dbg ge1 body | _ -> ge, None /// Instantiates optional pre and post conditions val pre_post_to_propositions : dbg:bool -> ge:genv -> etype:effect_type -> ret_value:term -> ret_abs_binder:option binder -> ret_type:type_info -> opt_pre:option term -> opt_post:option term -> parents:list term_view (* to look for state variables for the pre *) -> children:list term_view (* to look for state variables for the pre and post *) -> Tac (genv & option proposition & option proposition)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> ge: FStar.InteractiveHelpers.Base.genv -> etype: FStar.InteractiveHelpers.ExploreTerm.effect_type -> ret_value: FStar.Stubs.Reflection.Types.term -> ret_abs_binder: FStar.Pervasives.Native.option FStar.Stubs.Reflection.Types.binder -> ret_type: FStar.InteractiveHelpers.ExploreTerm.type_info -> opt_pre: FStar.Pervasives.Native.option FStar.Stubs.Reflection.Types.term -> opt_post: FStar.Pervasives.Native.option FStar.Stubs.Reflection.Types.term -> parents: Prims.list FStar.Stubs.Reflection.V1.Data.term_view -> children: Prims.list FStar.Stubs.Reflection.V1.Data.term_view -> FStar.Tactics.Effect.Tac ((FStar.InteractiveHelpers.Base.genv * FStar.Pervasives.Native.option FStar.InteractiveHelpers.Propositions.proposition) * FStar.Pervasives.Native.option FStar.InteractiveHelpers.Propositions.proposition)
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.InteractiveHelpers.Base.genv", "FStar.InteractiveHelpers.ExploreTerm.effect_type", "FStar.Stubs.Reflection.Types.term", "FStar.Pervasives.Native.option", "FStar.Stubs.Reflection.Types.binder", "FStar.InteractiveHelpers.ExploreTerm.type_info", "Prims.list", "FStar.Stubs.Reflection.V1.Data.term_view", "FStar.Pervasives.Native.Mktuple3", "FStar.InteractiveHelpers.Propositions.proposition", "FStar.Pervasives.Native.tuple3", "Prims.unit", "FStar.InteractiveHelpers.Base.print_dbg", "FStar.Tactics.V1.Derived.try_with", "FStar.InteractiveHelpers.Base.opt_mk_app_norm", "FStar.InteractiveHelpers.Base.__proj__Mkgenv__item__env", "Prims.exn", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "Prims.Nil", "Prims.Cons", "FStar.List.Tot.Base.flatten", "FStar.Stubs.Reflection.Types.bv", "FStar.Stubs.Reflection.Types.typ", "Prims.string", "FStar.Reflection.V1.Derived.mk_binder", "FStar.Stubs.Tactics.V1.Builtins.pack", "FStar.Stubs.Reflection.V1.Data.Tv_Var", "FStar.InteractiveHelpers.Base.genv_push_fresh_var", "FStar.InteractiveHelpers.Effectful.find_mem_in_related", "FStar.Pervasives.Native.tuple5", "FStar.InteractiveHelpers.Base.genv_push_two_fresh_vars", "FStar.InteractiveHelpers.Effectful.introduce_variables_for_opt_abs", "FStar.InteractiveHelpers.Effectful.pre_post_type", "FStar.InteractiveHelpers.Effectful.check_opt_pre_post_type", "FStar.InteractiveHelpers.ExploreTerm.__proj__Mktype_info__item__ty", "Prims.op_Hat", "FStar.InteractiveHelpers.Base.option_to_string", "FStar.Stubs.Tactics.V1.Builtins.term_to_string" ]
[]
false
true
false
false
false
let pre_post_to_propositions dbg ge0 etype v ret_abs_binder ret_type opt_pre opt_post parents children =
print_dbg dbg "[> pre_post_to_propositions: begin"; print_dbg dbg ("- uninstantiated pre: " ^ option_to_string term_to_string opt_pre); print_dbg dbg ("- uninstantiated post: " ^ option_to_string term_to_string opt_post); let brs = match ret_abs_binder with | None -> [] | Some b -> [b] in let ge3, (pre_values, pre_binders), (post_values, post_binders) = match etype with | E_Lemma -> print_dbg dbg "E_Lemma"; ge0, ([], []), ([(`())], []) | E_Total | E_GTotal -> print_dbg dbg "E_Total/E_GTotal"; ge0, ([], []), ([], []) | E_PURE | E_Pure -> print_dbg dbg "E_PURE/E_Pure"; ge0, ([], []), ([v], brs) | E_Stack | E_ST -> print_dbg dbg "E_Stack/E_ST"; print_dbg dbg "Looking for the initial state in the context"; let b1_opt = find_mem_in_related dbg ge0 parents in print_dbg dbg "Looking for the final state in the context"; let b2_opt = find_mem_in_related dbg ge0 children in let opt_push_fresh_state opt_bvt basename ge : Tac (term & binder & genv) = match opt_bvt with | Some (bv, ty) -> pack (Tv_Var bv), mk_binder bv ty, ge | None -> genv_push_fresh_var ge basename (`HS.mem) in let h1, b1, ge1 = opt_push_fresh_state b1_opt "__h0_" ge0 in let h2, b2, ge2 = opt_push_fresh_state b2_opt "__h1_" ge1 in ge2, ([h1], [b1]), ([h1; v; h2], List.Tot.flatten ([[b1]; brs; [b2]])) | E_Unknown -> let pp_type = check_opt_pre_post_type dbg ge0.env opt_pre ret_type.ty opt_post in match pp_type with | Some PP_Pure -> print_dbg dbg "PP_Pure"; ge0, ([], []), ([v], brs) | Some (PP_State state_type) -> print_dbg dbg "PP_State"; let s1, b1, s2, b2, ge1 = genv_push_two_fresh_vars ge0 "__s" state_type in ge1, ([s1], [b1]), ([s1; v; s2], List.Tot.flatten ([[b1]; brs; [b2]])) | Some PP_Unknown -> print_dbg dbg "PP_Unknown"; let pre_values, pre_binders, ge1 = introduce_variables_for_opt_abs ge0 opt_pre in let post_values, post_binders, ge1 = introduce_variables_for_opt_abs ge1 opt_post in ge1, (pre_values, pre_binders), (post_values, post_binders) | _ -> print_dbg dbg "No pre and no post"; ge0, ([], []), ([], []) in let pre_prop = opt_mk_app_norm ge3.env opt_pre pre_values in let post_prop = try opt_mk_app_norm ge3.env opt_post post_values with | _ -> print_dbg dbg "Dropping a postcondition because of incoherent typing"; None in print_dbg dbg "[> pre_post_to_propositions: end"; ge3, pre_prop, post_prop
false
Vale.Interop.Types.fst
Vale.Interop.Types.base_typ_as_type
val base_typ_as_type (t: base_typ) : eqtype
val base_typ_as_type (t: base_typ) : eqtype
let base_typ_as_type (t:base_typ) : eqtype = let open W in let open Vale.Def.Types_s in match t with | TUInt8 -> UInt8.t | TUInt16 -> UInt16.t | TUInt32 -> UInt32.t | TUInt64 -> UInt64.t | TUInt128 -> quad32
{ "file_name": "vale/specs/interop/Vale.Interop.Types.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 22, "end_line": 20, "start_col": 0, "start_line": 12 }
module Vale.Interop.Types open FStar.Mul include Vale.Arch.HeapTypes_s module B = LowStar.Buffer module IB = LowStar.ImmutableBuffer module MB = LowStar.Monotonic.Buffer module DV = LowStar.BufferView.Down module W = Vale.Def.Words_s module L = FStar.List.Tot
{ "checked_file": "/", "dependencies": [ "Vale.Interop.Views.fsti.checked", "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Arch.HeapTypes_s.fst.checked", "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.ImmutableBuffer.fst.checked", "LowStar.BufferView.Down.fsti.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked" ], "interface_file": false, "source_file": "Vale.Interop.Types.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "Vale.Def.Words_s", "short_module": "W" }, { "abbrev": true, "full_module": "LowStar.BufferView.Down", "short_module": "DV" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": true, "full_module": "LowStar.ImmutableBuffer", "short_module": "IB" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "Vale.Arch.HeapTypes_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
t: Vale.Arch.HeapTypes_s.base_typ -> Prims.eqtype
Prims.Tot
[ "total" ]
[]
[ "Vale.Arch.HeapTypes_s.base_typ", "FStar.UInt8.t", "FStar.UInt16.t", "FStar.UInt32.t", "FStar.UInt64.t", "Vale.Def.Types_s.quad32", "Prims.eqtype" ]
[]
false
false
false
true
false
let base_typ_as_type (t: base_typ) : eqtype =
let open W in let open Vale.Def.Types_s in match t with | TUInt8 -> UInt8.t | TUInt16 -> UInt16.t | TUInt32 -> UInt32.t | TUInt64 -> UInt64.t | TUInt128 -> quad32
false
Vale.Interop.Types.fst
Vale.Interop.Types.down_view
val down_view (t: base_typ) : DV.view (base_typ_as_type t) UInt8.t
val down_view (t: base_typ) : DV.view (base_typ_as_type t) UInt8.t
let down_view (t:base_typ) : DV.view (base_typ_as_type t) UInt8.t = match t with | TUInt8 -> Vale.Interop.Views.down_view8 | TUInt16 -> Vale.Interop.Views.down_view16 | TUInt32 -> Vale.Interop.Views.down_view32 | TUInt64 -> Vale.Interop.Views.down_view64 | TUInt128 -> Vale.Interop.Views.down_view128
{ "file_name": "vale/specs/interop/Vale.Interop.Types.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 47, "end_line": 40, "start_col": 0, "start_line": 35 }
module Vale.Interop.Types open FStar.Mul include Vale.Arch.HeapTypes_s module B = LowStar.Buffer module IB = LowStar.ImmutableBuffer module MB = LowStar.Monotonic.Buffer module DV = LowStar.BufferView.Down module W = Vale.Def.Words_s module L = FStar.List.Tot [@__reduce__] let base_typ_as_type (t:base_typ) : eqtype = let open W in let open Vale.Def.Types_s in match t with | TUInt8 -> UInt8.t | TUInt16 -> UInt16.t | TUInt32 -> UInt32.t | TUInt64 -> UInt64.t | TUInt128 -> quad32 [@__reduce__] unfold // without the unfold, "% view_n t" leads to very slow Z3 performance let view_n_unfold (t:base_typ) : pos = match t with | TUInt8 -> 1 | TUInt16 -> 2 | TUInt32 -> 4 | TUInt64 -> 8 | TUInt128 -> 16 let view_n (t:base_typ) : pos = view_n_unfold t
{ "checked_file": "/", "dependencies": [ "Vale.Interop.Views.fsti.checked", "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Arch.HeapTypes_s.fst.checked", "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.ImmutableBuffer.fst.checked", "LowStar.BufferView.Down.fsti.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked" ], "interface_file": false, "source_file": "Vale.Interop.Types.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "Vale.Def.Words_s", "short_module": "W" }, { "abbrev": true, "full_module": "LowStar.BufferView.Down", "short_module": "DV" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": true, "full_module": "LowStar.ImmutableBuffer", "short_module": "IB" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "Vale.Arch.HeapTypes_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
t: Vale.Arch.HeapTypes_s.base_typ -> LowStar.BufferView.Down.view (Vale.Interop.Types.base_typ_as_type t) FStar.UInt8.t
Prims.Tot
[ "total" ]
[]
[ "Vale.Arch.HeapTypes_s.base_typ", "Vale.Interop.Views.down_view8", "Vale.Interop.Views.down_view16", "Vale.Interop.Views.down_view32", "Vale.Interop.Views.down_view64", "Vale.Interop.Views.down_view128", "LowStar.BufferView.Down.view", "Vale.Interop.Types.base_typ_as_type", "FStar.UInt8.t" ]
[]
false
false
false
false
false
let down_view (t: base_typ) : DV.view (base_typ_as_type t) UInt8.t =
match t with | TUInt8 -> Vale.Interop.Views.down_view8 | TUInt16 -> Vale.Interop.Views.down_view16 | TUInt32 -> Vale.Interop.Views.down_view32 | TUInt64 -> Vale.Interop.Views.down_view64 | TUInt128 -> Vale.Interop.Views.down_view128
false
Vale.Interop.Types.fst
Vale.Interop.Types.b8_preorder
val b8_preorder (writeable: bool) (a: Type0) : MB.srel a
val b8_preorder (writeable: bool) (a: Type0) : MB.srel a
let b8_preorder (writeable:bool) (a:Type0) : MB.srel a = match writeable with | true -> B.trivial_preorder a | false -> IB.immutable_preorder a
{ "file_name": "vale/specs/interop/Vale.Interop.Types.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 36, "end_line": 45, "start_col": 0, "start_line": 42 }
module Vale.Interop.Types open FStar.Mul include Vale.Arch.HeapTypes_s module B = LowStar.Buffer module IB = LowStar.ImmutableBuffer module MB = LowStar.Monotonic.Buffer module DV = LowStar.BufferView.Down module W = Vale.Def.Words_s module L = FStar.List.Tot [@__reduce__] let base_typ_as_type (t:base_typ) : eqtype = let open W in let open Vale.Def.Types_s in match t with | TUInt8 -> UInt8.t | TUInt16 -> UInt16.t | TUInt32 -> UInt32.t | TUInt64 -> UInt64.t | TUInt128 -> quad32 [@__reduce__] unfold // without the unfold, "% view_n t" leads to very slow Z3 performance let view_n_unfold (t:base_typ) : pos = match t with | TUInt8 -> 1 | TUInt16 -> 2 | TUInt32 -> 4 | TUInt64 -> 8 | TUInt128 -> 16 let view_n (t:base_typ) : pos = view_n_unfold t [@__reduce__] let down_view (t:base_typ) : DV.view (base_typ_as_type t) UInt8.t = match t with | TUInt8 -> Vale.Interop.Views.down_view8 | TUInt16 -> Vale.Interop.Views.down_view16 | TUInt32 -> Vale.Interop.Views.down_view32 | TUInt64 -> Vale.Interop.Views.down_view64 | TUInt128 -> Vale.Interop.Views.down_view128
{ "checked_file": "/", "dependencies": [ "Vale.Interop.Views.fsti.checked", "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Arch.HeapTypes_s.fst.checked", "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.ImmutableBuffer.fst.checked", "LowStar.BufferView.Down.fsti.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked" ], "interface_file": false, "source_file": "Vale.Interop.Types.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "Vale.Def.Words_s", "short_module": "W" }, { "abbrev": true, "full_module": "LowStar.BufferView.Down", "short_module": "DV" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": true, "full_module": "LowStar.ImmutableBuffer", "short_module": "IB" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "Vale.Arch.HeapTypes_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
writeable: Prims.bool -> a: Type0 -> LowStar.Monotonic.Buffer.srel a
Prims.Tot
[ "total" ]
[]
[ "Prims.bool", "LowStar.Buffer.trivial_preorder", "LowStar.ImmutableBuffer.immutable_preorder", "LowStar.Monotonic.Buffer.srel" ]
[]
false
false
false
true
false
let b8_preorder (writeable: bool) (a: Type0) : MB.srel a =
match writeable with | true -> B.trivial_preorder a | false -> IB.immutable_preorder a
false
Vale.Interop.Types.fst
Vale.Interop.Types.view_n_unfold
val view_n_unfold (t: base_typ) : pos
val view_n_unfold (t: base_typ) : pos
let view_n_unfold (t:base_typ) : pos = match t with | TUInt8 -> 1 | TUInt16 -> 2 | TUInt32 -> 4 | TUInt64 -> 8 | TUInt128 -> 16
{ "file_name": "vale/specs/interop/Vale.Interop.Types.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 18, "end_line": 30, "start_col": 0, "start_line": 24 }
module Vale.Interop.Types open FStar.Mul include Vale.Arch.HeapTypes_s module B = LowStar.Buffer module IB = LowStar.ImmutableBuffer module MB = LowStar.Monotonic.Buffer module DV = LowStar.BufferView.Down module W = Vale.Def.Words_s module L = FStar.List.Tot [@__reduce__] let base_typ_as_type (t:base_typ) : eqtype = let open W in let open Vale.Def.Types_s in match t with | TUInt8 -> UInt8.t | TUInt16 -> UInt16.t | TUInt32 -> UInt32.t | TUInt64 -> UInt64.t | TUInt128 -> quad32 [@__reduce__]
{ "checked_file": "/", "dependencies": [ "Vale.Interop.Views.fsti.checked", "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Arch.HeapTypes_s.fst.checked", "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.ImmutableBuffer.fst.checked", "LowStar.BufferView.Down.fsti.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked" ], "interface_file": false, "source_file": "Vale.Interop.Types.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "Vale.Def.Words_s", "short_module": "W" }, { "abbrev": true, "full_module": "LowStar.BufferView.Down", "short_module": "DV" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": true, "full_module": "LowStar.ImmutableBuffer", "short_module": "IB" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "Vale.Arch.HeapTypes_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
t: Vale.Arch.HeapTypes_s.base_typ -> Prims.pos
Prims.Tot
[ "total" ]
[]
[ "Vale.Arch.HeapTypes_s.base_typ", "Prims.pos" ]
[]
false
false
false
true
false
let view_n_unfold (t: base_typ) : pos =
match t with | TUInt8 -> 1 | TUInt16 -> 2 | TUInt32 -> 4 | TUInt64 -> 8 | TUInt128 -> 16
false
Pulse.Lib.InvList.fst
Pulse.Lib.InvList.shift_invlist_one
val shift_invlist_one (#a:Type0) (p : vprop) (i : inv p) (is : invlist{not (mem_inv (invlist_names is) i)}) (#pre:vprop) (#post : a -> vprop) (f : unit -> stt_atomic a #Unobservable emp_inames (invlist_v ((| p, i |) :: is) ** pre) (fun v -> invlist_v ((| p, i |) :: is) ** post v)) : unit -> stt_atomic a #Unobservable emp_inames (invlist_v is ** (p ** pre)) (fun v -> invlist_v is ** (p ** post v))
val shift_invlist_one (#a:Type0) (p : vprop) (i : inv p) (is : invlist{not (mem_inv (invlist_names is) i)}) (#pre:vprop) (#post : a -> vprop) (f : unit -> stt_atomic a #Unobservable emp_inames (invlist_v ((| p, i |) :: is) ** pre) (fun v -> invlist_v ((| p, i |) :: is) ** post v)) : unit -> stt_atomic a #Unobservable emp_inames (invlist_v is ** (p ** pre)) (fun v -> invlist_v is ** (p ** post v))
let shift_invlist_one = __shift_invlist_one
{ "file_name": "share/steel/examples/pulse/lib/pledge/Pulse.Lib.InvList.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 43, "end_line": 33, "start_col": 0, "start_line": 33 }
module Pulse.Lib.InvList open Pulse.Lib.Pervasives module T0 = Pulse.Lib.Priv.Trade0 ```pulse unobservable fn __shift_invlist_one (#a:Type0) (p : vprop) (i : inv p) (is : invlist{not (mem_inv (invlist_names is) i)}) (#pre:vprop) (#post : a -> vprop) (f : unit -> stt_atomic a #Unobservable emp_inames (invlist_v (add_one (| p, i |) is) ** pre) (fun v -> invlist_v (add_one (| p, i |) is) ** post v)) (_:unit) requires invlist_v is ** (p ** pre) returns v:a ensures invlist_v is ** (p ** post v) { rewrite (p ** invlist_v is) as (invlist_v (add_one (|p, i|) is)); let v = f (); rewrite (invlist_v (add_one (|p, i|) is)) as (p ** invlist_v is); v }
{ "checked_file": "/", "dependencies": [ "Pulse.Lib.Priv.Trade0.fsti.checked", "Pulse.Lib.Pervasives.fst.checked", "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "Pulse.Lib.InvList.fst" }
[ { "abbrev": true, "full_module": "Pulse.Lib.Priv.Trade0", "short_module": "T0" }, { "abbrev": false, "full_module": "Pulse.Lib.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: Pulse.Lib.Core.vprop -> i: Pulse.Lib.Core.inv p -> is: Pulse.Lib.InvList.invlist { Prims.op_Negation (FStar.Ghost.reveal (Pulse.Lib.Core.mem_inv (Pulse.Lib.InvList.invlist_names is) i)) } -> f: (_: Prims.unit -> Pulse.Lib.Core.stt_atomic a Pulse.Lib.Core.emp_inames (Pulse.Lib.InvList.invlist_v ((| p, i |) :: is) ** pre) (fun v -> Pulse.Lib.InvList.invlist_v ((| p, i |) :: is) ** post v)) -> _: Prims.unit -> Pulse.Lib.Core.stt_atomic a Pulse.Lib.Core.emp_inames (Pulse.Lib.InvList.invlist_v is ** p ** pre) (fun v -> Pulse.Lib.InvList.invlist_v is ** p ** post v)
Prims.Tot
[ "total" ]
[]
[ "Pulse.Lib.InvList.__shift_invlist_one" ]
[]
false
false
false
false
false
let shift_invlist_one =
__shift_invlist_one
false
FStar.InteractiveHelpers.Effectful.fst
FStar.InteractiveHelpers.Effectful.eterm_info_to_assertions
val eterm_info_to_assertions : dbg:bool -> with_gpre:bool -> with_gpost:bool -> ge:genv -> t:term -> is_let:bool (* the term is the bound expression in a let binding *) -> is_assert:bool (* the term is an assert - in which case we only output the precondition *) -> info:eterm_info -> opt_bind_var:option term (* if let binding: the bound var *) -> opt_c:option typ_or_comp -> parents:list term_view -> children:list term_view -> Tac (genv & assertions)
val eterm_info_to_assertions : dbg:bool -> with_gpre:bool -> with_gpost:bool -> ge:genv -> t:term -> is_let:bool (* the term is the bound expression in a let binding *) -> is_assert:bool (* the term is an assert - in which case we only output the precondition *) -> info:eterm_info -> opt_bind_var:option term (* if let binding: the bound var *) -> opt_c:option typ_or_comp -> parents:list term_view -> children:list term_view -> Tac (genv & assertions)
let eterm_info_to_assertions dbg with_gpre with_gpost ge t is_let is_assert info bind_var opt_c parents children = print_dbg dbg "[> eterm_info_to_assertions"; (* Introduce additional variables to instantiate the return type refinement, * the precondition, the postcondition and the goal *) (* First, the return value: returns an updated env, the value to use for * the return type and a list of abstract binders *) let einfo = info.einfo in let ge0, (v : term), (opt_b : option binder) = match bind_var with | Some v -> ge, v, None | None -> (* If the studied term is stateless, we can use it directly in the * propositions. If the return value is of type unit, we can just use (). * Otherwise we need to introduce a variable. * For the reason why we do this: remember that the studied term might be * a return value: it is not necessarily bound in a let. *) if effect_type_is_stateful einfo.ei_type then if is_unit_type einfo.ei_ret_type.ty then ge, `(), None else let b = fresh_binder ge.env "__ret" einfo.ei_ret_type.ty in let bv = bv_of_binder b in let tm = pack (Tv_Var bv) in genv_push_binder ge b true None, tm, Some b else ge, t, None in (* Generate propositions from the pre and the post-conditions *) (**) print_dbg dbg "> Instantiating local pre/post"; let ge1, pre_prop, post_prop = pre_post_to_propositions dbg ge0 einfo.ei_type v opt_b einfo.ei_ret_type einfo.ei_pre einfo.ei_post parents children in print_dbg dbg ("- pre prop: " ^ option_to_string term_to_string pre_prop); print_dbg dbg ("- post prop: " ^ option_to_string term_to_string post_prop); (* If the term is an assertion/assumption, only output the postcondition - * note that in the case of an assertion, the pre and the post are the same, * but in the case of an assumption, only the post is interesting *) if is_assert then begin print_dbg dbg "The term is an assert: only keep the postcondition"; ge1, { pres = opt_cons post_prop []; posts = [] } end else begin (* Generate propositions from the target computation (pre, post, type cast) *) let ge2, gparams_props, gpre_prop, gcast_props, gpost_prop = (* Check if we do the computation (which can be expensive) - note that * computing the global postcondition makes sense only if the focused * term is the return value and thus not a let-binding *) let with_goal : bool = with_gpre || ((not is_let) && with_gpost) in begin match opt_c, with_goal with | Some c, true -> let ei = typ_or_comp_to_effect_info dbg ge1 c in print_dbg dbg ("- target effect: " ^ effect_info_to_string ei); print_dbg dbg ("- global unfilt. pre: " ^ option_to_string term_to_string ei.ei_pre); print_dbg dbg ("- global unfilt. post: " ^ option_to_string term_to_string ei.ei_post); (* The parameters' type information. To be used only if the variables are not * shadowed (the parameters themselves, but also the variables inside the refinements) *) let gparams_props = begin if with_gpre then begin print_dbg dbg "Generating assertions from the global parameters' types"; print_dbg dbg ("Current genv:\n" ^ genv_to_string ge1); (* Retrieve the types and pair them with the parameters - note that * we need to reverse the list of parameters (the outer parameter was * added first in the list and is thus last) *) let params = rev (List.Tot.map (fun x -> (x, type_of_binder x)) (params_of_typ_or_comp c)) in iteri (fun i (b, _) -> print_dbg dbg ("Global parameter " ^ string_of_int i ^ ": " ^ binder_to_string b)) params; (* Filter the shadowed parameters *) let params = filter (fun (b, _)-> not (binder_is_shadowed ge1 b)) params in (* Generate the propositions *) let param_to_props (x : (binder & typ)) : Tac (list term) = let b, ty = x in let bv = bv_of_binder b in print_dbg dbg ("Generating assertions from global parameter: " ^ binder_to_string b); let tinfo = get_type_info_from_type ty in let v = pack (Tv_Var bv) in let p1 = mk_has_type v tinfo.ty in let pl = match tinfo.refin with | None -> [] | Some r -> let p2 = mk_app_norm ge1.env r [v] in (* Discard the proposition generated from the type refinement if * it contains shadowed variables *) if term_has_shadowed_variables ge1 p2 then begin print_dbg dbg "Discarding type refinement because of shadowed variables"; [] end else begin print_dbg dbg "Keeping type refinement"; [p2] end in p1 :: pl in let props = map param_to_props params in List.Tot.flatten props end else begin print_dbg dbg "Ignoring the global parameters' types"; [] end end <: Tac (list term) in (* The global pre-condition is to be used only if none of its variables * are shadowed (which implies that we are close enough to the top of * the function *) let gpre = match ei.ei_pre, with_gpre with | Some pre, true -> if term_has_shadowed_variables ge1 pre then begin print_dbg dbg "Dropping the global precondition because of shadowed variables"; None end else ei.ei_pre | _ -> None in (* The global post-condition and the type cast are relevant only if the * studied term is not the definition in a let binding *) let gpost, gcast_props = if not with_gpost then None, [] else if is_let then begin print_dbg dbg "Dropping the global postcondition and return type because we are studying a let expression"; None, [] end else (* Because of the way the studied function is rewritten before being sent to F* * we might have a problem with the return type (we might instantiate * the return variable from the global post or from the return type * refinement with a variable whose type is not valid for that, triggering * an exception. In that case, we drop the post and the target type * refinement. Note that here only the type refinement may be instantiated, * we thus also need to check for the post inside ``pre_post_to_propositions`` *) try print_dbg dbg "> Generating propositions from the global type cast"; print_dbg dbg ("- known type: " ^ type_info_to_string einfo.ei_ret_type); print_dbg dbg ("- exp. type : " ^ type_info_to_string ei.ei_ret_type); let gcast = mk_cast_info v (Some einfo.ei_ret_type) (Some ei.ei_ret_type) in print_dbg dbg (cast_info_to_string gcast); let gcast_props = cast_info_to_propositions dbg ge1 gcast in print_dbg dbg "> Propositions for global type cast:"; print_dbg dbg (list_to_string term_to_string gcast_props); ei.ei_post, List.Tot.rev gcast_props with | _ -> print_dbg dbg "Dropping the global postcondition and return type because of incoherent typing"; None, [] in (* Generate the propositions from the precondition and the postcondition *) (* TODO: not sure about the return type parameter: maybe catch failures *) print_dbg dbg "> Instantiating global pre/post"; (* Note that we need to revert the lists of parents terms *) (* For the children: * - if the focused term is the return value and is pure: go look for * a state variable introduced before * - otherwise, use the children in revert order *) let gchildren = if is_let then rev children (* the postcondition should have been dropped anyway *) else if effect_type_is_stateful einfo.ei_type then rev children else parents in let ge2, gpre_prop, gpost_prop = pre_post_to_propositions dbg ge1 ei.ei_type v opt_b einfo.ei_ret_type gpre gpost (rev parents) gchildren in (* Some debugging output *) print_dbg dbg ("- global pre prop: " ^ option_to_string term_to_string gpre_prop); print_dbg dbg ("- global post prop: " ^ option_to_string term_to_string gpost_prop); (* Return type: *) ge2, gparams_props, gpre_prop, gcast_props, gpost_prop | _, _ -> ge1, [], None, [], None end <: Tac _ in (* Generate the propositions: *) (* - from the parameters' cast info *) let params_props = cast_info_list_to_propositions dbg ge2 info.parameters in (* - from the return type *) let (ret_values : list term), (ret_binders : list binder) = if E_Lemma? einfo.ei_type then ([] <: list term), ([] <: list binder) else [v], (match opt_b with | Some b -> [b] | None -> []) in let ret_has_type_prop = match ret_values with | [v] -> Some (mk_has_type v einfo.ei_ret_type.ty) | _ -> None in let ret_refin_prop = opt_mk_app_norm ge2.env (get_opt_refinment einfo.ei_ret_type) ret_values in (* Concatenate, revert and return *) let pres = opt_cons gpre_prop (List.append params_props (opt_cons pre_prop [])) in let pres = append gparams_props pres in let posts = opt_cons ret_has_type_prop (opt_cons ret_refin_prop (opt_cons post_prop (List.append gcast_props (opt_cons gpost_prop [])))) in (* Debugging output *) print_dbg dbg "- generated pres:"; if dbg then iter (fun x -> print (term_to_string x)) pres; print_dbg dbg "- generated posts:"; if dbg then iter (fun x -> print (term_to_string x)) posts; ge2, { pres = pres; posts = posts } end
{ "file_name": "ulib/experimental/FStar.InteractiveHelpers.Effectful.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 7, "end_line": 962, "start_col": 0, "start_line": 764 }
module FStar.InteractiveHelpers.Effectful module HS = FStar.HyperStack open FStar.List open FStar.Tactics open FStar.Mul open FStar.InteractiveHelpers.Base open FStar.InteractiveHelpers.ExploreTerm open FStar.InteractiveHelpers.Propositions let term_eq = FStar.Tactics.term_eq_old /// Effectful term analysis: retrieve information about an effectful term, including /// its return type, its arguments, its correctly instantiated pre/postcondition, etc. #push-options "--z3rlimit 15 --fuel 0 --ifuel 1" (*** Effectful term analysis *) /// Cast information noeq type cast_info = { term : term; p_ty : option type_info; // The type of the term exp_ty : option type_info; // The type of the expected parameter } let mk_cast_info t p_ty exp_ty : cast_info = Mkcast_info t p_ty exp_ty val cast_info_to_string : cast_info -> Tac string let cast_info_to_string info = "Mkcast_info (" ^ term_to_string info.term ^ ") (" ^ option_to_string type_info_to_string info.p_ty ^ ") (" ^ option_to_string type_info_to_string info.exp_ty ^ ")" /// Extracts the effectful information from a computation noeq type effect_info = { ei_type : effect_type; ei_ret_type : type_info; ei_pre : option term; ei_post : option term; } let mk_effect_info = Mkeffect_info /// Convert a ``typ_or_comp`` to cast information val effect_info_to_string : effect_info -> Tac string let effect_info_to_string c = "Mkeffect_info " ^ effect_type_to_string c.ei_type ^ " (" ^ option_to_string term_to_string c.ei_pre ^ ") (" ^ type_info_to_string c.ei_ret_type ^ ") (" ^ option_to_string term_to_string c.ei_post ^ ")" /// Effectful term information noeq type eterm_info = { einfo : effect_info; (* Head and parameters of the decomposition of the term into a function application *) head : term; parameters : list cast_info; } val eterm_info_to_string : eterm_info -> Tac string let eterm_info_to_string info = let params = map (fun x -> "(" ^ cast_info_to_string x ^ "); \n") info.parameters in let params_str = List.Tot.fold_left (fun x y -> x ^ y) "" params in "Mketerm_info (" ^ effect_info_to_string info.einfo ^ ") (" ^ term_to_string info.head ^ ")\n[" ^ params_str ^ "]" let mk_eterm_info = Mketerm_info (**** Step 1 *) /// Decompose a function application between its body and parameters val decompose_application : env -> term -> Tac (term & list cast_info) #push-options "--ifuel 1" let rec decompose_application_aux (e : env) (t : term) : Tac (term & list cast_info) = match inspect t with | Tv_App hd (a, qualif) -> let hd0, params = decompose_application_aux e hd in (* Parameter type *) let a_type = get_type_info e a in (* Type expected by the function *) let hd_ty = safe_tc e hd in let param_type = match hd_ty with | None -> None | Some hd_ty' -> match inspect hd_ty' with | Tv_Arrow b c -> Some (get_type_info_from_type (binder_sort b)) | _ -> None in let cast_info = mk_cast_info a a_type param_type in hd0, cast_info :: params | _ -> t, [] #pop-options let decompose_application e t = let hd, params = decompose_application_aux e t in hd, List.Tot.rev params /// Computes an effect type, its return type and its (optional) pre and post val comp_view_to_effect_info : dbg:bool -> comp_view -> Tac (option effect_info) let comp_view_to_effect_info dbg cv = match cv with | C_Total ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_GTotal ret_ty -> let ret_type_info = get_type_info_from_type ret_ty in Some (mk_effect_info E_Total ret_type_info None None) | C_Lemma pre post patterns -> (* We use unit as the return type information *) let pre = prettify_term dbg pre in let post = prettify_term dbg post in Some (mk_effect_info E_Lemma unit_type_info (Some pre) (Some post)) | C_Eff univs eff_name ret_ty eff_args _ -> print_dbg dbg ("comp_view_to_effect_info: C_Eff " ^ flatten_name eff_name); let ret_type_info = get_type_info_from_type ret_ty in let etype = effect_name_to_type eff_name in let mk_res = mk_effect_info etype ret_type_info in let eff_args = map (fun (x,a) -> (prettify_term dbg x, a)) eff_args in begin match etype, eff_args with | E_PURE, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Pure, [(pre, _); (post, _)] | E_Stack, [(pre, _); (post, _)] | E_ST, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) (* If the effect is unknown and there are two parameters or less, we make the * guess that the first one is a pre-condition and the second one is a * post-condition *) | E_Unknown, [] -> Some (mk_res None None) | E_Unknown, [(pre, _)] -> Some (mk_res (Some pre) None) | E_Unknown, [(pre, _); (post, _)] -> Some (mk_res (Some pre) (Some post)) | _ -> None end val comp_to_effect_info : dbg:bool -> comp -> Tac (option effect_info) let comp_to_effect_info dbg c = let cv : comp_view = inspect_comp c in comp_view_to_effect_info dbg cv val compute_effect_info : dbg:bool -> env -> term -> Tac (option effect_info) let compute_effect_info dbg e tm = match safe_tcc e tm with | Some c -> comp_to_effect_info dbg c | None -> None /// Converts a ``typ_or_comp`` to an ``effect_info`` by flushing the instantiations /// stored in the ``typ_or_comp``. let typ_or_comp_to_effect_info (dbg : bool) (ge : genv) (c : typ_or_comp) : Tac effect_info = (* match c with | TC_Typ ty pl num_unflushed -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv pl num_unflushed -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo *) let c = flush_typ_or_comp dbg ge.env c in match c with | TC_Typ ty _ _ -> let tinfo = get_type_info_from_type ty in mk_effect_info E_Total tinfo None None | TC_Comp cv _ _ -> let opt_einfo = comp_to_effect_info dbg cv in match opt_einfo with | None -> mfail ("typ_or_comp_to_effect_info failed on: " ^ acomp_to_string cv) | Some einfo -> einfo /// ``tcc`` often returns a lifted effect which is not what we want (ex.: a /// lemma called inside a Stack function may have been lifted to Stack, but /// when studying this term effect, we want to retrieve its non-lifted effect). /// The workaround is to decompose the term if it is an application, then retrieve /// the effect of the head, and reconstruct it. /// Note: I tried inspecting then repacking the term before calling ``tcc`` to /// see if it allows to "forget" the context: it doesn't work. val tcc_no_lift : env -> term -> Tac comp let tcc_no_lift e t = match inspect t with | Tv_App _ _ -> let hd, args = collect_app t in let c = tcc e hd in inst_comp e c (List.Tot.map fst args) | _ -> (* Fall back to ``tcc`` *) tcc e t /// Returns the effectful information about a term val compute_eterm_info : dbg:bool -> env -> term -> Tac eterm_info #push-options "--ifuel 2" let compute_eterm_info (dbg : bool) (e : env) (t : term) = (* Decompose the term if it is a function application *) let hd, parameters = decompose_application e t in try begin let c : comp = tcc_no_lift e t in let opt_einfo = comp_to_effect_info dbg c in match opt_einfo with | None -> mfail ("compute_eterm_info: failed on: " ^ term_to_string t) | Some einfo -> mk_eterm_info einfo hd parameters end with | TacticFailure msg -> mfail ("compute_eterm_info: failure: '" ^ msg ^ "'") | e -> raise e #pop-options (***** Types, casts and refinements *) (* TODO: those are not needed anymore *) let has_refinement (ty:type_info) : Tot bool = Some? ty.refin let get_refinement (ty:type_info{Some? ty.refin}) : Tot term = Some?.v ty.refin let get_opt_refinment (ty:type_info) : Tot (option term) = ty.refin let get_rawest_type (ty:type_info) : Tot typ = ty.ty /// Compare the type of a parameter and its expected type type type_comparison = | Refines | Same_raw_type | Unknown #push-options "--ifuel 1" let type_comparison_to_string c = match c with | Refines -> "Refines" | Same_raw_type -> "Same_raw_type" | Unknown -> "Unknown" #pop-options // TODO: without debugging information generation, is Tot let compare_types (dbg : bool) (info1 info2 : type_info) : Tac (c:type_comparison{c = Same_raw_type ==> has_refinement info2}) = print_dbg dbg "[> compare_types"; if term_eq info1.ty info2.ty then let _ = print_dbg dbg "-> types are equal" in if has_refinement info2 then let _ = print_dbg dbg "-> 2nd type has refinement" in // This doesn't work like in C: we need to check if info1 has a // refinement, then we can compare the refinements inside ANOTHER if if has_refinement info1 then let _ = print_dbg dbg "-> 1st type has refinement" in if term_eq (get_refinement info1) (get_refinement info2) then let _ = print_dbg dbg "-> Refines" in Refines else let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 1st type has no refinement" in let _ = print_dbg dbg "-> Same_raw_type" in Same_raw_type // Same raw type but can't say anything about the expected refinement else let _ = print_dbg dbg "-> 2nd type has no refinement: Refines" in Refines // The first type is more precise than the second type else let _ = print_dbg dbg "types are not equal" in Unknown let compare_cast_types (dbg : bool) (p:cast_info) : Tac (c:type_comparison{ ((c = Refines \/ c = Same_raw_type) ==> (Some? p.p_ty /\ Some? p.exp_ty)) /\ (c = Same_raw_type ==> has_refinement (Some?.v p.exp_ty))}) = print_dbg dbg "[> compare_cast_types"; match p.p_ty, p.exp_ty with | Some info1, Some info2 -> compare_types dbg info1 info2 | _ -> Unknown (*/// Retrieve the list of types from the parameters stored in ``typ_or_comp``. val typ_or_comp_to_param_types : typ_or_comp -> Tot (list typ) let typ_or_comp_to_param_types c = let pl = params_of_typ_or_comp c in List.Tot.map type_of_binder pl *) (**** Step 2 *) /// The retrieved type refinements and post-conditions are not instantiated (they /// are lambda functions): instantiate them to get propositions. /// Generate a term of the form [has_type x ty] val mk_has_type : term -> typ -> Tac term let mk_has_type t ty = let params = [(ty, Q_Implicit); (t, Q_Explicit); (ty, Q_Explicit)] in mk_app (`has_type) params // TODO: I don't understand why I need ifuel 2 here #push-options "--ifuel 2" /// Generate the propositions equivalent to a correct type cast. /// Note that the type refinements need to be instantiated. val cast_info_to_propositions : bool -> genv -> cast_info -> Tac (list proposition) let cast_info_to_propositions dbg ge ci = print_dbg dbg ("[> cast_info_to_propositions:\n" ^ cast_info_to_string ci); match compare_cast_types dbg ci with | Refines -> print_dbg dbg ("-> Comparison result: Refines"); [] | Same_raw_type -> print_dbg dbg ("-> Comparison result: Same_raw_type"); let refin = get_refinement (Some?.v ci.exp_ty) in let inst_refin = mk_app_norm ge.env refin [ci.term] in [inst_refin] | Unknown -> print_dbg dbg ("-> Comparison result: Unknown"); match ci.p_ty, ci.exp_ty with | Some p_ty, Some e_ty -> let p_rty = get_rawest_type p_ty in let e_rty = get_rawest_type e_ty in (* For the type cast, we generate an assertion of the form: * [> has_type (p <: type_of_p) expected_type * The reason is that we want the user to know which parameter is * concerned (hence the ``has_type``), and which types should be * related (hence the ascription). *) let ascr_term = pack (Tv_AscribedT ci.term p_rty None false) in let has_type_params = [(p_rty, Q_Implicit); (ascr_term, Q_Explicit); (e_rty, Q_Explicit)] in let type_cast = mk_app (`has_type) has_type_params in (* Expected type's refinement *) let inst_opt_refin = opt_mk_app_norm ge.env e_ty.refin [ci.term] in opt_cons inst_opt_refin [type_cast] | _ -> [] #pop-options /// Generates a list of propositions from a list of ``cast_info``. Note that /// the user should revert the list before printing the propositions. val cast_info_list_to_propositions : bool -> genv -> list cast_info -> Tac (list proposition) let cast_info_list_to_propositions dbg ge ls = let lsl = map (cast_info_to_propositions dbg ge) ls in flatten lsl /// When dealing with unknown effects, we try to guess what the pre and post-conditions /// are. The effects are indeed very likely to have a pre and a post-condition, /// and to be parameterized with an internal effect state, so that the pre and posts /// have the following shapes: /// - pre : STATE -> Type0 /// - post : STATE -> RET -> STATE -> Type0 /// Or (no state/pure): /// - pre : Type0 /// - post : RET -> Type0 /// We try to detect that with the following functions: noeq type pre_post_type = | PP_Unknown | PP_Pure | PP_State : (state_type:term) -> pre_post_type val compute_pre_type : bool -> env -> term -> Tac pre_post_type let compute_pre_type dbg e pre = print_dbg dbg "[> compute_pre_type"; match safe_tc e pre with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [], true -> print_dbg dbg "PP_Pure"; PP_Pure | [b], true -> print_dbg dbg ("PP_State " ^ (term_to_string (type_of_binder b))); PP_State (type_of_binder b) | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val opt_remove_refin : typ -> Tac typ let opt_remove_refin ty = match inspect ty with | Tv_Refine _ sort _ -> sort | _ -> ty val compute_post_type : bool -> env -> term -> term -> Tac pre_post_type let compute_post_type dbg e ret_type post = print_dbg dbg "[> compute_post_type"; let get_tot_ret_type c : Tac (option term_view) = match get_total_or_gtotal_ret_type c with | Some ret_ty -> Some (inspect ret_ty) | None -> None in match safe_tc e post with | None -> print_dbg dbg "safe_tc failed"; PP_Unknown | Some ty -> print_dbg dbg "safe_tc succeeded"; let brs, c = collect_arr_bs ty in print_dbg dbg ("num binders: " ^ string_of_int (List.Tot.length brs)); match brs, is_total_or_gtotal c with | [r], true -> (* Pure *) print_dbg dbg "PP_Pure"; PP_Pure | [s1; r; s2], true -> (* Stateful: check that the state types are coherent *) let r_ty = type_of_binder r in let s1_ty = type_of_binder s1 in (* After testing with Stack: the final state seems to have a refinement * (which gives the postcondition) so we need to remove it to match * it against the initial state *) let s2_ty = opt_remove_refin (type_of_binder s2) in let ret_type_eq = term_eq ret_type r_ty in let state_type_eq = term_eq s1_ty s2_ty in print_dbg dbg ("- ret type:\n-- target: " ^ term_to_string ret_type ^ "\n-- binder: " ^ term_to_string r_ty); print_dbg dbg ("- state types:\n-- binder1: " ^ term_to_string s1_ty ^ "\n-- binder2: " ^ term_to_string s2_ty); print_dbg dbg ("- ret type equality: " ^ string_of_bool ret_type_eq); print_dbg dbg ("- state types equality: " ^ string_of_bool state_type_eq); if ret_type_eq && state_type_eq then begin print_dbg dbg ("PP_State" ^ term_to_string (type_of_binder s1)); PP_State (type_of_binder s1) end else begin print_dbg dbg "PP_Unknown"; PP_Unknown end | _ -> print_dbg dbg "PP_Unknown"; PP_Unknown val check_pre_post_type : bool -> env -> term -> term -> term -> Tac pre_post_type let check_pre_post_type dbg e pre ret_type post = print_dbg dbg "[> check_pre_post_type"; match compute_pre_type dbg e pre, compute_post_type dbg e ret_type post with | PP_Pure, PP_Pure -> print_dbg dbg "PP_Pure, PP_Pure"; PP_Pure | PP_State ty1, PP_State ty2 -> print_dbg dbg "PP_State, PP_State"; if term_eq ty1 ty2 then PP_State ty1 else PP_Unknown | _ -> print_dbg dbg "_, _"; PP_Unknown val check_opt_pre_post_type : bool -> env -> option term -> term -> option term -> Tac (option pre_post_type) let check_opt_pre_post_type dbg e opt_pre ret_type opt_post = print_dbg dbg "[> check_opt_pre_post_type"; match opt_pre, opt_post with | Some pre, Some post -> print_dbg dbg "Some pre, Some post"; Some (check_pre_post_type dbg e pre ret_type post) | Some pre, None -> print_dbg dbg "Some pre, None"; Some (compute_pre_type dbg e pre) | None, Some post -> print_dbg dbg "None, Some post"; Some (compute_post_type dbg e ret_type post) | None, None -> print_dbg dbg "None, None"; None val _introduce_variables_for_abs : genv -> typ -> Tac (list term & list binder & genv) let rec _introduce_variables_for_abs ge ty = match inspect ty with | Tv_Arrow b c -> let ge1, b1 = genv_push_fresh_binder ge ("__" ^ name_of_binder b) (type_of_binder b) in let bv1 = bv_of_binder b1 in let v1 = pack (Tv_Var bv1) in begin match get_total_or_gtotal_ret_type c with | Some ty1 -> let vl, bl, ge2 = _introduce_variables_for_abs ge1 ty1 in v1 :: vl, b1 :: bl, ge2 | None -> [v1], [b1], ge1 end | _ -> [], [], ge val introduce_variables_for_abs : genv -> term -> Tac (list term & list binder & genv) let introduce_variables_for_abs ge tm = match safe_tc ge.env tm with | Some ty -> _introduce_variables_for_abs ge ty | None -> [], [], ge val introduce_variables_for_opt_abs : genv -> option term -> Tac (list term & list binder & genv) let introduce_variables_for_opt_abs ge opt_tm = match opt_tm with | Some tm -> introduce_variables_for_abs ge tm | None -> [], [], ge val effect_type_is_stateful : effect_type -> Tot bool let effect_type_is_stateful etype = match etype with | E_Total | E_GTotal | E_Lemma | E_PURE | E_Pure -> false | E_Stack | E_ST | E_Unknown -> true let is_st_get dbg t : Tac bool = print_dbg dbg ("[> is_st_get:\n" ^ term_to_string t); match inspect t with | Tv_App hd (a, qual) -> print_dbg dbg "-> Is Tv_App"; begin match inspect hd with | Tv_FVar fv -> print_dbg dbg ("-> Head is Tv_FVar: " ^ fv_to_string fv); fv_eq_name fv ["FStar"; "HyperStack"; "ST"; "get"] | _ -> print_dbg dbg "-> Head is not Tv_FVar"; false end | _ -> print_dbg dbg "-> Is not Tv_App"; false let is_let_st_get dbg (t : term_view) = print_dbg dbg ("[> is_let_st_get:\n" ^ term_to_string t); match t with | Tv_Let recf attrs bv ty def body -> print_dbg dbg "The term is a let expression"; if is_st_get dbg def then Some (bv, ty) else None | _ -> print_dbg dbg "The term is not a let expression"; None // TODO: Define relation between parents and children in and use it in explore_term // app: head or arg // let : bv or def or child // match: scrutinee or branch // ascribed: e or ty /// Check if a term's computation is effectful. The return type is option /// because we may not be able to retrieve the term computation. val term_has_effectful_comp : bool -> env -> term -> Tac (option bool) let term_has_effectful_comp dbg e tm = print_dbg dbg "[> term_has_effectful_comp"; let einfo_opt = compute_effect_info dbg e tm in match einfo_opt with | Some einfo -> print_dbg dbg ("Effect type: " ^ effect_type_to_string einfo.ei_type); Some (not (effect_type_is_pure einfo.ei_type)) | None -> print_dbg dbg "Could not compute effect info"; None /// Check if a related term is effectful. This is used to look for instances of /// ``HS.mem`` to instantiate pre/postconditions, which means that the term should /// be a parent/child term of the term under study, as generated by ``explore_term`` /// (otherwise the way we check that a term is effectful doesn't make sense). /// The computation is an overapproximation: it may happen that, for instance, we /// can't compute a term computation. In this case, we consider that the term is /// effectful. There are also situations in which we may not be sure which term to /// consider. let related_term_is_effectul dbg ge tv : Tac bool = let is_effectful tm = term_has_effectful_comp dbg ge.env tm <> Some false in match tv with | Tv_Var _ | Tv_BVar _ | Tv_FVar _ -> false | Tv_App hd (a, qual) -> (* The term under focus should be the app itself or an argument *) false | Tv_Abs br body -> false | Tv_Arrow br c0 -> false | Tv_Type _ -> false | Tv_Refine bv sort ref -> false | Tv_Const _ -> false | Tv_Uvar _ _ -> false | Tv_Let recf attrs bv ty def body -> is_effectful def | Tv_Match scrutinee _ret_opt branches -> (* TODO: we need to keep track of the relation between parents and children *) (* We assume the term under focus is in one the branches of the match - this * assumption is safe: in the worst case, we won't be able to find a mem to use. * Besides, in practice it is uncommon (impossible?) to use an effectful term * as the scrutinee of a match *) is_effectful scrutinee | Tv_AscribedT e ty tac _ -> false (* The focused term should be inside the ascription *) | Tv_AscribedC e c tac _ -> false (* The focused term should be inside the ascription *) | _ -> (* Unknown: keep things safe *) true /// Look for a term of the form ``let h = ST.get ()`` in a list of parent/children terms /// and return the let-bound bv. Abort the search if we find a non-effectful term. /// The typical usages of this function are the following: /// - look for a state variable to instantiate the precondition of the term under focus /// - look for state variables for the pre/postconditions of a term defined before /// the term under focus. val find_mem_in_related: dbg:bool -> ge:genv -> tms:list term_view -> Tac (option (bv & typ)) let rec find_mem_in_related dbg ge tms = match tms with | [] -> None | tv :: tms' -> print_dbg dbg ("[> find_mem_in_related:\n" ^ term_to_string tv); match is_let_st_get dbg tv with | Some bvt -> print_dbg dbg "Term is of the form `let x = FStar.HyperStack.ST.get ()`: success"; Some bvt | None -> print_dbg dbg "Term is not of the form `let x = FStar.HyperStack.ST.get ()`: continuing"; if related_term_is_effectul dbg ge tv then begin print_dbg dbg "Term is effectful: stopping here"; None end else begin print_dbg dbg "Term is not effectful: continuing"; find_mem_in_related dbg ge tms' end // TODO: not used for now /// Look for a term of the form ``let h = ST.get ()`` in a child term (the /// focused term is supposed to be a subterm of the definition of a let-construct). val find_mem_in_children: dbg:bool -> ge:genv -> child:term -> Tac (genv & option bv) let rec find_mem_in_children dbg ge child = (* We stop whenever we find an expression which is not a let-binding *) match inspect child with | Tv_Let recf attrs bv ty def body -> if is_st_get dbg def then ge, Some bv else if term_has_effectful_comp dbg ge.env def <> Some false then ge, None else let ge1 = genv_push_bv ge bv ty false None in find_mem_in_children dbg ge1 body | _ -> ge, None /// Instantiates optional pre and post conditions val pre_post_to_propositions : dbg:bool -> ge:genv -> etype:effect_type -> ret_value:term -> ret_abs_binder:option binder -> ret_type:type_info -> opt_pre:option term -> opt_post:option term -> parents:list term_view (* to look for state variables for the pre *) -> children:list term_view (* to look for state variables for the pre and post *) -> Tac (genv & option proposition & option proposition) let pre_post_to_propositions dbg ge0 etype v ret_abs_binder ret_type opt_pre opt_post parents children = print_dbg dbg "[> pre_post_to_propositions: begin"; print_dbg dbg ("- uninstantiated pre: " ^ option_to_string term_to_string opt_pre); print_dbg dbg ("- uninstantiated post: " ^ option_to_string term_to_string opt_post); let brs = match ret_abs_binder with | None -> [] | Some b -> [b] in (* Analyze the pre and the postcondition and introduce the necessary variables *) let ge3, (pre_values, pre_binders), (post_values, post_binders) = match etype with | E_Lemma -> print_dbg dbg "E_Lemma"; ge0, ([], []), ([(`())], []) | E_Total | E_GTotal -> print_dbg dbg "E_Total/E_GTotal"; ge0, ([], []), ([], []) | E_PURE | E_Pure -> print_dbg dbg "E_PURE/E_Pure"; ge0, ([], []), ([v], brs) | E_Stack | E_ST -> print_dbg dbg "E_Stack/E_ST"; (* Look for state variables in the context *) print_dbg dbg "Looking for the initial state in the context"; let b1_opt = find_mem_in_related dbg ge0 parents in print_dbg dbg "Looking for the final state in the context"; let b2_opt = find_mem_in_related dbg ge0 children in (* Introduce state variables if necessary *) let opt_push_fresh_state opt_bvt basename ge : Tac (term & binder & genv) = match opt_bvt with | Some (bv, ty) -> pack (Tv_Var bv), mk_binder bv ty, ge | None -> genv_push_fresh_var ge basename (`HS.mem) in let h1, b1, ge1 = opt_push_fresh_state b1_opt "__h0_" ge0 in let h2, b2, ge2 = opt_push_fresh_state b2_opt "__h1_" ge1 in ge2, ([h1], [b1]), ([h1; v; h2], List.Tot.flatten ([b1]::brs::[[b2]])) | E_Unknown -> (* We don't know what the effect is and the current pre and post-conditions * are currently guesses. Introduce any necessary variable abstracted by * those parameters *) (* The pre and post-conditions are likely to have the same shape as * one of Pure or Stack (depending on whether we use or not an internal * state). We try to check that and to instantiate them accordingly *) let pp_type = check_opt_pre_post_type dbg ge0.env opt_pre ret_type.ty opt_post in begin match pp_type with | Some PP_Pure -> print_dbg dbg "PP_Pure"; (* We only need the return value *) ge0, ([], []), ([v], brs) | Some (PP_State state_type) -> print_dbg dbg "PP_State"; (* Introduce variables for the states *) let s1, b1, s2, b2, ge1 = genv_push_two_fresh_vars ge0 "__s" state_type in ge1, ([s1], [b1]), ([s1; v; s2], List.Tot.flatten ([b1]::brs::[[b2]])) | Some PP_Unknown -> print_dbg dbg "PP_Unknown"; (* Introduce variables for all the values, for the pre and the post *) let pre_values, pre_binders, ge1 = introduce_variables_for_opt_abs ge0 opt_pre in let post_values, post_binders, ge1 = introduce_variables_for_opt_abs ge1 opt_post in ge1, (pre_values, pre_binders), (post_values, post_binders) | _ -> print_dbg dbg "No pre and no post"; (* No pre and no post *) ge0, ([], []), ([], []) end in (* Generate the propositions: *) (* - from the precondition *) let pre_prop = opt_mk_app_norm ge3.env opt_pre pre_values in (* - from the postcondition - note that in the case of a global post-condition * we might try to instantiate the return variable with a variable whose * type is not correct, leading to an error. We thus catch errors below and * drop the post if there is a problem *) let post_prop = try opt_mk_app_norm ge3.env opt_post post_values with | _ -> print_dbg dbg "Dropping a postcondition because of incoherent typing"; None in (* return *) print_dbg dbg "[> pre_post_to_propositions: end"; ge3, pre_prop, post_prop /// Convert effectful type information to a list of propositions. May have to /// introduce additional binders for the preconditions/postconditions/goal (hence /// the environment in the return type). /// The ``bind_var`` parameter is a variable if the studied term was bound in a let /// expression. val eterm_info_to_assertions : dbg:bool -> with_gpre:bool -> with_gpost:bool -> ge:genv -> t:term -> is_let:bool (* the term is the bound expression in a let binding *) -> is_assert:bool (* the term is an assert - in which case we only output the precondition *) -> info:eterm_info -> opt_bind_var:option term (* if let binding: the bound var *) -> opt_c:option typ_or_comp -> parents:list term_view -> children:list term_view -> Tac (genv & assertions)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.InteractiveHelpers.Propositions.fst.checked", "FStar.InteractiveHelpers.ExploreTerm.fst.checked", "FStar.InteractiveHelpers.Base.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "FStar.InteractiveHelpers.Effectful.fst" }
[ { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Propositions", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.ExploreTerm", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.List", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.InteractiveHelpers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 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": 15, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dbg: Prims.bool -> with_gpre: Prims.bool -> with_gpost: Prims.bool -> ge: FStar.InteractiveHelpers.Base.genv -> t: FStar.Stubs.Reflection.Types.term -> is_let: Prims.bool -> is_assert: Prims.bool -> info: FStar.InteractiveHelpers.Effectful.eterm_info -> opt_bind_var: FStar.Pervasives.Native.option FStar.Stubs.Reflection.Types.term -> opt_c: FStar.Pervasives.Native.option FStar.InteractiveHelpers.ExploreTerm.typ_or_comp -> parents: Prims.list FStar.Stubs.Reflection.V1.Data.term_view -> children: Prims.list FStar.Stubs.Reflection.V1.Data.term_view -> FStar.Tactics.Effect.Tac (FStar.InteractiveHelpers.Base.genv * FStar.InteractiveHelpers.Propositions.assertions)
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.bool", "FStar.InteractiveHelpers.Base.genv", "FStar.Stubs.Reflection.Types.term", "FStar.InteractiveHelpers.Effectful.eterm_info", "FStar.Pervasives.Native.option", "FStar.InteractiveHelpers.ExploreTerm.typ_or_comp", "Prims.list", "FStar.Stubs.Reflection.V1.Data.term_view", "FStar.Stubs.Reflection.Types.binder", "FStar.InteractiveHelpers.Propositions.proposition", "FStar.Pervasives.Native.Mktuple2", "FStar.InteractiveHelpers.Propositions.assertions", "FStar.InteractiveHelpers.Propositions.Mkassertions", "FStar.InteractiveHelpers.Base.opt_cons", "Prims.Nil", "FStar.Pervasives.Native.tuple2", "Prims.unit", "FStar.InteractiveHelpers.Base.print_dbg", "FStar.Tactics.Util.iter", "FStar.Stubs.Tactics.V1.Builtins.print", "Prims.string", "FStar.Stubs.Tactics.V1.Builtins.term_to_string", "FStar.List.Tot.Base.append", "FStar.InteractiveHelpers.Base.opt_mk_app_norm", "FStar.InteractiveHelpers.Base.__proj__Mkgenv__item__env", "FStar.InteractiveHelpers.Effectful.get_opt_refinment", "FStar.InteractiveHelpers.Effectful.__proj__Mkeffect_info__item__ei_ret_type", "FStar.Pervasives.Native.Some", "FStar.InteractiveHelpers.Effectful.mk_has_type", "FStar.InteractiveHelpers.ExploreTerm.__proj__Mktype_info__item__ty", "FStar.Pervasives.Native.None", "FStar.InteractiveHelpers.ExploreTerm.uu___is_E_Lemma", "FStar.InteractiveHelpers.Effectful.__proj__Mkeffect_info__item__ei_type", "Prims.Cons", "FStar.InteractiveHelpers.Effectful.cast_info_list_to_propositions", "FStar.InteractiveHelpers.Effectful.__proj__Mketerm_info__item__parameters", "FStar.Pervasives.Native.tuple5", "FStar.Pervasives.Native.Mktuple5", "Prims.op_Hat", "FStar.InteractiveHelpers.Base.option_to_string", "FStar.Pervasives.Native.tuple3", "FStar.InteractiveHelpers.Effectful.pre_post_to_propositions", "FStar.List.Tot.Base.rev", "FStar.InteractiveHelpers.Effectful.effect_type_is_stateful", "Prims.op_Negation", "FStar.Tactics.V1.Derived.try_with", "FStar.InteractiveHelpers.Effectful.__proj__Mkeffect_info__item__ei_post", "FStar.InteractiveHelpers.Base.list_to_string", "FStar.InteractiveHelpers.Effectful.cast_info_to_propositions", "FStar.InteractiveHelpers.Effectful.cast_info_to_string", "FStar.InteractiveHelpers.Effectful.cast_info", "FStar.InteractiveHelpers.Effectful.mk_cast_info", "FStar.InteractiveHelpers.ExploreTerm.type_info", "FStar.InteractiveHelpers.ExploreTerm.type_info_to_string", "Prims.exn", "FStar.InteractiveHelpers.Effectful.__proj__Mkeffect_info__item__ei_pre", "FStar.InteractiveHelpers.ExploreTerm.term_has_shadowed_variables", "FStar.List.Tot.Base.flatten", "FStar.Tactics.Util.map", "FStar.Stubs.Reflection.Types.typ", "FStar.InteractiveHelpers.ExploreTerm.__proj__Mktype_info__item__refin", "FStar.InteractiveHelpers.Base.mk_app_norm", "FStar.Stubs.Tactics.V1.Builtins.pack", "FStar.Stubs.Reflection.V1.Data.Tv_Var", "FStar.InteractiveHelpers.ExploreTerm.get_type_info_from_type", "FStar.Tactics.V1.Derived.binder_to_string", "FStar.Stubs.Reflection.Types.bv", "FStar.Reflection.V1.Derived.bv_of_binder", "FStar.Tactics.Util.filter", "FStar.InteractiveHelpers.Base.binder_is_shadowed", "FStar.Tactics.Util.iteri", "Prims.int", "Prims.string_of_int", "FStar.List.Tot.Base.map", "FStar.Reflection.V1.Derived.type_of_binder", "FStar.InteractiveHelpers.ExploreTerm.params_of_typ_or_comp", "FStar.InteractiveHelpers.Base.genv_to_string", "FStar.InteractiveHelpers.Effectful.effect_info_to_string", "FStar.InteractiveHelpers.Effectful.effect_info", "FStar.InteractiveHelpers.Effectful.typ_or_comp_to_effect_info", "Prims.op_BarBar", "Prims.op_AmpAmp", "FStar.Pervasives.Native.Mktuple3", "FStar.InteractiveHelpers.Base.genv_push_binder", "FStar.InteractiveHelpers.Base.fresh_binder", "FStar.InteractiveHelpers.ExploreTerm.is_unit_type", "FStar.InteractiveHelpers.Effectful.__proj__Mketerm_info__item__einfo" ]
[]
false
true
false
false
false
let eterm_info_to_assertions dbg with_gpre with_gpost ge t is_let is_assert info bind_var opt_c parents children =
print_dbg dbg "[> eterm_info_to_assertions"; let einfo = info.einfo in let ge0, (v: term), (opt_b: option binder) = match bind_var with | Some v -> ge, v, None | None -> if effect_type_is_stateful einfo.ei_type then if is_unit_type einfo.ei_ret_type.ty then ge, (`()), None else let b = fresh_binder ge.env "__ret" einfo.ei_ret_type.ty in let bv = bv_of_binder b in let tm = pack (Tv_Var bv) in genv_push_binder ge b true None, tm, Some b else ge, t, None in print_dbg dbg "> Instantiating local pre/post"; let ge1, pre_prop, post_prop = pre_post_to_propositions dbg ge0 einfo.ei_type v opt_b einfo.ei_ret_type einfo.ei_pre einfo.ei_post parents children in print_dbg dbg ("- pre prop: " ^ option_to_string term_to_string pre_prop); print_dbg dbg ("- post prop: " ^ option_to_string term_to_string post_prop); if is_assert then (print_dbg dbg "The term is an assert: only keep the postcondition"; ge1, { pres = opt_cons post_prop []; posts = [] }) else let ge2, gparams_props, gpre_prop, gcast_props, gpost_prop = let with_goal:bool = with_gpre || ((not is_let) && with_gpost) in (match opt_c, with_goal with | Some c, true -> let ei = typ_or_comp_to_effect_info dbg ge1 c in print_dbg dbg ("- target effect: " ^ effect_info_to_string ei); print_dbg dbg ("- global unfilt. pre: " ^ option_to_string term_to_string ei.ei_pre); print_dbg dbg ("- global unfilt. post: " ^ option_to_string term_to_string ei.ei_post); let gparams_props = (if with_gpre then (print_dbg dbg "Generating assertions from the global parameters' types"; print_dbg dbg ("Current genv:\n" ^ genv_to_string ge1); let params = rev (List.Tot.map (fun x -> (x, type_of_binder x)) (params_of_typ_or_comp c)) in iteri (fun i (b, _) -> print_dbg dbg ("Global parameter " ^ string_of_int i ^ ": " ^ binder_to_string b)) params; let params = filter (fun (b, _) -> not (binder_is_shadowed ge1 b)) params in let param_to_props (x: (binder & typ)) : Tac (list term) = let b, ty = x in let bv = bv_of_binder b in print_dbg dbg ("Generating assertions from global parameter: " ^ binder_to_string b); let tinfo = get_type_info_from_type ty in let v = pack (Tv_Var bv) in let p1 = mk_has_type v tinfo.ty in let pl = match tinfo.refin with | None -> [] | Some r -> let p2 = mk_app_norm ge1.env r [v] in if term_has_shadowed_variables ge1 p2 then (print_dbg dbg "Discarding type refinement because of shadowed variables"; []) else (print_dbg dbg "Keeping type refinement"; [p2]) in p1 :: pl in let props = map param_to_props params in List.Tot.flatten props) else (print_dbg dbg "Ignoring the global parameters' types"; [])) <: Tac (list term) in let gpre = match ei.ei_pre, with_gpre with | Some pre, true -> if term_has_shadowed_variables ge1 pre then (print_dbg dbg "Dropping the global precondition because of shadowed variables"; None) else ei.ei_pre | _ -> None in let gpost, gcast_props = if not with_gpost then None, [] else if is_let then (print_dbg dbg "Dropping the global postcondition and return type because we are studying a let expression" ; None, []) else try (print_dbg dbg "> Generating propositions from the global type cast"; print_dbg dbg ("- known type: " ^ type_info_to_string einfo.ei_ret_type); print_dbg dbg ("- exp. type : " ^ type_info_to_string ei.ei_ret_type); let gcast = mk_cast_info v (Some einfo.ei_ret_type) (Some ei.ei_ret_type) in print_dbg dbg (cast_info_to_string gcast); let gcast_props = cast_info_to_propositions dbg ge1 gcast in print_dbg dbg "> Propositions for global type cast:"; print_dbg dbg (list_to_string term_to_string gcast_props); ei.ei_post, List.Tot.rev gcast_props) with | _ -> print_dbg dbg "Dropping the global postcondition and return type because of incoherent typing"; None, [] in print_dbg dbg "> Instantiating global pre/post"; let gchildren = if is_let then rev children else if effect_type_is_stateful einfo.ei_type then rev children else parents in let ge2, gpre_prop, gpost_prop = pre_post_to_propositions dbg ge1 ei.ei_type v opt_b einfo.ei_ret_type gpre gpost (rev parents) gchildren in print_dbg dbg ("- global pre prop: " ^ option_to_string term_to_string gpre_prop); print_dbg dbg ("- global post prop: " ^ option_to_string term_to_string gpost_prop); ge2, gparams_props, gpre_prop, gcast_props, gpost_prop | _, _ -> ge1, [], None, [], None) <: Tac _ in let params_props = cast_info_list_to_propositions dbg ge2 info.parameters in let (ret_values: list term), (ret_binders: list binder) = if E_Lemma? einfo.ei_type then ([] <: list term), ([] <: list binder) else [v], (match opt_b with | Some b -> [b] | None -> []) in let ret_has_type_prop = match ret_values with | [v] -> Some (mk_has_type v einfo.ei_ret_type.ty) | _ -> None in let ret_refin_prop = opt_mk_app_norm ge2.env (get_opt_refinment einfo.ei_ret_type) ret_values in let pres = opt_cons gpre_prop (List.append params_props (opt_cons pre_prop [])) in let pres = append gparams_props pres in let posts = opt_cons ret_has_type_prop (opt_cons ret_refin_prop (opt_cons post_prop (List.append gcast_props (opt_cons gpost_prop [])))) in print_dbg dbg "- generated pres:"; if dbg then iter (fun x -> print (term_to_string x)) pres; print_dbg dbg "- generated posts:"; if dbg then iter (fun x -> print (term_to_string x)) posts; ge2, { pres = pres; posts = posts }
false
Vale.X64.Decls.fsti
Vale.X64.Decls.vale_heap
val vale_heap : Type
let vale_heap = M.vale_heap
{ "file_name": "vale/code/arch/x64/Vale.X64.Decls.fsti", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 34, "end_line": 20, "start_col": 7, "start_line": 20 }
module Vale.X64.Decls open FStar.Mul open Vale.Arch.HeapTypes_s open Vale.Arch.HeapImpl module M = Vale.X64.Memory module S = Vale.X64.Stack_i module Map16 = Vale.Lib.Map16 // This interface should hide all of Machine_Semantics_s. // (It should not refer to Machine_Semantics_s, directly or indirectly.) // It should not refer to StateLemmas, Lemmas, or Print_s, // because they refer to Machine_Semantics_s. // Stack_i, Memory, Regs, Flags and State are ok, because they do not refer to Machine_Semantics_s. open Vale.Def.Prop_s open Vale.X64.Machine_s open Vale.X64.State open Vale.Def.Types_s
{ "checked_file": "/", "dependencies": [ "Vale.X64.State.fsti.checked", "Vale.X64.Stack_i.fsti.checked", "Vale.X64.Memory.fsti.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Flags.fsti.checked", "Vale.Lib.Map16.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Def.Prop_s.fst.checked", "Vale.Arch.HeapTypes_s.fst.checked", "Vale.Arch.HeapImpl.fsti.checked", "prims.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Map.fsti.checked", "FStar.All.fst.checked" ], "interface_file": false, "source_file": "Vale.X64.Decls.fsti" }
[ { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.State", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Prop_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Lib.Map16", "short_module": "Map16" }, { "abbrev": true, "full_module": "Vale.X64.Stack_i", "short_module": "S" }, { "abbrev": true, "full_module": "Vale.X64.Memory", "short_module": "M" }, { "abbrev": false, "full_module": "Vale.Arch.HeapImpl", "short_module": null }, { "abbrev": false, "full_module": "Vale.Arch.HeapTypes_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Type
Prims.Tot
[ "total" ]
[]
[ "Vale.X64.Memory.vale_heap" ]
[]
false
false
false
true
true
let vale_heap =
M.vale_heap
false
Vale.X64.Decls.fsti
Vale.X64.Decls.vale_full_heap
val vale_full_heap : Type
let vale_full_heap = M.vale_full_heap
{ "file_name": "vale/code/arch/x64/Vale.X64.Decls.fsti", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 44, "end_line": 21, "start_col": 7, "start_line": 21 }
module Vale.X64.Decls open FStar.Mul open Vale.Arch.HeapTypes_s open Vale.Arch.HeapImpl module M = Vale.X64.Memory module S = Vale.X64.Stack_i module Map16 = Vale.Lib.Map16 // This interface should hide all of Machine_Semantics_s. // (It should not refer to Machine_Semantics_s, directly or indirectly.) // It should not refer to StateLemmas, Lemmas, or Print_s, // because they refer to Machine_Semantics_s. // Stack_i, Memory, Regs, Flags and State are ok, because they do not refer to Machine_Semantics_s. open Vale.Def.Prop_s open Vale.X64.Machine_s open Vale.X64.State open Vale.Def.Types_s
{ "checked_file": "/", "dependencies": [ "Vale.X64.State.fsti.checked", "Vale.X64.Stack_i.fsti.checked", "Vale.X64.Memory.fsti.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Flags.fsti.checked", "Vale.Lib.Map16.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Def.Prop_s.fst.checked", "Vale.Arch.HeapTypes_s.fst.checked", "Vale.Arch.HeapImpl.fsti.checked", "prims.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Map.fsti.checked", "FStar.All.fst.checked" ], "interface_file": false, "source_file": "Vale.X64.Decls.fsti" }
[ { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.State", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Prop_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Lib.Map16", "short_module": "Map16" }, { "abbrev": true, "full_module": "Vale.X64.Stack_i", "short_module": "S" }, { "abbrev": true, "full_module": "Vale.X64.Memory", "short_module": "M" }, { "abbrev": false, "full_module": "Vale.Arch.HeapImpl", "short_module": null }, { "abbrev": false, "full_module": "Vale.Arch.HeapTypes_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Type
Prims.Tot
[ "total" ]
[]
[ "Vale.X64.Memory.vale_full_heap" ]
[]
false
false
false
true
true
let vale_full_heap =
M.vale_full_heap
false
Vale.X64.Decls.fsti
Vale.X64.Decls.heaplet_id
val heaplet_id : Type0
let heaplet_id = M.heaplet_id
{ "file_name": "vale/code/arch/x64/Vale.X64.Decls.fsti", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 36, "end_line": 22, "start_col": 7, "start_line": 22 }
module Vale.X64.Decls open FStar.Mul open Vale.Arch.HeapTypes_s open Vale.Arch.HeapImpl module M = Vale.X64.Memory module S = Vale.X64.Stack_i module Map16 = Vale.Lib.Map16 // This interface should hide all of Machine_Semantics_s. // (It should not refer to Machine_Semantics_s, directly or indirectly.) // It should not refer to StateLemmas, Lemmas, or Print_s, // because they refer to Machine_Semantics_s. // Stack_i, Memory, Regs, Flags and State are ok, because they do not refer to Machine_Semantics_s. open Vale.Def.Prop_s open Vale.X64.Machine_s open Vale.X64.State open Vale.Def.Types_s unfold let vale_heap = M.vale_heap
{ "checked_file": "/", "dependencies": [ "Vale.X64.State.fsti.checked", "Vale.X64.Stack_i.fsti.checked", "Vale.X64.Memory.fsti.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Flags.fsti.checked", "Vale.Lib.Map16.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Def.Prop_s.fst.checked", "Vale.Arch.HeapTypes_s.fst.checked", "Vale.Arch.HeapImpl.fsti.checked", "prims.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Map.fsti.checked", "FStar.All.fst.checked" ], "interface_file": false, "source_file": "Vale.X64.Decls.fsti" }
[ { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.State", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Prop_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Lib.Map16", "short_module": "Map16" }, { "abbrev": true, "full_module": "Vale.X64.Stack_i", "short_module": "S" }, { "abbrev": true, "full_module": "Vale.X64.Memory", "short_module": "M" }, { "abbrev": false, "full_module": "Vale.Arch.HeapImpl", "short_module": null }, { "abbrev": false, "full_module": "Vale.Arch.HeapTypes_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Type0
Prims.Tot
[ "total" ]
[]
[ "Vale.X64.Memory.heaplet_id" ]
[]
false
false
false
true
true
let heaplet_id =
M.heaplet_id
false
Vale.X64.Decls.fsti
Vale.X64.Decls.quad32
val quad32 : Prims.eqtype
let quad32 = quad32
{ "file_name": "vale/code/arch/x64/Vale.X64.Decls.fsti", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 26, "end_line": 23, "start_col": 7, "start_line": 23 }
module Vale.X64.Decls open FStar.Mul open Vale.Arch.HeapTypes_s open Vale.Arch.HeapImpl module M = Vale.X64.Memory module S = Vale.X64.Stack_i module Map16 = Vale.Lib.Map16 // This interface should hide all of Machine_Semantics_s. // (It should not refer to Machine_Semantics_s, directly or indirectly.) // It should not refer to StateLemmas, Lemmas, or Print_s, // because they refer to Machine_Semantics_s. // Stack_i, Memory, Regs, Flags and State are ok, because they do not refer to Machine_Semantics_s. open Vale.Def.Prop_s open Vale.X64.Machine_s open Vale.X64.State open Vale.Def.Types_s unfold let vale_heap = M.vale_heap unfold let vale_full_heap = M.vale_full_heap
{ "checked_file": "/", "dependencies": [ "Vale.X64.State.fsti.checked", "Vale.X64.Stack_i.fsti.checked", "Vale.X64.Memory.fsti.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Flags.fsti.checked", "Vale.Lib.Map16.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Def.Prop_s.fst.checked", "Vale.Arch.HeapTypes_s.fst.checked", "Vale.Arch.HeapImpl.fsti.checked", "prims.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Map.fsti.checked", "FStar.All.fst.checked" ], "interface_file": false, "source_file": "Vale.X64.Decls.fsti" }
[ { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.State", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Prop_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Lib.Map16", "short_module": "Map16" }, { "abbrev": true, "full_module": "Vale.X64.Stack_i", "short_module": "S" }, { "abbrev": true, "full_module": "Vale.X64.Memory", "short_module": "M" }, { "abbrev": false, "full_module": "Vale.Arch.HeapImpl", "short_module": null }, { "abbrev": false, "full_module": "Vale.Arch.HeapTypes_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Prims.eqtype
Prims.Tot
[ "total" ]
[]
[ "Vale.Def.Types_s.quad32" ]
[]
false
false
false
true
false
let quad32 =
quad32
false