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 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Unification.fst | Unification.lemma_subst_term_idem | val lemma_subst_term_idem: s:subst -> t:term -> Lemma
(requires (ok s))
(ensures (subst_term s (subst_term s t) = subst_term s t)) | val lemma_subst_term_idem: s:subst -> t:term -> Lemma
(requires (ok s))
(ensures (subst_term s (subst_term s t) = subst_term s t)) | let rec lemma_subst_term_idem s t = match t with
| V x -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_subst_term_idem s t1; lemma_subst_term_idem s t2 | {
"file_name": "examples/algorithms/Unification.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 69,
"end_line": 272,
"start_col": 0,
"start_line": 270
} | (*
Copyright 2008-2015 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(*
A verified implementation of first-order unification.
The termination argument is based on Ana Bove's Licentiate Thesis:
Programming in Martin-Löf Type Theory Unification - A non-trivial Example
Department of Computer Science, Chalmers University of Technology
1999
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.40.3532
But, is done here directly with lex orderings.
*)
module Unification
open FStar.List.Tot
(* First, a missing lemma from the list library *)
let op_At = append
val lemma_shift_append: #a:eqtype -> l:list a -> x:a -> m:list a -> Lemma
(ensures ( (l@(x::m)) = ((l@[x])@m)))
let rec lemma_shift_append #a l x m = match l with
| [] -> ()
| hd::tl -> lemma_shift_append tl x m
(* A term language with variables V and pairs F *)
type term =
| V : i:nat -> term
| F : t1:term -> t2:term -> term
(* Finite, ordered sets of variables *)
val nat_order : OrdSet.cmp nat
let nat_order :(f:(nat -> nat -> bool){OrdSet.total_order nat f}) = fun x y -> x <= y
type varset = OrdSet.ordset nat nat_order
val empty_vars : varset
let empty_vars = OrdSet.empty
(* Collecting the variables in a term *)
val vars : term -> Tot varset
let rec vars = function
| V i -> OrdSet.singleton i
| F t1 t2 -> OrdSet.union (vars t1) (vars t2)
(* Equations among pairs of terms, to be solved *)
type eqns = list (term * term)
(* All the variables in a set of equations *)
val evars : eqns -> Tot varset
let rec evars = function
| [] -> empty_vars
| (x, y)::tl -> OrdSet.union (OrdSet.union (vars x) (vars y)) (evars tl)
(* Counting variables; used in the termination metric *)
val n_evars : eqns -> Tot nat
let n_evars eqns = OrdSet.size (evars eqns)
(* Counting the number of F-applications; also for the termination metric *)
val funs : term -> Tot nat
let rec funs = function
| V _ -> 0
| F t1 t2 -> 1 + funs t1 + funs t2
val efuns : eqns -> Tot nat
let rec efuns = function
| [] -> 0
| (x,y)::tl -> funs x + funs y + efuns tl
(* Counting the number of equations with variables on the the RHS;
// also for the termination metric *)
val n_flex_rhs : eqns -> Tot nat
let rec n_flex_rhs = function
| [] -> 0
| (V _, V _)::tl
| (_ , V _)::tl -> 1 + n_flex_rhs tl
| _::tl -> n_flex_rhs tl
(* A point substitution *)
type subst = (nat * term)
(* Composition of point substitutions *)
type lsubst = list subst
val subst_term : subst -> term -> Tot term
let rec subst_term s t = match t with
| V x -> if x = fst s then snd s else V x
| F t1 t2 -> F (subst_term s t1) (subst_term s t2)
val lsubst_term : list subst -> term -> Tot term
let lsubst_term = fold_right subst_term
let occurs x t = OrdSet.mem x (vars t)
let ok s = not (occurs (fst s) (snd s))
val lsubst_eqns: list subst -> eqns -> Tot eqns
let rec lsubst_eqns l = function
| [] -> []
| (x,y)::tl -> (lsubst_term l x, lsubst_term l y)::lsubst_eqns l tl
val lemma_lsubst_eqns_nil: e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns [] e = e))
[SMTPat (lsubst_eqns [] e)]
let rec lemma_lsubst_eqns_nil = function
| [] -> ()
| _::tl -> lemma_lsubst_eqns_nil tl
(* A couple of lemmas about variable counts.
// Both of these rely on extensional equality of sets.
// So we need to use eq_lemma explicitly *)
val evars_permute_hd : x:term -> y:term -> tl:eqns -> Lemma
(ensures (evars ((x, y)::tl) = evars ((y, x)::tl)))
let evars_permute_hd x y tl = OrdSet.eq_lemma (evars ((x,y)::tl)) (evars ((y,x)::tl))
val evars_unfun : x:term -> y:term -> x':term -> y':term -> tl:eqns -> Lemma
(ensures (evars ((F x y, F x' y')::tl) = evars ((x, x')::(y, y')::tl)))
let evars_unfun x y x' y' tl = OrdSet.eq_lemma (evars ((F x y, F x' y')::tl)) (evars ((x, x')::(y, y')::tl))
(* Eliminating a variable reduces the variable count *)
val lemma_vars_decrease: s:subst -> t':term -> Lemma
(requires (ok s))
(ensures (OrdSet.subset (vars (subst_term s t'))
(OrdSet.remove (fst s) (OrdSet.union (vars (snd s)) (vars t')))))
let rec lemma_vars_decrease s t' = match t' with
| V x -> ()
| F t1 t2 ->
lemma_vars_decrease s t1;
lemma_vars_decrease s t2
(* Lifting the prior lemma to equations *)
val vars_decrease_eqns: x:nat -> t:term -> e:eqns -> Lemma
(requires (ok (x, t)))
(ensures (OrdSet.subset (evars (lsubst_eqns [x,t] e))
(OrdSet.remove x (evars ((V x, t)::e)))))
let rec vars_decrease_eqns x t e = match e with
| [] -> ()
| hd::tl -> lemma_vars_decrease (x,t) (fst hd);
lemma_vars_decrease (x,t) (snd hd);
vars_decrease_eqns x t tl
val unify : e:eqns -> list subst -> Tot (option (list subst))
(decreases %[n_evars e; efuns e; n_flex_rhs e])
let rec unify e s = match e with
| [] -> Some s
| (V x, t)::tl ->
if V? t && V?.i t = x
then unify tl s //t is a flex-rhs
else if occurs x t
then None
else (vars_decrease_eqns x t tl;
unify (lsubst_eqns [x,t] tl) ((x,t)::s))
| (t, V x)::tl -> //flex-rhs
evars_permute_hd t (V x) tl;
unify ((V x, t)::tl) s
| (F t1 t2, F t1' t2')::tl -> //efuns reduces
evars_unfun t1 t2 t1' t2' tl;
unify ((t1, t1')::(t2, t2')::tl) s
(* All equations are solved when each one is just reflexive *)
val solved : eqns -> Tot bool
let rec solved = function
| [] -> true
| (x,y)::tl -> x=y && solved tl
val lsubst_distributes_over_F: l:list subst -> t1:term -> t2:term -> Lemma
(requires (True))
(ensures (lsubst_term l (F t1 t2) = F (lsubst_term l t1) (lsubst_term l t2)))
[SMTPat (lsubst_term l (F t1 t2))]
let rec lsubst_distributes_over_F l t1 t2 = match l with
| [] -> ()
| hd::tl -> lsubst_distributes_over_F tl t1 t2
let extend_subst s l = s::l
let extend_lsubst l l' = l @ l'
val lemma_extend_lsubst_distributes_term: l:list subst -> l':list subst -> e:term -> Lemma
(requires True)
(ensures (lsubst_term (extend_lsubst l l') e = lsubst_term l (lsubst_term l' e)))
let rec lemma_extend_lsubst_distributes_term l l' e = match l with
| [] -> ()
| hd::tl -> lemma_extend_lsubst_distributes_term tl l' e
val lemma_extend_lsubst_distributes_eqns: l:list subst -> l':list subst -> e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns (extend_lsubst l l') e = lsubst_eqns l (lsubst_eqns l' e)))
[SMTPat (lsubst_eqns (extend_lsubst l l') e)]
let rec lemma_extend_lsubst_distributes_eqns l l' e = match e with
| [] -> ()
| (t1, t2)::tl ->
lemma_extend_lsubst_distributes_term l l' t1;
lemma_extend_lsubst_distributes_term l l' t2;
lemma_extend_lsubst_distributes_eqns l l' tl
val lemma_subst_id: x:nat -> z:term -> y:term -> Lemma
(requires (not (occurs x y)))
(ensures (subst_term (x,z) y = y))
let rec lemma_subst_id x z y = match y with
| V x' -> ()
| F t1 t2 -> lemma_subst_id x z t1; lemma_subst_id x z t2
let neutral s (x, t) = subst_term s (V x) = V x && subst_term s t = t && ok (x, t)
let neutral_l l (x, t) = lsubst_term l (V x) = V x && lsubst_term l t = t && ok (x, t)
val lemma_lsubst_term_commutes: s:subst -> l:list subst -> e:term -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_term [s] (lsubst_term l (subst_term s e)) =
lsubst_term [s] (lsubst_term l e)))
let rec lemma_lsubst_term_commutes s l e = match e with
| V y -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2
val lemma_lsubst_eqns_commutes: s:subst -> l:list subst -> e:eqns -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_eqns [s] (lsubst_eqns l (lsubst_eqns [s] e)) =
lsubst_eqns [s] (lsubst_eqns l e)))
let rec lemma_lsubst_eqns_commutes s l = function
| [] -> ()
| (t1,t2)::tl -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2;
lemma_lsubst_eqns_commutes s l tl
let (++) l1 l2 = extend_lsubst l1 l2
let sub l e = lsubst_eqns l e
let test l1 l2 l3 = assert (l1 ++ l2 ++ l3 == (l1 ++ l2) ++ l3)
val key_lemma: x:nat -> y:term -> tl:eqns -> l:list subst -> lpre:list subst -> l'':list subst -> Lemma
(requires (l'' = lpre ++ ([x,y] ++ l)
/\ not (occurs x y)
/\ l `sub` ((V x, y)::tl) = (V x, y)::tl
/\ solved (l'' `sub` ([x, y] `sub` tl))))
(ensures (solved (l'' `sub` ((V x,y)::tl))))
let key_lemma x y tl l lpre l'' =
let xy = [x,y] in
let xyl = xy++l in
let vxy = V x, y in
assert (l'' `sub` (vxy::tl)
== lpre `sub` (xy `sub` (l `sub` (vxy::tl))));
lemma_lsubst_eqns_commutes (x,y) l (vxy :: tl);
assert (lpre `sub` (xy `sub` (l `sub` (vxy::tl)))
== lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl)))));
assert (lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl))))
== l'' `sub` (xy `sub` (vxy :: tl)));
lemma_subst_id x y y
val lemma_subst_term_idem: s:subst -> t:term -> Lemma
(requires (ok s)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.OrdSet.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "Unification.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | s: Unification.subst -> t: Unification.term
-> FStar.Pervasives.Lemma (requires Unification.ok s)
(ensures Unification.subst_term s (Unification.subst_term s t) = Unification.subst_term s t) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Unification.subst",
"Unification.term",
"Prims.nat",
"Unification.lemma_subst_id",
"FStar.Pervasives.Native.fst",
"FStar.Pervasives.Native.snd",
"Unification.lemma_subst_term_idem",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec lemma_subst_term_idem s t =
| match t with
| V x -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 ->
lemma_subst_term_idem s t1;
lemma_subst_term_idem s t2 | false |
Unification.fst | Unification.lsubst_funs_monotone | val lsubst_funs_monotone: l:list subst -> t:term -> Lemma
(ensures (funs (lsubst_term l t) >= funs t)) | val lsubst_funs_monotone: l:list subst -> t:term -> Lemma
(ensures (funs (lsubst_term l t) >= funs t)) | let rec lsubst_funs_monotone l t = match l with
| [] -> ()
| hd::tl ->
lsubst_funs_monotone tl t; subst_funs_monotone hd (lsubst_term tl t) | {
"file_name": "examples/algorithms/Unification.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 72,
"end_line": 294,
"start_col": 0,
"start_line": 291
} | (*
Copyright 2008-2015 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(*
A verified implementation of first-order unification.
The termination argument is based on Ana Bove's Licentiate Thesis:
Programming in Martin-Löf Type Theory Unification - A non-trivial Example
Department of Computer Science, Chalmers University of Technology
1999
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.40.3532
But, is done here directly with lex orderings.
*)
module Unification
open FStar.List.Tot
(* First, a missing lemma from the list library *)
let op_At = append
val lemma_shift_append: #a:eqtype -> l:list a -> x:a -> m:list a -> Lemma
(ensures ( (l@(x::m)) = ((l@[x])@m)))
let rec lemma_shift_append #a l x m = match l with
| [] -> ()
| hd::tl -> lemma_shift_append tl x m
(* A term language with variables V and pairs F *)
type term =
| V : i:nat -> term
| F : t1:term -> t2:term -> term
(* Finite, ordered sets of variables *)
val nat_order : OrdSet.cmp nat
let nat_order :(f:(nat -> nat -> bool){OrdSet.total_order nat f}) = fun x y -> x <= y
type varset = OrdSet.ordset nat nat_order
val empty_vars : varset
let empty_vars = OrdSet.empty
(* Collecting the variables in a term *)
val vars : term -> Tot varset
let rec vars = function
| V i -> OrdSet.singleton i
| F t1 t2 -> OrdSet.union (vars t1) (vars t2)
(* Equations among pairs of terms, to be solved *)
type eqns = list (term * term)
(* All the variables in a set of equations *)
val evars : eqns -> Tot varset
let rec evars = function
| [] -> empty_vars
| (x, y)::tl -> OrdSet.union (OrdSet.union (vars x) (vars y)) (evars tl)
(* Counting variables; used in the termination metric *)
val n_evars : eqns -> Tot nat
let n_evars eqns = OrdSet.size (evars eqns)
(* Counting the number of F-applications; also for the termination metric *)
val funs : term -> Tot nat
let rec funs = function
| V _ -> 0
| F t1 t2 -> 1 + funs t1 + funs t2
val efuns : eqns -> Tot nat
let rec efuns = function
| [] -> 0
| (x,y)::tl -> funs x + funs y + efuns tl
(* Counting the number of equations with variables on the the RHS;
// also for the termination metric *)
val n_flex_rhs : eqns -> Tot nat
let rec n_flex_rhs = function
| [] -> 0
| (V _, V _)::tl
| (_ , V _)::tl -> 1 + n_flex_rhs tl
| _::tl -> n_flex_rhs tl
(* A point substitution *)
type subst = (nat * term)
(* Composition of point substitutions *)
type lsubst = list subst
val subst_term : subst -> term -> Tot term
let rec subst_term s t = match t with
| V x -> if x = fst s then snd s else V x
| F t1 t2 -> F (subst_term s t1) (subst_term s t2)
val lsubst_term : list subst -> term -> Tot term
let lsubst_term = fold_right subst_term
let occurs x t = OrdSet.mem x (vars t)
let ok s = not (occurs (fst s) (snd s))
val lsubst_eqns: list subst -> eqns -> Tot eqns
let rec lsubst_eqns l = function
| [] -> []
| (x,y)::tl -> (lsubst_term l x, lsubst_term l y)::lsubst_eqns l tl
val lemma_lsubst_eqns_nil: e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns [] e = e))
[SMTPat (lsubst_eqns [] e)]
let rec lemma_lsubst_eqns_nil = function
| [] -> ()
| _::tl -> lemma_lsubst_eqns_nil tl
(* A couple of lemmas about variable counts.
// Both of these rely on extensional equality of sets.
// So we need to use eq_lemma explicitly *)
val evars_permute_hd : x:term -> y:term -> tl:eqns -> Lemma
(ensures (evars ((x, y)::tl) = evars ((y, x)::tl)))
let evars_permute_hd x y tl = OrdSet.eq_lemma (evars ((x,y)::tl)) (evars ((y,x)::tl))
val evars_unfun : x:term -> y:term -> x':term -> y':term -> tl:eqns -> Lemma
(ensures (evars ((F x y, F x' y')::tl) = evars ((x, x')::(y, y')::tl)))
let evars_unfun x y x' y' tl = OrdSet.eq_lemma (evars ((F x y, F x' y')::tl)) (evars ((x, x')::(y, y')::tl))
(* Eliminating a variable reduces the variable count *)
val lemma_vars_decrease: s:subst -> t':term -> Lemma
(requires (ok s))
(ensures (OrdSet.subset (vars (subst_term s t'))
(OrdSet.remove (fst s) (OrdSet.union (vars (snd s)) (vars t')))))
let rec lemma_vars_decrease s t' = match t' with
| V x -> ()
| F t1 t2 ->
lemma_vars_decrease s t1;
lemma_vars_decrease s t2
(* Lifting the prior lemma to equations *)
val vars_decrease_eqns: x:nat -> t:term -> e:eqns -> Lemma
(requires (ok (x, t)))
(ensures (OrdSet.subset (evars (lsubst_eqns [x,t] e))
(OrdSet.remove x (evars ((V x, t)::e)))))
let rec vars_decrease_eqns x t e = match e with
| [] -> ()
| hd::tl -> lemma_vars_decrease (x,t) (fst hd);
lemma_vars_decrease (x,t) (snd hd);
vars_decrease_eqns x t tl
val unify : e:eqns -> list subst -> Tot (option (list subst))
(decreases %[n_evars e; efuns e; n_flex_rhs e])
let rec unify e s = match e with
| [] -> Some s
| (V x, t)::tl ->
if V? t && V?.i t = x
then unify tl s //t is a flex-rhs
else if occurs x t
then None
else (vars_decrease_eqns x t tl;
unify (lsubst_eqns [x,t] tl) ((x,t)::s))
| (t, V x)::tl -> //flex-rhs
evars_permute_hd t (V x) tl;
unify ((V x, t)::tl) s
| (F t1 t2, F t1' t2')::tl -> //efuns reduces
evars_unfun t1 t2 t1' t2' tl;
unify ((t1, t1')::(t2, t2')::tl) s
(* All equations are solved when each one is just reflexive *)
val solved : eqns -> Tot bool
let rec solved = function
| [] -> true
| (x,y)::tl -> x=y && solved tl
val lsubst_distributes_over_F: l:list subst -> t1:term -> t2:term -> Lemma
(requires (True))
(ensures (lsubst_term l (F t1 t2) = F (lsubst_term l t1) (lsubst_term l t2)))
[SMTPat (lsubst_term l (F t1 t2))]
let rec lsubst_distributes_over_F l t1 t2 = match l with
| [] -> ()
| hd::tl -> lsubst_distributes_over_F tl t1 t2
let extend_subst s l = s::l
let extend_lsubst l l' = l @ l'
val lemma_extend_lsubst_distributes_term: l:list subst -> l':list subst -> e:term -> Lemma
(requires True)
(ensures (lsubst_term (extend_lsubst l l') e = lsubst_term l (lsubst_term l' e)))
let rec lemma_extend_lsubst_distributes_term l l' e = match l with
| [] -> ()
| hd::tl -> lemma_extend_lsubst_distributes_term tl l' e
val lemma_extend_lsubst_distributes_eqns: l:list subst -> l':list subst -> e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns (extend_lsubst l l') e = lsubst_eqns l (lsubst_eqns l' e)))
[SMTPat (lsubst_eqns (extend_lsubst l l') e)]
let rec lemma_extend_lsubst_distributes_eqns l l' e = match e with
| [] -> ()
| (t1, t2)::tl ->
lemma_extend_lsubst_distributes_term l l' t1;
lemma_extend_lsubst_distributes_term l l' t2;
lemma_extend_lsubst_distributes_eqns l l' tl
val lemma_subst_id: x:nat -> z:term -> y:term -> Lemma
(requires (not (occurs x y)))
(ensures (subst_term (x,z) y = y))
let rec lemma_subst_id x z y = match y with
| V x' -> ()
| F t1 t2 -> lemma_subst_id x z t1; lemma_subst_id x z t2
let neutral s (x, t) = subst_term s (V x) = V x && subst_term s t = t && ok (x, t)
let neutral_l l (x, t) = lsubst_term l (V x) = V x && lsubst_term l t = t && ok (x, t)
val lemma_lsubst_term_commutes: s:subst -> l:list subst -> e:term -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_term [s] (lsubst_term l (subst_term s e)) =
lsubst_term [s] (lsubst_term l e)))
let rec lemma_lsubst_term_commutes s l e = match e with
| V y -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2
val lemma_lsubst_eqns_commutes: s:subst -> l:list subst -> e:eqns -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_eqns [s] (lsubst_eqns l (lsubst_eqns [s] e)) =
lsubst_eqns [s] (lsubst_eqns l e)))
let rec lemma_lsubst_eqns_commutes s l = function
| [] -> ()
| (t1,t2)::tl -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2;
lemma_lsubst_eqns_commutes s l tl
let (++) l1 l2 = extend_lsubst l1 l2
let sub l e = lsubst_eqns l e
let test l1 l2 l3 = assert (l1 ++ l2 ++ l3 == (l1 ++ l2) ++ l3)
val key_lemma: x:nat -> y:term -> tl:eqns -> l:list subst -> lpre:list subst -> l'':list subst -> Lemma
(requires (l'' = lpre ++ ([x,y] ++ l)
/\ not (occurs x y)
/\ l `sub` ((V x, y)::tl) = (V x, y)::tl
/\ solved (l'' `sub` ([x, y] `sub` tl))))
(ensures (solved (l'' `sub` ((V x,y)::tl))))
let key_lemma x y tl l lpre l'' =
let xy = [x,y] in
let xyl = xy++l in
let vxy = V x, y in
assert (l'' `sub` (vxy::tl)
== lpre `sub` (xy `sub` (l `sub` (vxy::tl))));
lemma_lsubst_eqns_commutes (x,y) l (vxy :: tl);
assert (lpre `sub` (xy `sub` (l `sub` (vxy::tl)))
== lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl)))));
assert (lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl))))
== l'' `sub` (xy `sub` (vxy :: tl)));
lemma_subst_id x y y
val lemma_subst_term_idem: s:subst -> t:term -> Lemma
(requires (ok s))
(ensures (subst_term s (subst_term s t) = subst_term s t))
let rec lemma_subst_term_idem s t = match t with
| V x -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_subst_term_idem s t1; lemma_subst_term_idem s t2
val lemma_subst_eqns_idem: s:subst -> e:eqns -> Lemma
(requires (ok s))
(ensures (lsubst_eqns [s] (lsubst_eqns [s] e) = lsubst_eqns [s] e))
let rec lemma_subst_eqns_idem s = function
| [] -> ()
| (x, y)::tl -> lemma_subst_eqns_idem s tl;
lemma_subst_term_idem s x;
lemma_subst_term_idem s y
val subst_funs_monotone: s:subst -> t:term -> Lemma
(ensures (funs (subst_term s t) >= funs t))
let rec subst_funs_monotone s = function
| V x -> ()
| F t1 t2 -> subst_funs_monotone s t1; subst_funs_monotone s t2
val lsubst_funs_monotone: l:list subst -> t:term -> Lemma | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.OrdSet.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "Unification.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list Unification.subst -> t: Unification.term
-> FStar.Pervasives.Lemma
(ensures Unification.funs (Unification.lsubst_term l t) >= Unification.funs t) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"Unification.subst",
"Unification.term",
"Unification.subst_funs_monotone",
"Unification.lsubst_term",
"Prims.unit",
"Unification.lsubst_funs_monotone"
] | [
"recursion"
] | false | false | true | false | false | let rec lsubst_funs_monotone l t =
| match l with
| [] -> ()
| hd :: tl ->
lsubst_funs_monotone tl t;
subst_funs_monotone hd (lsubst_term tl t) | false |
Unification.fst | Unification.subst_funs_monotone | val subst_funs_monotone: s:subst -> t:term -> Lemma
(ensures (funs (subst_term s t) >= funs t)) | val subst_funs_monotone: s:subst -> t:term -> Lemma
(ensures (funs (subst_term s t) >= funs t)) | let rec subst_funs_monotone s = function
| V x -> ()
| F t1 t2 -> subst_funs_monotone s t1; subst_funs_monotone s t2 | {
"file_name": "examples/algorithms/Unification.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 65,
"end_line": 287,
"start_col": 0,
"start_line": 285
} | (*
Copyright 2008-2015 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(*
A verified implementation of first-order unification.
The termination argument is based on Ana Bove's Licentiate Thesis:
Programming in Martin-Löf Type Theory Unification - A non-trivial Example
Department of Computer Science, Chalmers University of Technology
1999
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.40.3532
But, is done here directly with lex orderings.
*)
module Unification
open FStar.List.Tot
(* First, a missing lemma from the list library *)
let op_At = append
val lemma_shift_append: #a:eqtype -> l:list a -> x:a -> m:list a -> Lemma
(ensures ( (l@(x::m)) = ((l@[x])@m)))
let rec lemma_shift_append #a l x m = match l with
| [] -> ()
| hd::tl -> lemma_shift_append tl x m
(* A term language with variables V and pairs F *)
type term =
| V : i:nat -> term
| F : t1:term -> t2:term -> term
(* Finite, ordered sets of variables *)
val nat_order : OrdSet.cmp nat
let nat_order :(f:(nat -> nat -> bool){OrdSet.total_order nat f}) = fun x y -> x <= y
type varset = OrdSet.ordset nat nat_order
val empty_vars : varset
let empty_vars = OrdSet.empty
(* Collecting the variables in a term *)
val vars : term -> Tot varset
let rec vars = function
| V i -> OrdSet.singleton i
| F t1 t2 -> OrdSet.union (vars t1) (vars t2)
(* Equations among pairs of terms, to be solved *)
type eqns = list (term * term)
(* All the variables in a set of equations *)
val evars : eqns -> Tot varset
let rec evars = function
| [] -> empty_vars
| (x, y)::tl -> OrdSet.union (OrdSet.union (vars x) (vars y)) (evars tl)
(* Counting variables; used in the termination metric *)
val n_evars : eqns -> Tot nat
let n_evars eqns = OrdSet.size (evars eqns)
(* Counting the number of F-applications; also for the termination metric *)
val funs : term -> Tot nat
let rec funs = function
| V _ -> 0
| F t1 t2 -> 1 + funs t1 + funs t2
val efuns : eqns -> Tot nat
let rec efuns = function
| [] -> 0
| (x,y)::tl -> funs x + funs y + efuns tl
(* Counting the number of equations with variables on the the RHS;
// also for the termination metric *)
val n_flex_rhs : eqns -> Tot nat
let rec n_flex_rhs = function
| [] -> 0
| (V _, V _)::tl
| (_ , V _)::tl -> 1 + n_flex_rhs tl
| _::tl -> n_flex_rhs tl
(* A point substitution *)
type subst = (nat * term)
(* Composition of point substitutions *)
type lsubst = list subst
val subst_term : subst -> term -> Tot term
let rec subst_term s t = match t with
| V x -> if x = fst s then snd s else V x
| F t1 t2 -> F (subst_term s t1) (subst_term s t2)
val lsubst_term : list subst -> term -> Tot term
let lsubst_term = fold_right subst_term
let occurs x t = OrdSet.mem x (vars t)
let ok s = not (occurs (fst s) (snd s))
val lsubst_eqns: list subst -> eqns -> Tot eqns
let rec lsubst_eqns l = function
| [] -> []
| (x,y)::tl -> (lsubst_term l x, lsubst_term l y)::lsubst_eqns l tl
val lemma_lsubst_eqns_nil: e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns [] e = e))
[SMTPat (lsubst_eqns [] e)]
let rec lemma_lsubst_eqns_nil = function
| [] -> ()
| _::tl -> lemma_lsubst_eqns_nil tl
(* A couple of lemmas about variable counts.
// Both of these rely on extensional equality of sets.
// So we need to use eq_lemma explicitly *)
val evars_permute_hd : x:term -> y:term -> tl:eqns -> Lemma
(ensures (evars ((x, y)::tl) = evars ((y, x)::tl)))
let evars_permute_hd x y tl = OrdSet.eq_lemma (evars ((x,y)::tl)) (evars ((y,x)::tl))
val evars_unfun : x:term -> y:term -> x':term -> y':term -> tl:eqns -> Lemma
(ensures (evars ((F x y, F x' y')::tl) = evars ((x, x')::(y, y')::tl)))
let evars_unfun x y x' y' tl = OrdSet.eq_lemma (evars ((F x y, F x' y')::tl)) (evars ((x, x')::(y, y')::tl))
(* Eliminating a variable reduces the variable count *)
val lemma_vars_decrease: s:subst -> t':term -> Lemma
(requires (ok s))
(ensures (OrdSet.subset (vars (subst_term s t'))
(OrdSet.remove (fst s) (OrdSet.union (vars (snd s)) (vars t')))))
let rec lemma_vars_decrease s t' = match t' with
| V x -> ()
| F t1 t2 ->
lemma_vars_decrease s t1;
lemma_vars_decrease s t2
(* Lifting the prior lemma to equations *)
val vars_decrease_eqns: x:nat -> t:term -> e:eqns -> Lemma
(requires (ok (x, t)))
(ensures (OrdSet.subset (evars (lsubst_eqns [x,t] e))
(OrdSet.remove x (evars ((V x, t)::e)))))
let rec vars_decrease_eqns x t e = match e with
| [] -> ()
| hd::tl -> lemma_vars_decrease (x,t) (fst hd);
lemma_vars_decrease (x,t) (snd hd);
vars_decrease_eqns x t tl
val unify : e:eqns -> list subst -> Tot (option (list subst))
(decreases %[n_evars e; efuns e; n_flex_rhs e])
let rec unify e s = match e with
| [] -> Some s
| (V x, t)::tl ->
if V? t && V?.i t = x
then unify tl s //t is a flex-rhs
else if occurs x t
then None
else (vars_decrease_eqns x t tl;
unify (lsubst_eqns [x,t] tl) ((x,t)::s))
| (t, V x)::tl -> //flex-rhs
evars_permute_hd t (V x) tl;
unify ((V x, t)::tl) s
| (F t1 t2, F t1' t2')::tl -> //efuns reduces
evars_unfun t1 t2 t1' t2' tl;
unify ((t1, t1')::(t2, t2')::tl) s
(* All equations are solved when each one is just reflexive *)
val solved : eqns -> Tot bool
let rec solved = function
| [] -> true
| (x,y)::tl -> x=y && solved tl
val lsubst_distributes_over_F: l:list subst -> t1:term -> t2:term -> Lemma
(requires (True))
(ensures (lsubst_term l (F t1 t2) = F (lsubst_term l t1) (lsubst_term l t2)))
[SMTPat (lsubst_term l (F t1 t2))]
let rec lsubst_distributes_over_F l t1 t2 = match l with
| [] -> ()
| hd::tl -> lsubst_distributes_over_F tl t1 t2
let extend_subst s l = s::l
let extend_lsubst l l' = l @ l'
val lemma_extend_lsubst_distributes_term: l:list subst -> l':list subst -> e:term -> Lemma
(requires True)
(ensures (lsubst_term (extend_lsubst l l') e = lsubst_term l (lsubst_term l' e)))
let rec lemma_extend_lsubst_distributes_term l l' e = match l with
| [] -> ()
| hd::tl -> lemma_extend_lsubst_distributes_term tl l' e
val lemma_extend_lsubst_distributes_eqns: l:list subst -> l':list subst -> e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns (extend_lsubst l l') e = lsubst_eqns l (lsubst_eqns l' e)))
[SMTPat (lsubst_eqns (extend_lsubst l l') e)]
let rec lemma_extend_lsubst_distributes_eqns l l' e = match e with
| [] -> ()
| (t1, t2)::tl ->
lemma_extend_lsubst_distributes_term l l' t1;
lemma_extend_lsubst_distributes_term l l' t2;
lemma_extend_lsubst_distributes_eqns l l' tl
val lemma_subst_id: x:nat -> z:term -> y:term -> Lemma
(requires (not (occurs x y)))
(ensures (subst_term (x,z) y = y))
let rec lemma_subst_id x z y = match y with
| V x' -> ()
| F t1 t2 -> lemma_subst_id x z t1; lemma_subst_id x z t2
let neutral s (x, t) = subst_term s (V x) = V x && subst_term s t = t && ok (x, t)
let neutral_l l (x, t) = lsubst_term l (V x) = V x && lsubst_term l t = t && ok (x, t)
val lemma_lsubst_term_commutes: s:subst -> l:list subst -> e:term -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_term [s] (lsubst_term l (subst_term s e)) =
lsubst_term [s] (lsubst_term l e)))
let rec lemma_lsubst_term_commutes s l e = match e with
| V y -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2
val lemma_lsubst_eqns_commutes: s:subst -> l:list subst -> e:eqns -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_eqns [s] (lsubst_eqns l (lsubst_eqns [s] e)) =
lsubst_eqns [s] (lsubst_eqns l e)))
let rec lemma_lsubst_eqns_commutes s l = function
| [] -> ()
| (t1,t2)::tl -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2;
lemma_lsubst_eqns_commutes s l tl
let (++) l1 l2 = extend_lsubst l1 l2
let sub l e = lsubst_eqns l e
let test l1 l2 l3 = assert (l1 ++ l2 ++ l3 == (l1 ++ l2) ++ l3)
val key_lemma: x:nat -> y:term -> tl:eqns -> l:list subst -> lpre:list subst -> l'':list subst -> Lemma
(requires (l'' = lpre ++ ([x,y] ++ l)
/\ not (occurs x y)
/\ l `sub` ((V x, y)::tl) = (V x, y)::tl
/\ solved (l'' `sub` ([x, y] `sub` tl))))
(ensures (solved (l'' `sub` ((V x,y)::tl))))
let key_lemma x y tl l lpre l'' =
let xy = [x,y] in
let xyl = xy++l in
let vxy = V x, y in
assert (l'' `sub` (vxy::tl)
== lpre `sub` (xy `sub` (l `sub` (vxy::tl))));
lemma_lsubst_eqns_commutes (x,y) l (vxy :: tl);
assert (lpre `sub` (xy `sub` (l `sub` (vxy::tl)))
== lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl)))));
assert (lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl))))
== l'' `sub` (xy `sub` (vxy :: tl)));
lemma_subst_id x y y
val lemma_subst_term_idem: s:subst -> t:term -> Lemma
(requires (ok s))
(ensures (subst_term s (subst_term s t) = subst_term s t))
let rec lemma_subst_term_idem s t = match t with
| V x -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_subst_term_idem s t1; lemma_subst_term_idem s t2
val lemma_subst_eqns_idem: s:subst -> e:eqns -> Lemma
(requires (ok s))
(ensures (lsubst_eqns [s] (lsubst_eqns [s] e) = lsubst_eqns [s] e))
let rec lemma_subst_eqns_idem s = function
| [] -> ()
| (x, y)::tl -> lemma_subst_eqns_idem s tl;
lemma_subst_term_idem s x;
lemma_subst_term_idem s y
val subst_funs_monotone: s:subst -> t:term -> Lemma | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.OrdSet.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "Unification.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | s: Unification.subst -> t: Unification.term
-> FStar.Pervasives.Lemma
(ensures Unification.funs (Unification.subst_term s t) >= Unification.funs t) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Unification.subst",
"Unification.term",
"Prims.nat",
"Unification.subst_funs_monotone",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec subst_funs_monotone s =
| function
| V x -> ()
| F t1 t2 ->
subst_funs_monotone s t1;
subst_funs_monotone s t2 | false |
Unification.fst | Unification.lemma_subst_idem | val lemma_subst_idem: l:list subst -> x:nat -> t:term -> t':term -> Lemma
(requires (lsubst_term l (V x) = lsubst_term l t))
(ensures (lsubst_term l (subst_term (x,t) t') = lsubst_term l t')) | val lemma_subst_idem: l:list subst -> x:nat -> t:term -> t':term -> Lemma
(requires (lsubst_term l (V x) = lsubst_term l t))
(ensures (lsubst_term l (subst_term (x,t) t') = lsubst_term l t')) | let rec lemma_subst_idem l x t t' = match t' with
| V y -> ()
| F t1 t2 -> lemma_subst_idem l x t t1; lemma_subst_idem l x t t2 | {
"file_name": "examples/algorithms/Unification.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 67,
"end_line": 325,
"start_col": 0,
"start_line": 323
} | (*
Copyright 2008-2015 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(*
A verified implementation of first-order unification.
The termination argument is based on Ana Bove's Licentiate Thesis:
Programming in Martin-Löf Type Theory Unification - A non-trivial Example
Department of Computer Science, Chalmers University of Technology
1999
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.40.3532
But, is done here directly with lex orderings.
*)
module Unification
open FStar.List.Tot
(* First, a missing lemma from the list library *)
let op_At = append
val lemma_shift_append: #a:eqtype -> l:list a -> x:a -> m:list a -> Lemma
(ensures ( (l@(x::m)) = ((l@[x])@m)))
let rec lemma_shift_append #a l x m = match l with
| [] -> ()
| hd::tl -> lemma_shift_append tl x m
(* A term language with variables V and pairs F *)
type term =
| V : i:nat -> term
| F : t1:term -> t2:term -> term
(* Finite, ordered sets of variables *)
val nat_order : OrdSet.cmp nat
let nat_order :(f:(nat -> nat -> bool){OrdSet.total_order nat f}) = fun x y -> x <= y
type varset = OrdSet.ordset nat nat_order
val empty_vars : varset
let empty_vars = OrdSet.empty
(* Collecting the variables in a term *)
val vars : term -> Tot varset
let rec vars = function
| V i -> OrdSet.singleton i
| F t1 t2 -> OrdSet.union (vars t1) (vars t2)
(* Equations among pairs of terms, to be solved *)
type eqns = list (term * term)
(* All the variables in a set of equations *)
val evars : eqns -> Tot varset
let rec evars = function
| [] -> empty_vars
| (x, y)::tl -> OrdSet.union (OrdSet.union (vars x) (vars y)) (evars tl)
(* Counting variables; used in the termination metric *)
val n_evars : eqns -> Tot nat
let n_evars eqns = OrdSet.size (evars eqns)
(* Counting the number of F-applications; also for the termination metric *)
val funs : term -> Tot nat
let rec funs = function
| V _ -> 0
| F t1 t2 -> 1 + funs t1 + funs t2
val efuns : eqns -> Tot nat
let rec efuns = function
| [] -> 0
| (x,y)::tl -> funs x + funs y + efuns tl
(* Counting the number of equations with variables on the the RHS;
// also for the termination metric *)
val n_flex_rhs : eqns -> Tot nat
let rec n_flex_rhs = function
| [] -> 0
| (V _, V _)::tl
| (_ , V _)::tl -> 1 + n_flex_rhs tl
| _::tl -> n_flex_rhs tl
(* A point substitution *)
type subst = (nat * term)
(* Composition of point substitutions *)
type lsubst = list subst
val subst_term : subst -> term -> Tot term
let rec subst_term s t = match t with
| V x -> if x = fst s then snd s else V x
| F t1 t2 -> F (subst_term s t1) (subst_term s t2)
val lsubst_term : list subst -> term -> Tot term
let lsubst_term = fold_right subst_term
let occurs x t = OrdSet.mem x (vars t)
let ok s = not (occurs (fst s) (snd s))
val lsubst_eqns: list subst -> eqns -> Tot eqns
let rec lsubst_eqns l = function
| [] -> []
| (x,y)::tl -> (lsubst_term l x, lsubst_term l y)::lsubst_eqns l tl
val lemma_lsubst_eqns_nil: e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns [] e = e))
[SMTPat (lsubst_eqns [] e)]
let rec lemma_lsubst_eqns_nil = function
| [] -> ()
| _::tl -> lemma_lsubst_eqns_nil tl
(* A couple of lemmas about variable counts.
// Both of these rely on extensional equality of sets.
// So we need to use eq_lemma explicitly *)
val evars_permute_hd : x:term -> y:term -> tl:eqns -> Lemma
(ensures (evars ((x, y)::tl) = evars ((y, x)::tl)))
let evars_permute_hd x y tl = OrdSet.eq_lemma (evars ((x,y)::tl)) (evars ((y,x)::tl))
val evars_unfun : x:term -> y:term -> x':term -> y':term -> tl:eqns -> Lemma
(ensures (evars ((F x y, F x' y')::tl) = evars ((x, x')::(y, y')::tl)))
let evars_unfun x y x' y' tl = OrdSet.eq_lemma (evars ((F x y, F x' y')::tl)) (evars ((x, x')::(y, y')::tl))
(* Eliminating a variable reduces the variable count *)
val lemma_vars_decrease: s:subst -> t':term -> Lemma
(requires (ok s))
(ensures (OrdSet.subset (vars (subst_term s t'))
(OrdSet.remove (fst s) (OrdSet.union (vars (snd s)) (vars t')))))
let rec lemma_vars_decrease s t' = match t' with
| V x -> ()
| F t1 t2 ->
lemma_vars_decrease s t1;
lemma_vars_decrease s t2
(* Lifting the prior lemma to equations *)
val vars_decrease_eqns: x:nat -> t:term -> e:eqns -> Lemma
(requires (ok (x, t)))
(ensures (OrdSet.subset (evars (lsubst_eqns [x,t] e))
(OrdSet.remove x (evars ((V x, t)::e)))))
let rec vars_decrease_eqns x t e = match e with
| [] -> ()
| hd::tl -> lemma_vars_decrease (x,t) (fst hd);
lemma_vars_decrease (x,t) (snd hd);
vars_decrease_eqns x t tl
val unify : e:eqns -> list subst -> Tot (option (list subst))
(decreases %[n_evars e; efuns e; n_flex_rhs e])
let rec unify e s = match e with
| [] -> Some s
| (V x, t)::tl ->
if V? t && V?.i t = x
then unify tl s //t is a flex-rhs
else if occurs x t
then None
else (vars_decrease_eqns x t tl;
unify (lsubst_eqns [x,t] tl) ((x,t)::s))
| (t, V x)::tl -> //flex-rhs
evars_permute_hd t (V x) tl;
unify ((V x, t)::tl) s
| (F t1 t2, F t1' t2')::tl -> //efuns reduces
evars_unfun t1 t2 t1' t2' tl;
unify ((t1, t1')::(t2, t2')::tl) s
(* All equations are solved when each one is just reflexive *)
val solved : eqns -> Tot bool
let rec solved = function
| [] -> true
| (x,y)::tl -> x=y && solved tl
val lsubst_distributes_over_F: l:list subst -> t1:term -> t2:term -> Lemma
(requires (True))
(ensures (lsubst_term l (F t1 t2) = F (lsubst_term l t1) (lsubst_term l t2)))
[SMTPat (lsubst_term l (F t1 t2))]
let rec lsubst_distributes_over_F l t1 t2 = match l with
| [] -> ()
| hd::tl -> lsubst_distributes_over_F tl t1 t2
let extend_subst s l = s::l
let extend_lsubst l l' = l @ l'
val lemma_extend_lsubst_distributes_term: l:list subst -> l':list subst -> e:term -> Lemma
(requires True)
(ensures (lsubst_term (extend_lsubst l l') e = lsubst_term l (lsubst_term l' e)))
let rec lemma_extend_lsubst_distributes_term l l' e = match l with
| [] -> ()
| hd::tl -> lemma_extend_lsubst_distributes_term tl l' e
val lemma_extend_lsubst_distributes_eqns: l:list subst -> l':list subst -> e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns (extend_lsubst l l') e = lsubst_eqns l (lsubst_eqns l' e)))
[SMTPat (lsubst_eqns (extend_lsubst l l') e)]
let rec lemma_extend_lsubst_distributes_eqns l l' e = match e with
| [] -> ()
| (t1, t2)::tl ->
lemma_extend_lsubst_distributes_term l l' t1;
lemma_extend_lsubst_distributes_term l l' t2;
lemma_extend_lsubst_distributes_eqns l l' tl
val lemma_subst_id: x:nat -> z:term -> y:term -> Lemma
(requires (not (occurs x y)))
(ensures (subst_term (x,z) y = y))
let rec lemma_subst_id x z y = match y with
| V x' -> ()
| F t1 t2 -> lemma_subst_id x z t1; lemma_subst_id x z t2
let neutral s (x, t) = subst_term s (V x) = V x && subst_term s t = t && ok (x, t)
let neutral_l l (x, t) = lsubst_term l (V x) = V x && lsubst_term l t = t && ok (x, t)
val lemma_lsubst_term_commutes: s:subst -> l:list subst -> e:term -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_term [s] (lsubst_term l (subst_term s e)) =
lsubst_term [s] (lsubst_term l e)))
let rec lemma_lsubst_term_commutes s l e = match e with
| V y -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2
val lemma_lsubst_eqns_commutes: s:subst -> l:list subst -> e:eqns -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_eqns [s] (lsubst_eqns l (lsubst_eqns [s] e)) =
lsubst_eqns [s] (lsubst_eqns l e)))
let rec lemma_lsubst_eqns_commutes s l = function
| [] -> ()
| (t1,t2)::tl -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2;
lemma_lsubst_eqns_commutes s l tl
let (++) l1 l2 = extend_lsubst l1 l2
let sub l e = lsubst_eqns l e
let test l1 l2 l3 = assert (l1 ++ l2 ++ l3 == (l1 ++ l2) ++ l3)
val key_lemma: x:nat -> y:term -> tl:eqns -> l:list subst -> lpre:list subst -> l'':list subst -> Lemma
(requires (l'' = lpre ++ ([x,y] ++ l)
/\ not (occurs x y)
/\ l `sub` ((V x, y)::tl) = (V x, y)::tl
/\ solved (l'' `sub` ([x, y] `sub` tl))))
(ensures (solved (l'' `sub` ((V x,y)::tl))))
let key_lemma x y tl l lpre l'' =
let xy = [x,y] in
let xyl = xy++l in
let vxy = V x, y in
assert (l'' `sub` (vxy::tl)
== lpre `sub` (xy `sub` (l `sub` (vxy::tl))));
lemma_lsubst_eqns_commutes (x,y) l (vxy :: tl);
assert (lpre `sub` (xy `sub` (l `sub` (vxy::tl)))
== lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl)))));
assert (lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl))))
== l'' `sub` (xy `sub` (vxy :: tl)));
lemma_subst_id x y y
val lemma_subst_term_idem: s:subst -> t:term -> Lemma
(requires (ok s))
(ensures (subst_term s (subst_term s t) = subst_term s t))
let rec lemma_subst_term_idem s t = match t with
| V x -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_subst_term_idem s t1; lemma_subst_term_idem s t2
val lemma_subst_eqns_idem: s:subst -> e:eqns -> Lemma
(requires (ok s))
(ensures (lsubst_eqns [s] (lsubst_eqns [s] e) = lsubst_eqns [s] e))
let rec lemma_subst_eqns_idem s = function
| [] -> ()
| (x, y)::tl -> lemma_subst_eqns_idem s tl;
lemma_subst_term_idem s x;
lemma_subst_term_idem s y
val subst_funs_monotone: s:subst -> t:term -> Lemma
(ensures (funs (subst_term s t) >= funs t))
let rec subst_funs_monotone s = function
| V x -> ()
| F t1 t2 -> subst_funs_monotone s t1; subst_funs_monotone s t2
val lsubst_funs_monotone: l:list subst -> t:term -> Lemma
(ensures (funs (lsubst_term l t) >= funs t))
let rec lsubst_funs_monotone l t = match l with
| [] -> ()
| hd::tl ->
lsubst_funs_monotone tl t; subst_funs_monotone hd (lsubst_term tl t)
val lemma_occurs_not_solveable_aux: x:nat -> t:term{occurs x t /\ not (V? t)} -> s:list subst -> Lemma
(funs (lsubst_term s t) >= (funs t + funs (lsubst_term s (V x))))
let rec lemma_occurs_not_solveable_aux x t s = match t with
| F t1 t2 ->
if occurs x t1
then let _ = lsubst_funs_monotone s t2 in
match t1 with
| V y -> ()
| _ -> lemma_occurs_not_solveable_aux x t1 s
else if occurs x t2
then let _ = lsubst_funs_monotone s t1 in
match t2 with
| V y -> ()
| _ -> lemma_occurs_not_solveable_aux x t2 s
else ()
type not_solveable s =
forall l. lsubst_term l (fst s) <> lsubst_term l (snd s)
val lemma_occurs_not_solveable: x:nat -> t:term -> Lemma
(requires (occurs x t /\ not (V? t)))
(ensures (not_solveable (V x, t)))
let lemma_occurs_not_solveable x t = FStar.Classical.forall_intro (lemma_occurs_not_solveable_aux x t)
val lemma_subst_idem: l:list subst -> x:nat -> t:term -> t':term -> Lemma
(requires (lsubst_term l (V x) = lsubst_term l t)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.OrdSet.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "Unification.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list Unification.subst -> x: Prims.nat -> t: Unification.term -> t': Unification.term
-> FStar.Pervasives.Lemma
(requires Unification.lsubst_term l (Unification.V x) = Unification.lsubst_term l t)
(ensures
Unification.lsubst_term l (Unification.subst_term (x, t) t') = Unification.lsubst_term l t') | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"Unification.subst",
"Prims.nat",
"Unification.term",
"Unification.lemma_subst_idem",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec lemma_subst_idem l x t t' =
| match t' with
| V y -> ()
| F t1 t2 ->
lemma_subst_idem l x t t1;
lemma_subst_idem l x t t2 | false |
Unification.fst | Unification.lemma_subst_eqns_idem | val lemma_subst_eqns_idem: s:subst -> e:eqns -> Lemma
(requires (ok s))
(ensures (lsubst_eqns [s] (lsubst_eqns [s] e) = lsubst_eqns [s] e)) | val lemma_subst_eqns_idem: s:subst -> e:eqns -> Lemma
(requires (ok s))
(ensures (lsubst_eqns [s] (lsubst_eqns [s] e) = lsubst_eqns [s] e)) | let rec lemma_subst_eqns_idem s = function
| [] -> ()
| (x, y)::tl -> lemma_subst_eqns_idem s tl;
lemma_subst_term_idem s x;
lemma_subst_term_idem s y | {
"file_name": "examples/algorithms/Unification.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 34,
"end_line": 281,
"start_col": 0,
"start_line": 277
} | (*
Copyright 2008-2015 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(*
A verified implementation of first-order unification.
The termination argument is based on Ana Bove's Licentiate Thesis:
Programming in Martin-Löf Type Theory Unification - A non-trivial Example
Department of Computer Science, Chalmers University of Technology
1999
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.40.3532
But, is done here directly with lex orderings.
*)
module Unification
open FStar.List.Tot
(* First, a missing lemma from the list library *)
let op_At = append
val lemma_shift_append: #a:eqtype -> l:list a -> x:a -> m:list a -> Lemma
(ensures ( (l@(x::m)) = ((l@[x])@m)))
let rec lemma_shift_append #a l x m = match l with
| [] -> ()
| hd::tl -> lemma_shift_append tl x m
(* A term language with variables V and pairs F *)
type term =
| V : i:nat -> term
| F : t1:term -> t2:term -> term
(* Finite, ordered sets of variables *)
val nat_order : OrdSet.cmp nat
let nat_order :(f:(nat -> nat -> bool){OrdSet.total_order nat f}) = fun x y -> x <= y
type varset = OrdSet.ordset nat nat_order
val empty_vars : varset
let empty_vars = OrdSet.empty
(* Collecting the variables in a term *)
val vars : term -> Tot varset
let rec vars = function
| V i -> OrdSet.singleton i
| F t1 t2 -> OrdSet.union (vars t1) (vars t2)
(* Equations among pairs of terms, to be solved *)
type eqns = list (term * term)
(* All the variables in a set of equations *)
val evars : eqns -> Tot varset
let rec evars = function
| [] -> empty_vars
| (x, y)::tl -> OrdSet.union (OrdSet.union (vars x) (vars y)) (evars tl)
(* Counting variables; used in the termination metric *)
val n_evars : eqns -> Tot nat
let n_evars eqns = OrdSet.size (evars eqns)
(* Counting the number of F-applications; also for the termination metric *)
val funs : term -> Tot nat
let rec funs = function
| V _ -> 0
| F t1 t2 -> 1 + funs t1 + funs t2
val efuns : eqns -> Tot nat
let rec efuns = function
| [] -> 0
| (x,y)::tl -> funs x + funs y + efuns tl
(* Counting the number of equations with variables on the the RHS;
// also for the termination metric *)
val n_flex_rhs : eqns -> Tot nat
let rec n_flex_rhs = function
| [] -> 0
| (V _, V _)::tl
| (_ , V _)::tl -> 1 + n_flex_rhs tl
| _::tl -> n_flex_rhs tl
(* A point substitution *)
type subst = (nat * term)
(* Composition of point substitutions *)
type lsubst = list subst
val subst_term : subst -> term -> Tot term
let rec subst_term s t = match t with
| V x -> if x = fst s then snd s else V x
| F t1 t2 -> F (subst_term s t1) (subst_term s t2)
val lsubst_term : list subst -> term -> Tot term
let lsubst_term = fold_right subst_term
let occurs x t = OrdSet.mem x (vars t)
let ok s = not (occurs (fst s) (snd s))
val lsubst_eqns: list subst -> eqns -> Tot eqns
let rec lsubst_eqns l = function
| [] -> []
| (x,y)::tl -> (lsubst_term l x, lsubst_term l y)::lsubst_eqns l tl
val lemma_lsubst_eqns_nil: e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns [] e = e))
[SMTPat (lsubst_eqns [] e)]
let rec lemma_lsubst_eqns_nil = function
| [] -> ()
| _::tl -> lemma_lsubst_eqns_nil tl
(* A couple of lemmas about variable counts.
// Both of these rely on extensional equality of sets.
// So we need to use eq_lemma explicitly *)
val evars_permute_hd : x:term -> y:term -> tl:eqns -> Lemma
(ensures (evars ((x, y)::tl) = evars ((y, x)::tl)))
let evars_permute_hd x y tl = OrdSet.eq_lemma (evars ((x,y)::tl)) (evars ((y,x)::tl))
val evars_unfun : x:term -> y:term -> x':term -> y':term -> tl:eqns -> Lemma
(ensures (evars ((F x y, F x' y')::tl) = evars ((x, x')::(y, y')::tl)))
let evars_unfun x y x' y' tl = OrdSet.eq_lemma (evars ((F x y, F x' y')::tl)) (evars ((x, x')::(y, y')::tl))
(* Eliminating a variable reduces the variable count *)
val lemma_vars_decrease: s:subst -> t':term -> Lemma
(requires (ok s))
(ensures (OrdSet.subset (vars (subst_term s t'))
(OrdSet.remove (fst s) (OrdSet.union (vars (snd s)) (vars t')))))
let rec lemma_vars_decrease s t' = match t' with
| V x -> ()
| F t1 t2 ->
lemma_vars_decrease s t1;
lemma_vars_decrease s t2
(* Lifting the prior lemma to equations *)
val vars_decrease_eqns: x:nat -> t:term -> e:eqns -> Lemma
(requires (ok (x, t)))
(ensures (OrdSet.subset (evars (lsubst_eqns [x,t] e))
(OrdSet.remove x (evars ((V x, t)::e)))))
let rec vars_decrease_eqns x t e = match e with
| [] -> ()
| hd::tl -> lemma_vars_decrease (x,t) (fst hd);
lemma_vars_decrease (x,t) (snd hd);
vars_decrease_eqns x t tl
val unify : e:eqns -> list subst -> Tot (option (list subst))
(decreases %[n_evars e; efuns e; n_flex_rhs e])
let rec unify e s = match e with
| [] -> Some s
| (V x, t)::tl ->
if V? t && V?.i t = x
then unify tl s //t is a flex-rhs
else if occurs x t
then None
else (vars_decrease_eqns x t tl;
unify (lsubst_eqns [x,t] tl) ((x,t)::s))
| (t, V x)::tl -> //flex-rhs
evars_permute_hd t (V x) tl;
unify ((V x, t)::tl) s
| (F t1 t2, F t1' t2')::tl -> //efuns reduces
evars_unfun t1 t2 t1' t2' tl;
unify ((t1, t1')::(t2, t2')::tl) s
(* All equations are solved when each one is just reflexive *)
val solved : eqns -> Tot bool
let rec solved = function
| [] -> true
| (x,y)::tl -> x=y && solved tl
val lsubst_distributes_over_F: l:list subst -> t1:term -> t2:term -> Lemma
(requires (True))
(ensures (lsubst_term l (F t1 t2) = F (lsubst_term l t1) (lsubst_term l t2)))
[SMTPat (lsubst_term l (F t1 t2))]
let rec lsubst_distributes_over_F l t1 t2 = match l with
| [] -> ()
| hd::tl -> lsubst_distributes_over_F tl t1 t2
let extend_subst s l = s::l
let extend_lsubst l l' = l @ l'
val lemma_extend_lsubst_distributes_term: l:list subst -> l':list subst -> e:term -> Lemma
(requires True)
(ensures (lsubst_term (extend_lsubst l l') e = lsubst_term l (lsubst_term l' e)))
let rec lemma_extend_lsubst_distributes_term l l' e = match l with
| [] -> ()
| hd::tl -> lemma_extend_lsubst_distributes_term tl l' e
val lemma_extend_lsubst_distributes_eqns: l:list subst -> l':list subst -> e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns (extend_lsubst l l') e = lsubst_eqns l (lsubst_eqns l' e)))
[SMTPat (lsubst_eqns (extend_lsubst l l') e)]
let rec lemma_extend_lsubst_distributes_eqns l l' e = match e with
| [] -> ()
| (t1, t2)::tl ->
lemma_extend_lsubst_distributes_term l l' t1;
lemma_extend_lsubst_distributes_term l l' t2;
lemma_extend_lsubst_distributes_eqns l l' tl
val lemma_subst_id: x:nat -> z:term -> y:term -> Lemma
(requires (not (occurs x y)))
(ensures (subst_term (x,z) y = y))
let rec lemma_subst_id x z y = match y with
| V x' -> ()
| F t1 t2 -> lemma_subst_id x z t1; lemma_subst_id x z t2
let neutral s (x, t) = subst_term s (V x) = V x && subst_term s t = t && ok (x, t)
let neutral_l l (x, t) = lsubst_term l (V x) = V x && lsubst_term l t = t && ok (x, t)
val lemma_lsubst_term_commutes: s:subst -> l:list subst -> e:term -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_term [s] (lsubst_term l (subst_term s e)) =
lsubst_term [s] (lsubst_term l e)))
let rec lemma_lsubst_term_commutes s l e = match e with
| V y -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2
val lemma_lsubst_eqns_commutes: s:subst -> l:list subst -> e:eqns -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_eqns [s] (lsubst_eqns l (lsubst_eqns [s] e)) =
lsubst_eqns [s] (lsubst_eqns l e)))
let rec lemma_lsubst_eqns_commutes s l = function
| [] -> ()
| (t1,t2)::tl -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2;
lemma_lsubst_eqns_commutes s l tl
let (++) l1 l2 = extend_lsubst l1 l2
let sub l e = lsubst_eqns l e
let test l1 l2 l3 = assert (l1 ++ l2 ++ l3 == (l1 ++ l2) ++ l3)
val key_lemma: x:nat -> y:term -> tl:eqns -> l:list subst -> lpre:list subst -> l'':list subst -> Lemma
(requires (l'' = lpre ++ ([x,y] ++ l)
/\ not (occurs x y)
/\ l `sub` ((V x, y)::tl) = (V x, y)::tl
/\ solved (l'' `sub` ([x, y] `sub` tl))))
(ensures (solved (l'' `sub` ((V x,y)::tl))))
let key_lemma x y tl l lpre l'' =
let xy = [x,y] in
let xyl = xy++l in
let vxy = V x, y in
assert (l'' `sub` (vxy::tl)
== lpre `sub` (xy `sub` (l `sub` (vxy::tl))));
lemma_lsubst_eqns_commutes (x,y) l (vxy :: tl);
assert (lpre `sub` (xy `sub` (l `sub` (vxy::tl)))
== lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl)))));
assert (lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl))))
== l'' `sub` (xy `sub` (vxy :: tl)));
lemma_subst_id x y y
val lemma_subst_term_idem: s:subst -> t:term -> Lemma
(requires (ok s))
(ensures (subst_term s (subst_term s t) = subst_term s t))
let rec lemma_subst_term_idem s t = match t with
| V x -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_subst_term_idem s t1; lemma_subst_term_idem s t2
val lemma_subst_eqns_idem: s:subst -> e:eqns -> Lemma
(requires (ok s)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.OrdSet.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "Unification.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | s: Unification.subst -> e: Unification.eqns
-> FStar.Pervasives.Lemma (requires Unification.ok s)
(ensures
Unification.lsubst_eqns [s] (Unification.lsubst_eqns [s] e) = Unification.lsubst_eqns [s] e) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Unification.subst",
"Unification.eqns",
"Unification.term",
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"Unification.lemma_subst_term_idem",
"Prims.unit",
"Unification.lemma_subst_eqns_idem"
] | [
"recursion"
] | false | false | true | false | false | let rec lemma_subst_eqns_idem s =
| function
| [] -> ()
| (x, y) :: tl ->
lemma_subst_eqns_idem s tl;
lemma_subst_term_idem s x;
lemma_subst_term_idem s y | false |
Unification.fst | Unification.lemma_occurs_not_solveable | val lemma_occurs_not_solveable: x:nat -> t:term -> Lemma
(requires (occurs x t /\ not (V? t)))
(ensures (not_solveable (V x, t))) | val lemma_occurs_not_solveable: x:nat -> t:term -> Lemma
(requires (occurs x t /\ not (V? t)))
(ensures (not_solveable (V x, t))) | let lemma_occurs_not_solveable x t = FStar.Classical.forall_intro (lemma_occurs_not_solveable_aux x t) | {
"file_name": "examples/algorithms/Unification.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 102,
"end_line": 318,
"start_col": 0,
"start_line": 318
} | (*
Copyright 2008-2015 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(*
A verified implementation of first-order unification.
The termination argument is based on Ana Bove's Licentiate Thesis:
Programming in Martin-Löf Type Theory Unification - A non-trivial Example
Department of Computer Science, Chalmers University of Technology
1999
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.40.3532
But, is done here directly with lex orderings.
*)
module Unification
open FStar.List.Tot
(* First, a missing lemma from the list library *)
let op_At = append
val lemma_shift_append: #a:eqtype -> l:list a -> x:a -> m:list a -> Lemma
(ensures ( (l@(x::m)) = ((l@[x])@m)))
let rec lemma_shift_append #a l x m = match l with
| [] -> ()
| hd::tl -> lemma_shift_append tl x m
(* A term language with variables V and pairs F *)
type term =
| V : i:nat -> term
| F : t1:term -> t2:term -> term
(* Finite, ordered sets of variables *)
val nat_order : OrdSet.cmp nat
let nat_order :(f:(nat -> nat -> bool){OrdSet.total_order nat f}) = fun x y -> x <= y
type varset = OrdSet.ordset nat nat_order
val empty_vars : varset
let empty_vars = OrdSet.empty
(* Collecting the variables in a term *)
val vars : term -> Tot varset
let rec vars = function
| V i -> OrdSet.singleton i
| F t1 t2 -> OrdSet.union (vars t1) (vars t2)
(* Equations among pairs of terms, to be solved *)
type eqns = list (term * term)
(* All the variables in a set of equations *)
val evars : eqns -> Tot varset
let rec evars = function
| [] -> empty_vars
| (x, y)::tl -> OrdSet.union (OrdSet.union (vars x) (vars y)) (evars tl)
(* Counting variables; used in the termination metric *)
val n_evars : eqns -> Tot nat
let n_evars eqns = OrdSet.size (evars eqns)
(* Counting the number of F-applications; also for the termination metric *)
val funs : term -> Tot nat
let rec funs = function
| V _ -> 0
| F t1 t2 -> 1 + funs t1 + funs t2
val efuns : eqns -> Tot nat
let rec efuns = function
| [] -> 0
| (x,y)::tl -> funs x + funs y + efuns tl
(* Counting the number of equations with variables on the the RHS;
// also for the termination metric *)
val n_flex_rhs : eqns -> Tot nat
let rec n_flex_rhs = function
| [] -> 0
| (V _, V _)::tl
| (_ , V _)::tl -> 1 + n_flex_rhs tl
| _::tl -> n_flex_rhs tl
(* A point substitution *)
type subst = (nat * term)
(* Composition of point substitutions *)
type lsubst = list subst
val subst_term : subst -> term -> Tot term
let rec subst_term s t = match t with
| V x -> if x = fst s then snd s else V x
| F t1 t2 -> F (subst_term s t1) (subst_term s t2)
val lsubst_term : list subst -> term -> Tot term
let lsubst_term = fold_right subst_term
let occurs x t = OrdSet.mem x (vars t)
let ok s = not (occurs (fst s) (snd s))
val lsubst_eqns: list subst -> eqns -> Tot eqns
let rec lsubst_eqns l = function
| [] -> []
| (x,y)::tl -> (lsubst_term l x, lsubst_term l y)::lsubst_eqns l tl
val lemma_lsubst_eqns_nil: e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns [] e = e))
[SMTPat (lsubst_eqns [] e)]
let rec lemma_lsubst_eqns_nil = function
| [] -> ()
| _::tl -> lemma_lsubst_eqns_nil tl
(* A couple of lemmas about variable counts.
// Both of these rely on extensional equality of sets.
// So we need to use eq_lemma explicitly *)
val evars_permute_hd : x:term -> y:term -> tl:eqns -> Lemma
(ensures (evars ((x, y)::tl) = evars ((y, x)::tl)))
let evars_permute_hd x y tl = OrdSet.eq_lemma (evars ((x,y)::tl)) (evars ((y,x)::tl))
val evars_unfun : x:term -> y:term -> x':term -> y':term -> tl:eqns -> Lemma
(ensures (evars ((F x y, F x' y')::tl) = evars ((x, x')::(y, y')::tl)))
let evars_unfun x y x' y' tl = OrdSet.eq_lemma (evars ((F x y, F x' y')::tl)) (evars ((x, x')::(y, y')::tl))
(* Eliminating a variable reduces the variable count *)
val lemma_vars_decrease: s:subst -> t':term -> Lemma
(requires (ok s))
(ensures (OrdSet.subset (vars (subst_term s t'))
(OrdSet.remove (fst s) (OrdSet.union (vars (snd s)) (vars t')))))
let rec lemma_vars_decrease s t' = match t' with
| V x -> ()
| F t1 t2 ->
lemma_vars_decrease s t1;
lemma_vars_decrease s t2
(* Lifting the prior lemma to equations *)
val vars_decrease_eqns: x:nat -> t:term -> e:eqns -> Lemma
(requires (ok (x, t)))
(ensures (OrdSet.subset (evars (lsubst_eqns [x,t] e))
(OrdSet.remove x (evars ((V x, t)::e)))))
let rec vars_decrease_eqns x t e = match e with
| [] -> ()
| hd::tl -> lemma_vars_decrease (x,t) (fst hd);
lemma_vars_decrease (x,t) (snd hd);
vars_decrease_eqns x t tl
val unify : e:eqns -> list subst -> Tot (option (list subst))
(decreases %[n_evars e; efuns e; n_flex_rhs e])
let rec unify e s = match e with
| [] -> Some s
| (V x, t)::tl ->
if V? t && V?.i t = x
then unify tl s //t is a flex-rhs
else if occurs x t
then None
else (vars_decrease_eqns x t tl;
unify (lsubst_eqns [x,t] tl) ((x,t)::s))
| (t, V x)::tl -> //flex-rhs
evars_permute_hd t (V x) tl;
unify ((V x, t)::tl) s
| (F t1 t2, F t1' t2')::tl -> //efuns reduces
evars_unfun t1 t2 t1' t2' tl;
unify ((t1, t1')::(t2, t2')::tl) s
(* All equations are solved when each one is just reflexive *)
val solved : eqns -> Tot bool
let rec solved = function
| [] -> true
| (x,y)::tl -> x=y && solved tl
val lsubst_distributes_over_F: l:list subst -> t1:term -> t2:term -> Lemma
(requires (True))
(ensures (lsubst_term l (F t1 t2) = F (lsubst_term l t1) (lsubst_term l t2)))
[SMTPat (lsubst_term l (F t1 t2))]
let rec lsubst_distributes_over_F l t1 t2 = match l with
| [] -> ()
| hd::tl -> lsubst_distributes_over_F tl t1 t2
let extend_subst s l = s::l
let extend_lsubst l l' = l @ l'
val lemma_extend_lsubst_distributes_term: l:list subst -> l':list subst -> e:term -> Lemma
(requires True)
(ensures (lsubst_term (extend_lsubst l l') e = lsubst_term l (lsubst_term l' e)))
let rec lemma_extend_lsubst_distributes_term l l' e = match l with
| [] -> ()
| hd::tl -> lemma_extend_lsubst_distributes_term tl l' e
val lemma_extend_lsubst_distributes_eqns: l:list subst -> l':list subst -> e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns (extend_lsubst l l') e = lsubst_eqns l (lsubst_eqns l' e)))
[SMTPat (lsubst_eqns (extend_lsubst l l') e)]
let rec lemma_extend_lsubst_distributes_eqns l l' e = match e with
| [] -> ()
| (t1, t2)::tl ->
lemma_extend_lsubst_distributes_term l l' t1;
lemma_extend_lsubst_distributes_term l l' t2;
lemma_extend_lsubst_distributes_eqns l l' tl
val lemma_subst_id: x:nat -> z:term -> y:term -> Lemma
(requires (not (occurs x y)))
(ensures (subst_term (x,z) y = y))
let rec lemma_subst_id x z y = match y with
| V x' -> ()
| F t1 t2 -> lemma_subst_id x z t1; lemma_subst_id x z t2
let neutral s (x, t) = subst_term s (V x) = V x && subst_term s t = t && ok (x, t)
let neutral_l l (x, t) = lsubst_term l (V x) = V x && lsubst_term l t = t && ok (x, t)
val lemma_lsubst_term_commutes: s:subst -> l:list subst -> e:term -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_term [s] (lsubst_term l (subst_term s e)) =
lsubst_term [s] (lsubst_term l e)))
let rec lemma_lsubst_term_commutes s l e = match e with
| V y -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2
val lemma_lsubst_eqns_commutes: s:subst -> l:list subst -> e:eqns -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_eqns [s] (lsubst_eqns l (lsubst_eqns [s] e)) =
lsubst_eqns [s] (lsubst_eqns l e)))
let rec lemma_lsubst_eqns_commutes s l = function
| [] -> ()
| (t1,t2)::tl -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2;
lemma_lsubst_eqns_commutes s l tl
let (++) l1 l2 = extend_lsubst l1 l2
let sub l e = lsubst_eqns l e
let test l1 l2 l3 = assert (l1 ++ l2 ++ l3 == (l1 ++ l2) ++ l3)
val key_lemma: x:nat -> y:term -> tl:eqns -> l:list subst -> lpre:list subst -> l'':list subst -> Lemma
(requires (l'' = lpre ++ ([x,y] ++ l)
/\ not (occurs x y)
/\ l `sub` ((V x, y)::tl) = (V x, y)::tl
/\ solved (l'' `sub` ([x, y] `sub` tl))))
(ensures (solved (l'' `sub` ((V x,y)::tl))))
let key_lemma x y tl l lpre l'' =
let xy = [x,y] in
let xyl = xy++l in
let vxy = V x, y in
assert (l'' `sub` (vxy::tl)
== lpre `sub` (xy `sub` (l `sub` (vxy::tl))));
lemma_lsubst_eqns_commutes (x,y) l (vxy :: tl);
assert (lpre `sub` (xy `sub` (l `sub` (vxy::tl)))
== lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl)))));
assert (lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl))))
== l'' `sub` (xy `sub` (vxy :: tl)));
lemma_subst_id x y y
val lemma_subst_term_idem: s:subst -> t:term -> Lemma
(requires (ok s))
(ensures (subst_term s (subst_term s t) = subst_term s t))
let rec lemma_subst_term_idem s t = match t with
| V x -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_subst_term_idem s t1; lemma_subst_term_idem s t2
val lemma_subst_eqns_idem: s:subst -> e:eqns -> Lemma
(requires (ok s))
(ensures (lsubst_eqns [s] (lsubst_eqns [s] e) = lsubst_eqns [s] e))
let rec lemma_subst_eqns_idem s = function
| [] -> ()
| (x, y)::tl -> lemma_subst_eqns_idem s tl;
lemma_subst_term_idem s x;
lemma_subst_term_idem s y
val subst_funs_monotone: s:subst -> t:term -> Lemma
(ensures (funs (subst_term s t) >= funs t))
let rec subst_funs_monotone s = function
| V x -> ()
| F t1 t2 -> subst_funs_monotone s t1; subst_funs_monotone s t2
val lsubst_funs_monotone: l:list subst -> t:term -> Lemma
(ensures (funs (lsubst_term l t) >= funs t))
let rec lsubst_funs_monotone l t = match l with
| [] -> ()
| hd::tl ->
lsubst_funs_monotone tl t; subst_funs_monotone hd (lsubst_term tl t)
val lemma_occurs_not_solveable_aux: x:nat -> t:term{occurs x t /\ not (V? t)} -> s:list subst -> Lemma
(funs (lsubst_term s t) >= (funs t + funs (lsubst_term s (V x))))
let rec lemma_occurs_not_solveable_aux x t s = match t with
| F t1 t2 ->
if occurs x t1
then let _ = lsubst_funs_monotone s t2 in
match t1 with
| V y -> ()
| _ -> lemma_occurs_not_solveable_aux x t1 s
else if occurs x t2
then let _ = lsubst_funs_monotone s t1 in
match t2 with
| V y -> ()
| _ -> lemma_occurs_not_solveable_aux x t2 s
else ()
type not_solveable s =
forall l. lsubst_term l (fst s) <> lsubst_term l (snd s)
val lemma_occurs_not_solveable: x:nat -> t:term -> Lemma
(requires (occurs x t /\ not (V? t))) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.OrdSet.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "Unification.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Prims.nat -> t: Unification.term
-> FStar.Pervasives.Lemma (requires Unification.occurs x t /\ Prims.op_Negation (V? t))
(ensures Unification.not_solveable (Unification.V x, t)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Unification.term",
"FStar.Classical.forall_intro",
"Prims.list",
"Unification.subst",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"Unification.funs",
"Unification.lsubst_term",
"Prims.op_Addition",
"Unification.V",
"Unification.lemma_occurs_not_solveable_aux",
"Prims.unit"
] | [] | false | false | true | false | false | let lemma_occurs_not_solveable x t =
| FStar.Classical.forall_intro (lemma_occurs_not_solveable_aux x t) | false |
Unification.fst | Unification.lemma_occurs_not_solveable_aux | val lemma_occurs_not_solveable_aux: x:nat -> t:term{occurs x t /\ not (V? t)} -> s:list subst -> Lemma
(funs (lsubst_term s t) >= (funs t + funs (lsubst_term s (V x)))) | val lemma_occurs_not_solveable_aux: x:nat -> t:term{occurs x t /\ not (V? t)} -> s:list subst -> Lemma
(funs (lsubst_term s t) >= (funs t + funs (lsubst_term s (V x)))) | let rec lemma_occurs_not_solveable_aux x t s = match t with
| F t1 t2 ->
if occurs x t1
then let _ = lsubst_funs_monotone s t2 in
match t1 with
| V y -> ()
| _ -> lemma_occurs_not_solveable_aux x t1 s
else if occurs x t2
then let _ = lsubst_funs_monotone s t1 in
match t2 with
| V y -> ()
| _ -> lemma_occurs_not_solveable_aux x t2 s
else () | {
"file_name": "examples/algorithms/Unification.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 11,
"end_line": 310,
"start_col": 0,
"start_line": 298
} | (*
Copyright 2008-2015 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(*
A verified implementation of first-order unification.
The termination argument is based on Ana Bove's Licentiate Thesis:
Programming in Martin-Löf Type Theory Unification - A non-trivial Example
Department of Computer Science, Chalmers University of Technology
1999
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.40.3532
But, is done here directly with lex orderings.
*)
module Unification
open FStar.List.Tot
(* First, a missing lemma from the list library *)
let op_At = append
val lemma_shift_append: #a:eqtype -> l:list a -> x:a -> m:list a -> Lemma
(ensures ( (l@(x::m)) = ((l@[x])@m)))
let rec lemma_shift_append #a l x m = match l with
| [] -> ()
| hd::tl -> lemma_shift_append tl x m
(* A term language with variables V and pairs F *)
type term =
| V : i:nat -> term
| F : t1:term -> t2:term -> term
(* Finite, ordered sets of variables *)
val nat_order : OrdSet.cmp nat
let nat_order :(f:(nat -> nat -> bool){OrdSet.total_order nat f}) = fun x y -> x <= y
type varset = OrdSet.ordset nat nat_order
val empty_vars : varset
let empty_vars = OrdSet.empty
(* Collecting the variables in a term *)
val vars : term -> Tot varset
let rec vars = function
| V i -> OrdSet.singleton i
| F t1 t2 -> OrdSet.union (vars t1) (vars t2)
(* Equations among pairs of terms, to be solved *)
type eqns = list (term * term)
(* All the variables in a set of equations *)
val evars : eqns -> Tot varset
let rec evars = function
| [] -> empty_vars
| (x, y)::tl -> OrdSet.union (OrdSet.union (vars x) (vars y)) (evars tl)
(* Counting variables; used in the termination metric *)
val n_evars : eqns -> Tot nat
let n_evars eqns = OrdSet.size (evars eqns)
(* Counting the number of F-applications; also for the termination metric *)
val funs : term -> Tot nat
let rec funs = function
| V _ -> 0
| F t1 t2 -> 1 + funs t1 + funs t2
val efuns : eqns -> Tot nat
let rec efuns = function
| [] -> 0
| (x,y)::tl -> funs x + funs y + efuns tl
(* Counting the number of equations with variables on the the RHS;
// also for the termination metric *)
val n_flex_rhs : eqns -> Tot nat
let rec n_flex_rhs = function
| [] -> 0
| (V _, V _)::tl
| (_ , V _)::tl -> 1 + n_flex_rhs tl
| _::tl -> n_flex_rhs tl
(* A point substitution *)
type subst = (nat * term)
(* Composition of point substitutions *)
type lsubst = list subst
val subst_term : subst -> term -> Tot term
let rec subst_term s t = match t with
| V x -> if x = fst s then snd s else V x
| F t1 t2 -> F (subst_term s t1) (subst_term s t2)
val lsubst_term : list subst -> term -> Tot term
let lsubst_term = fold_right subst_term
let occurs x t = OrdSet.mem x (vars t)
let ok s = not (occurs (fst s) (snd s))
val lsubst_eqns: list subst -> eqns -> Tot eqns
let rec lsubst_eqns l = function
| [] -> []
| (x,y)::tl -> (lsubst_term l x, lsubst_term l y)::lsubst_eqns l tl
val lemma_lsubst_eqns_nil: e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns [] e = e))
[SMTPat (lsubst_eqns [] e)]
let rec lemma_lsubst_eqns_nil = function
| [] -> ()
| _::tl -> lemma_lsubst_eqns_nil tl
(* A couple of lemmas about variable counts.
// Both of these rely on extensional equality of sets.
// So we need to use eq_lemma explicitly *)
val evars_permute_hd : x:term -> y:term -> tl:eqns -> Lemma
(ensures (evars ((x, y)::tl) = evars ((y, x)::tl)))
let evars_permute_hd x y tl = OrdSet.eq_lemma (evars ((x,y)::tl)) (evars ((y,x)::tl))
val evars_unfun : x:term -> y:term -> x':term -> y':term -> tl:eqns -> Lemma
(ensures (evars ((F x y, F x' y')::tl) = evars ((x, x')::(y, y')::tl)))
let evars_unfun x y x' y' tl = OrdSet.eq_lemma (evars ((F x y, F x' y')::tl)) (evars ((x, x')::(y, y')::tl))
(* Eliminating a variable reduces the variable count *)
val lemma_vars_decrease: s:subst -> t':term -> Lemma
(requires (ok s))
(ensures (OrdSet.subset (vars (subst_term s t'))
(OrdSet.remove (fst s) (OrdSet.union (vars (snd s)) (vars t')))))
let rec lemma_vars_decrease s t' = match t' with
| V x -> ()
| F t1 t2 ->
lemma_vars_decrease s t1;
lemma_vars_decrease s t2
(* Lifting the prior lemma to equations *)
val vars_decrease_eqns: x:nat -> t:term -> e:eqns -> Lemma
(requires (ok (x, t)))
(ensures (OrdSet.subset (evars (lsubst_eqns [x,t] e))
(OrdSet.remove x (evars ((V x, t)::e)))))
let rec vars_decrease_eqns x t e = match e with
| [] -> ()
| hd::tl -> lemma_vars_decrease (x,t) (fst hd);
lemma_vars_decrease (x,t) (snd hd);
vars_decrease_eqns x t tl
val unify : e:eqns -> list subst -> Tot (option (list subst))
(decreases %[n_evars e; efuns e; n_flex_rhs e])
let rec unify e s = match e with
| [] -> Some s
| (V x, t)::tl ->
if V? t && V?.i t = x
then unify tl s //t is a flex-rhs
else if occurs x t
then None
else (vars_decrease_eqns x t tl;
unify (lsubst_eqns [x,t] tl) ((x,t)::s))
| (t, V x)::tl -> //flex-rhs
evars_permute_hd t (V x) tl;
unify ((V x, t)::tl) s
| (F t1 t2, F t1' t2')::tl -> //efuns reduces
evars_unfun t1 t2 t1' t2' tl;
unify ((t1, t1')::(t2, t2')::tl) s
(* All equations are solved when each one is just reflexive *)
val solved : eqns -> Tot bool
let rec solved = function
| [] -> true
| (x,y)::tl -> x=y && solved tl
val lsubst_distributes_over_F: l:list subst -> t1:term -> t2:term -> Lemma
(requires (True))
(ensures (lsubst_term l (F t1 t2) = F (lsubst_term l t1) (lsubst_term l t2)))
[SMTPat (lsubst_term l (F t1 t2))]
let rec lsubst_distributes_over_F l t1 t2 = match l with
| [] -> ()
| hd::tl -> lsubst_distributes_over_F tl t1 t2
let extend_subst s l = s::l
let extend_lsubst l l' = l @ l'
val lemma_extend_lsubst_distributes_term: l:list subst -> l':list subst -> e:term -> Lemma
(requires True)
(ensures (lsubst_term (extend_lsubst l l') e = lsubst_term l (lsubst_term l' e)))
let rec lemma_extend_lsubst_distributes_term l l' e = match l with
| [] -> ()
| hd::tl -> lemma_extend_lsubst_distributes_term tl l' e
val lemma_extend_lsubst_distributes_eqns: l:list subst -> l':list subst -> e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns (extend_lsubst l l') e = lsubst_eqns l (lsubst_eqns l' e)))
[SMTPat (lsubst_eqns (extend_lsubst l l') e)]
let rec lemma_extend_lsubst_distributes_eqns l l' e = match e with
| [] -> ()
| (t1, t2)::tl ->
lemma_extend_lsubst_distributes_term l l' t1;
lemma_extend_lsubst_distributes_term l l' t2;
lemma_extend_lsubst_distributes_eqns l l' tl
val lemma_subst_id: x:nat -> z:term -> y:term -> Lemma
(requires (not (occurs x y)))
(ensures (subst_term (x,z) y = y))
let rec lemma_subst_id x z y = match y with
| V x' -> ()
| F t1 t2 -> lemma_subst_id x z t1; lemma_subst_id x z t2
let neutral s (x, t) = subst_term s (V x) = V x && subst_term s t = t && ok (x, t)
let neutral_l l (x, t) = lsubst_term l (V x) = V x && lsubst_term l t = t && ok (x, t)
val lemma_lsubst_term_commutes: s:subst -> l:list subst -> e:term -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_term [s] (lsubst_term l (subst_term s e)) =
lsubst_term [s] (lsubst_term l e)))
let rec lemma_lsubst_term_commutes s l e = match e with
| V y -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2
val lemma_lsubst_eqns_commutes: s:subst -> l:list subst -> e:eqns -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_eqns [s] (lsubst_eqns l (lsubst_eqns [s] e)) =
lsubst_eqns [s] (lsubst_eqns l e)))
let rec lemma_lsubst_eqns_commutes s l = function
| [] -> ()
| (t1,t2)::tl -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2;
lemma_lsubst_eqns_commutes s l tl
let (++) l1 l2 = extend_lsubst l1 l2
let sub l e = lsubst_eqns l e
let test l1 l2 l3 = assert (l1 ++ l2 ++ l3 == (l1 ++ l2) ++ l3)
val key_lemma: x:nat -> y:term -> tl:eqns -> l:list subst -> lpre:list subst -> l'':list subst -> Lemma
(requires (l'' = lpre ++ ([x,y] ++ l)
/\ not (occurs x y)
/\ l `sub` ((V x, y)::tl) = (V x, y)::tl
/\ solved (l'' `sub` ([x, y] `sub` tl))))
(ensures (solved (l'' `sub` ((V x,y)::tl))))
let key_lemma x y tl l lpre l'' =
let xy = [x,y] in
let xyl = xy++l in
let vxy = V x, y in
assert (l'' `sub` (vxy::tl)
== lpre `sub` (xy `sub` (l `sub` (vxy::tl))));
lemma_lsubst_eqns_commutes (x,y) l (vxy :: tl);
assert (lpre `sub` (xy `sub` (l `sub` (vxy::tl)))
== lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl)))));
assert (lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl))))
== l'' `sub` (xy `sub` (vxy :: tl)));
lemma_subst_id x y y
val lemma_subst_term_idem: s:subst -> t:term -> Lemma
(requires (ok s))
(ensures (subst_term s (subst_term s t) = subst_term s t))
let rec lemma_subst_term_idem s t = match t with
| V x -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_subst_term_idem s t1; lemma_subst_term_idem s t2
val lemma_subst_eqns_idem: s:subst -> e:eqns -> Lemma
(requires (ok s))
(ensures (lsubst_eqns [s] (lsubst_eqns [s] e) = lsubst_eqns [s] e))
let rec lemma_subst_eqns_idem s = function
| [] -> ()
| (x, y)::tl -> lemma_subst_eqns_idem s tl;
lemma_subst_term_idem s x;
lemma_subst_term_idem s y
val subst_funs_monotone: s:subst -> t:term -> Lemma
(ensures (funs (subst_term s t) >= funs t))
let rec subst_funs_monotone s = function
| V x -> ()
| F t1 t2 -> subst_funs_monotone s t1; subst_funs_monotone s t2
val lsubst_funs_monotone: l:list subst -> t:term -> Lemma
(ensures (funs (lsubst_term l t) >= funs t))
let rec lsubst_funs_monotone l t = match l with
| [] -> ()
| hd::tl ->
lsubst_funs_monotone tl t; subst_funs_monotone hd (lsubst_term tl t)
val lemma_occurs_not_solveable_aux: x:nat -> t:term{occurs x t /\ not (V? t)} -> s:list subst -> Lemma | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.OrdSet.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "Unification.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: Prims.nat ->
t: Unification.term{Unification.occurs x t /\ Prims.op_Negation (V? t)} ->
s: Prims.list Unification.subst
-> FStar.Pervasives.Lemma
(ensures
Unification.funs (Unification.lsubst_term s t) >=
Unification.funs t + Unification.funs (Unification.lsubst_term s (Unification.V x))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Unification.term",
"Prims.l_and",
"Prims.b2t",
"Unification.occurs",
"Prims.op_Negation",
"Unification.uu___is_V",
"Prims.list",
"Unification.subst",
"Unification.lemma_occurs_not_solveable_aux",
"Prims.unit",
"Unification.lsubst_funs_monotone",
"Prims.bool"
] | [
"recursion"
] | false | false | true | false | false | let rec lemma_occurs_not_solveable_aux x t s =
| match t with
| F t1 t2 ->
if occurs x t1
then
let _ = lsubst_funs_monotone s t2 in
match t1 with
| V y -> ()
| _ -> lemma_occurs_not_solveable_aux x t1 s
else
if occurs x t2
then
let _ = lsubst_funs_monotone s t1 in
match t2 with
| V y -> ()
| _ -> lemma_occurs_not_solveable_aux x t2 s | false |
Unification.fst | Unification.lemma_subst_eqns | val lemma_subst_eqns: l:list subst -> x:nat -> t:term -> e:eqns -> Lemma
(requires (lsubst_term l (V x) = lsubst_term l t))
(ensures (lsubst_eqns l (lsubst_eqns [x,t] e) = lsubst_eqns l e)) | val lemma_subst_eqns: l:list subst -> x:nat -> t:term -> e:eqns -> Lemma
(requires (lsubst_term l (V x) = lsubst_term l t))
(ensures (lsubst_eqns l (lsubst_eqns [x,t] e) = lsubst_eqns l e)) | let rec lemma_subst_eqns l x t = function
| [] -> ()
| (t1,t2)::tl ->
lemma_subst_idem l x t t1;
lemma_subst_idem l x t t2;
lemma_subst_eqns l x t tl | {
"file_name": "examples/algorithms/Unification.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 29,
"end_line": 335,
"start_col": 0,
"start_line": 330
} | (*
Copyright 2008-2015 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(*
A verified implementation of first-order unification.
The termination argument is based on Ana Bove's Licentiate Thesis:
Programming in Martin-Löf Type Theory Unification - A non-trivial Example
Department of Computer Science, Chalmers University of Technology
1999
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.40.3532
But, is done here directly with lex orderings.
*)
module Unification
open FStar.List.Tot
(* First, a missing lemma from the list library *)
let op_At = append
val lemma_shift_append: #a:eqtype -> l:list a -> x:a -> m:list a -> Lemma
(ensures ( (l@(x::m)) = ((l@[x])@m)))
let rec lemma_shift_append #a l x m = match l with
| [] -> ()
| hd::tl -> lemma_shift_append tl x m
(* A term language with variables V and pairs F *)
type term =
| V : i:nat -> term
| F : t1:term -> t2:term -> term
(* Finite, ordered sets of variables *)
val nat_order : OrdSet.cmp nat
let nat_order :(f:(nat -> nat -> bool){OrdSet.total_order nat f}) = fun x y -> x <= y
type varset = OrdSet.ordset nat nat_order
val empty_vars : varset
let empty_vars = OrdSet.empty
(* Collecting the variables in a term *)
val vars : term -> Tot varset
let rec vars = function
| V i -> OrdSet.singleton i
| F t1 t2 -> OrdSet.union (vars t1) (vars t2)
(* Equations among pairs of terms, to be solved *)
type eqns = list (term * term)
(* All the variables in a set of equations *)
val evars : eqns -> Tot varset
let rec evars = function
| [] -> empty_vars
| (x, y)::tl -> OrdSet.union (OrdSet.union (vars x) (vars y)) (evars tl)
(* Counting variables; used in the termination metric *)
val n_evars : eqns -> Tot nat
let n_evars eqns = OrdSet.size (evars eqns)
(* Counting the number of F-applications; also for the termination metric *)
val funs : term -> Tot nat
let rec funs = function
| V _ -> 0
| F t1 t2 -> 1 + funs t1 + funs t2
val efuns : eqns -> Tot nat
let rec efuns = function
| [] -> 0
| (x,y)::tl -> funs x + funs y + efuns tl
(* Counting the number of equations with variables on the the RHS;
// also for the termination metric *)
val n_flex_rhs : eqns -> Tot nat
let rec n_flex_rhs = function
| [] -> 0
| (V _, V _)::tl
| (_ , V _)::tl -> 1 + n_flex_rhs tl
| _::tl -> n_flex_rhs tl
(* A point substitution *)
type subst = (nat * term)
(* Composition of point substitutions *)
type lsubst = list subst
val subst_term : subst -> term -> Tot term
let rec subst_term s t = match t with
| V x -> if x = fst s then snd s else V x
| F t1 t2 -> F (subst_term s t1) (subst_term s t2)
val lsubst_term : list subst -> term -> Tot term
let lsubst_term = fold_right subst_term
let occurs x t = OrdSet.mem x (vars t)
let ok s = not (occurs (fst s) (snd s))
val lsubst_eqns: list subst -> eqns -> Tot eqns
let rec lsubst_eqns l = function
| [] -> []
| (x,y)::tl -> (lsubst_term l x, lsubst_term l y)::lsubst_eqns l tl
val lemma_lsubst_eqns_nil: e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns [] e = e))
[SMTPat (lsubst_eqns [] e)]
let rec lemma_lsubst_eqns_nil = function
| [] -> ()
| _::tl -> lemma_lsubst_eqns_nil tl
(* A couple of lemmas about variable counts.
// Both of these rely on extensional equality of sets.
// So we need to use eq_lemma explicitly *)
val evars_permute_hd : x:term -> y:term -> tl:eqns -> Lemma
(ensures (evars ((x, y)::tl) = evars ((y, x)::tl)))
let evars_permute_hd x y tl = OrdSet.eq_lemma (evars ((x,y)::tl)) (evars ((y,x)::tl))
val evars_unfun : x:term -> y:term -> x':term -> y':term -> tl:eqns -> Lemma
(ensures (evars ((F x y, F x' y')::tl) = evars ((x, x')::(y, y')::tl)))
let evars_unfun x y x' y' tl = OrdSet.eq_lemma (evars ((F x y, F x' y')::tl)) (evars ((x, x')::(y, y')::tl))
(* Eliminating a variable reduces the variable count *)
val lemma_vars_decrease: s:subst -> t':term -> Lemma
(requires (ok s))
(ensures (OrdSet.subset (vars (subst_term s t'))
(OrdSet.remove (fst s) (OrdSet.union (vars (snd s)) (vars t')))))
let rec lemma_vars_decrease s t' = match t' with
| V x -> ()
| F t1 t2 ->
lemma_vars_decrease s t1;
lemma_vars_decrease s t2
(* Lifting the prior lemma to equations *)
val vars_decrease_eqns: x:nat -> t:term -> e:eqns -> Lemma
(requires (ok (x, t)))
(ensures (OrdSet.subset (evars (lsubst_eqns [x,t] e))
(OrdSet.remove x (evars ((V x, t)::e)))))
let rec vars_decrease_eqns x t e = match e with
| [] -> ()
| hd::tl -> lemma_vars_decrease (x,t) (fst hd);
lemma_vars_decrease (x,t) (snd hd);
vars_decrease_eqns x t tl
val unify : e:eqns -> list subst -> Tot (option (list subst))
(decreases %[n_evars e; efuns e; n_flex_rhs e])
let rec unify e s = match e with
| [] -> Some s
| (V x, t)::tl ->
if V? t && V?.i t = x
then unify tl s //t is a flex-rhs
else if occurs x t
then None
else (vars_decrease_eqns x t tl;
unify (lsubst_eqns [x,t] tl) ((x,t)::s))
| (t, V x)::tl -> //flex-rhs
evars_permute_hd t (V x) tl;
unify ((V x, t)::tl) s
| (F t1 t2, F t1' t2')::tl -> //efuns reduces
evars_unfun t1 t2 t1' t2' tl;
unify ((t1, t1')::(t2, t2')::tl) s
(* All equations are solved when each one is just reflexive *)
val solved : eqns -> Tot bool
let rec solved = function
| [] -> true
| (x,y)::tl -> x=y && solved tl
val lsubst_distributes_over_F: l:list subst -> t1:term -> t2:term -> Lemma
(requires (True))
(ensures (lsubst_term l (F t1 t2) = F (lsubst_term l t1) (lsubst_term l t2)))
[SMTPat (lsubst_term l (F t1 t2))]
let rec lsubst_distributes_over_F l t1 t2 = match l with
| [] -> ()
| hd::tl -> lsubst_distributes_over_F tl t1 t2
let extend_subst s l = s::l
let extend_lsubst l l' = l @ l'
val lemma_extend_lsubst_distributes_term: l:list subst -> l':list subst -> e:term -> Lemma
(requires True)
(ensures (lsubst_term (extend_lsubst l l') e = lsubst_term l (lsubst_term l' e)))
let rec lemma_extend_lsubst_distributes_term l l' e = match l with
| [] -> ()
| hd::tl -> lemma_extend_lsubst_distributes_term tl l' e
val lemma_extend_lsubst_distributes_eqns: l:list subst -> l':list subst -> e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns (extend_lsubst l l') e = lsubst_eqns l (lsubst_eqns l' e)))
[SMTPat (lsubst_eqns (extend_lsubst l l') e)]
let rec lemma_extend_lsubst_distributes_eqns l l' e = match e with
| [] -> ()
| (t1, t2)::tl ->
lemma_extend_lsubst_distributes_term l l' t1;
lemma_extend_lsubst_distributes_term l l' t2;
lemma_extend_lsubst_distributes_eqns l l' tl
val lemma_subst_id: x:nat -> z:term -> y:term -> Lemma
(requires (not (occurs x y)))
(ensures (subst_term (x,z) y = y))
let rec lemma_subst_id x z y = match y with
| V x' -> ()
| F t1 t2 -> lemma_subst_id x z t1; lemma_subst_id x z t2
let neutral s (x, t) = subst_term s (V x) = V x && subst_term s t = t && ok (x, t)
let neutral_l l (x, t) = lsubst_term l (V x) = V x && lsubst_term l t = t && ok (x, t)
val lemma_lsubst_term_commutes: s:subst -> l:list subst -> e:term -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_term [s] (lsubst_term l (subst_term s e)) =
lsubst_term [s] (lsubst_term l e)))
let rec lemma_lsubst_term_commutes s l e = match e with
| V y -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2
val lemma_lsubst_eqns_commutes: s:subst -> l:list subst -> e:eqns -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_eqns [s] (lsubst_eqns l (lsubst_eqns [s] e)) =
lsubst_eqns [s] (lsubst_eqns l e)))
let rec lemma_lsubst_eqns_commutes s l = function
| [] -> ()
| (t1,t2)::tl -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2;
lemma_lsubst_eqns_commutes s l tl
let (++) l1 l2 = extend_lsubst l1 l2
let sub l e = lsubst_eqns l e
let test l1 l2 l3 = assert (l1 ++ l2 ++ l3 == (l1 ++ l2) ++ l3)
val key_lemma: x:nat -> y:term -> tl:eqns -> l:list subst -> lpre:list subst -> l'':list subst -> Lemma
(requires (l'' = lpre ++ ([x,y] ++ l)
/\ not (occurs x y)
/\ l `sub` ((V x, y)::tl) = (V x, y)::tl
/\ solved (l'' `sub` ([x, y] `sub` tl))))
(ensures (solved (l'' `sub` ((V x,y)::tl))))
let key_lemma x y tl l lpre l'' =
let xy = [x,y] in
let xyl = xy++l in
let vxy = V x, y in
assert (l'' `sub` (vxy::tl)
== lpre `sub` (xy `sub` (l `sub` (vxy::tl))));
lemma_lsubst_eqns_commutes (x,y) l (vxy :: tl);
assert (lpre `sub` (xy `sub` (l `sub` (vxy::tl)))
== lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl)))));
assert (lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl))))
== l'' `sub` (xy `sub` (vxy :: tl)));
lemma_subst_id x y y
val lemma_subst_term_idem: s:subst -> t:term -> Lemma
(requires (ok s))
(ensures (subst_term s (subst_term s t) = subst_term s t))
let rec lemma_subst_term_idem s t = match t with
| V x -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_subst_term_idem s t1; lemma_subst_term_idem s t2
val lemma_subst_eqns_idem: s:subst -> e:eqns -> Lemma
(requires (ok s))
(ensures (lsubst_eqns [s] (lsubst_eqns [s] e) = lsubst_eqns [s] e))
let rec lemma_subst_eqns_idem s = function
| [] -> ()
| (x, y)::tl -> lemma_subst_eqns_idem s tl;
lemma_subst_term_idem s x;
lemma_subst_term_idem s y
val subst_funs_monotone: s:subst -> t:term -> Lemma
(ensures (funs (subst_term s t) >= funs t))
let rec subst_funs_monotone s = function
| V x -> ()
| F t1 t2 -> subst_funs_monotone s t1; subst_funs_monotone s t2
val lsubst_funs_monotone: l:list subst -> t:term -> Lemma
(ensures (funs (lsubst_term l t) >= funs t))
let rec lsubst_funs_monotone l t = match l with
| [] -> ()
| hd::tl ->
lsubst_funs_monotone tl t; subst_funs_monotone hd (lsubst_term tl t)
val lemma_occurs_not_solveable_aux: x:nat -> t:term{occurs x t /\ not (V? t)} -> s:list subst -> Lemma
(funs (lsubst_term s t) >= (funs t + funs (lsubst_term s (V x))))
let rec lemma_occurs_not_solveable_aux x t s = match t with
| F t1 t2 ->
if occurs x t1
then let _ = lsubst_funs_monotone s t2 in
match t1 with
| V y -> ()
| _ -> lemma_occurs_not_solveable_aux x t1 s
else if occurs x t2
then let _ = lsubst_funs_monotone s t1 in
match t2 with
| V y -> ()
| _ -> lemma_occurs_not_solveable_aux x t2 s
else ()
type not_solveable s =
forall l. lsubst_term l (fst s) <> lsubst_term l (snd s)
val lemma_occurs_not_solveable: x:nat -> t:term -> Lemma
(requires (occurs x t /\ not (V? t)))
(ensures (not_solveable (V x, t)))
let lemma_occurs_not_solveable x t = FStar.Classical.forall_intro (lemma_occurs_not_solveable_aux x t)
val lemma_subst_idem: l:list subst -> x:nat -> t:term -> t':term -> Lemma
(requires (lsubst_term l (V x) = lsubst_term l t))
(ensures (lsubst_term l (subst_term (x,t) t') = lsubst_term l t'))
let rec lemma_subst_idem l x t t' = match t' with
| V y -> ()
| F t1 t2 -> lemma_subst_idem l x t t1; lemma_subst_idem l x t t2
val lemma_subst_eqns: l:list subst -> x:nat -> t:term -> e:eqns -> Lemma
(requires (lsubst_term l (V x) = lsubst_term l t)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.OrdSet.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "Unification.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list Unification.subst -> x: Prims.nat -> t: Unification.term -> e: Unification.eqns
-> FStar.Pervasives.Lemma
(requires Unification.lsubst_term l (Unification.V x) = Unification.lsubst_term l t)
(ensures
Unification.lsubst_eqns l (Unification.lsubst_eqns [x, t] e) = Unification.lsubst_eqns l e) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"Unification.subst",
"Prims.nat",
"Unification.term",
"Unification.eqns",
"FStar.Pervasives.Native.tuple2",
"Unification.lemma_subst_eqns",
"Prims.unit",
"Unification.lemma_subst_idem"
] | [
"recursion"
] | false | false | true | false | false | let rec lemma_subst_eqns l x t =
| function
| [] -> ()
| (t1, t2) :: tl ->
lemma_subst_idem l x t t1;
lemma_subst_idem l x t t2;
lemma_subst_eqns l x t tl | false |
Unification.fst | Unification.unify_eqns | val unify_eqns : e:eqns -> Tot (option lsubst) | val unify_eqns : e:eqns -> Tot (option lsubst) | let unify_eqns e = unify e [] | {
"file_name": "examples/algorithms/Unification.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 29,
"end_line": 400,
"start_col": 0,
"start_line": 400
} | (*
Copyright 2008-2015 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(*
A verified implementation of first-order unification.
The termination argument is based on Ana Bove's Licentiate Thesis:
Programming in Martin-Löf Type Theory Unification - A non-trivial Example
Department of Computer Science, Chalmers University of Technology
1999
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.40.3532
But, is done here directly with lex orderings.
*)
module Unification
open FStar.List.Tot
(* First, a missing lemma from the list library *)
let op_At = append
val lemma_shift_append: #a:eqtype -> l:list a -> x:a -> m:list a -> Lemma
(ensures ( (l@(x::m)) = ((l@[x])@m)))
let rec lemma_shift_append #a l x m = match l with
| [] -> ()
| hd::tl -> lemma_shift_append tl x m
(* A term language with variables V and pairs F *)
type term =
| V : i:nat -> term
| F : t1:term -> t2:term -> term
(* Finite, ordered sets of variables *)
val nat_order : OrdSet.cmp nat
let nat_order :(f:(nat -> nat -> bool){OrdSet.total_order nat f}) = fun x y -> x <= y
type varset = OrdSet.ordset nat nat_order
val empty_vars : varset
let empty_vars = OrdSet.empty
(* Collecting the variables in a term *)
val vars : term -> Tot varset
let rec vars = function
| V i -> OrdSet.singleton i
| F t1 t2 -> OrdSet.union (vars t1) (vars t2)
(* Equations among pairs of terms, to be solved *)
type eqns = list (term * term)
(* All the variables in a set of equations *)
val evars : eqns -> Tot varset
let rec evars = function
| [] -> empty_vars
| (x, y)::tl -> OrdSet.union (OrdSet.union (vars x) (vars y)) (evars tl)
(* Counting variables; used in the termination metric *)
val n_evars : eqns -> Tot nat
let n_evars eqns = OrdSet.size (evars eqns)
(* Counting the number of F-applications; also for the termination metric *)
val funs : term -> Tot nat
let rec funs = function
| V _ -> 0
| F t1 t2 -> 1 + funs t1 + funs t2
val efuns : eqns -> Tot nat
let rec efuns = function
| [] -> 0
| (x,y)::tl -> funs x + funs y + efuns tl
(* Counting the number of equations with variables on the the RHS;
// also for the termination metric *)
val n_flex_rhs : eqns -> Tot nat
let rec n_flex_rhs = function
| [] -> 0
| (V _, V _)::tl
| (_ , V _)::tl -> 1 + n_flex_rhs tl
| _::tl -> n_flex_rhs tl
(* A point substitution *)
type subst = (nat * term)
(* Composition of point substitutions *)
type lsubst = list subst
val subst_term : subst -> term -> Tot term
let rec subst_term s t = match t with
| V x -> if x = fst s then snd s else V x
| F t1 t2 -> F (subst_term s t1) (subst_term s t2)
val lsubst_term : list subst -> term -> Tot term
let lsubst_term = fold_right subst_term
let occurs x t = OrdSet.mem x (vars t)
let ok s = not (occurs (fst s) (snd s))
val lsubst_eqns: list subst -> eqns -> Tot eqns
let rec lsubst_eqns l = function
| [] -> []
| (x,y)::tl -> (lsubst_term l x, lsubst_term l y)::lsubst_eqns l tl
val lemma_lsubst_eqns_nil: e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns [] e = e))
[SMTPat (lsubst_eqns [] e)]
let rec lemma_lsubst_eqns_nil = function
| [] -> ()
| _::tl -> lemma_lsubst_eqns_nil tl
(* A couple of lemmas about variable counts.
// Both of these rely on extensional equality of sets.
// So we need to use eq_lemma explicitly *)
val evars_permute_hd : x:term -> y:term -> tl:eqns -> Lemma
(ensures (evars ((x, y)::tl) = evars ((y, x)::tl)))
let evars_permute_hd x y tl = OrdSet.eq_lemma (evars ((x,y)::tl)) (evars ((y,x)::tl))
val evars_unfun : x:term -> y:term -> x':term -> y':term -> tl:eqns -> Lemma
(ensures (evars ((F x y, F x' y')::tl) = evars ((x, x')::(y, y')::tl)))
let evars_unfun x y x' y' tl = OrdSet.eq_lemma (evars ((F x y, F x' y')::tl)) (evars ((x, x')::(y, y')::tl))
(* Eliminating a variable reduces the variable count *)
val lemma_vars_decrease: s:subst -> t':term -> Lemma
(requires (ok s))
(ensures (OrdSet.subset (vars (subst_term s t'))
(OrdSet.remove (fst s) (OrdSet.union (vars (snd s)) (vars t')))))
let rec lemma_vars_decrease s t' = match t' with
| V x -> ()
| F t1 t2 ->
lemma_vars_decrease s t1;
lemma_vars_decrease s t2
(* Lifting the prior lemma to equations *)
val vars_decrease_eqns: x:nat -> t:term -> e:eqns -> Lemma
(requires (ok (x, t)))
(ensures (OrdSet.subset (evars (lsubst_eqns [x,t] e))
(OrdSet.remove x (evars ((V x, t)::e)))))
let rec vars_decrease_eqns x t e = match e with
| [] -> ()
| hd::tl -> lemma_vars_decrease (x,t) (fst hd);
lemma_vars_decrease (x,t) (snd hd);
vars_decrease_eqns x t tl
val unify : e:eqns -> list subst -> Tot (option (list subst))
(decreases %[n_evars e; efuns e; n_flex_rhs e])
let rec unify e s = match e with
| [] -> Some s
| (V x, t)::tl ->
if V? t && V?.i t = x
then unify tl s //t is a flex-rhs
else if occurs x t
then None
else (vars_decrease_eqns x t tl;
unify (lsubst_eqns [x,t] tl) ((x,t)::s))
| (t, V x)::tl -> //flex-rhs
evars_permute_hd t (V x) tl;
unify ((V x, t)::tl) s
| (F t1 t2, F t1' t2')::tl -> //efuns reduces
evars_unfun t1 t2 t1' t2' tl;
unify ((t1, t1')::(t2, t2')::tl) s
(* All equations are solved when each one is just reflexive *)
val solved : eqns -> Tot bool
let rec solved = function
| [] -> true
| (x,y)::tl -> x=y && solved tl
val lsubst_distributes_over_F: l:list subst -> t1:term -> t2:term -> Lemma
(requires (True))
(ensures (lsubst_term l (F t1 t2) = F (lsubst_term l t1) (lsubst_term l t2)))
[SMTPat (lsubst_term l (F t1 t2))]
let rec lsubst_distributes_over_F l t1 t2 = match l with
| [] -> ()
| hd::tl -> lsubst_distributes_over_F tl t1 t2
let extend_subst s l = s::l
let extend_lsubst l l' = l @ l'
val lemma_extend_lsubst_distributes_term: l:list subst -> l':list subst -> e:term -> Lemma
(requires True)
(ensures (lsubst_term (extend_lsubst l l') e = lsubst_term l (lsubst_term l' e)))
let rec lemma_extend_lsubst_distributes_term l l' e = match l with
| [] -> ()
| hd::tl -> lemma_extend_lsubst_distributes_term tl l' e
val lemma_extend_lsubst_distributes_eqns: l:list subst -> l':list subst -> e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns (extend_lsubst l l') e = lsubst_eqns l (lsubst_eqns l' e)))
[SMTPat (lsubst_eqns (extend_lsubst l l') e)]
let rec lemma_extend_lsubst_distributes_eqns l l' e = match e with
| [] -> ()
| (t1, t2)::tl ->
lemma_extend_lsubst_distributes_term l l' t1;
lemma_extend_lsubst_distributes_term l l' t2;
lemma_extend_lsubst_distributes_eqns l l' tl
val lemma_subst_id: x:nat -> z:term -> y:term -> Lemma
(requires (not (occurs x y)))
(ensures (subst_term (x,z) y = y))
let rec lemma_subst_id x z y = match y with
| V x' -> ()
| F t1 t2 -> lemma_subst_id x z t1; lemma_subst_id x z t2
let neutral s (x, t) = subst_term s (V x) = V x && subst_term s t = t && ok (x, t)
let neutral_l l (x, t) = lsubst_term l (V x) = V x && lsubst_term l t = t && ok (x, t)
val lemma_lsubst_term_commutes: s:subst -> l:list subst -> e:term -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_term [s] (lsubst_term l (subst_term s e)) =
lsubst_term [s] (lsubst_term l e)))
let rec lemma_lsubst_term_commutes s l e = match e with
| V y -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2
val lemma_lsubst_eqns_commutes: s:subst -> l:list subst -> e:eqns -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_eqns [s] (lsubst_eqns l (lsubst_eqns [s] e)) =
lsubst_eqns [s] (lsubst_eqns l e)))
let rec lemma_lsubst_eqns_commutes s l = function
| [] -> ()
| (t1,t2)::tl -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2;
lemma_lsubst_eqns_commutes s l tl
let (++) l1 l2 = extend_lsubst l1 l2
let sub l e = lsubst_eqns l e
let test l1 l2 l3 = assert (l1 ++ l2 ++ l3 == (l1 ++ l2) ++ l3)
val key_lemma: x:nat -> y:term -> tl:eqns -> l:list subst -> lpre:list subst -> l'':list subst -> Lemma
(requires (l'' = lpre ++ ([x,y] ++ l)
/\ not (occurs x y)
/\ l `sub` ((V x, y)::tl) = (V x, y)::tl
/\ solved (l'' `sub` ([x, y] `sub` tl))))
(ensures (solved (l'' `sub` ((V x,y)::tl))))
let key_lemma x y tl l lpre l'' =
let xy = [x,y] in
let xyl = xy++l in
let vxy = V x, y in
assert (l'' `sub` (vxy::tl)
== lpre `sub` (xy `sub` (l `sub` (vxy::tl))));
lemma_lsubst_eqns_commutes (x,y) l (vxy :: tl);
assert (lpre `sub` (xy `sub` (l `sub` (vxy::tl)))
== lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl)))));
assert (lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl))))
== l'' `sub` (xy `sub` (vxy :: tl)));
lemma_subst_id x y y
val lemma_subst_term_idem: s:subst -> t:term -> Lemma
(requires (ok s))
(ensures (subst_term s (subst_term s t) = subst_term s t))
let rec lemma_subst_term_idem s t = match t with
| V x -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_subst_term_idem s t1; lemma_subst_term_idem s t2
val lemma_subst_eqns_idem: s:subst -> e:eqns -> Lemma
(requires (ok s))
(ensures (lsubst_eqns [s] (lsubst_eqns [s] e) = lsubst_eqns [s] e))
let rec lemma_subst_eqns_idem s = function
| [] -> ()
| (x, y)::tl -> lemma_subst_eqns_idem s tl;
lemma_subst_term_idem s x;
lemma_subst_term_idem s y
val subst_funs_monotone: s:subst -> t:term -> Lemma
(ensures (funs (subst_term s t) >= funs t))
let rec subst_funs_monotone s = function
| V x -> ()
| F t1 t2 -> subst_funs_monotone s t1; subst_funs_monotone s t2
val lsubst_funs_monotone: l:list subst -> t:term -> Lemma
(ensures (funs (lsubst_term l t) >= funs t))
let rec lsubst_funs_monotone l t = match l with
| [] -> ()
| hd::tl ->
lsubst_funs_monotone tl t; subst_funs_monotone hd (lsubst_term tl t)
val lemma_occurs_not_solveable_aux: x:nat -> t:term{occurs x t /\ not (V? t)} -> s:list subst -> Lemma
(funs (lsubst_term s t) >= (funs t + funs (lsubst_term s (V x))))
let rec lemma_occurs_not_solveable_aux x t s = match t with
| F t1 t2 ->
if occurs x t1
then let _ = lsubst_funs_monotone s t2 in
match t1 with
| V y -> ()
| _ -> lemma_occurs_not_solveable_aux x t1 s
else if occurs x t2
then let _ = lsubst_funs_monotone s t1 in
match t2 with
| V y -> ()
| _ -> lemma_occurs_not_solveable_aux x t2 s
else ()
type not_solveable s =
forall l. lsubst_term l (fst s) <> lsubst_term l (snd s)
val lemma_occurs_not_solveable: x:nat -> t:term -> Lemma
(requires (occurs x t /\ not (V? t)))
(ensures (not_solveable (V x, t)))
let lemma_occurs_not_solveable x t = FStar.Classical.forall_intro (lemma_occurs_not_solveable_aux x t)
val lemma_subst_idem: l:list subst -> x:nat -> t:term -> t':term -> Lemma
(requires (lsubst_term l (V x) = lsubst_term l t))
(ensures (lsubst_term l (subst_term (x,t) t') = lsubst_term l t'))
let rec lemma_subst_idem l x t t' = match t' with
| V y -> ()
| F t1 t2 -> lemma_subst_idem l x t t1; lemma_subst_idem l x t t2
val lemma_subst_eqns: l:list subst -> x:nat -> t:term -> e:eqns -> Lemma
(requires (lsubst_term l (V x) = lsubst_term l t))
(ensures (lsubst_eqns l (lsubst_eqns [x,t] e) = lsubst_eqns l e))
let rec lemma_subst_eqns l x t = function
| [] -> ()
| (t1,t2)::tl ->
lemma_subst_idem l x t t1;
lemma_subst_idem l x t t2;
lemma_subst_eqns l x t tl
type not_solveable_eqns e =
forall l. not (solved (lsubst_eqns l e))
val lemma_not_solveable_cons_aux: x:nat -> t:term -> tl:eqns -> l:list subst -> Lemma
(requires (not_solveable_eqns (lsubst_eqns [x,t] tl)
/\ solved (lsubst_eqns l ((V x,t)::tl))))
(ensures False)
[SMTPat (solved (lsubst_eqns l ((V x,t)::tl)))]
let lemma_not_solveable_cons_aux x t tl l = lemma_subst_eqns l x t tl
val lemma_not_solveable_cons: x:nat -> t:term -> tl:eqns -> Lemma
(requires (not_solveable_eqns (lsubst_eqns [x,t] tl)))
(ensures (not_solveable_eqns ((V x, t)::tl)))
let lemma_not_solveable_cons x t tl = ()
val unify_correct_aux: l:list subst -> e:eqns -> Pure (list subst)
(requires (b2t (lsubst_eqns l e = e)))
(ensures (fun m ->
match unify e l with
| None -> not_solveable_eqns e
| Some l' ->
l' = (m @ l)
/\ solved (lsubst_eqns l' e)))
(decreases %[n_evars e; efuns e; n_flex_rhs e])
#set-options "--z3rlimit 100"
let rec unify_correct_aux l = function
| [] -> []
| hd::tl ->
begin match hd with
| (V x, y) ->
if V? y && V?.i y=x
then unify_correct_aux l tl
else if occurs x y
then (lemma_occurs_not_solveable x y; [])
else begin
let s = (x,y) in
let l' = extend_subst s l in
vars_decrease_eqns x y tl;
let tl' = lsubst_eqns [s] tl in
lemma_extend_lsubst_distributes_eqns [s] l tl;
assert (lsubst_eqns l' tl' = lsubst_eqns [s] (lsubst_eqns l (lsubst_eqns [s] tl)));
lemma_lsubst_eqns_commutes s l tl;
lemma_subst_eqns_idem s tl;
let lpre = unify_correct_aux l' tl' in
begin match unify tl' l' with
| None -> lemma_not_solveable_cons x y tl; []
| Some l'' ->
key_lemma x y tl l lpre l'';
lemma_shift_append lpre s l;
lpre@[s]
end
end
| (y, V x) ->
evars_permute_hd y (V x) tl;
unify_correct_aux l ((V x, y)::tl)
| F t1 t2, F s1 s2 ->
evars_unfun t1 t2 s1 s2 tl;
unify_correct_aux l ((t1, s1)::(t2, s2)::tl)
end | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.OrdSet.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "Unification.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | e: Unification.eqns -> FStar.Pervasives.Native.option Unification.lsubst | Prims.Tot | [
"total"
] | [] | [
"Unification.eqns",
"Unification.unify",
"Prims.Nil",
"Unification.subst",
"FStar.Pervasives.Native.option",
"Unification.lsubst"
] | [] | false | false | false | true | false | let unify_eqns e =
| unify e [] | false |
Unification.fst | Unification.lemma_not_solveable_cons_aux | val lemma_not_solveable_cons_aux: x:nat -> t:term -> tl:eqns -> l:list subst -> Lemma
(requires (not_solveable_eqns (lsubst_eqns [x,t] tl)
/\ solved (lsubst_eqns l ((V x,t)::tl))))
(ensures False)
[SMTPat (solved (lsubst_eqns l ((V x,t)::tl)))] | val lemma_not_solveable_cons_aux: x:nat -> t:term -> tl:eqns -> l:list subst -> Lemma
(requires (not_solveable_eqns (lsubst_eqns [x,t] tl)
/\ solved (lsubst_eqns l ((V x,t)::tl))))
(ensures False)
[SMTPat (solved (lsubst_eqns l ((V x,t)::tl)))] | let lemma_not_solveable_cons_aux x t tl l = lemma_subst_eqns l x t tl | {
"file_name": "examples/algorithms/Unification.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 69,
"end_line": 345,
"start_col": 0,
"start_line": 345
} | (*
Copyright 2008-2015 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(*
A verified implementation of first-order unification.
The termination argument is based on Ana Bove's Licentiate Thesis:
Programming in Martin-Löf Type Theory Unification - A non-trivial Example
Department of Computer Science, Chalmers University of Technology
1999
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.40.3532
But, is done here directly with lex orderings.
*)
module Unification
open FStar.List.Tot
(* First, a missing lemma from the list library *)
let op_At = append
val lemma_shift_append: #a:eqtype -> l:list a -> x:a -> m:list a -> Lemma
(ensures ( (l@(x::m)) = ((l@[x])@m)))
let rec lemma_shift_append #a l x m = match l with
| [] -> ()
| hd::tl -> lemma_shift_append tl x m
(* A term language with variables V and pairs F *)
type term =
| V : i:nat -> term
| F : t1:term -> t2:term -> term
(* Finite, ordered sets of variables *)
val nat_order : OrdSet.cmp nat
let nat_order :(f:(nat -> nat -> bool){OrdSet.total_order nat f}) = fun x y -> x <= y
type varset = OrdSet.ordset nat nat_order
val empty_vars : varset
let empty_vars = OrdSet.empty
(* Collecting the variables in a term *)
val vars : term -> Tot varset
let rec vars = function
| V i -> OrdSet.singleton i
| F t1 t2 -> OrdSet.union (vars t1) (vars t2)
(* Equations among pairs of terms, to be solved *)
type eqns = list (term * term)
(* All the variables in a set of equations *)
val evars : eqns -> Tot varset
let rec evars = function
| [] -> empty_vars
| (x, y)::tl -> OrdSet.union (OrdSet.union (vars x) (vars y)) (evars tl)
(* Counting variables; used in the termination metric *)
val n_evars : eqns -> Tot nat
let n_evars eqns = OrdSet.size (evars eqns)
(* Counting the number of F-applications; also for the termination metric *)
val funs : term -> Tot nat
let rec funs = function
| V _ -> 0
| F t1 t2 -> 1 + funs t1 + funs t2
val efuns : eqns -> Tot nat
let rec efuns = function
| [] -> 0
| (x,y)::tl -> funs x + funs y + efuns tl
(* Counting the number of equations with variables on the the RHS;
// also for the termination metric *)
val n_flex_rhs : eqns -> Tot nat
let rec n_flex_rhs = function
| [] -> 0
| (V _, V _)::tl
| (_ , V _)::tl -> 1 + n_flex_rhs tl
| _::tl -> n_flex_rhs tl
(* A point substitution *)
type subst = (nat * term)
(* Composition of point substitutions *)
type lsubst = list subst
val subst_term : subst -> term -> Tot term
let rec subst_term s t = match t with
| V x -> if x = fst s then snd s else V x
| F t1 t2 -> F (subst_term s t1) (subst_term s t2)
val lsubst_term : list subst -> term -> Tot term
let lsubst_term = fold_right subst_term
let occurs x t = OrdSet.mem x (vars t)
let ok s = not (occurs (fst s) (snd s))
val lsubst_eqns: list subst -> eqns -> Tot eqns
let rec lsubst_eqns l = function
| [] -> []
| (x,y)::tl -> (lsubst_term l x, lsubst_term l y)::lsubst_eqns l tl
val lemma_lsubst_eqns_nil: e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns [] e = e))
[SMTPat (lsubst_eqns [] e)]
let rec lemma_lsubst_eqns_nil = function
| [] -> ()
| _::tl -> lemma_lsubst_eqns_nil tl
(* A couple of lemmas about variable counts.
// Both of these rely on extensional equality of sets.
// So we need to use eq_lemma explicitly *)
val evars_permute_hd : x:term -> y:term -> tl:eqns -> Lemma
(ensures (evars ((x, y)::tl) = evars ((y, x)::tl)))
let evars_permute_hd x y tl = OrdSet.eq_lemma (evars ((x,y)::tl)) (evars ((y,x)::tl))
val evars_unfun : x:term -> y:term -> x':term -> y':term -> tl:eqns -> Lemma
(ensures (evars ((F x y, F x' y')::tl) = evars ((x, x')::(y, y')::tl)))
let evars_unfun x y x' y' tl = OrdSet.eq_lemma (evars ((F x y, F x' y')::tl)) (evars ((x, x')::(y, y')::tl))
(* Eliminating a variable reduces the variable count *)
val lemma_vars_decrease: s:subst -> t':term -> Lemma
(requires (ok s))
(ensures (OrdSet.subset (vars (subst_term s t'))
(OrdSet.remove (fst s) (OrdSet.union (vars (snd s)) (vars t')))))
let rec lemma_vars_decrease s t' = match t' with
| V x -> ()
| F t1 t2 ->
lemma_vars_decrease s t1;
lemma_vars_decrease s t2
(* Lifting the prior lemma to equations *)
val vars_decrease_eqns: x:nat -> t:term -> e:eqns -> Lemma
(requires (ok (x, t)))
(ensures (OrdSet.subset (evars (lsubst_eqns [x,t] e))
(OrdSet.remove x (evars ((V x, t)::e)))))
let rec vars_decrease_eqns x t e = match e with
| [] -> ()
| hd::tl -> lemma_vars_decrease (x,t) (fst hd);
lemma_vars_decrease (x,t) (snd hd);
vars_decrease_eqns x t tl
val unify : e:eqns -> list subst -> Tot (option (list subst))
(decreases %[n_evars e; efuns e; n_flex_rhs e])
let rec unify e s = match e with
| [] -> Some s
| (V x, t)::tl ->
if V? t && V?.i t = x
then unify tl s //t is a flex-rhs
else if occurs x t
then None
else (vars_decrease_eqns x t tl;
unify (lsubst_eqns [x,t] tl) ((x,t)::s))
| (t, V x)::tl -> //flex-rhs
evars_permute_hd t (V x) tl;
unify ((V x, t)::tl) s
| (F t1 t2, F t1' t2')::tl -> //efuns reduces
evars_unfun t1 t2 t1' t2' tl;
unify ((t1, t1')::(t2, t2')::tl) s
(* All equations are solved when each one is just reflexive *)
val solved : eqns -> Tot bool
let rec solved = function
| [] -> true
| (x,y)::tl -> x=y && solved tl
val lsubst_distributes_over_F: l:list subst -> t1:term -> t2:term -> Lemma
(requires (True))
(ensures (lsubst_term l (F t1 t2) = F (lsubst_term l t1) (lsubst_term l t2)))
[SMTPat (lsubst_term l (F t1 t2))]
let rec lsubst_distributes_over_F l t1 t2 = match l with
| [] -> ()
| hd::tl -> lsubst_distributes_over_F tl t1 t2
let extend_subst s l = s::l
let extend_lsubst l l' = l @ l'
val lemma_extend_lsubst_distributes_term: l:list subst -> l':list subst -> e:term -> Lemma
(requires True)
(ensures (lsubst_term (extend_lsubst l l') e = lsubst_term l (lsubst_term l' e)))
let rec lemma_extend_lsubst_distributes_term l l' e = match l with
| [] -> ()
| hd::tl -> lemma_extend_lsubst_distributes_term tl l' e
val lemma_extend_lsubst_distributes_eqns: l:list subst -> l':list subst -> e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns (extend_lsubst l l') e = lsubst_eqns l (lsubst_eqns l' e)))
[SMTPat (lsubst_eqns (extend_lsubst l l') e)]
let rec lemma_extend_lsubst_distributes_eqns l l' e = match e with
| [] -> ()
| (t1, t2)::tl ->
lemma_extend_lsubst_distributes_term l l' t1;
lemma_extend_lsubst_distributes_term l l' t2;
lemma_extend_lsubst_distributes_eqns l l' tl
val lemma_subst_id: x:nat -> z:term -> y:term -> Lemma
(requires (not (occurs x y)))
(ensures (subst_term (x,z) y = y))
let rec lemma_subst_id x z y = match y with
| V x' -> ()
| F t1 t2 -> lemma_subst_id x z t1; lemma_subst_id x z t2
let neutral s (x, t) = subst_term s (V x) = V x && subst_term s t = t && ok (x, t)
let neutral_l l (x, t) = lsubst_term l (V x) = V x && lsubst_term l t = t && ok (x, t)
val lemma_lsubst_term_commutes: s:subst -> l:list subst -> e:term -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_term [s] (lsubst_term l (subst_term s e)) =
lsubst_term [s] (lsubst_term l e)))
let rec lemma_lsubst_term_commutes s l e = match e with
| V y -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2
val lemma_lsubst_eqns_commutes: s:subst -> l:list subst -> e:eqns -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_eqns [s] (lsubst_eqns l (lsubst_eqns [s] e)) =
lsubst_eqns [s] (lsubst_eqns l e)))
let rec lemma_lsubst_eqns_commutes s l = function
| [] -> ()
| (t1,t2)::tl -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2;
lemma_lsubst_eqns_commutes s l tl
let (++) l1 l2 = extend_lsubst l1 l2
let sub l e = lsubst_eqns l e
let test l1 l2 l3 = assert (l1 ++ l2 ++ l3 == (l1 ++ l2) ++ l3)
val key_lemma: x:nat -> y:term -> tl:eqns -> l:list subst -> lpre:list subst -> l'':list subst -> Lemma
(requires (l'' = lpre ++ ([x,y] ++ l)
/\ not (occurs x y)
/\ l `sub` ((V x, y)::tl) = (V x, y)::tl
/\ solved (l'' `sub` ([x, y] `sub` tl))))
(ensures (solved (l'' `sub` ((V x,y)::tl))))
let key_lemma x y tl l lpre l'' =
let xy = [x,y] in
let xyl = xy++l in
let vxy = V x, y in
assert (l'' `sub` (vxy::tl)
== lpre `sub` (xy `sub` (l `sub` (vxy::tl))));
lemma_lsubst_eqns_commutes (x,y) l (vxy :: tl);
assert (lpre `sub` (xy `sub` (l `sub` (vxy::tl)))
== lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl)))));
assert (lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl))))
== l'' `sub` (xy `sub` (vxy :: tl)));
lemma_subst_id x y y
val lemma_subst_term_idem: s:subst -> t:term -> Lemma
(requires (ok s))
(ensures (subst_term s (subst_term s t) = subst_term s t))
let rec lemma_subst_term_idem s t = match t with
| V x -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_subst_term_idem s t1; lemma_subst_term_idem s t2
val lemma_subst_eqns_idem: s:subst -> e:eqns -> Lemma
(requires (ok s))
(ensures (lsubst_eqns [s] (lsubst_eqns [s] e) = lsubst_eqns [s] e))
let rec lemma_subst_eqns_idem s = function
| [] -> ()
| (x, y)::tl -> lemma_subst_eqns_idem s tl;
lemma_subst_term_idem s x;
lemma_subst_term_idem s y
val subst_funs_monotone: s:subst -> t:term -> Lemma
(ensures (funs (subst_term s t) >= funs t))
let rec subst_funs_monotone s = function
| V x -> ()
| F t1 t2 -> subst_funs_monotone s t1; subst_funs_monotone s t2
val lsubst_funs_monotone: l:list subst -> t:term -> Lemma
(ensures (funs (lsubst_term l t) >= funs t))
let rec lsubst_funs_monotone l t = match l with
| [] -> ()
| hd::tl ->
lsubst_funs_monotone tl t; subst_funs_monotone hd (lsubst_term tl t)
val lemma_occurs_not_solveable_aux: x:nat -> t:term{occurs x t /\ not (V? t)} -> s:list subst -> Lemma
(funs (lsubst_term s t) >= (funs t + funs (lsubst_term s (V x))))
let rec lemma_occurs_not_solveable_aux x t s = match t with
| F t1 t2 ->
if occurs x t1
then let _ = lsubst_funs_monotone s t2 in
match t1 with
| V y -> ()
| _ -> lemma_occurs_not_solveable_aux x t1 s
else if occurs x t2
then let _ = lsubst_funs_monotone s t1 in
match t2 with
| V y -> ()
| _ -> lemma_occurs_not_solveable_aux x t2 s
else ()
type not_solveable s =
forall l. lsubst_term l (fst s) <> lsubst_term l (snd s)
val lemma_occurs_not_solveable: x:nat -> t:term -> Lemma
(requires (occurs x t /\ not (V? t)))
(ensures (not_solveable (V x, t)))
let lemma_occurs_not_solveable x t = FStar.Classical.forall_intro (lemma_occurs_not_solveable_aux x t)
val lemma_subst_idem: l:list subst -> x:nat -> t:term -> t':term -> Lemma
(requires (lsubst_term l (V x) = lsubst_term l t))
(ensures (lsubst_term l (subst_term (x,t) t') = lsubst_term l t'))
let rec lemma_subst_idem l x t t' = match t' with
| V y -> ()
| F t1 t2 -> lemma_subst_idem l x t t1; lemma_subst_idem l x t t2
val lemma_subst_eqns: l:list subst -> x:nat -> t:term -> e:eqns -> Lemma
(requires (lsubst_term l (V x) = lsubst_term l t))
(ensures (lsubst_eqns l (lsubst_eqns [x,t] e) = lsubst_eqns l e))
let rec lemma_subst_eqns l x t = function
| [] -> ()
| (t1,t2)::tl ->
lemma_subst_idem l x t t1;
lemma_subst_idem l x t t2;
lemma_subst_eqns l x t tl
type not_solveable_eqns e =
forall l. not (solved (lsubst_eqns l e))
val lemma_not_solveable_cons_aux: x:nat -> t:term -> tl:eqns -> l:list subst -> Lemma
(requires (not_solveable_eqns (lsubst_eqns [x,t] tl)
/\ solved (lsubst_eqns l ((V x,t)::tl))))
(ensures False) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.OrdSet.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "Unification.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Prims.nat -> t: Unification.term -> tl: Unification.eqns -> l: Prims.list Unification.subst
-> FStar.Pervasives.Lemma
(requires
Unification.not_solveable_eqns (Unification.lsubst_eqns [x, t] tl) /\
Unification.solved (Unification.lsubst_eqns l ((Unification.V x, t) :: tl)))
(ensures Prims.l_False)
[SMTPat (Unification.solved (Unification.lsubst_eqns l ((Unification.V x, t) :: tl)))] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Unification.term",
"Unification.eqns",
"Prims.list",
"Unification.subst",
"Unification.lemma_subst_eqns",
"Prims.unit"
] | [] | true | false | true | false | false | let lemma_not_solveable_cons_aux x t tl l =
| lemma_subst_eqns l x t tl | false |
FStar.Algebra.CommMonoid.Fold.Nested.fsti | FStar.Algebra.CommMonoid.Fold.Nested.transpose_generator | val transpose_generator
(#c: _)
(#m0 #mk #n0 #nk: int)
(gen: (ifrom_ito m0 mk -> ifrom_ito n0 nk -> c))
: (f: (ifrom_ito n0 nk -> ifrom_ito m0 mk -> c){forall i j. f j i == gen i j}) | val transpose_generator
(#c: _)
(#m0 #mk #n0 #nk: int)
(gen: (ifrom_ito m0 mk -> ifrom_ito n0 nk -> c))
: (f: (ifrom_ito n0 nk -> ifrom_ito m0 mk -> c){forall i j. f j i == gen i j}) | let transpose_generator #c (#m0 #mk: int)
(#n0 #nk: int)
(gen: ifrom_ito m0 mk -> ifrom_ito n0 nk -> c)
: (f: (ifrom_ito n0 nk -> ifrom_ito m0 mk -> c) { forall i j. f j i == gen i j })
= fun j i -> gen i j | {
"file_name": "ulib/FStar.Algebra.CommMonoid.Fold.Nested.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 22,
"end_line": 41,
"start_col": 0,
"start_line": 37
} | (*
Copyright 2022 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author: A. Rozanov
*)
(*
Here we reason about nested folds of functions over arbitrary
integer intervals. We call such functions generators.
*)
module FStar.Algebra.CommMonoid.Fold.Nested
module CF = FStar.Algebra.CommMonoid.Fold
module CE = FStar.Algebra.CommMonoid.Equiv
open FStar.IntegerIntervals
(* This constructs a generator function that has its arguments in reverse
order. Useful when reasoning about nested folds, transposed matrices, etc.
Note how this utility is more general than transposed_matrix_gen
found in FStar.Seq.Matrix -- but for zero-based domains, latter is | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.IntegerIntervals.fst.checked",
"FStar.Algebra.CommMonoid.Fold.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Algebra.CommMonoid.Fold.Nested.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.IntegerIntervals",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Fold",
"short_module": "CF"
},
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid.Fold",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid.Fold",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | gen: (_: FStar.IntegerIntervals.ifrom_ito m0 mk -> _: FStar.IntegerIntervals.ifrom_ito n0 nk -> c)
-> f:
(_: FStar.IntegerIntervals.ifrom_ito n0 nk -> _: FStar.IntegerIntervals.ifrom_ito m0 mk -> c)
{ forall (i: FStar.IntegerIntervals.ifrom_ito m0 mk)
(j: FStar.IntegerIntervals.ifrom_ito n0 nk).
f j i == gen i j } | Prims.Tot | [
"total"
] | [] | [
"Prims.int",
"FStar.IntegerIntervals.ifrom_ito",
"Prims.l_Forall",
"Prims.eq2"
] | [] | false | false | false | false | false | let transpose_generator
#c
(#m0: int)
(#mk: int)
(#n0: int)
(#nk: int)
(gen: (ifrom_ito m0 mk -> ifrom_ito n0 nk -> c))
: (f: (ifrom_ito n0 nk -> ifrom_ito m0 mk -> c){forall i j. f j i == gen i j}) =
| fun j i -> gen i j | false |
FStar.Algebra.CommMonoid.Fold.Nested.fsti | FStar.Algebra.CommMonoid.Fold.Nested.double_fold | val double_fold : cm: FStar.Algebra.CommMonoid.Equiv.cm c eq ->
g: (_: FStar.IntegerIntervals.ifrom_ito a0 ak -> _: FStar.IntegerIntervals.ifrom_ito b0 bk -> c)
-> c | let double_fold #c #eq #a0 (#ak: not_less_than a0) #b0 (#bk:not_less_than b0)
(cm: CE.cm c eq)
(g: ifrom_ito a0 ak -> ifrom_ito b0 bk -> c) =
CF.fold cm a0 ak (fun (i: ifrom_ito a0 ak) -> CF.fold cm b0 bk (g i)) | {
"file_name": "ulib/FStar.Algebra.CommMonoid.Fold.Nested.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 71,
"end_line": 46,
"start_col": 0,
"start_line": 43
} | (*
Copyright 2022 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author: A. Rozanov
*)
(*
Here we reason about nested folds of functions over arbitrary
integer intervals. We call such functions generators.
*)
module FStar.Algebra.CommMonoid.Fold.Nested
module CF = FStar.Algebra.CommMonoid.Fold
module CE = FStar.Algebra.CommMonoid.Equiv
open FStar.IntegerIntervals
(* This constructs a generator function that has its arguments in reverse
order. Useful when reasoning about nested folds, transposed matrices, etc.
Note how this utility is more general than transposed_matrix_gen
found in FStar.Seq.Matrix -- but for zero-based domains, latter is
more convenient. *)
let transpose_generator #c (#m0 #mk: int)
(#n0 #nk: int)
(gen: ifrom_ito m0 mk -> ifrom_ito n0 nk -> c)
: (f: (ifrom_ito n0 nk -> ifrom_ito m0 mk -> c) { forall i j. f j i == gen i j })
= fun j i -> gen i j | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.IntegerIntervals.fst.checked",
"FStar.Algebra.CommMonoid.Fold.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Algebra.CommMonoid.Fold.Nested.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.IntegerIntervals",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Fold",
"short_module": "CF"
},
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid.Fold",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid.Fold",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
cm: FStar.Algebra.CommMonoid.Equiv.cm c eq ->
g: (_: FStar.IntegerIntervals.ifrom_ito a0 ak -> _: FStar.IntegerIntervals.ifrom_ito b0 bk -> c)
-> c | Prims.Tot | [
"total"
] | [] | [
"FStar.Algebra.CommMonoid.Equiv.equiv",
"Prims.int",
"FStar.IntegerIntervals.not_less_than",
"FStar.Algebra.CommMonoid.Equiv.cm",
"FStar.IntegerIntervals.ifrom_ito",
"FStar.Algebra.CommMonoid.Fold.fold"
] | [] | false | false | false | false | false | let double_fold
#c
#eq
#a0
(#ak: not_less_than a0)
#b0
(#bk: not_less_than b0)
(cm: CE.cm c eq)
(g: (ifrom_ito a0 ak -> ifrom_ito b0 bk -> c))
=
| CF.fold cm a0 ak (fun (i: ifrom_ito a0 ak) -> CF.fold cm b0 bk (g i)) | false |
|
Steel.ST.Array.Util.fst | Steel.ST.Array.Util.forall2_body | val forall2_body:
#a: Type0 ->
#b: Type0 ->
n: US.t ->
a0: A.array a ->
a1: A.array b ->
p: (a -> b -> bool) ->
r: R.ref US.t ->
p0: perm ->
p1: perm ->
s0: G.erased (Seq.seq a) ->
s1: G.erased (Seq.seq b) ->
squash (Seq.length s0 == US.v n /\ Seq.length s0 == Seq.length s1) ->
unit
-> STT unit
(forall2_inv n a0 a1 p r p0 p1 s0 s1 () true)
(fun _ -> exists_ (forall2_inv n a0 a1 p r p0 p1 s0 s1 ())) | val forall2_body:
#a: Type0 ->
#b: Type0 ->
n: US.t ->
a0: A.array a ->
a1: A.array b ->
p: (a -> b -> bool) ->
r: R.ref US.t ->
p0: perm ->
p1: perm ->
s0: G.erased (Seq.seq a) ->
s1: G.erased (Seq.seq b) ->
squash (Seq.length s0 == US.v n /\ Seq.length s0 == Seq.length s1) ->
unit
-> STT unit
(forall2_inv n a0 a1 p r p0 p1 s0 s1 () true)
(fun _ -> exists_ (forall2_inv n a0 a1 p r p0 p1 s0 s1 ())) | let forall2_body
(#a #b:Type0)
(n:US.t)
(a0:A.array a)
(a1:A.array b)
(p:a -> b -> bool)
(r:R.ref US.t)
(p0 p1:perm)
(s0:G.erased (Seq.seq a))
(s1:G.erased (Seq.seq b))
(_:squash (Seq.length s0 == US.v n /\ Seq.length s0 == Seq.length s1))
: unit ->
STT unit
(forall2_inv n a0 a1 p r p0 p1 s0 s1 () true)
(fun _ -> exists_ (forall2_inv n a0 a1 p r p0 p1 s0 s1 ()))
= fun _ ->
let _ = elim_exists () in
elim_pure _;
elim_pure _;
//atomic increment?
let i = R.read r in
R.write r (US.add i 1sz);
intro_pure (forall2_pure_inv n p s0 s1 () (US.add i 1sz));
intro_pure (forall2_pure_inv_b n p s0 s1 ()
(US.add i 1sz)
((US.add i 1sz) `US.lt` n && p (Seq.index s0 (US.v (US.add i 1sz)))
(Seq.index s1 (US.v (US.add i 1sz)))));
intro_exists
(US.add i 1sz)
(forall2_pred n a0 a1 p r p0 p1 s0 s1 ()
((US.add i 1sz) `US.lt` n && p (Seq.index s0 (US.v (US.add i 1sz)))
(Seq.index s1 (US.v (US.add i 1sz)))));
intro_exists
((US.add i 1sz) `US.lt` n && p (Seq.index s0 (US.v (US.add i 1sz)))
(Seq.index s1 (US.v (US.add i 1sz))))
(forall2_inv n a0 a1 p r p0 p1 s0 s1 ()) | {
"file_name": "lib/steel/Steel.ST.Array.Util.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 46,
"end_line": 404,
"start_col": 0,
"start_line": 367
} | (*
Copyright 2021 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.ST.Array.Util
module G = FStar.Ghost
module US = FStar.SizeT
module R = Steel.ST.Reference
module A = Steel.ST.Array
module Loops = Steel.ST.Loops
open Steel.FractionalPermission
open Steel.ST.Effect
open Steel.ST.Util
/// Implementation of array_literal using a for loop
let array_literal_inv_pure
(#a:Type0)
(n:US.t)
(f:(i:US.t{US.v i < US.v n} -> a))
(i:Loops.nat_at_most n)
(s:Seq.seq a)
: prop
= forall (j:nat).
(j < i /\ j < Seq.length s) ==> Seq.index s j == f (US.uint_to_t j)
[@@ __reduce__]
let array_literal_inv
(#a:Type0)
(n:US.t)
(arr:A.array a)
(f:(i:US.t{US.v i < US.v n} -> a))
(i:Loops.nat_at_most n)
: Seq.seq a -> vprop
= fun s ->
A.pts_to arr full_perm s
`star`
pure (array_literal_inv_pure n f i s)
inline_for_extraction
let array_literal_loop_body
(#a:Type0)
(n:US.t)
(arr:A.array a{A.length arr == US.v n})
(f:(i:US.t{US.v i < US.v n} -> a))
: i:Loops.u32_between 0sz n ->
STT unit
(exists_ (array_literal_inv n arr f (US.v i)))
(fun _ -> exists_ (array_literal_inv n arr f (US.v i + 1)))
= fun i ->
let s = elim_exists () in
();
A.pts_to_length arr s;
elim_pure (array_literal_inv_pure n f (US.v i) s);
A.write arr i (f i);
intro_pure
(array_literal_inv_pure n f (US.v i + 1) (Seq.upd s (US.v i) (f i)));
intro_exists
(Seq.upd s (US.v i) (f i))
(array_literal_inv n arr f (US.v i + 1))
let array_literal #a n f =
let arr = A.alloc (f 0sz) n in
intro_pure
(array_literal_inv_pure n f 1 (Seq.create (US.v n) (f 0sz)));
intro_exists
(Seq.create (US.v n) (f 0sz))
(array_literal_inv n arr f 1);
Loops.for_loop
1sz
n
(fun i -> exists_ (array_literal_inv n arr f i))
(array_literal_loop_body n arr f);
let s = elim_exists () in
A.pts_to_length arr s;
elim_pure (array_literal_inv_pure n f (US.v n) s);
assert (Seq.equal s (Seq.init (US.v n) (fun i -> f (US.uint_to_t i))));
rewrite (A.pts_to arr full_perm s) _;
return arr
/// Implementation of for_all using a while loop
let forall_pure_inv
(#a:Type0)
(n:US.t)
(p:a -> bool)
(s:Seq.seq a)
(_:squash (Seq.length s == US.v n))
(i:US.t)
: prop
= i `US.lte` n /\ (forall (j:nat). j < US.v i ==> p (Seq.index s j))
let forall_pure_inv_b
(#a:Type0)
(n:US.t)
(p:a -> bool)
(s:Seq.seq a)
(_:squash (Seq.length s == US.v n))
(i:US.t)
(b:bool)
: prop
= b == (i `US.lt` n && p (Seq.index s (US.v i)))
[@@ __reduce__]
let forall_pred
(#a:Type0)
(n:US.t)
(arr:A.array a)
(p:a -> bool)
(r:R.ref US.t)
(perm:perm)
(s:Seq.seq a)
(_:squash (Seq.length s == US.v n))
(b:bool)
: US.t -> vprop
= fun i ->
A.pts_to arr perm s
`star`
R.pts_to r full_perm i
`star`
pure (forall_pure_inv n p s () i)
`star`
pure (forall_pure_inv_b n p s () i b)
[@@ __reduce__]
let forall_inv
(#a:Type0)
(n:US.t)
(arr:A.array a)
(p:a -> bool)
(r:R.ref US.t)
(perm:perm)
(s:Seq.seq a)
(_:squash (Seq.length s == US.v n))
: bool -> vprop
= fun b -> exists_ (forall_pred n arr p r perm s () b)
inline_for_extraction
let forall_cond
(#a:Type0)
(n:US.t)
(arr:A.array a)
(p:a -> bool)
(r:R.ref US.t)
(perm:perm)
(s:G.erased (Seq.seq a))
(_:squash (Seq.length s == US.v n))
: unit ->
STT bool
(exists_ (forall_inv n arr p r perm s ()))
(forall_inv n arr p r perm s ())
= fun _ ->
let _ = elim_exists () in
let _ = elim_exists () in
elim_pure _;
elim_pure _;
let i = R.read r in
let b = i = n in
let res =
if b then return false
else let elt = A.read arr i in
return (p elt) in
intro_pure (forall_pure_inv n p s () i);
intro_pure (forall_pure_inv_b n p s () i res);
intro_exists i (forall_pred n arr p r perm s () res);
return res
inline_for_extraction
let forall_body
(#a:Type0)
(n:US.t)
(arr:A.array a)
(p:a -> bool)
(r:R.ref US.t)
(perm:perm)
(s:G.erased (Seq.seq a))
(_:squash (Seq.length s == US.v n))
: unit ->
STT unit
(forall_inv n arr p r perm s () true)
(fun _ -> exists_ (forall_inv n arr p r perm s ()))
= fun _ ->
let _ = elim_exists () in
elim_pure _;
elim_pure _;
//atomic increment?
let i = R.read r in
R.write r (US.add i 1sz);
intro_pure (forall_pure_inv n p s () (US.add i 1sz));
intro_pure (forall_pure_inv_b n p s ()
(US.add i 1sz)
((US.add i 1sz) `US.lt` n && p (Seq.index s (US.v (US.add i 1sz)))));
intro_exists
(US.add i 1sz)
(forall_pred n arr p r perm s ()
((US.add i 1sz) `US.lt` n && p (Seq.index s (US.v (US.add i 1sz)))));
intro_exists
((US.add i 1sz) `US.lt` n && p (Seq.index s (US.v (US.add i 1sz))))
(forall_inv n arr p r perm s ())
let for_all #a #perm #s n arr p =
A.pts_to_length arr s;
let b = n = 0sz in
if b then return true
else begin
//This could be stack allocated
let r = R.alloc 0sz in
intro_pure (forall_pure_inv n p s () 0sz);
intro_pure
(forall_pure_inv_b n p s () 0sz
(0sz `US.lt` n && p (Seq.index s (US.v 0sz))));
intro_exists 0sz
(forall_pred n arr p r perm s ()
(0sz `US.lt` n && p (Seq.index s (US.v 0sz))));
intro_exists
(0sz `US.lt` n && p (Seq.index s (US.v 0sz)))
(forall_inv n arr p r perm s ());
Loops.while_loop
(forall_inv n arr p r perm s ())
(forall_cond n arr p r perm s ())
(forall_body n arr p r perm s ());
let _ = elim_exists () in
let _ = elim_pure _ in
let _ = elim_pure _ in
let i = R.read r in
//This free would go away if we had stack allocations
R.free r;
return (i = n)
end
/// Implementation of for_all2 using a while loop
let forall2_pure_inv
(#a #b:Type0)
(n:US.t)
(p:a -> b -> bool)
(s0:Seq.seq a)
(s1:Seq.seq b)
(_:squash (Seq.length s0 == US.v n /\ Seq.length s0 == Seq.length s1))
(i:US.t)
: prop
= i `US.lte` n /\ (forall (j:nat). j < US.v i ==> p (Seq.index s0 j) (Seq.index s1 j))
let forall2_pure_inv_b
(#a #b:Type0)
(n:US.t)
(p:a -> b -> bool)
(s0:Seq.seq a)
(s1:Seq.seq b)
(_:squash (Seq.length s0 == US.v n /\ Seq.length s0 == Seq.length s1))
(i:US.t)
(g:bool)
: prop
= g == (i `US.lt` n && p (Seq.index s0 (US.v i)) (Seq.index s1 (US.v i)))
[@@ __reduce__]
let forall2_pred
(#a #b:Type0)
(n:US.t)
(a0:A.array a)
(a1:A.array b)
(p:a -> b -> bool)
(r:R.ref US.t)
(p0 p1:perm)
(s0:Seq.seq a)
(s1:Seq.seq b)
(_:squash (Seq.length s0 == US.v n /\ Seq.length s0 == Seq.length s1))
(g:bool)
: US.t -> vprop
= fun i ->
A.pts_to a0 p0 s0
`star`
A.pts_to a1 p1 s1
`star`
R.pts_to r full_perm i
`star`
pure (forall2_pure_inv n p s0 s1 () i)
`star`
pure (forall2_pure_inv_b n p s0 s1 () i g)
[@@ __reduce__]
let forall2_inv
(#a #b:Type0)
(n:US.t)
(a0:A.array a)
(a1:A.array b)
(p:a -> b -> bool)
(r:R.ref US.t)
(p0 p1:perm)
(s0:Seq.seq a)
(s1:Seq.seq b)
(_:squash (Seq.length s0 == US.v n /\ Seq.length s0 == Seq.length s1))
: bool -> vprop
= fun g -> exists_ (forall2_pred n a0 a1 p r p0 p1 s0 s1 () g)
inline_for_extraction
let forall2_cond
(#a #b:Type0)
(n:US.t)
(a0:A.array a)
(a1:A.array b)
(p:a -> b -> bool)
(r:R.ref US.t)
(p0 p1:perm)
(s0:G.erased (Seq.seq a))
(s1:G.erased (Seq.seq b))
(_:squash (Seq.length s0 == US.v n /\ Seq.length s0 == Seq.length s1))
: unit ->
STT bool
(exists_ (forall2_inv n a0 a1 p r p0 p1 s0 s1 ()))
(forall2_inv n a0 a1 p r p0 p1 s0 s1 ())
= fun _ ->
let _ = elim_exists () in
let _ = elim_exists () in
elim_pure _;
elim_pure _;
let i = R.read r in
let b = i = n in
let res =
if b then return false
else let elt0 = A.read a0 i in
let elt1 = A.read a1 i in
return (p elt0 elt1) in
intro_pure (forall2_pure_inv n p s0 s1 () i);
intro_pure (forall2_pure_inv_b n p s0 s1 () i res);
intro_exists i (forall2_pred n a0 a1 p r p0 p1 s0 s1 () res);
return res | {
"checked_file": "/",
"dependencies": [
"Steel.ST.Util.fsti.checked",
"Steel.ST.Reference.fsti.checked",
"Steel.ST.Loops.fsti.checked",
"Steel.ST.Effect.fsti.checked",
"Steel.ST.Array.fsti.checked",
"Steel.FractionalPermission.fst.checked",
"prims.fst.checked",
"FStar.SizeT.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": true,
"source_file": "Steel.ST.Array.Util.fst"
} | [
{
"abbrev": false,
"full_module": "Steel.ST.Util",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.FractionalPermission",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.ST.Loops",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Steel.ST.Array",
"short_module": "A"
},
{
"abbrev": true,
"full_module": "Steel.ST.Reference",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.SizeT",
"short_module": "US"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": false,
"full_module": "Steel.ST.Util",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.FractionalPermission",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.ST.Array",
"short_module": "A"
},
{
"abbrev": true,
"full_module": "FStar.SizeT",
"short_module": "US"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": false,
"full_module": "Steel.ST.Array",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST.Array",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
n: FStar.SizeT.t ->
a0: Steel.ST.Array.array a ->
a1: Steel.ST.Array.array b ->
p: (_: a -> _: b -> Prims.bool) ->
r: Steel.ST.Reference.ref FStar.SizeT.t ->
p0: Steel.FractionalPermission.perm ->
p1: Steel.FractionalPermission.perm ->
s0: FStar.Ghost.erased (FStar.Seq.Base.seq a) ->
s1: FStar.Ghost.erased (FStar.Seq.Base.seq b) ->
_:
Prims.squash (FStar.Seq.Base.length (FStar.Ghost.reveal s0) == FStar.SizeT.v n /\
FStar.Seq.Base.length (FStar.Ghost.reveal s0) ==
FStar.Seq.Base.length (FStar.Ghost.reveal s1)) ->
_: Prims.unit
-> Steel.ST.Effect.STT Prims.unit | Steel.ST.Effect.STT | [] | [] | [
"FStar.SizeT.t",
"Steel.ST.Array.array",
"Prims.bool",
"Steel.ST.Reference.ref",
"Steel.FractionalPermission.perm",
"FStar.Ghost.erased",
"FStar.Seq.Base.seq",
"Prims.squash",
"Prims.l_and",
"Prims.eq2",
"Prims.nat",
"FStar.Seq.Base.length",
"FStar.Ghost.reveal",
"FStar.SizeT.v",
"Prims.unit",
"Steel.ST.Util.intro_exists",
"FStar.Ghost.hide",
"FStar.Set.set",
"Steel.Memory.iname",
"FStar.Set.empty",
"Prims.op_AmpAmp",
"FStar.SizeT.lt",
"FStar.SizeT.add",
"FStar.SizeT.__uint_to_t",
"FStar.Seq.Base.index",
"Steel.ST.Array.Util.forall2_inv",
"Steel.ST.Array.Util.forall2_pred",
"Steel.ST.Util.intro_pure",
"Steel.ST.Array.Util.forall2_pure_inv_b",
"Steel.ST.Array.Util.forall2_pure_inv",
"Steel.ST.Reference.write",
"Steel.ST.Reference.read",
"Steel.FractionalPermission.full_perm",
"Steel.ST.Util.elim_pure",
"Steel.ST.Util.elim_exists",
"Steel.Effect.Common.VStar",
"Steel.ST.Array.pts_to",
"Steel.ST.Reference.pts_to",
"Steel.ST.Util.pure",
"Steel.Effect.Common.vprop",
"Steel.ST.Util.exists_"
] | [] | false | true | false | false | false | let forall2_body
(#a: Type0)
(#b: Type0)
(n: US.t)
(a0: A.array a)
(a1: A.array b)
(p: (a -> b -> bool))
(r: R.ref US.t)
(p0: perm)
(p1: perm)
(s0: G.erased (Seq.seq a))
(s1: G.erased (Seq.seq b))
(_: squash (Seq.length s0 == US.v n /\ Seq.length s0 == Seq.length s1))
: unit
-> STT unit
(forall2_inv n a0 a1 p r p0 p1 s0 s1 () true)
(fun _ -> exists_ (forall2_inv n a0 a1 p r p0 p1 s0 s1 ())) =
| fun _ ->
let _ = elim_exists () in
elim_pure _;
elim_pure _;
let i = R.read r in
R.write r (US.add i 1sz);
intro_pure (forall2_pure_inv n p s0 s1 () (US.add i 1sz));
intro_pure (forall2_pure_inv_b n
p
s0
s1
()
(US.add i 1sz)
((US.add i 1sz)
`US.lt`
n &&
p (Seq.index s0 (US.v (US.add i 1sz))) (Seq.index s1 (US.v (US.add i 1sz)))));
intro_exists (US.add i 1sz)
(forall2_pred n a0 a1 p r p0 p1 s0 s1 ()
((US.add i 1sz)
`US.lt`
n &&
p (Seq.index s0 (US.v (US.add i 1sz))) (Seq.index s1 (US.v (US.add i 1sz)))));
intro_exists ((US.add i 1sz)
`US.lt`
n &&
p (Seq.index s0 (US.v (US.add i 1sz))) (Seq.index s1 (US.v (US.add i 1sz))))
(forall2_inv n a0 a1 p r p0 p1 s0 s1 ()) | false |
Unification.fst | Unification.unify_eqns_correct | val unify_eqns_correct: e:eqns -> Lemma
(requires True)
(ensures (if None? (unify_eqns e)
then not_solveable_eqns e
else solved (lsubst_eqns (Some?.v (unify_eqns e)) e))) | val unify_eqns_correct: e:eqns -> Lemma
(requires True)
(ensures (if None? (unify_eqns e)
then not_solveable_eqns e
else solved (lsubst_eqns (Some?.v (unify_eqns e)) e))) | let unify_eqns_correct e =
let _ = unify_correct_aux [] e in () | {
"file_name": "examples/algorithms/Unification.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 38,
"end_line": 408,
"start_col": 0,
"start_line": 407
} | (*
Copyright 2008-2015 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(*
A verified implementation of first-order unification.
The termination argument is based on Ana Bove's Licentiate Thesis:
Programming in Martin-Löf Type Theory Unification - A non-trivial Example
Department of Computer Science, Chalmers University of Technology
1999
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.40.3532
But, is done here directly with lex orderings.
*)
module Unification
open FStar.List.Tot
(* First, a missing lemma from the list library *)
let op_At = append
val lemma_shift_append: #a:eqtype -> l:list a -> x:a -> m:list a -> Lemma
(ensures ( (l@(x::m)) = ((l@[x])@m)))
let rec lemma_shift_append #a l x m = match l with
| [] -> ()
| hd::tl -> lemma_shift_append tl x m
(* A term language with variables V and pairs F *)
type term =
| V : i:nat -> term
| F : t1:term -> t2:term -> term
(* Finite, ordered sets of variables *)
val nat_order : OrdSet.cmp nat
let nat_order :(f:(nat -> nat -> bool){OrdSet.total_order nat f}) = fun x y -> x <= y
type varset = OrdSet.ordset nat nat_order
val empty_vars : varset
let empty_vars = OrdSet.empty
(* Collecting the variables in a term *)
val vars : term -> Tot varset
let rec vars = function
| V i -> OrdSet.singleton i
| F t1 t2 -> OrdSet.union (vars t1) (vars t2)
(* Equations among pairs of terms, to be solved *)
type eqns = list (term * term)
(* All the variables in a set of equations *)
val evars : eqns -> Tot varset
let rec evars = function
| [] -> empty_vars
| (x, y)::tl -> OrdSet.union (OrdSet.union (vars x) (vars y)) (evars tl)
(* Counting variables; used in the termination metric *)
val n_evars : eqns -> Tot nat
let n_evars eqns = OrdSet.size (evars eqns)
(* Counting the number of F-applications; also for the termination metric *)
val funs : term -> Tot nat
let rec funs = function
| V _ -> 0
| F t1 t2 -> 1 + funs t1 + funs t2
val efuns : eqns -> Tot nat
let rec efuns = function
| [] -> 0
| (x,y)::tl -> funs x + funs y + efuns tl
(* Counting the number of equations with variables on the the RHS;
// also for the termination metric *)
val n_flex_rhs : eqns -> Tot nat
let rec n_flex_rhs = function
| [] -> 0
| (V _, V _)::tl
| (_ , V _)::tl -> 1 + n_flex_rhs tl
| _::tl -> n_flex_rhs tl
(* A point substitution *)
type subst = (nat * term)
(* Composition of point substitutions *)
type lsubst = list subst
val subst_term : subst -> term -> Tot term
let rec subst_term s t = match t with
| V x -> if x = fst s then snd s else V x
| F t1 t2 -> F (subst_term s t1) (subst_term s t2)
val lsubst_term : list subst -> term -> Tot term
let lsubst_term = fold_right subst_term
let occurs x t = OrdSet.mem x (vars t)
let ok s = not (occurs (fst s) (snd s))
val lsubst_eqns: list subst -> eqns -> Tot eqns
let rec lsubst_eqns l = function
| [] -> []
| (x,y)::tl -> (lsubst_term l x, lsubst_term l y)::lsubst_eqns l tl
val lemma_lsubst_eqns_nil: e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns [] e = e))
[SMTPat (lsubst_eqns [] e)]
let rec lemma_lsubst_eqns_nil = function
| [] -> ()
| _::tl -> lemma_lsubst_eqns_nil tl
(* A couple of lemmas about variable counts.
// Both of these rely on extensional equality of sets.
// So we need to use eq_lemma explicitly *)
val evars_permute_hd : x:term -> y:term -> tl:eqns -> Lemma
(ensures (evars ((x, y)::tl) = evars ((y, x)::tl)))
let evars_permute_hd x y tl = OrdSet.eq_lemma (evars ((x,y)::tl)) (evars ((y,x)::tl))
val evars_unfun : x:term -> y:term -> x':term -> y':term -> tl:eqns -> Lemma
(ensures (evars ((F x y, F x' y')::tl) = evars ((x, x')::(y, y')::tl)))
let evars_unfun x y x' y' tl = OrdSet.eq_lemma (evars ((F x y, F x' y')::tl)) (evars ((x, x')::(y, y')::tl))
(* Eliminating a variable reduces the variable count *)
val lemma_vars_decrease: s:subst -> t':term -> Lemma
(requires (ok s))
(ensures (OrdSet.subset (vars (subst_term s t'))
(OrdSet.remove (fst s) (OrdSet.union (vars (snd s)) (vars t')))))
let rec lemma_vars_decrease s t' = match t' with
| V x -> ()
| F t1 t2 ->
lemma_vars_decrease s t1;
lemma_vars_decrease s t2
(* Lifting the prior lemma to equations *)
val vars_decrease_eqns: x:nat -> t:term -> e:eqns -> Lemma
(requires (ok (x, t)))
(ensures (OrdSet.subset (evars (lsubst_eqns [x,t] e))
(OrdSet.remove x (evars ((V x, t)::e)))))
let rec vars_decrease_eqns x t e = match e with
| [] -> ()
| hd::tl -> lemma_vars_decrease (x,t) (fst hd);
lemma_vars_decrease (x,t) (snd hd);
vars_decrease_eqns x t tl
val unify : e:eqns -> list subst -> Tot (option (list subst))
(decreases %[n_evars e; efuns e; n_flex_rhs e])
let rec unify e s = match e with
| [] -> Some s
| (V x, t)::tl ->
if V? t && V?.i t = x
then unify tl s //t is a flex-rhs
else if occurs x t
then None
else (vars_decrease_eqns x t tl;
unify (lsubst_eqns [x,t] tl) ((x,t)::s))
| (t, V x)::tl -> //flex-rhs
evars_permute_hd t (V x) tl;
unify ((V x, t)::tl) s
| (F t1 t2, F t1' t2')::tl -> //efuns reduces
evars_unfun t1 t2 t1' t2' tl;
unify ((t1, t1')::(t2, t2')::tl) s
(* All equations are solved when each one is just reflexive *)
val solved : eqns -> Tot bool
let rec solved = function
| [] -> true
| (x,y)::tl -> x=y && solved tl
val lsubst_distributes_over_F: l:list subst -> t1:term -> t2:term -> Lemma
(requires (True))
(ensures (lsubst_term l (F t1 t2) = F (lsubst_term l t1) (lsubst_term l t2)))
[SMTPat (lsubst_term l (F t1 t2))]
let rec lsubst_distributes_over_F l t1 t2 = match l with
| [] -> ()
| hd::tl -> lsubst_distributes_over_F tl t1 t2
let extend_subst s l = s::l
let extend_lsubst l l' = l @ l'
val lemma_extend_lsubst_distributes_term: l:list subst -> l':list subst -> e:term -> Lemma
(requires True)
(ensures (lsubst_term (extend_lsubst l l') e = lsubst_term l (lsubst_term l' e)))
let rec lemma_extend_lsubst_distributes_term l l' e = match l with
| [] -> ()
| hd::tl -> lemma_extend_lsubst_distributes_term tl l' e
val lemma_extend_lsubst_distributes_eqns: l:list subst -> l':list subst -> e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns (extend_lsubst l l') e = lsubst_eqns l (lsubst_eqns l' e)))
[SMTPat (lsubst_eqns (extend_lsubst l l') e)]
let rec lemma_extend_lsubst_distributes_eqns l l' e = match e with
| [] -> ()
| (t1, t2)::tl ->
lemma_extend_lsubst_distributes_term l l' t1;
lemma_extend_lsubst_distributes_term l l' t2;
lemma_extend_lsubst_distributes_eqns l l' tl
val lemma_subst_id: x:nat -> z:term -> y:term -> Lemma
(requires (not (occurs x y)))
(ensures (subst_term (x,z) y = y))
let rec lemma_subst_id x z y = match y with
| V x' -> ()
| F t1 t2 -> lemma_subst_id x z t1; lemma_subst_id x z t2
let neutral s (x, t) = subst_term s (V x) = V x && subst_term s t = t && ok (x, t)
let neutral_l l (x, t) = lsubst_term l (V x) = V x && lsubst_term l t = t && ok (x, t)
val lemma_lsubst_term_commutes: s:subst -> l:list subst -> e:term -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_term [s] (lsubst_term l (subst_term s e)) =
lsubst_term [s] (lsubst_term l e)))
let rec lemma_lsubst_term_commutes s l e = match e with
| V y -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2
val lemma_lsubst_eqns_commutes: s:subst -> l:list subst -> e:eqns -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_eqns [s] (lsubst_eqns l (lsubst_eqns [s] e)) =
lsubst_eqns [s] (lsubst_eqns l e)))
let rec lemma_lsubst_eqns_commutes s l = function
| [] -> ()
| (t1,t2)::tl -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2;
lemma_lsubst_eqns_commutes s l tl
let (++) l1 l2 = extend_lsubst l1 l2
let sub l e = lsubst_eqns l e
let test l1 l2 l3 = assert (l1 ++ l2 ++ l3 == (l1 ++ l2) ++ l3)
val key_lemma: x:nat -> y:term -> tl:eqns -> l:list subst -> lpre:list subst -> l'':list subst -> Lemma
(requires (l'' = lpre ++ ([x,y] ++ l)
/\ not (occurs x y)
/\ l `sub` ((V x, y)::tl) = (V x, y)::tl
/\ solved (l'' `sub` ([x, y] `sub` tl))))
(ensures (solved (l'' `sub` ((V x,y)::tl))))
let key_lemma x y tl l lpre l'' =
let xy = [x,y] in
let xyl = xy++l in
let vxy = V x, y in
assert (l'' `sub` (vxy::tl)
== lpre `sub` (xy `sub` (l `sub` (vxy::tl))));
lemma_lsubst_eqns_commutes (x,y) l (vxy :: tl);
assert (lpre `sub` (xy `sub` (l `sub` (vxy::tl)))
== lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl)))));
assert (lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl))))
== l'' `sub` (xy `sub` (vxy :: tl)));
lemma_subst_id x y y
val lemma_subst_term_idem: s:subst -> t:term -> Lemma
(requires (ok s))
(ensures (subst_term s (subst_term s t) = subst_term s t))
let rec lemma_subst_term_idem s t = match t with
| V x -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_subst_term_idem s t1; lemma_subst_term_idem s t2
val lemma_subst_eqns_idem: s:subst -> e:eqns -> Lemma
(requires (ok s))
(ensures (lsubst_eqns [s] (lsubst_eqns [s] e) = lsubst_eqns [s] e))
let rec lemma_subst_eqns_idem s = function
| [] -> ()
| (x, y)::tl -> lemma_subst_eqns_idem s tl;
lemma_subst_term_idem s x;
lemma_subst_term_idem s y
val subst_funs_monotone: s:subst -> t:term -> Lemma
(ensures (funs (subst_term s t) >= funs t))
let rec subst_funs_monotone s = function
| V x -> ()
| F t1 t2 -> subst_funs_monotone s t1; subst_funs_monotone s t2
val lsubst_funs_monotone: l:list subst -> t:term -> Lemma
(ensures (funs (lsubst_term l t) >= funs t))
let rec lsubst_funs_monotone l t = match l with
| [] -> ()
| hd::tl ->
lsubst_funs_monotone tl t; subst_funs_monotone hd (lsubst_term tl t)
val lemma_occurs_not_solveable_aux: x:nat -> t:term{occurs x t /\ not (V? t)} -> s:list subst -> Lemma
(funs (lsubst_term s t) >= (funs t + funs (lsubst_term s (V x))))
let rec lemma_occurs_not_solveable_aux x t s = match t with
| F t1 t2 ->
if occurs x t1
then let _ = lsubst_funs_monotone s t2 in
match t1 with
| V y -> ()
| _ -> lemma_occurs_not_solveable_aux x t1 s
else if occurs x t2
then let _ = lsubst_funs_monotone s t1 in
match t2 with
| V y -> ()
| _ -> lemma_occurs_not_solveable_aux x t2 s
else ()
type not_solveable s =
forall l. lsubst_term l (fst s) <> lsubst_term l (snd s)
val lemma_occurs_not_solveable: x:nat -> t:term -> Lemma
(requires (occurs x t /\ not (V? t)))
(ensures (not_solveable (V x, t)))
let lemma_occurs_not_solveable x t = FStar.Classical.forall_intro (lemma_occurs_not_solveable_aux x t)
val lemma_subst_idem: l:list subst -> x:nat -> t:term -> t':term -> Lemma
(requires (lsubst_term l (V x) = lsubst_term l t))
(ensures (lsubst_term l (subst_term (x,t) t') = lsubst_term l t'))
let rec lemma_subst_idem l x t t' = match t' with
| V y -> ()
| F t1 t2 -> lemma_subst_idem l x t t1; lemma_subst_idem l x t t2
val lemma_subst_eqns: l:list subst -> x:nat -> t:term -> e:eqns -> Lemma
(requires (lsubst_term l (V x) = lsubst_term l t))
(ensures (lsubst_eqns l (lsubst_eqns [x,t] e) = lsubst_eqns l e))
let rec lemma_subst_eqns l x t = function
| [] -> ()
| (t1,t2)::tl ->
lemma_subst_idem l x t t1;
lemma_subst_idem l x t t2;
lemma_subst_eqns l x t tl
type not_solveable_eqns e =
forall l. not (solved (lsubst_eqns l e))
val lemma_not_solveable_cons_aux: x:nat -> t:term -> tl:eqns -> l:list subst -> Lemma
(requires (not_solveable_eqns (lsubst_eqns [x,t] tl)
/\ solved (lsubst_eqns l ((V x,t)::tl))))
(ensures False)
[SMTPat (solved (lsubst_eqns l ((V x,t)::tl)))]
let lemma_not_solveable_cons_aux x t tl l = lemma_subst_eqns l x t tl
val lemma_not_solveable_cons: x:nat -> t:term -> tl:eqns -> Lemma
(requires (not_solveable_eqns (lsubst_eqns [x,t] tl)))
(ensures (not_solveable_eqns ((V x, t)::tl)))
let lemma_not_solveable_cons x t tl = ()
val unify_correct_aux: l:list subst -> e:eqns -> Pure (list subst)
(requires (b2t (lsubst_eqns l e = e)))
(ensures (fun m ->
match unify e l with
| None -> not_solveable_eqns e
| Some l' ->
l' = (m @ l)
/\ solved (lsubst_eqns l' e)))
(decreases %[n_evars e; efuns e; n_flex_rhs e])
#set-options "--z3rlimit 100"
let rec unify_correct_aux l = function
| [] -> []
| hd::tl ->
begin match hd with
| (V x, y) ->
if V? y && V?.i y=x
then unify_correct_aux l tl
else if occurs x y
then (lemma_occurs_not_solveable x y; [])
else begin
let s = (x,y) in
let l' = extend_subst s l in
vars_decrease_eqns x y tl;
let tl' = lsubst_eqns [s] tl in
lemma_extend_lsubst_distributes_eqns [s] l tl;
assert (lsubst_eqns l' tl' = lsubst_eqns [s] (lsubst_eqns l (lsubst_eqns [s] tl)));
lemma_lsubst_eqns_commutes s l tl;
lemma_subst_eqns_idem s tl;
let lpre = unify_correct_aux l' tl' in
begin match unify tl' l' with
| None -> lemma_not_solveable_cons x y tl; []
| Some l'' ->
key_lemma x y tl l lpre l'';
lemma_shift_append lpre s l;
lpre@[s]
end
end
| (y, V x) ->
evars_permute_hd y (V x) tl;
unify_correct_aux l ((V x, y)::tl)
| F t1 t2, F s1 s2 ->
evars_unfun t1 t2 s1 s2 tl;
unify_correct_aux l ((t1, s1)::(t2, s2)::tl)
end
val unify_eqns : e:eqns -> Tot (option lsubst)
let unify_eqns e = unify e []
val unify_eqns_correct: e:eqns -> Lemma
(requires True)
(ensures (if None? (unify_eqns e)
then not_solveable_eqns e | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.OrdSet.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "Unification.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | e: Unification.eqns
-> FStar.Pervasives.Lemma
(ensures
((match None? (Unification.unify_eqns e) with
| true -> Unification.not_solveable_eqns e
| _ -> Unification.solved (Unification.lsubst_eqns (Some?.v (Unification.unify_eqns e)) e)
)
<:
Type0)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Unification.eqns",
"Prims.list",
"Unification.subst",
"Unification.unify_correct_aux",
"Prims.Nil",
"Prims.unit"
] | [] | true | false | true | false | false | let unify_eqns_correct e =
| let _ = unify_correct_aux [] e in
() | false |
Unification.fst | Unification.unify_correct_aux | val unify_correct_aux: l:list subst -> e:eqns -> Pure (list subst)
(requires (b2t (lsubst_eqns l e = e)))
(ensures (fun m ->
match unify e l with
| None -> not_solveable_eqns e
| Some l' ->
l' = (m @ l)
/\ solved (lsubst_eqns l' e)))
(decreases %[n_evars e; efuns e; n_flex_rhs e]) | val unify_correct_aux: l:list subst -> e:eqns -> Pure (list subst)
(requires (b2t (lsubst_eqns l e = e)))
(ensures (fun m ->
match unify e l with
| None -> not_solveable_eqns e
| Some l' ->
l' = (m @ l)
/\ solved (lsubst_eqns l' e)))
(decreases %[n_evars e; efuns e; n_flex_rhs e]) | let rec unify_correct_aux l = function
| [] -> []
| hd::tl ->
begin match hd with
| (V x, y) ->
if V? y && V?.i y=x
then unify_correct_aux l tl
else if occurs x y
then (lemma_occurs_not_solveable x y; [])
else begin
let s = (x,y) in
let l' = extend_subst s l in
vars_decrease_eqns x y tl;
let tl' = lsubst_eqns [s] tl in
lemma_extend_lsubst_distributes_eqns [s] l tl;
assert (lsubst_eqns l' tl' = lsubst_eqns [s] (lsubst_eqns l (lsubst_eqns [s] tl)));
lemma_lsubst_eqns_commutes s l tl;
lemma_subst_eqns_idem s tl;
let lpre = unify_correct_aux l' tl' in
begin match unify tl' l' with
| None -> lemma_not_solveable_cons x y tl; []
| Some l'' ->
key_lemma x y tl l lpre l'';
lemma_shift_append lpre s l;
lpre@[s]
end
end
| (y, V x) ->
evars_permute_hd y (V x) tl;
unify_correct_aux l ((V x, y)::tl)
| F t1 t2, F s1 s2 ->
evars_unfun t1 t2 s1 s2 tl;
unify_correct_aux l ((t1, s1)::(t2, s2)::tl)
end | {
"file_name": "examples/algorithms/Unification.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 8,
"end_line": 397,
"start_col": 0,
"start_line": 362
} | (*
Copyright 2008-2015 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(*
A verified implementation of first-order unification.
The termination argument is based on Ana Bove's Licentiate Thesis:
Programming in Martin-Löf Type Theory Unification - A non-trivial Example
Department of Computer Science, Chalmers University of Technology
1999
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.40.3532
But, is done here directly with lex orderings.
*)
module Unification
open FStar.List.Tot
(* First, a missing lemma from the list library *)
let op_At = append
val lemma_shift_append: #a:eqtype -> l:list a -> x:a -> m:list a -> Lemma
(ensures ( (l@(x::m)) = ((l@[x])@m)))
let rec lemma_shift_append #a l x m = match l with
| [] -> ()
| hd::tl -> lemma_shift_append tl x m
(* A term language with variables V and pairs F *)
type term =
| V : i:nat -> term
| F : t1:term -> t2:term -> term
(* Finite, ordered sets of variables *)
val nat_order : OrdSet.cmp nat
let nat_order :(f:(nat -> nat -> bool){OrdSet.total_order nat f}) = fun x y -> x <= y
type varset = OrdSet.ordset nat nat_order
val empty_vars : varset
let empty_vars = OrdSet.empty
(* Collecting the variables in a term *)
val vars : term -> Tot varset
let rec vars = function
| V i -> OrdSet.singleton i
| F t1 t2 -> OrdSet.union (vars t1) (vars t2)
(* Equations among pairs of terms, to be solved *)
type eqns = list (term * term)
(* All the variables in a set of equations *)
val evars : eqns -> Tot varset
let rec evars = function
| [] -> empty_vars
| (x, y)::tl -> OrdSet.union (OrdSet.union (vars x) (vars y)) (evars tl)
(* Counting variables; used in the termination metric *)
val n_evars : eqns -> Tot nat
let n_evars eqns = OrdSet.size (evars eqns)
(* Counting the number of F-applications; also for the termination metric *)
val funs : term -> Tot nat
let rec funs = function
| V _ -> 0
| F t1 t2 -> 1 + funs t1 + funs t2
val efuns : eqns -> Tot nat
let rec efuns = function
| [] -> 0
| (x,y)::tl -> funs x + funs y + efuns tl
(* Counting the number of equations with variables on the the RHS;
// also for the termination metric *)
val n_flex_rhs : eqns -> Tot nat
let rec n_flex_rhs = function
| [] -> 0
| (V _, V _)::tl
| (_ , V _)::tl -> 1 + n_flex_rhs tl
| _::tl -> n_flex_rhs tl
(* A point substitution *)
type subst = (nat * term)
(* Composition of point substitutions *)
type lsubst = list subst
val subst_term : subst -> term -> Tot term
let rec subst_term s t = match t with
| V x -> if x = fst s then snd s else V x
| F t1 t2 -> F (subst_term s t1) (subst_term s t2)
val lsubst_term : list subst -> term -> Tot term
let lsubst_term = fold_right subst_term
let occurs x t = OrdSet.mem x (vars t)
let ok s = not (occurs (fst s) (snd s))
val lsubst_eqns: list subst -> eqns -> Tot eqns
let rec lsubst_eqns l = function
| [] -> []
| (x,y)::tl -> (lsubst_term l x, lsubst_term l y)::lsubst_eqns l tl
val lemma_lsubst_eqns_nil: e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns [] e = e))
[SMTPat (lsubst_eqns [] e)]
let rec lemma_lsubst_eqns_nil = function
| [] -> ()
| _::tl -> lemma_lsubst_eqns_nil tl
(* A couple of lemmas about variable counts.
// Both of these rely on extensional equality of sets.
// So we need to use eq_lemma explicitly *)
val evars_permute_hd : x:term -> y:term -> tl:eqns -> Lemma
(ensures (evars ((x, y)::tl) = evars ((y, x)::tl)))
let evars_permute_hd x y tl = OrdSet.eq_lemma (evars ((x,y)::tl)) (evars ((y,x)::tl))
val evars_unfun : x:term -> y:term -> x':term -> y':term -> tl:eqns -> Lemma
(ensures (evars ((F x y, F x' y')::tl) = evars ((x, x')::(y, y')::tl)))
let evars_unfun x y x' y' tl = OrdSet.eq_lemma (evars ((F x y, F x' y')::tl)) (evars ((x, x')::(y, y')::tl))
(* Eliminating a variable reduces the variable count *)
val lemma_vars_decrease: s:subst -> t':term -> Lemma
(requires (ok s))
(ensures (OrdSet.subset (vars (subst_term s t'))
(OrdSet.remove (fst s) (OrdSet.union (vars (snd s)) (vars t')))))
let rec lemma_vars_decrease s t' = match t' with
| V x -> ()
| F t1 t2 ->
lemma_vars_decrease s t1;
lemma_vars_decrease s t2
(* Lifting the prior lemma to equations *)
val vars_decrease_eqns: x:nat -> t:term -> e:eqns -> Lemma
(requires (ok (x, t)))
(ensures (OrdSet.subset (evars (lsubst_eqns [x,t] e))
(OrdSet.remove x (evars ((V x, t)::e)))))
let rec vars_decrease_eqns x t e = match e with
| [] -> ()
| hd::tl -> lemma_vars_decrease (x,t) (fst hd);
lemma_vars_decrease (x,t) (snd hd);
vars_decrease_eqns x t tl
val unify : e:eqns -> list subst -> Tot (option (list subst))
(decreases %[n_evars e; efuns e; n_flex_rhs e])
let rec unify e s = match e with
| [] -> Some s
| (V x, t)::tl ->
if V? t && V?.i t = x
then unify tl s //t is a flex-rhs
else if occurs x t
then None
else (vars_decrease_eqns x t tl;
unify (lsubst_eqns [x,t] tl) ((x,t)::s))
| (t, V x)::tl -> //flex-rhs
evars_permute_hd t (V x) tl;
unify ((V x, t)::tl) s
| (F t1 t2, F t1' t2')::tl -> //efuns reduces
evars_unfun t1 t2 t1' t2' tl;
unify ((t1, t1')::(t2, t2')::tl) s
(* All equations are solved when each one is just reflexive *)
val solved : eqns -> Tot bool
let rec solved = function
| [] -> true
| (x,y)::tl -> x=y && solved tl
val lsubst_distributes_over_F: l:list subst -> t1:term -> t2:term -> Lemma
(requires (True))
(ensures (lsubst_term l (F t1 t2) = F (lsubst_term l t1) (lsubst_term l t2)))
[SMTPat (lsubst_term l (F t1 t2))]
let rec lsubst_distributes_over_F l t1 t2 = match l with
| [] -> ()
| hd::tl -> lsubst_distributes_over_F tl t1 t2
let extend_subst s l = s::l
let extend_lsubst l l' = l @ l'
val lemma_extend_lsubst_distributes_term: l:list subst -> l':list subst -> e:term -> Lemma
(requires True)
(ensures (lsubst_term (extend_lsubst l l') e = lsubst_term l (lsubst_term l' e)))
let rec lemma_extend_lsubst_distributes_term l l' e = match l with
| [] -> ()
| hd::tl -> lemma_extend_lsubst_distributes_term tl l' e
val lemma_extend_lsubst_distributes_eqns: l:list subst -> l':list subst -> e:eqns -> Lemma
(requires True)
(ensures (lsubst_eqns (extend_lsubst l l') e = lsubst_eqns l (lsubst_eqns l' e)))
[SMTPat (lsubst_eqns (extend_lsubst l l') e)]
let rec lemma_extend_lsubst_distributes_eqns l l' e = match e with
| [] -> ()
| (t1, t2)::tl ->
lemma_extend_lsubst_distributes_term l l' t1;
lemma_extend_lsubst_distributes_term l l' t2;
lemma_extend_lsubst_distributes_eqns l l' tl
val lemma_subst_id: x:nat -> z:term -> y:term -> Lemma
(requires (not (occurs x y)))
(ensures (subst_term (x,z) y = y))
let rec lemma_subst_id x z y = match y with
| V x' -> ()
| F t1 t2 -> lemma_subst_id x z t1; lemma_subst_id x z t2
let neutral s (x, t) = subst_term s (V x) = V x && subst_term s t = t && ok (x, t)
let neutral_l l (x, t) = lsubst_term l (V x) = V x && lsubst_term l t = t && ok (x, t)
val lemma_lsubst_term_commutes: s:subst -> l:list subst -> e:term -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_term [s] (lsubst_term l (subst_term s e)) =
lsubst_term [s] (lsubst_term l e)))
let rec lemma_lsubst_term_commutes s l e = match e with
| V y -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2
val lemma_lsubst_eqns_commutes: s:subst -> l:list subst -> e:eqns -> Lemma
(requires (neutral_l l s))
(ensures (lsubst_eqns [s] (lsubst_eqns l (lsubst_eqns [s] e)) =
lsubst_eqns [s] (lsubst_eqns l e)))
let rec lemma_lsubst_eqns_commutes s l = function
| [] -> ()
| (t1,t2)::tl -> lemma_lsubst_term_commutes s l t1;
lemma_lsubst_term_commutes s l t2;
lemma_lsubst_eqns_commutes s l tl
let (++) l1 l2 = extend_lsubst l1 l2
let sub l e = lsubst_eqns l e
let test l1 l2 l3 = assert (l1 ++ l2 ++ l3 == (l1 ++ l2) ++ l3)
val key_lemma: x:nat -> y:term -> tl:eqns -> l:list subst -> lpre:list subst -> l'':list subst -> Lemma
(requires (l'' = lpre ++ ([x,y] ++ l)
/\ not (occurs x y)
/\ l `sub` ((V x, y)::tl) = (V x, y)::tl
/\ solved (l'' `sub` ([x, y] `sub` tl))))
(ensures (solved (l'' `sub` ((V x,y)::tl))))
let key_lemma x y tl l lpre l'' =
let xy = [x,y] in
let xyl = xy++l in
let vxy = V x, y in
assert (l'' `sub` (vxy::tl)
== lpre `sub` (xy `sub` (l `sub` (vxy::tl))));
lemma_lsubst_eqns_commutes (x,y) l (vxy :: tl);
assert (lpre `sub` (xy `sub` (l `sub` (vxy::tl)))
== lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl)))));
assert (lpre `sub` (xy `sub` (l `sub` (xy `sub` (vxy::tl))))
== l'' `sub` (xy `sub` (vxy :: tl)));
lemma_subst_id x y y
val lemma_subst_term_idem: s:subst -> t:term -> Lemma
(requires (ok s))
(ensures (subst_term s (subst_term s t) = subst_term s t))
let rec lemma_subst_term_idem s t = match t with
| V x -> lemma_subst_id (fst s) (snd s) (snd s)
| F t1 t2 -> lemma_subst_term_idem s t1; lemma_subst_term_idem s t2
val lemma_subst_eqns_idem: s:subst -> e:eqns -> Lemma
(requires (ok s))
(ensures (lsubst_eqns [s] (lsubst_eqns [s] e) = lsubst_eqns [s] e))
let rec lemma_subst_eqns_idem s = function
| [] -> ()
| (x, y)::tl -> lemma_subst_eqns_idem s tl;
lemma_subst_term_idem s x;
lemma_subst_term_idem s y
val subst_funs_monotone: s:subst -> t:term -> Lemma
(ensures (funs (subst_term s t) >= funs t))
let rec subst_funs_monotone s = function
| V x -> ()
| F t1 t2 -> subst_funs_monotone s t1; subst_funs_monotone s t2
val lsubst_funs_monotone: l:list subst -> t:term -> Lemma
(ensures (funs (lsubst_term l t) >= funs t))
let rec lsubst_funs_monotone l t = match l with
| [] -> ()
| hd::tl ->
lsubst_funs_monotone tl t; subst_funs_monotone hd (lsubst_term tl t)
val lemma_occurs_not_solveable_aux: x:nat -> t:term{occurs x t /\ not (V? t)} -> s:list subst -> Lemma
(funs (lsubst_term s t) >= (funs t + funs (lsubst_term s (V x))))
let rec lemma_occurs_not_solveable_aux x t s = match t with
| F t1 t2 ->
if occurs x t1
then let _ = lsubst_funs_monotone s t2 in
match t1 with
| V y -> ()
| _ -> lemma_occurs_not_solveable_aux x t1 s
else if occurs x t2
then let _ = lsubst_funs_monotone s t1 in
match t2 with
| V y -> ()
| _ -> lemma_occurs_not_solveable_aux x t2 s
else ()
type not_solveable s =
forall l. lsubst_term l (fst s) <> lsubst_term l (snd s)
val lemma_occurs_not_solveable: x:nat -> t:term -> Lemma
(requires (occurs x t /\ not (V? t)))
(ensures (not_solveable (V x, t)))
let lemma_occurs_not_solveable x t = FStar.Classical.forall_intro (lemma_occurs_not_solveable_aux x t)
val lemma_subst_idem: l:list subst -> x:nat -> t:term -> t':term -> Lemma
(requires (lsubst_term l (V x) = lsubst_term l t))
(ensures (lsubst_term l (subst_term (x,t) t') = lsubst_term l t'))
let rec lemma_subst_idem l x t t' = match t' with
| V y -> ()
| F t1 t2 -> lemma_subst_idem l x t t1; lemma_subst_idem l x t t2
val lemma_subst_eqns: l:list subst -> x:nat -> t:term -> e:eqns -> Lemma
(requires (lsubst_term l (V x) = lsubst_term l t))
(ensures (lsubst_eqns l (lsubst_eqns [x,t] e) = lsubst_eqns l e))
let rec lemma_subst_eqns l x t = function
| [] -> ()
| (t1,t2)::tl ->
lemma_subst_idem l x t t1;
lemma_subst_idem l x t t2;
lemma_subst_eqns l x t tl
type not_solveable_eqns e =
forall l. not (solved (lsubst_eqns l e))
val lemma_not_solveable_cons_aux: x:nat -> t:term -> tl:eqns -> l:list subst -> Lemma
(requires (not_solveable_eqns (lsubst_eqns [x,t] tl)
/\ solved (lsubst_eqns l ((V x,t)::tl))))
(ensures False)
[SMTPat (solved (lsubst_eqns l ((V x,t)::tl)))]
let lemma_not_solveable_cons_aux x t tl l = lemma_subst_eqns l x t tl
val lemma_not_solveable_cons: x:nat -> t:term -> tl:eqns -> Lemma
(requires (not_solveable_eqns (lsubst_eqns [x,t] tl)))
(ensures (not_solveable_eqns ((V x, t)::tl)))
let lemma_not_solveable_cons x t tl = ()
val unify_correct_aux: l:list subst -> e:eqns -> Pure (list subst)
(requires (b2t (lsubst_eqns l e = e)))
(ensures (fun m ->
match unify e l with
| None -> not_solveable_eqns e
| Some l' ->
l' = (m @ l)
/\ solved (lsubst_eqns l' e)))
(decreases %[n_evars e; efuns e; n_flex_rhs e]) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.OrdSet.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "Unification.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list Unification.subst -> e: Unification.eqns -> Prims.Pure (Prims.list Unification.subst) | Prims.Pure | [
""
] | [] | [
"Prims.list",
"Unification.subst",
"Unification.eqns",
"Prims.Nil",
"FStar.Pervasives.Native.tuple2",
"Unification.term",
"Prims.nat",
"Prims.op_AmpAmp",
"Unification.uu___is_V",
"Prims.op_Equality",
"Unification.__proj__V__item__i",
"Unification.unify_correct_aux",
"Prims.bool",
"Unification.occurs",
"Prims.unit",
"Unification.lemma_occurs_not_solveable",
"Unification.unify",
"Unification.lemma_not_solveable_cons",
"Unification.op_At",
"Prims.Cons",
"Unification.lemma_shift_append",
"Unification.key_lemma",
"Unification.lemma_subst_eqns_idem",
"Unification.lemma_lsubst_eqns_commutes",
"Prims._assert",
"Prims.b2t",
"Unification.lsubst_eqns",
"Unification.lemma_extend_lsubst_distributes_eqns",
"Unification.vars_decrease_eqns",
"Unification.extend_subst",
"FStar.Pervasives.Native.Mktuple2",
"Unification.V",
"Unification.evars_permute_hd",
"Unification.evars_unfun"
] | [
"recursion"
] | false | false | false | false | false | let rec unify_correct_aux l =
| function
| [] -> []
| hd :: tl ->
match hd with
| V x, y ->
if V? y && V?.i y = x
then unify_correct_aux l tl
else
if occurs x y
then
(lemma_occurs_not_solveable x y;
[])
else
let s = (x, y) in
let l' = extend_subst s l in
vars_decrease_eqns x y tl;
let tl' = lsubst_eqns [s] tl in
lemma_extend_lsubst_distributes_eqns [s] l tl;
assert (lsubst_eqns l' tl' = lsubst_eqns [s] (lsubst_eqns l (lsubst_eqns [s] tl)));
lemma_lsubst_eqns_commutes s l tl;
lemma_subst_eqns_idem s tl;
let lpre = unify_correct_aux l' tl' in
(match unify tl' l' with
| None ->
lemma_not_solveable_cons x y tl;
[]
| Some l'' ->
key_lemma x y tl l lpre l'';
lemma_shift_append lpre s l;
lpre @ [s])
| y, V x ->
evars_permute_hd y (V x) tl;
unify_correct_aux l ((V x, y) :: tl)
| F t1 t2, F s1 s2 ->
evars_unfun t1 t2 s1 s2 tl;
unify_correct_aux l ((t1, s1) :: (t2, s2) :: tl) | false |
FStar.OrdSet.fst | FStar.OrdSet.base_induction | val 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) | val 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) | 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))) | {
"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": 37,
"start_col": 0,
"start_line": 29
} | (*
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))) | {
"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 | p: (_: FStar.OrdSet.ordset t f -> Type0) -> x: FStar.OrdSet.ordset t f
-> FStar.Pervasives.Lemma
(requires
(forall (l: FStar.OrdSet.ordset t f {FStar.List.Tot.Base.length l < 2}). p l) /\
(forall (l: FStar.OrdSet.ordset t f {Cons? l}). p (Cons?.tl l) ==> p l))
(ensures p x)
(decreases FStar.List.Tot.Base.length x) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.op_LessThan",
"FStar.List.Tot.Base.length",
"Prims.bool",
"Prims.list",
"Prims._assert",
"Prims.__proj__Cons__item__tl",
"Prims.Cons",
"Prims.unit",
"FStar.OrdSet.base_induction",
"Prims.l_and",
"Prims.l_Forall",
"Prims.b2t",
"Prims.uu___is_Cons",
"Prims.l_imp",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | 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))) | false |
FStar.OrdSet.fst | FStar.OrdSet.empty | val empty : #a:eqtype -> #f:cmp a -> Tot (ordset a f) | val empty : #a:eqtype -> #f:cmp a -> Tot (ordset a f) | let empty #_ #_ = [] | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 20,
"end_line": 39,
"start_col": 0,
"start_line": 39
} | (*
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))) | {
"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 | FStar.OrdSet.ordset a f | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"Prims.Nil",
"FStar.OrdSet.ordset"
] | [] | false | false | false | false | false | let empty #_ #_ =
| [] | false |
FStar.OrdSet.fst | FStar.OrdSet.mem | val mem : #a:eqtype -> #f:cmp a -> a -> s:ordset a f -> Tot bool | val mem : #a:eqtype -> #f:cmp a -> a -> s:ordset a f -> Tot bool | let mem #_ #_ x s = List.Tot.mem x s | {
"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": 44,
"start_col": 0,
"start_line": 44
} | (*
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 | {
"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 | x: a -> s: FStar.OrdSet.ordset a f -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.List.Tot.Base.mem",
"Prims.bool"
] | [] | false | false | false | false | false | let mem #_ #_ x s =
| List.Tot.mem x s | false |
FStar.OrdSet.fst | FStar.OrdSet.simple_induction | val 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) | val 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) | 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))) | {
"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": 27,
"start_col": 0,
"start_line": 22
} | (*
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 _ _ = () | {
"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 | p: (_: FStar.OrdSet.ordset t f -> Type0) -> x: FStar.OrdSet.ordset t f
-> FStar.Pervasives.Lemma
(requires p [] /\ (forall (l: FStar.OrdSet.ordset t f {Cons? l}). p (Cons?.tl l) ==> p l))
(ensures p x) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.list",
"Prims._assert",
"Prims.__proj__Cons__item__tl",
"Prims.Cons",
"Prims.unit",
"FStar.OrdSet.simple_induction",
"Prims.l_and",
"Prims.Nil",
"Prims.l_Forall",
"Prims.b2t",
"Prims.uu___is_Cons",
"Prims.l_imp",
"Prims.squash",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | 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))) | false |
FStar.OrdSet.fst | FStar.OrdSet.tail | val tail (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) : ordset a f | val tail (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) : ordset a f | let tail #a #f s = Cons?.tl s <: ordset a f | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 43,
"end_line": 41,
"start_col": 0,
"start_line": 41
} | (*
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 #_ #_ = [] | {
"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.OrdSet.ordset a f | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.b2t",
"Prims.op_disEquality",
"FStar.OrdSet.empty",
"Prims.__proj__Cons__item__tl"
] | [] | false | false | false | false | false | let tail #a #f s =
| Cons?.tl s <: ordset a f | false |
FStar.OrdSet.fst | FStar.OrdSet.head | val head (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) : a | val head (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) : a | let head #_ #_ s = Cons?.hd 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": 42,
"start_col": 0,
"start_line": 42
} | (*
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 #_ #_ = [] | {
"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} -> a | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.b2t",
"Prims.op_disEquality",
"FStar.OrdSet.empty",
"Prims.__proj__Cons__item__hd"
] | [] | false | false | false | false | false | let head #_ #_ s =
| Cons?.hd s | false |
Steel.GhostMonotonicHigherReference.fst | Steel.GhostMonotonicHigherReference.share | val share (#inames:_)
(#a:Type)
(#p:Preorder.preorder a)
(r:ref a p)
(f:perm)
(v:a)
: SteelGhostT unit inames
(pts_to r f v)
(fun _ -> pts_to r (half_perm f) v `star` pts_to r (half_perm f) v) | val share (#inames:_)
(#a:Type)
(#p:Preorder.preorder a)
(r:ref a p)
(f:perm)
(v:a)
: SteelGhostT unit inames
(pts_to r f v)
(fun _ -> pts_to r (half_perm f) v `star` pts_to r (half_perm f) v) | let share #o (#a:Type) (#p:Preorder.preorder a) (r:ref a p) (f:perm) (v:a)
: SteelGhostT unit o
(pts_to r f v)
(fun _ -> pts_to r (half_perm f) v `star` pts_to r (half_perm f) v)
= let open Steel.Effect.Atomic in
elim_pts_to r f v;
let h : erased (history a p) = witness_exists () in
elim_pure _;
let sh = split_current h in
PR.share r h sh sh;
intro_pure (history_val sh v (half_perm f));
intro_exists #(history a p) sh (pts_to_body r (half_perm f) v);
intro_pts_to r (half_perm f) v;
intro_pure (history_val sh v (half_perm f));
intro_exists #(history a p) sh (pts_to_body r (half_perm f) v);
intro_pts_to r (half_perm f) v | {
"file_name": "lib/steel/Steel.GhostMonotonicHigherReference.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 34,
"end_line": 195,
"start_col": 0,
"start_line": 180
} | (*
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.GhostMonotonicHigherReference
open FStar.Ghost
open FStar.PCM
open Steel.Memory
open Steel.Effect.Atomic
open Steel.Effect
open Steel.GhostPCMReference
open Steel.FractionalPermission
open Steel.Preorder
module Preorder = FStar.Preorder
module Q = Steel.Preorder
module M = Steel.Memory
module PR = Steel.GhostPCMReference
module A = Steel.Effect.Atomic
open FStar.Real
#set-options "--ide_id_info_off"
let ref a p = PR.ref (history a p) pcm_history
[@@__reduce__]
let pts_to_body #a #p (r:ref a p) (f:perm) (v:a) (h:history a p) =
PR.pts_to r h `star`
pure (history_val h v f)
let pts_to' (#a:Type) (#p:Preorder.preorder a) (r:ref a p) (f:perm) (v: a) =
h_exists (pts_to_body r f v)
let pts_to_sl r f v = hp_of (pts_to' r f v)
let intro_pure #opened #a #p #f
(r:ref a p)
(v:a)
(h:history a p { history_val h v f })
: SteelGhostT unit opened
(PR.pts_to r h)
(fun _ -> pts_to_body r f v h)
= A.intro_pure (history_val h v f)
let intro_pure_full #opened #a #p #f
(r:ref a p)
(v:a)
(h:history a p { history_val h v f })
: SteelGhostT unit opened
(PR.pts_to r h)
(fun _ -> pts_to r f v)
= intro_pure #_ #a #p #f r v h;
intro_exists h (pts_to_body r f v)
let alloc #_ (#a:Type) (p:Preorder.preorder a) (v:a)
= let h = Current [v] full_perm in
assert (compatible pcm_history h h);
let x : ref a p = alloc h in
intro_pure_full x v h;
x
let extract_pure #a #uses #p #f
(r:ref a p)
(v:a)
(h:(history a p))
: SteelGhostT (_:unit{history_val h v f})
uses
(pts_to_body r f v h)
(fun _ -> pts_to_body r f v h)
= elim_pure (history_val h v f);
A.intro_pure (history_val h v f)
let elim_pure #a #uses #p #f
(r:ref a p)
(v:a)
(h:(history a p))
: SteelGhostT (_:unit{history_val h v f})
uses
(pts_to_body r f v h)
(fun _ -> PR.pts_to r h)
= let _ = extract_pure r v h in
drop (pure (history_val h v f))
let write (#opened: _) (#a:Type) (#p:Preorder.preorder a) (#v:a)
(r:ref a p) (x:a)
: SteelGhost unit opened (pts_to r full_perm v)
(fun v -> pts_to r full_perm x)
(requires fun _ -> p v x /\ True)
(ensures fun _ _ _ -> True)
= let h_old_e = witness_exists #_ #_ #(pts_to_body r full_perm v) () in
let _ = elim_pure r v h_old_e in
let h_old = read r in
let h: history a p = extend_history' h_old x in
write r h_old_e h;
intro_pure_full r x h
let witnessed #a #p r fact =
PR.witnessed r (lift_fact fact)
let get_squash (#p:prop) (_:unit{p}) : squash p = ()
let witness_thunk (#inames: _) (#a:Type) (#pcm:FStar.PCM.pcm a)
(r:PR.ref a pcm)
(fact:M.stable_property pcm)
(v:erased a)
(sq:squash (fact_valid_compat #_ #pcm fact v))
(_:unit)
: SteelAtomicUT (PR.witnessed r fact) inames
(PR.pts_to r v)
(fun _ -> PR.pts_to r v)
= witness r fact v sq
let witness (#inames: _) (#a:Type) (#q:perm) (#p:Preorder.preorder a)
(r:ref a p)
(fact:stable_property p)
(v:erased a)
(_:squash (fact v))
: SteelAtomicUT (witnessed r fact) inames
(pts_to r q v)
(fun _ -> pts_to r q v)
= let h = witness_exists #_ #_ #(pts_to_body r q v) () in
let _ = elim_pure #_ #_ #_ #q r v h in
assert (forall h'. compatible pcm_history h h' ==> lift_fact fact h');
lift_fact_is_stable #a #p fact;
let w = witness_thunk #_ #_ #(pcm_history #a #p) r (lift_fact fact) h () () in
intro_pure_full r v h;
rewrite_slprop (pts_to _ q _) (pts_to r q v) (fun _ -> ());
return w
let recall (#inames: _) (#a:Type u#1) (#q:perm) (#p:Preorder.preorder a) (fact:property a)
(r:ref a p) (v:erased a) (w:witnessed r fact)
= let h = witness_exists #_ #_ #(pts_to_body r q v) () in
let _ = elim_pure #_ #_ #_ #q r v h in
let h1 = recall (lift_fact fact) r h w in
intro_pure_full r v h;
rewrite_slprop (pts_to _ q _) (pts_to r q v) (fun _ -> ())
let elim_pts_to #o (#a:Type)
(#p:Preorder.preorder a)
(r:ref a p)
(f:perm)
(v:a)
: SteelGhostT unit o
(pts_to r f v)
(fun _ -> pts_to' r f v)
= rewrite_slprop _ _ (fun _ -> ())
let intro_pts_to #o (#a:Type)
(#p:Preorder.preorder a)
(r:ref a p)
(f:perm)
(v:a)
: SteelGhostT unit o
(pts_to' r f v)
(fun _ -> pts_to' r f v)
= rewrite_slprop _ _ (fun _ -> ()) | {
"checked_file": "/",
"dependencies": [
"Steel.Preorder.fst.checked",
"Steel.Memory.fsti.checked",
"Steel.GhostPCMReference.fsti.checked",
"Steel.FractionalPermission.fst.checked",
"Steel.Effect.Atomic.fsti.checked",
"Steel.Effect.fsti.checked",
"prims.fst.checked",
"FStar.Real.fsti.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.PCM.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": true,
"source_file": "Steel.GhostMonotonicHigherReference.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Real",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Effect.Atomic",
"short_module": "A"
},
{
"abbrev": true,
"full_module": "Steel.GhostPCMReference",
"short_module": "PR"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Steel.Preorder",
"short_module": "Q"
},
{
"abbrev": false,
"full_module": "Steel.Preorder",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.GhostPCMReference",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "Preorder"
},
{
"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.FractionalPermission",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.PCM",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | r: Steel.GhostMonotonicHigherReference.ref a p -> f: Steel.FractionalPermission.perm -> v: a
-> Steel.Effect.Atomic.SteelGhostT Prims.unit | Steel.Effect.Atomic.SteelGhostT | [] | [] | [
"Steel.Memory.inames",
"FStar.Preorder.preorder",
"Steel.GhostMonotonicHigherReference.ref",
"Steel.FractionalPermission.perm",
"Steel.GhostMonotonicHigherReference.intro_pts_to",
"Steel.FractionalPermission.half_perm",
"Prims.unit",
"Steel.Effect.Atomic.intro_exists",
"Steel.Preorder.history",
"Steel.GhostMonotonicHigherReference.pts_to_body",
"Steel.Effect.Atomic.intro_pure",
"Steel.Preorder.history_val",
"FStar.Ghost.hide",
"Steel.GhostPCMReference.share",
"Steel.Preorder.pcm_history",
"FStar.Ghost.reveal",
"Prims.l_and",
"Prims.b2t",
"Steel.Preorder.uu___is_Current",
"FStar.PCM.composable",
"Prims.eq2",
"FStar.PCM.op",
"Steel.Preorder.vhist",
"Steel.Preorder.__proj__Current__item__h",
"Steel.Preorder.hval",
"Steel.Preorder.__proj__Current__item__f",
"Steel.Preorder.split_current",
"Steel.Effect.Atomic.elim_pure",
"FStar.Ghost.erased",
"Steel.Effect.Atomic.witness_exists",
"Steel.GhostMonotonicHigherReference.elim_pts_to",
"Steel.GhostMonotonicHigherReference.pts_to",
"Steel.Effect.Common.star",
"Steel.Effect.Common.vprop"
] | [] | false | true | false | false | false | let share #o (#a: Type) (#p: Preorder.preorder a) (r: ref a p) (f: perm) (v: a)
: SteelGhostT unit
o
(pts_to r f v)
(fun _ -> (pts_to r (half_perm f) v) `star` (pts_to r (half_perm f) v)) =
| let open Steel.Effect.Atomic in
elim_pts_to r f v;
let h:erased (history a p) = witness_exists () in
elim_pure _;
let sh = split_current h in
PR.share r h sh sh;
intro_pure (history_val sh v (half_perm f));
intro_exists #(history a p) sh (pts_to_body r (half_perm f) v);
intro_pts_to r (half_perm f) v;
intro_pure (history_val sh v (half_perm f));
intro_exists #(history a p) sh (pts_to_body r (half_perm f) v);
intro_pts_to r (half_perm f) v | false |
FStar.OrdSet.fst | FStar.OrdSet.last_lib | val last_lib : s: FStar.OrdSet.ordset a f {s <> FStar.OrdSet.empty} -> a | let last_lib #a #f (s: ordset a f{s <> empty})
= snd (List.Tot.Base.unsnoc s) | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 32,
"end_line": 56,
"start_col": 0,
"start_line": 55
} | (*
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) | {
"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} -> a | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.b2t",
"Prims.op_disEquality",
"FStar.OrdSet.empty",
"FStar.Pervasives.Native.snd",
"Prims.list",
"FStar.List.Tot.Base.unsnoc"
] | [] | false | false | false | false | false | let last_lib #a #f (s: ordset a f {s <> empty}) =
| snd (List.Tot.Base.unsnoc s) | false |
|
FStar.OrdSet.fst | FStar.OrdSet.last_direct | val last_direct (#a #f: _) (s: ordset a f {s <> empty})
: (x: a{mem x s /\ (forall (z: a{mem z s}). f z x)}) | val last_direct (#a #f: _) (s: ordset a f {s <> empty})
: (x: a{mem x s /\ (forall (z: a{mem z s}). f z x)}) | 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) | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 35,
"end_line": 53,
"start_col": 0,
"start_line": 49
} | (*
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 | {
"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}
-> x: a{FStar.OrdSet.mem x s /\ (forall (z: a{FStar.OrdSet.mem z s}). f z x)} | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.b2t",
"Prims.op_disEquality",
"FStar.OrdSet.empty",
"Prims.list",
"FStar.OrdSet.last_direct",
"FStar.OrdSet.tail",
"Prims.l_and",
"FStar.OrdSet.mem",
"Prims.l_Forall"
] | [
"recursion"
] | false | false | false | false | false | 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) | false |
FStar.OrdSet.fst | FStar.OrdSet.last_eq | val last_eq (#a #f: _) (s: ordset a f {s <> empty}) : Lemma (last_direct s = last_lib s) | val last_eq (#a #f: _) (s: ordset a f {s <> empty}) : Lemma (last_direct s = last_lib 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 73,
"end_line": 60,
"start_col": 0,
"start_line": 58
} | (*
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) | {
"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.last_direct s = FStar.OrdSet.last_lib s) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.b2t",
"Prims.op_disEquality",
"FStar.OrdSet.empty",
"FStar.OrdSet.simple_induction",
"Prims.list",
"Prims.Nil",
"Prims.op_Equality",
"FStar.OrdSet.last_direct",
"FStar.OrdSet.last_lib",
"Prims.bool",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.last | val last (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty})
: Tot (x:a{(forall (z:a{mem z s}). f z x) /\ mem x s}) | val last (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty})
: Tot (x:a{(forall (z:a{mem z s}). f z x) /\ mem x s}) | let last #a #f s = last_eq s; last_lib s | {
"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": 62,
"start_col": 0,
"start_line": 62
} | (*
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 | {
"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}
-> x: a{(forall (z: a{FStar.OrdSet.mem z s}). f z x) /\ FStar.OrdSet.mem x s} | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.b2t",
"Prims.op_disEquality",
"FStar.OrdSet.empty",
"FStar.OrdSet.last_lib",
"Prims.unit",
"FStar.OrdSet.last_eq",
"Prims.l_and",
"Prims.l_Forall",
"FStar.OrdSet.mem"
] | [] | false | false | false | false | false | let last #a #f s =
| last_eq s;
last_lib s | false |
FStar.OrdSet.fst | FStar.OrdSet.liat_direct | val 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) }) | val 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) }) | 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)) | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 44,
"end_line": 70,
"start_col": 0,
"start_line": 64
} | (*
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 | {
"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}
-> l:
FStar.OrdSet.ordset a f
{ (forall (x: a). FStar.OrdSet.mem x l = (FStar.OrdSet.mem x s && x <> FStar.OrdSet.last s)) /\
(match FStar.OrdSet.tail s <> FStar.OrdSet.empty with
| true -> FStar.OrdSet.head s = FStar.OrdSet.head l
| _ -> true) } | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.b2t",
"Prims.op_disEquality",
"FStar.OrdSet.empty",
"Prims.Nil",
"Prims.list",
"Prims.Cons",
"FStar.OrdSet.liat_direct",
"Prims.l_and",
"Prims.l_Forall",
"Prims.op_Equality",
"Prims.bool",
"FStar.OrdSet.mem",
"Prims.op_AmpAmp",
"FStar.OrdSet.last",
"FStar.OrdSet.tail",
"FStar.OrdSet.head",
"Prims.logical"
] | [
"recursion"
] | false | false | false | false | false | 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)) | false |
FStar.OrdSet.fst | FStar.OrdSet.liat_lib | val liat_lib : s: FStar.OrdSet.ordset a f {s <> FStar.OrdSet.empty} -> Prims.list a | let liat_lib #a #f (s: ordset a f{s <> empty}) = fst (List.Tot.Base.unsnoc s) | {
"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": 72,
"start_col": 0,
"start_line": 72
} | (*
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)) | {
"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} -> Prims.list a | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.b2t",
"Prims.op_disEquality",
"FStar.OrdSet.empty",
"FStar.Pervasives.Native.fst",
"Prims.list",
"FStar.List.Tot.Base.unsnoc"
] | [] | false | false | false | false | false | let liat_lib #a #f (s: ordset a f {s <> empty}) =
| fst (List.Tot.Base.unsnoc s) | false |
|
FStar.OrdSet.fst | FStar.OrdSet.liat_eq | val liat_eq (#a #f: _) (s: ordset a f {s <> empty}) : Lemma (liat_direct s = liat_lib s) | val liat_eq (#a #f: _) (s: ordset a f {s <> empty}) : Lemma (liat_direct s = liat_lib 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 67,
"end_line": 76,
"start_col": 0,
"start_line": 74
} | (*
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) | {
"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.liat_direct s = FStar.OrdSet.liat_lib s) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.b2t",
"Prims.op_disEquality",
"FStar.OrdSet.empty",
"FStar.OrdSet.simple_induction",
"Prims.list",
"Prims.Nil",
"Prims.op_Equality",
"FStar.OrdSet.liat_direct",
"FStar.OrdSet.liat_lib",
"Prims.bool",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.liat | val liat (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty}) : Tot (l:ordset a f{
(forall x. mem x l = (mem x s && (x <> last s))) /\
(if tail s <> empty then (l <> empty) && (head s = head l) else true)
}) | val liat (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty}) : Tot (l:ordset a f{
(forall x. mem x l = (mem x s && (x <> last s))) /\
(if tail s <> empty then (l <> empty) && (head s = head l) else true)
}) | let liat #a #f s = liat_eq s; liat_lib s | {
"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": 78,
"start_col": 0,
"start_line": 78
} | (*
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 | {
"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}
-> l:
FStar.OrdSet.ordset a f
{ (forall (x: a). FStar.OrdSet.mem x l = (FStar.OrdSet.mem x s && x <> FStar.OrdSet.last s)) /\
(match FStar.OrdSet.tail s <> FStar.OrdSet.empty with
| true -> l <> FStar.OrdSet.empty && FStar.OrdSet.head s = FStar.OrdSet.head l
| _ -> true) } | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.b2t",
"Prims.op_disEquality",
"FStar.OrdSet.empty",
"FStar.OrdSet.liat_lib",
"Prims.unit",
"FStar.OrdSet.liat_eq",
"Prims.l_and",
"Prims.l_Forall",
"Prims.op_Equality",
"Prims.bool",
"FStar.OrdSet.mem",
"Prims.op_AmpAmp",
"FStar.OrdSet.last",
"FStar.OrdSet.tail",
"FStar.OrdSet.head"
] | [] | false | false | false | false | false | let liat #a #f s =
| liat_eq s;
liat_lib s | false |
FStar.OrdSet.fst | FStar.OrdSet.unsnoc | val unsnoc (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty}) : Tot (p:(ordset a f * a){
p = (liat s, last s)
}) | val unsnoc (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty}) : Tot (p:(ordset a f * a){
p = (liat s, last s)
}) | let unsnoc #a #f s =
liat_eq s;
last_eq s;
let l = List.Tot.Base.unsnoc s in
(fst l, snd l) | {
"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": 84,
"start_col": 0,
"start_line": 80
} | (*
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 | {
"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}
-> p: (FStar.OrdSet.ordset a f * a) {p = (FStar.OrdSet.liat s, FStar.OrdSet.last s)} | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.b2t",
"Prims.op_disEquality",
"FStar.OrdSet.empty",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Pervasives.Native.fst",
"Prims.list",
"FStar.Pervasives.Native.snd",
"FStar.Pervasives.Native.tuple2",
"FStar.List.Tot.Base.unsnoc",
"Prims.unit",
"FStar.OrdSet.last_eq",
"FStar.OrdSet.liat_eq",
"Prims.op_Equality",
"FStar.OrdSet.liat",
"FStar.OrdSet.last"
] | [] | false | false | false | false | false | let unsnoc #a #f s =
| liat_eq s;
last_eq s;
let l = List.Tot.Base.unsnoc s in
(fst l, snd l) | false |
FStar.OrdSet.fst | FStar.OrdSet.insert_mem | val insert_mem (#a: eqtype) (#f: _) (x: a) (s: ordset a f) : Lemma (mem x (insert' x s)) | val insert_mem (#a: eqtype) (#f: _) (x: a) (s: ordset a f) : Lemma (mem x (insert' x s)) | 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) | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 48,
"end_line": 108,
"start_col": 0,
"start_line": 106
} | (*
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) | {
"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 | x: a -> s: FStar.OrdSet.ordset a f
-> FStar.Pervasives.Lemma (ensures FStar.OrdSet.mem x (FStar.OrdSet.insert' x s)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.op_disEquality",
"FStar.OrdSet.empty",
"FStar.OrdSet.insert_mem",
"FStar.OrdSet.tail",
"Prims.bool",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.b2t",
"FStar.OrdSet.mem",
"FStar.OrdSet.insert'",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | 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) | false |
FStar.OrdSet.fst | FStar.OrdSet.size' | val size' : s: FStar.OrdSet.ordset a f -> Prims.nat | let size' (#a:eqtype) (#f:cmp a) (s:ordset a f) = List.Tot.length s | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 67,
"end_line": 138,
"start_col": 0,
"start_line": 138
} | (*
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) | {
"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 -> Prims.nat | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.List.Tot.Base.length",
"Prims.nat"
] | [] | false | false | false | false | false | let size' (#a: eqtype) (#f: cmp a) (s: ordset a f) =
| List.Tot.length s | false |
|
FStar.OrdSet.fst | FStar.OrdSet.as_list | val as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{
sorted f l /\
(forall x. (List.Tot.mem x l = mem x s))
}) | val as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{
sorted f l /\
(forall x. (List.Tot.mem x l = mem x s))
}) | let as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{sorted f l}) = s | {
"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": 86,
"start_col": 0,
"start_line": 86
} | (*
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) | {
"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
-> l:
Prims.list a
{ FStar.OrdSet.sorted f l /\
(forall (x: a). FStar.List.Tot.Base.mem x l = FStar.OrdSet.mem x s) } | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.list",
"Prims.b2t",
"FStar.OrdSet.sorted",
"Prims.l_and",
"Prims.l_Forall",
"Prims.op_Equality",
"Prims.bool",
"FStar.List.Tot.Base.mem",
"FStar.OrdSet.mem"
] | [] | false | false | false | false | false | let as_list (#a: eqtype) (#f: cmp a) (s: ordset a f) : Tot (l: list a {sorted f l}) =
| s | false |
FStar.OrdSet.fst | FStar.OrdSet.insert_sub | val insert_sub (#a: eqtype) (#f x: _) (s: ordset a f) (test: _)
: Lemma (mem test (insert' x s) = (mem test s || test = x)) | val insert_sub (#a: eqtype) (#f x: _) (s: ordset a f) (test: _)
: Lemma (mem test (insert' x s) = (mem test s || test = x)) | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 81,
"end_line": 112,
"start_col": 0,
"start_line": 110
} | (*
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) | {
"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 | x: a -> s: FStar.OrdSet.ordset a f -> test: a
-> FStar.Pervasives.Lemma
(ensures
FStar.OrdSet.mem test (FStar.OrdSet.insert' x s) = (FStar.OrdSet.mem test s || test = x)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.simple_induction",
"Prims.b2t",
"Prims.op_Equality",
"Prims.bool",
"FStar.OrdSet.mem",
"FStar.OrdSet.insert'",
"Prims.op_BarBar",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.union | val union : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f) | val union : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f) | let rec union #_ #_ s1 s2 = match s1 with
| [] -> s2
| hd::tl -> union tl (insert' hd s2) | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 38,
"end_line": 125,
"start_col": 0,
"start_line": 123
} | (*
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 | {
"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.OrdSet.ordset a f | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.list",
"FStar.OrdSet.union",
"FStar.OrdSet.insert'"
] | [
"recursion"
] | false | false | false | false | false | let rec union #_ #_ s1 s2 =
| match s1 with
| [] -> s2
| hd :: tl -> union tl (insert' hd s2) | false |
FStar.OrdSet.fst | FStar.OrdSet.insert' | 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)))}) | 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) | {
"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": 99,
"start_col": 0,
"start_line": 93
} | (*
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 \/ | {
"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 | x: a -> s: FStar.OrdSet.ordset a f
-> l:
FStar.OrdSet.ordset a f
{ let s = FStar.OrdSet.as_list s in
let l = FStar.OrdSet.as_list l in
Cons? l /\ (FStar.OrdSet.head l = x \/ Cons? s /\ FStar.OrdSet.head l = FStar.OrdSet.head s)
} | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.Cons",
"Prims.Nil",
"Prims.list",
"Prims.op_Equality",
"Prims.bool",
"FStar.OrdSet.insert'",
"Prims.l_and",
"Prims.b2t",
"Prims.uu___is_Cons",
"Prims.l_or",
"FStar.OrdSet.head",
"FStar.OrdSet.sorted",
"Prims.l_Forall",
"FStar.List.Tot.Base.mem",
"FStar.OrdSet.mem",
"FStar.OrdSet.as_list"
] | [
"recursion"
] | false | false | false | false | false | 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) | false |
FStar.OrdSet.fst | FStar.OrdSet.distinct | val distinct (#a:eqtype) (f:cmp a) (l: list a) : Pure (ordset a f)
(requires True) (ensures fun z -> forall x. (mem x z = List.Tot.Base.mem x l)) | val distinct (#a:eqtype) (f:cmp a) (l: list a) : Pure (ordset a f)
(requires True) (ensures fun z -> forall x. (mem x z = List.Tot.Base.mem x l)) | let distinct #a f l = distinct_props f l; distinct' f l | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 55,
"end_line": 121,
"start_col": 0,
"start_line": 121
} | (*
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)) | {
"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 | f: FStar.OrdSet.cmp a -> l: Prims.list a -> Prims.Pure (FStar.OrdSet.ordset a f) | Prims.Pure | [] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"Prims.list",
"FStar.OrdSet.distinct'",
"Prims.unit",
"FStar.OrdSet.distinct_props",
"FStar.OrdSet.ordset"
] | [] | false | false | false | false | false | let distinct #a f l =
| distinct_props f l;
distinct' f l | false |
FStar.OrdSet.fst | FStar.OrdSet.remove | val remove : #a:eqtype -> #f:cmp a -> a -> ordset a f -> Tot (ordset a f) | val remove : #a:eqtype -> #f:cmp a -> a -> ordset a f -> Tot (ordset a f) | let remove #a #f x s = remove' #_ #f x s | {
"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": 251,
"start_col": 0,
"start_line": 251
} | (*
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 | {
"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 | x: a -> s: FStar.OrdSet.ordset a f -> FStar.OrdSet.ordset a f | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.remove'"
] | [] | false | false | false | false | false | let remove #a #f x s =
| remove' #_ #f x s | false |
FStar.OrdSet.fst | FStar.OrdSet.not_mem_aux | val 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)) | val 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)) | 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) | {
"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": 146,
"start_col": 0,
"start_line": 143
} | (*
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 | {
"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 | x: a -> s: FStar.OrdSet.ordset a f
-> FStar.Pervasives.Lemma
(requires FStar.OrdSet.size' s > 0 && FStar.OrdSet.head s <> x && f x (FStar.OrdSet.head s))
(ensures Prims.op_Negation (FStar.OrdSet.mem x s)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.op_disEquality",
"Prims.list",
"FStar.OrdSet.tail",
"Prims.Nil",
"FStar.OrdSet.not_mem_aux",
"Prims.bool",
"Prims.unit",
"Prims.b2t",
"Prims.op_AmpAmp",
"Prims.op_GreaterThan",
"FStar.OrdSet.size'",
"FStar.OrdSet.head",
"Prims.squash",
"Prims.op_Negation",
"FStar.OrdSet.mem",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | 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) | false |
FStar.OrdSet.fst | FStar.OrdSet.intersect | val intersect : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f) | val intersect : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f) | let intersect #a #f s1 s2 = smart_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": 49,
"end_line": 247,
"start_col": 0,
"start_line": 247
} | (*
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 | {
"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.OrdSet.ordset a f | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.smart_intersect"
] | [] | false | false | false | false | false | let intersect #a #f s1 s2 =
| smart_intersect s1 s2 | false |
FStar.OrdSet.fst | FStar.OrdSet.size | val size : #a:eqtype -> #f:cmp a -> ordset a f -> Tot nat | val size : #a:eqtype -> #f:cmp a -> ordset a f -> Tot nat | let size #a #f s = size' s | {
"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": 253,
"start_col": 0,
"start_line": 253
} | (*
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 | {
"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 -> Prims.nat | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.size'",
"Prims.nat"
] | [] | false | false | false | false | false | let size #a #f s =
| size' s | false |
FStar.OrdSet.fst | FStar.OrdSet.remove' | 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)))}) | 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) | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 27,
"end_line": 136,
"start_col": 0,
"start_line": 132
} | (*
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)))}) | {
"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 | x: a -> s: FStar.OrdSet.ordset a f
-> l:
FStar.OrdSet.ordset a f
{ (Nil? s ==> Nil? l) /\ (Cons? s ==> FStar.OrdSet.head s = x ==> l = FStar.OrdSet.tail s) /\
(Cons? s ==> ~(FStar.OrdSet.head s == x) ==> Cons? l /\ FStar.OrdSet.head l = Cons?.hd s) } | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.Nil",
"Prims.list",
"Prims.op_Equality",
"Prims.bool",
"Prims.Cons",
"FStar.OrdSet.remove'",
"Prims.l_and",
"Prims.l_imp",
"Prims.b2t",
"Prims.uu___is_Nil",
"Prims.uu___is_Cons",
"FStar.OrdSet.head",
"FStar.OrdSet.tail",
"Prims.l_not",
"Prims.eq2",
"Prims.__proj__Cons__item__hd"
] | [
"recursion"
] | false | false | false | false | false | 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) | false |
FStar.OrdSet.fst | FStar.OrdSet.subset' | val subset' : s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> Prims.bool | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 27,
"end_line": 153,
"start_col": 0,
"start_line": 148
} | (*
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) | {
"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 -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.Pervasives.Native.Mktuple2",
"Prims.list",
"Prims.op_AmpAmp",
"Prims.op_Equality",
"FStar.OrdSet.subset'",
"Prims.bool",
"Prims.op_Negation"
] | [
"recursion"
] | false | false | false | false | false | let rec subset' #a #f (s1: ordset a f) (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 | false |
|
FStar.OrdSet.fst | FStar.OrdSet.distinct_props | val distinct_props (#a: _) (f: cmp a) (l: list a)
: Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) | val distinct_props (#a: _) (f: cmp a) (l: list a)
: Lemma (forall x. (mem x (distinct' f l) = List.Tot.Base.mem x l)) | 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)) | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 63,
"end_line": 119,
"start_col": 0,
"start_line": 114
} | (*
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 | {
"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 | f: FStar.OrdSet.cmp a -> l: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
forall (x: a). FStar.OrdSet.mem x (FStar.OrdSet.distinct' f l) = FStar.List.Tot.Base.mem x l) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"Prims.list",
"FStar.Classical.forall_intro",
"Prims.b2t",
"Prims.op_Equality",
"Prims.bool",
"FStar.OrdSet.mem",
"FStar.OrdSet.insert'",
"FStar.OrdSet.distinct'",
"Prims.op_BarBar",
"FStar.OrdSet.insert_sub",
"Prims.unit",
"FStar.OrdSet.distinct_props",
"Prims.l_True",
"Prims.squash",
"Prims.l_Forall",
"FStar.List.Tot.Base.mem",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | 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)) | false |
FStar.OrdSet.fst | FStar.OrdSet.subset | val subset : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot bool | val subset : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot bool | let subset #a #f s1 s2 = subset' 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": 38,
"end_line": 255,
"start_col": 0,
"start_line": 255
} | (*
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 | {
"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 -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.subset'",
"Prims.bool"
] | [] | false | false | false | false | false | let subset #a #f s1 s2 =
| subset' s1 s2 | false |
FStar.OrdSet.fst | FStar.OrdSet.self_is_subset | val self_is_subset (#a #f: _) (s: ordset a f) : Lemma (subset' s s) | val self_is_subset (#a #f: _) (s: ordset a f) : Lemma (subset' 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 | {
"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": 160,
"start_col": 0,
"start_line": 159
} | (*
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 | {
"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.subset' s s) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.simple_induction",
"Prims.b2t",
"FStar.OrdSet.subset'",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.liat_length | val liat_length (#a #f: _) (s: ordset a f {s <> empty}) : Lemma (size' (liat s) = ((size' s) - 1)) | val liat_length (#a #f: _) (s: ordset a f {s <> empty}) : Lemma (size' (liat s) = ((size' s) - 1)) | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 91,
"end_line": 141,
"start_col": 0,
"start_line": 140
} | (*
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 | {
"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",
"FStar.OrdSet.empty",
"FStar.OrdSet.simple_induction",
"Prims.op_Equality",
"Prims.int",
"FStar.OrdSet.size'",
"FStar.OrdSet.liat",
"Prims.op_Subtraction",
"Prims.bool",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.remove_until_gt_prop | val remove_until_gt_prop (#a #f: _) (s: ordset a f) (x test: a)
: Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) | val remove_until_gt_prop (#a #f: _) (s: ordset a f) (x test: a)
: Lemma (f test x ==> not (mem test (fst (remove_until_greater_than x s)))) | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 48,
"end_line": 198,
"start_col": 0,
"start_line": 189
} | (*
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 | {
"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 -> x: a -> test: a
-> FStar.Pervasives.Lemma
(ensures
f test x ==>
Prims.op_Negation (FStar.OrdSet.mem test
(FStar.Pervasives.Native.fst (FStar.OrdSet.remove_until_greater_than x s)))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.list",
"Prims.op_disEquality",
"FStar.OrdSet.remove_until_gt_prop",
"Prims.bool",
"Prims.unit",
"FStar.Classical.move_requires",
"Prims.b2t",
"Prims.op_AmpAmp",
"Prims.op_Negation",
"FStar.OrdSet.mem",
"FStar.Pervasives.Native.fst",
"FStar.OrdSet.remove_until_greater_than",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern",
"Prims.l_True",
"Prims.l_imp"
] | [
"recursion"
] | false | false | true | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.mem_implies_f | val mem_implies_f (#a #f: _) (s: ordset a f) (x: a)
: Lemma (requires mem x s) (ensures f (Cons?.hd s) x) | val mem_implies_f (#a #f: _) (s: ordset a f) (x: a)
: Lemma (requires mem x s) (ensures f (Cons?.hd s) x) | 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 | {
"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": 210,
"start_col": 0,
"start_line": 208
} | (*
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 | {
"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 -> x: a
-> FStar.Pervasives.Lemma (requires FStar.OrdSet.mem x s) (ensures f (Cons?.hd s) x) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.simple_induction",
"Prims.l_imp",
"Prims.b2t",
"FStar.OrdSet.mem",
"FStar.OrdSet.head",
"Prims.unit",
"Prims.squash",
"Prims.__proj__Cons__item__hd",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.tail_is_subset | val tail_is_subset (#a #f: _) (s: ordset a f {size' s > 0}) : Lemma ((Cons?.tl s) `subset'` s) | val tail_is_subset (#a #f: _) (s: ordset a f {size' s > 0}) : Lemma ((Cons?.tl s) `subset'` s) | 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 | {
"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": 157,
"start_col": 0,
"start_line": 155
} | (*
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 | {
"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.OrdSet.size' s > 0}
-> FStar.Pervasives.Lemma (ensures FStar.OrdSet.subset' (Cons?.tl s) s) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.b2t",
"Prims.op_GreaterThan",
"FStar.OrdSet.size'",
"FStar.OrdSet.simple_induction",
"Prims.op_BarBar",
"Prims.op_Equality",
"Prims.int",
"FStar.OrdSet.subset'",
"Prims.__proj__Cons__item__tl",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.remove_until_gt_mem | val remove_until_gt_mem (#a #f: _) (s: ordset a f) (x test: a)
: Lemma
(mem test (fst (remove_until_greater_than x s)) = (mem test s && f x test && (x <> test))) | val remove_until_gt_mem (#a #f: _) (s: ordset a f) (x test: a)
: Lemma
(mem test (fst (remove_until_greater_than x s)) = (mem test s && f x test && (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 | {
"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": 206,
"start_col": 0,
"start_line": 200
} | (*
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 | {
"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 -> x: a -> test: a
-> FStar.Pervasives.Lemma
(ensures
FStar.OrdSet.mem test
(FStar.Pervasives.Native.fst (FStar.OrdSet.remove_until_greater_than x s)) =
(FStar.OrdSet.mem test s && f x test && x <> test)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.op_GreaterThan",
"FStar.OrdSet.size'",
"FStar.OrdSet.remove_until_gt_mem",
"FStar.OrdSet.tail",
"Prims.bool",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.b2t",
"Prims.op_Equality",
"FStar.OrdSet.mem",
"FStar.Pervasives.Native.fst",
"FStar.OrdSet.remove_until_greater_than",
"Prims.op_AmpAmp",
"Prims.op_disEquality",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.choose | val choose : #a:eqtype -> #f:cmp a -> s:ordset a f -> Tot (option a) | val choose : #a:eqtype -> #f:cmp a -> s:ordset a f -> Tot (option a) | let choose #a #f s = match s with | [] -> None | x::_ -> Some x | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 63,
"end_line": 249,
"start_col": 0,
"start_line": 249
} | (*
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 | {
"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.Native.option a | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.Pervasives.Native.None",
"Prims.list",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.option"
] | [] | false | false | false | false | false | let choose #a #f s =
| match s with
| [] -> None
| x :: _ -> Some x | false |
FStar.OrdSet.fst | FStar.OrdSet.remove_until_greater_than | val 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)))) } | val 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)))) } | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 57,
"end_line": 187,
"start_col": 0,
"start_line": 166
} | (*
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) | {
"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 | x: a -> s: FStar.OrdSet.ordset a f
-> z:
(FStar.OrdSet.ordset a f * Prims.bool)
{ FStar.OrdSet.size' (FStar.Pervasives.Native.fst z) <= FStar.OrdSet.size' s &&
Prims.op_Negation (FStar.OrdSet.mem x (FStar.Pervasives.Native.fst z)) &&
FStar.OrdSet.subset' (FStar.Pervasives.Native.fst z) s &&
FStar.Pervasives.Native.snd z = FStar.OrdSet.mem x s &&
(match FStar.Pervasives.Native.fst z with
| Prims.Nil #_ -> true
| Prims.Cons #_ _ _ -> FStar.OrdSet.sorted f (x :: FStar.Pervasives.Native.fst z)) } | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.Pervasives.Native.Mktuple2",
"Prims.bool",
"Prims.Nil",
"Prims.list",
"Prims.op_Equality",
"Prims.unit",
"FStar.OrdSet.tail_is_subset",
"Prims.op_GreaterThan",
"FStar.OrdSet.size'",
"FStar.OrdSet.not_mem_aux",
"FStar.OrdSet.self_is_subset",
"FStar.OrdSet.remove_until_greater_than",
"FStar.Pervasives.Native.tuple2",
"Prims.b2t",
"Prims.op_AmpAmp",
"Prims.op_LessThanOrEqual",
"FStar.Pervasives.Native.fst",
"Prims.op_Negation",
"FStar.OrdSet.mem",
"FStar.OrdSet.subset'",
"FStar.Pervasives.Native.snd",
"FStar.OrdSet.sorted",
"Prims.Cons"
] | [
"recursion"
] | false | false | false | false | false | 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
(if size' t > 0 then not_mem_aux x t;
tail_is_subset s;
(t, true))
else
if f x h
then
(not_mem_aux x s;
self_is_subset s;
(s, false))
else remove_until_greater_than x t | false |
FStar.OrdSet.fst | FStar.OrdSet.singleton | val singleton : #a:eqtype -> #f:cmp a -> a -> Tot (ordset a f) | val singleton : #a:eqtype -> #f:cmp a -> a -> Tot (ordset a f) | let singleton (#a:eqtype) #f x = [x] | {
"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": 257,
"start_col": 0,
"start_line": 257
} | (*
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 | {
"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 | x: a -> FStar.OrdSet.ordset a f | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"Prims.Cons",
"Prims.Nil",
"FStar.OrdSet.ordset"
] | [] | false | false | false | false | false | let singleton (#a: eqtype) #f x =
| [x] | false |
FStar.OrdSet.fst | FStar.OrdSet.ncmp | val ncmp : x: Prims.nat -> y: Prims.nat -> Prims.bool | let ncmp (x y:nat) = x <= y | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 27,
"end_line": 350,
"start_col": 0,
"start_line": 350
} | (*
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) = () | {
"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 | x: Prims.nat -> y: Prims.nat -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.op_LessThanOrEqual",
"Prims.bool"
] | [] | false | false | false | true | false | let ncmp (x y: nat) =
| x <= y | false |
|
FStar.OrdSet.fst | FStar.OrdSet.minus | val minus : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f) | val minus : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f) | let minus #a #f s1 s2 = smart_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": 41,
"end_line": 353,
"start_col": 0,
"start_line": 353
} | (*
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]) | {
"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.OrdSet.ordset a f | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.smart_minus"
] | [] | false | false | false | false | false | let minus #a #f s1 s2 =
| smart_minus s1 s2 | false |
FStar.OrdSet.fst | FStar.OrdSet.not_mem_of_tail | val 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) | val 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) | 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 | {
"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": 267,
"start_col": 0,
"start_line": 265
} | (*
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) = () | {
"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.OrdSet.size s > 0} -> x: a
-> FStar.Pervasives.Lemma
(ensures
Prims.op_Negation (FStar.OrdSet.mem x (FStar.OrdSet.tail s)) =
Prims.op_Negation (FStar.OrdSet.mem x s) ||
x = FStar.OrdSet.head s) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.b2t",
"Prims.op_GreaterThan",
"FStar.OrdSet.size",
"FStar.OrdSet.simple_induction",
"Prims.l_imp",
"FStar.OrdSet.mem",
"FStar.OrdSet.head",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.op_BarBar",
"Prims.op_Equality",
"Prims.bool",
"Prims.op_Negation",
"FStar.OrdSet.tail",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.remove_until_gt_exclusion | val remove_until_gt_exclusion (#a #f: _) (s: ordset a f) (x test: a)
: Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s)))))
(ensures x = test || not (mem test s)) | val remove_until_gt_exclusion (#a #f: _) (s: ordset a f) (x test: a)
: Lemma (requires f x test && (not (mem test (fst (remove_until_greater_than x s)))))
(ensures x = test || not (mem test s)) | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 30,
"end_line": 290,
"start_col": 0,
"start_line": 287
} | (*
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) | {
"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 -> x: a -> test: a
-> FStar.Pervasives.Lemma
(requires
f x test &&
Prims.op_Negation (FStar.OrdSet.mem test
(FStar.Pervasives.Native.fst (FStar.OrdSet.remove_until_greater_than x s))))
(ensures x = test || Prims.op_Negation (FStar.OrdSet.mem test s)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.remove_until_gt_mem",
"Prims.unit",
"Prims.b2t",
"Prims.op_AmpAmp",
"Prims.op_Negation",
"FStar.OrdSet.mem",
"FStar.Pervasives.Native.fst",
"Prims.bool",
"FStar.OrdSet.remove_until_greater_than",
"Prims.squash",
"Prims.op_BarBar",
"Prims.op_Equality",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.head_is_never_in_tail | val head_is_never_in_tail (#a #f: _) (s: ordset a f {size s > 0})
: Lemma (not (mem (head s) (tail s))) | val head_is_never_in_tail (#a #f: _) (s: ordset a f {size s > 0})
: Lemma (not (mem (head s) (tail s))) | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 53,
"end_line": 317,
"start_col": 0,
"start_line": 316
} | (*
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 | {
"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.OrdSet.size s > 0}
-> FStar.Pervasives.Lemma
(ensures Prims.op_Negation (FStar.OrdSet.mem (FStar.OrdSet.head s) (FStar.OrdSet.tail s))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.b2t",
"Prims.op_GreaterThan",
"FStar.OrdSet.size",
"FStar.OrdSet.set_props",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.op_Negation",
"FStar.OrdSet.mem",
"FStar.OrdSet.head",
"FStar.OrdSet.tail",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.same_members_means_eq | val same_members_means_eq (#a #f: _) (s1 s2: ordset a f)
: Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) | val same_members_means_eq (#a #f: _) (s1 s2: ordset a f)
: Lemma (requires forall x. mem x s1 = mem x s2) (ensures s1 == s2) | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 57,
"end_line": 281,
"start_col": 0,
"start_line": 274
} | (*
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) | {
"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 forall (x: a). FStar.OrdSet.mem x s1 = FStar.OrdSet.mem x s2)
(ensures s1 == s2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.op_GreaterThan",
"FStar.OrdSet.size",
"Prims._assert",
"Prims.b2t",
"FStar.OrdSet.mem",
"FStar.OrdSet.head",
"Prims.bool",
"Prims.unit",
"Prims.list",
"FStar.OrdSet.same_members_means_eq",
"FStar.OrdSet.set_props",
"Prims.l_Forall",
"Prims.op_Equality",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec same_members_means_eq #a #f (s1: ordset a f) (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 | false |
FStar.OrdSet.fst | FStar.OrdSet.subset_transitivity | val subset_transitivity (#a #f: _) (p q r: ordset a f)
: Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) | val subset_transitivity (#a #f: _) (p q r: ordset a f)
: Lemma (requires p `subset` q /\ q `subset` r) (ensures p `subset` r) | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 24,
"end_line": 314,
"start_col": 0,
"start_line": 309
} | (*
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) | {
"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 | p: FStar.OrdSet.ordset a f -> q: FStar.OrdSet.ordset a f -> r: FStar.OrdSet.ordset a f
-> FStar.Pervasives.Lemma (requires FStar.OrdSet.subset p q /\ FStar.OrdSet.subset q r)
(ensures FStar.OrdSet.subset p r) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.mem_implies_subset",
"Prims.unit",
"FStar.OrdSet.subset_implies_mem",
"Prims.l_and",
"Prims.b2t",
"FStar.OrdSet.subset",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let subset_transitivity #a #f (p: ordset a f) (q: ordset a f) (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 | false |
FStar.OrdSet.fst | FStar.OrdSet.set_props | val 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)) | val 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)) | 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) | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 43,
"end_line": 272,
"start_col": 0,
"start_line": 269
} | (*
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 | {
"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
((match FStar.OrdSet.size s > 0 with
| true ->
forall (x: a).
FStar.OrdSet.mem x (FStar.OrdSet.tail s) ==>
f (FStar.OrdSet.head s) x /\ FStar.OrdSet.head s <> x
| _ -> forall (x: a). Prims.op_Negation (FStar.OrdSet.mem x s))
<:
Type0)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.op_GreaterThan",
"FStar.OrdSet.size",
"FStar.OrdSet.set_props",
"FStar.OrdSet.tail",
"Prims.bool",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_Forall",
"Prims.l_imp",
"Prims.b2t",
"FStar.OrdSet.mem",
"Prims.l_and",
"FStar.OrdSet.head",
"Prims.op_disEquality",
"Prims.op_Negation",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | 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) | false |
FStar.OrdSet.fst | FStar.OrdSet.intersect_is_symmetric | val intersect_is_symmetric (#a #f: _) (s1 s2: ordset a f)
: Lemma (intersect s1 s2 = intersect s2 s1) | val intersect_is_symmetric (#a #f: _) (s1 s2: ordset a f)
: Lemma (intersect s1 s2 = intersect s2 s1) | 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) | {
"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": 285,
"start_col": 0,
"start_line": 283
} | (*
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 | {
"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) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.same_members_means_eq",
"FStar.OrdSet.intersect",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.b2t",
"Prims.op_Equality",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let intersect_is_symmetric #a #f (s1: ordset a f) (s2: ordset a f)
: Lemma (intersect s1 s2 = intersect s2 s1) =
| same_members_means_eq (intersect s1 s2) (intersect s2 s1) | false |
FStar.OrdSet.fst | FStar.OrdSet.eq_lemma | val eq_lemma: #a:eqtype -> #f:cmp a -> s1:ordset a f -> s2:ordset a f
-> Lemma (requires (equal s1 s2))
(ensures (s1 = s2))
[SMTPat (equal s1 s2)] | val eq_lemma: #a:eqtype -> #f:cmp a -> s1:ordset a f -> s2:ordset a f
-> Lemma (requires (equal s1 s2))
(ensures (s1 = s2))
[SMTPat (equal s1 s2)] | let eq_lemma #a #f s1 s2 = same_members_means_eq 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": 54,
"end_line": 357,
"start_col": 0,
"start_line": 357
} | (*
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 | {
"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.equal s1 s2)
(ensures s1 = s2)
[SMTPat (FStar.OrdSet.equal s1 s2)] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.same_members_means_eq",
"Prims.unit"
] | [] | true | false | true | false | false | let eq_lemma #a #f s1 s2 =
| same_members_means_eq s1 s2 | false |
FStar.OrdSet.fst | FStar.OrdSet.strict_subset | val strict_subset: #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot bool | val strict_subset: #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot bool | let strict_subset #a #f s1 s2 = s1 <> s2 && subset 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": 56,
"end_line": 355,
"start_col": 0,
"start_line": 355
} | (*
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 | {
"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 -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.op_AmpAmp",
"Prims.op_disEquality",
"FStar.OrdSet.subset",
"Prims.bool"
] | [] | false | false | false | false | false | let strict_subset #a #f s1 s2 =
| s1 <> s2 && subset s1 s2 | false |
FStar.OrdSet.fst | FStar.OrdSet.smart_intersect | val 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) | val 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) | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 9,
"end_line": 245,
"start_col": 0,
"start_line": 219
} | (*
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]. | {
"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
-> Prims.Tot
(z:
FStar.OrdSet.ordset a f
{ (forall (x: a). FStar.OrdSet.mem x z = (FStar.OrdSet.mem x s1 && FStar.OrdSet.mem x s2)) /\
(forall (x: a{FStar.OrdSet.sorted f (x :: s1)}). FStar.OrdSet.sorted f (x :: z)) /\
(forall (x: a{FStar.OrdSet.sorted f (x :: s2)}). FStar.OrdSet.sorted f (x :: z)) }) | Prims.Tot | [
"total",
""
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.Nil",
"Prims.list",
"Prims.op_Equality",
"Prims.Cons",
"FStar.OrdSet.smart_intersect",
"Prims.bool",
"Prims.l_and",
"Prims.l_Forall",
"Prims.b2t",
"FStar.OrdSet.mem",
"Prims.op_AmpAmp",
"FStar.OrdSet.sorted",
"Prims.unit",
"FStar.Classical.forall_intro",
"Prims.l_imp",
"Prims.__proj__Cons__item__hd",
"FStar.Classical.move_requires",
"FStar.OrdSet.mem_implies_f",
"FStar.Pervasives.Native.fst",
"FStar.OrdSet.remove_until_greater_than",
"Prims.op_disEquality",
"FStar.OrdSet.remove_until_gt_mem",
"FStar.Pervasives.Native.tuple2",
"Prims.op_LessThanOrEqual",
"FStar.OrdSet.size'",
"Prims.op_Negation",
"FStar.OrdSet.subset'",
"FStar.Pervasives.Native.snd"
] | [
"recursion"
] | false | false | false | false | false | let rec smart_intersect #a #f (s1: ordset a f) (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
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) | false |
FStar.OrdSet.fst | FStar.OrdSet.subset_implies_mem | val subset_implies_mem (#a #f: _) (p q: ordset a f)
: Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) | val subset_implies_mem (#a #f: _) (p q: ordset a f)
: Lemma (subset p q ==> (forall x. mem x p ==> mem x q)) | 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) | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 37,
"end_line": 307,
"start_col": 0,
"start_line": 302
} | (*
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) | {
"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 | p: FStar.OrdSet.ordset a f -> q: FStar.OrdSet.ordset a f
-> FStar.Pervasives.Lemma
(ensures
FStar.OrdSet.subset p q ==> (forall (x: a). FStar.OrdSet.mem x p ==> FStar.OrdSet.mem x q)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.op_AmpAmp",
"Prims.uu___is_Cons",
"Prims.op_Equality",
"FStar.OrdSet.head",
"FStar.OrdSet.subset_implies_mem",
"FStar.OrdSet.tail",
"Prims.bool",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_imp",
"Prims.b2t",
"FStar.OrdSet.subset",
"Prims.l_Forall",
"FStar.OrdSet.mem",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec subset_implies_mem #a #f (p: ordset a f) (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) | false |
FStar.OrdSet.fst | FStar.OrdSet.mem_implies_subset | val mem_implies_subset (#a #f: _) (s1 s2: ordset a f)
: Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) | val mem_implies_subset (#a #f: _) (s1 s2: ordset a f)
: Lemma ((forall x. mem x s1 ==> mem x s2) ==> subset s1 s2) | 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) | {
"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": 300,
"start_col": 0,
"start_line": 292
} | (*
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 | {
"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 s1 ==> FStar.OrdSet.mem x s2) ==> FStar.OrdSet.subset s1 s2
) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.list",
"Prims.op_AmpAmp",
"Prims.op_GreaterThan",
"FStar.OrdSet.size",
"FStar.OrdSet.head",
"FStar.OrdSet.mem_implies_subset",
"FStar.OrdSet.tail",
"Prims.bool",
"Prims.unit",
"FStar.OrdSet.set_props",
"Prims.l_True",
"Prims.squash",
"Prims.l_imp",
"Prims.l_Forall",
"Prims.b2t",
"FStar.OrdSet.mem",
"FStar.OrdSet.subset",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec mem_implies_subset #a #f (s1: ordset a f) (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) | false |
FStar.OrdSet.fst | FStar.OrdSet.mem_union | val mem_union: #a:eqtype -> #f:cmp a -> s1:ordset a f -> s2:ordset a f -> x:a
-> Lemma (requires True)
(ensures (mem #a #f x (union #a #f s1 s2) =
(mem #a #f x s1 || mem #a #f x s2)))
[SMTPat (mem #a #f x (union #a #f s1 s2))] | val mem_union: #a:eqtype -> #f:cmp a -> s1:ordset a f -> s2:ordset a f -> x:a
-> Lemma (requires True)
(ensures (mem #a #f x (union #a #f s1 s2) =
(mem #a #f x s1 || mem #a #f x s2)))
[SMTPat (mem #a #f x (union #a #f s1 s2))] | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 24,
"end_line": 371,
"start_col": 0,
"start_line": 367
} | (*
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 | {
"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 -> x: a
-> FStar.Pervasives.Lemma
(ensures
FStar.OrdSet.mem x (FStar.OrdSet.union s1 s2) =
(FStar.OrdSet.mem x s1 || FStar.OrdSet.mem x s2))
[SMTPat (FStar.OrdSet.mem x (FStar.OrdSet.union s1 s2))] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.op_GreaterThan",
"FStar.OrdSet.size",
"Prims.list",
"FStar.OrdSet.mem_insert",
"Prims.unit",
"FStar.OrdSet.mem_union",
"FStar.OrdSet.insert'",
"Prims.bool"
] | [
"recursion"
] | false | false | true | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.mem_insert | val mem_insert (#a: eqtype) (#f: _) (el: a) (s: ordset a f) (x: a)
: Lemma (mem x (insert' el s) = (x = el || mem x s)) | val mem_insert (#a: eqtype) (#f: _) (el: a) (s: ordset a f) (x: a)
: Lemma (mem x (insert' el s) = (x = el || mem x s)) | 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 | {
"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": 365,
"start_col": 0,
"start_line": 363
} | (*
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 #_ #_ _ _ = () | {
"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 | el: a -> s: FStar.OrdSet.ordset a f -> x: a
-> FStar.Pervasives.Lemma
(ensures FStar.OrdSet.mem x (FStar.OrdSet.insert' el s) = (x = el || FStar.OrdSet.mem x s)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.simple_induction",
"Prims.b2t",
"Prims.op_Equality",
"Prims.bool",
"FStar.OrdSet.mem",
"FStar.OrdSet.insert'",
"Prims.op_BarBar",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.mem_subset | val mem_subset: #a:eqtype -> #f:cmp a -> s1:ordset a f -> s2:ordset a f
-> Lemma (requires True)
(ensures (subset #a #f s1 s2 <==>
(forall x. mem #a #f x s1 ==> mem #a #f x s2)))
[SMTPat (subset #a #f s1 s2)] | val mem_subset: #a:eqtype -> #f:cmp a -> s1:ordset a f -> s2:ordset a f
-> Lemma (requires True)
(ensures (subset #a #f s1 s2 <==>
(forall x. mem #a #f x s1 ==> mem #a #f x s2)))
[SMTPat (subset #a #f s1 s2)] | let mem_subset (#a:eqtype) #f s1 s2 =
subset_implies_mem s1 s2;
mem_implies_subset 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": 26,
"end_line": 377,
"start_col": 0,
"start_line": 375
} | (*
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 = () | {
"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.subset s1 s2 <==>
(forall (x: a). FStar.OrdSet.mem x s1 ==> FStar.OrdSet.mem x s2))
[SMTPat (FStar.OrdSet.subset s1 s2)] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.mem_implies_subset",
"Prims.unit",
"FStar.OrdSet.subset_implies_mem"
] | [] | true | false | true | false | false | let mem_subset (#a: eqtype) #f s1 s2 =
| subset_implies_mem s1 s2;
mem_implies_subset s1 s2 | false |
FStar.OrdSet.fst | FStar.OrdSet.fold | val fold (#a:eqtype) (#acc:Type) (#f:cmp a) (g:acc -> a -> acc) (init:acc) (s:ordset a f)
: Tot acc | val fold (#a:eqtype) (#acc:Type) (#f:cmp a) (g:acc -> a -> acc) (init:acc) (s:ordset a f)
: Tot acc | let fold #a #acc #f g init s = List.Tot.fold_left g init 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": 431,
"start_col": 0,
"start_line": 431
} | (*
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 | {
"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 | g: (_: acc -> _: a -> acc) -> init: acc -> s: FStar.OrdSet.ordset a f -> acc | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.List.Tot.Base.fold_left"
] | [] | false | false | false | false | false | let fold #a #acc #f g init s =
| List.Tot.fold_left g init s | false |
FStar.OrdSet.fst | FStar.OrdSet.mem_remove | val mem_remove: #a:eqtype -> #f:cmp a -> x:a -> y:a -> s:ordset a f
-> Lemma (requires True)
(ensures (mem #a #f x (remove #a #f y s) =
(mem #a #f x s && not (x = y))))
[SMTPat (mem #a #f x (remove #a #f y s))] | val mem_remove: #a:eqtype -> #f:cmp a -> x:a -> y:a -> s:ordset a f
-> Lemma (requires True)
(ensures (mem #a #f x (remove #a #f y s) =
(mem #a #f x s && not (x = y))))
[SMTPat (mem #a #f x (remove #a #f y 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)) | {
"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": 384,
"start_col": 0,
"start_line": 383
} | (*
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 = () | {
"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 | x: a -> y: a -> s: FStar.OrdSet.ordset a f
-> FStar.Pervasives.Lemma
(ensures
FStar.OrdSet.mem x (FStar.OrdSet.remove y s) =
(FStar.OrdSet.mem x s && Prims.op_Negation (x = y)))
[SMTPat (FStar.OrdSet.mem x (FStar.OrdSet.remove y s))] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.op_GreaterThan",
"FStar.OrdSet.size",
"FStar.OrdSet.mem_remove",
"FStar.OrdSet.tail",
"Prims.unit",
"FStar.OrdSet.set_props",
"Prims.bool"
] | [
"recursion"
] | false | false | true | false | false | let rec mem_remove (#a: eqtype) #f x y s =
| if size s > 0
then
(set_props s;
mem_remove x y (tail s)) | false |
FStar.OrdSet.fst | FStar.OrdSet.eq_remove | val eq_remove: #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f
-> Lemma (requires (not (mem #a #f x s)))
(ensures (s = remove #a #f x s))
[SMTPat (remove #a #f x s)] | val eq_remove: #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f
-> Lemma (requires (not (mem #a #f x s)))
(ensures (s = remove #a #f x s))
[SMTPat (remove #a #f x s)] | let eq_remove (#a:eqtype) #f x s
= simple_induction (fun p -> not (mem x p) ==> p = remove x p) s | {
"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": 387,
"start_col": 0,
"start_line": 386
} | (*
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)) | {
"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 | x: a -> s: FStar.OrdSet.ordset a f
-> FStar.Pervasives.Lemma (requires Prims.op_Negation (FStar.OrdSet.mem x s))
(ensures s = FStar.OrdSet.remove x s)
[SMTPat (FStar.OrdSet.remove x s)] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.simple_induction",
"Prims.l_imp",
"Prims.b2t",
"Prims.op_Negation",
"FStar.OrdSet.mem",
"Prims.op_Equality",
"FStar.OrdSet.remove",
"Prims.unit"
] | [] | false | false | true | false | false | let eq_remove (#a: eqtype) #f x s =
| simple_induction (fun p -> not (mem x p) ==> p = remove x p) s | false |
FStar.OrdSet.fst | FStar.OrdSet.insert_when_already_exists | val insert_when_already_exists (#a: eqtype) (#f: _) (s: ordset a f) (x: a)
: Lemma (requires mem x s) (ensures insert' x s == s) | val insert_when_already_exists (#a: eqtype) (#f: _) (s: ordset a f) (x: a)
: Lemma (requires mem x s) (ensures insert' x s == s) | 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 | {
"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": 405,
"start_col": 0,
"start_line": 402
} | (*
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' | {
"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 -> x: a
-> FStar.Pervasives.Lemma (requires FStar.OrdSet.mem x s) (ensures FStar.OrdSet.insert' x s == s) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.simple_induction",
"Prims.l_iff",
"Prims.b2t",
"FStar.OrdSet.mem",
"Prims.op_Equality",
"FStar.OrdSet.insert'",
"Prims.unit",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.size_remove | val size_remove: #a:eqtype -> #f:cmp a -> y:a -> s:ordset a f
-> Lemma (requires (mem #a #f y s))
(ensures (size #a #f s = size #a #f (remove #a #f y s) + 1))
[SMTPat (size #a #f (remove #a #f y s))] | val size_remove: #a:eqtype -> #f:cmp a -> y:a -> s:ordset a f
-> Lemma (requires (mem #a #f y s))
(ensures (size #a #f s = size #a #f (remove #a #f y s) + 1))
[SMTPat (size #a #f (remove #a #f y s))] | let rec size_remove (#a:eqtype) #f x s = match s with
| hd::tl -> if x<>hd then size_remove #_ #f x tl | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 50,
"end_line": 392,
"start_col": 0,
"start_line": 391
} | (*
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 = () | {
"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 | y: a -> s: FStar.OrdSet.ordset a f
-> FStar.Pervasives.Lemma (requires FStar.OrdSet.mem y s)
(ensures FStar.OrdSet.size s = FStar.OrdSet.size (FStar.OrdSet.remove y s) + 1)
[SMTPat (FStar.OrdSet.size (FStar.OrdSet.remove y s))] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.list",
"Prims.op_disEquality",
"FStar.OrdSet.size_remove",
"Prims.bool",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec size_remove (#a: eqtype) #f x s =
| match s with | hd :: tl -> if x <> hd then size_remove #_ #f x tl | false |
FStar.OrdSet.fst | FStar.OrdSet.size_of_union_left | val size_of_union_left (#a: eqtype) (#f: _) (s1 s2: ordset a f)
: Lemma (ensures size (union s1 s2) >= size s2) | val size_of_union_left (#a: eqtype) (#f: _) (s1 s2: ordset a f)
: Lemma (ensures size (union s1 s2) >= size s2) | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 37,
"end_line": 420,
"start_col": 0,
"start_line": 415
} | (*
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 | {
"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 s2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.list",
"FStar.OrdSet.precise_size_insert",
"Prims.unit",
"FStar.OrdSet.size_of_union_left",
"FStar.OrdSet.insert'",
"Prims.l_True",
"Prims.squash",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"FStar.OrdSet.size",
"FStar.OrdSet.union",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec size_of_union_left (#a: eqtype) #f (s1: ordset a f) (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 | false |
FStar.OrdSet.fst | FStar.OrdSet.precise_size_insert | val 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)) | val 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)) | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 53,
"end_line": 413,
"start_col": 0,
"start_line": 411
} | (*
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 | {
"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 -> x: a
-> FStar.Pervasives.Lemma
(ensures
FStar.OrdSet.size (FStar.OrdSet.insert' x s) =
(match FStar.OrdSet.mem x s with
| true -> FStar.OrdSet.size s
| _ -> FStar.OrdSet.size s + 1)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.op_GreaterThan",
"FStar.OrdSet.size",
"FStar.OrdSet.precise_size_insert",
"FStar.OrdSet.tail",
"Prims.bool",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"FStar.OrdSet.insert'",
"FStar.OrdSet.mem",
"Prims.op_Addition",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.size_insert | val size_insert (#a: eqtype) (#f: _) (s: ordset a f) (x: a) : Lemma (size (insert' x s) >= size s) | val size_insert (#a: eqtype) (#f: _) (s: ordset a f) (x: a) : Lemma (size (insert' x s) >= size 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 | {
"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": 409,
"start_col": 0,
"start_line": 407
} | (*
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 | {
"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 -> x: a
-> FStar.Pervasives.Lemma
(ensures FStar.OrdSet.size (FStar.OrdSet.insert' x s) >= FStar.OrdSet.size s) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.simple_induction",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"FStar.OrdSet.size",
"FStar.OrdSet.insert'",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.size_of_union_right | val size_of_union_right (#a: eqtype) (#f: _) (s1 s2: ordset a f)
: Lemma (ensures size (union s1 s2) >= size s1) | val size_of_union_right (#a: eqtype) (#f: _) (s1 s2: ordset a f)
: Lemma (ensures size (union s1 s2) >= size s1) | 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 | {
"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": 425,
"start_col": 0,
"start_line": 422
} | (*
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 | {
"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.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.size_of_union_left",
"Prims.unit",
"FStar.OrdSet.eq_lemma",
"FStar.OrdSet.union",
"Prims.l_True",
"Prims.squash",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"FStar.OrdSet.size",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let size_of_union_right (#a: eqtype) #f (s1: ordset a f) (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 | false |
FStar.OrdSet.fst | FStar.OrdSet.subset_size | val subset_size: #a:eqtype -> #f:cmp a -> x:ordset a f -> y:ordset a f
-> Lemma (requires (subset #a #f x y))
(ensures (size #a #f x <= size #a #f y))
[SMTPat (subset #a #f x y)] | val subset_size: #a:eqtype -> #f:cmp a -> x:ordset a f -> y:ordset a f
-> Lemma (requires (subset #a #f x y))
(ensures (size #a #f x <= size #a #f y))
[SMTPat (subset #a #f x y)] | 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' | {
"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": 400,
"start_col": 0,
"start_line": 396
} | (*
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 = () | {
"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 | x: FStar.OrdSet.ordset a f -> y: FStar.OrdSet.ordset a f
-> FStar.Pervasives.Lemma (requires FStar.OrdSet.subset x y)
(ensures FStar.OrdSet.size x <= FStar.OrdSet.size y)
[SMTPat (FStar.OrdSet.subset x y)] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.Pervasives.Native.Mktuple2",
"Prims.list",
"Prims.op_AmpAmp",
"Prims.op_Equality",
"FStar.OrdSet.subset_size",
"Prims.bool",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | 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' | false |
FStar.OrdSet.fst | FStar.OrdSet.smart_minus | val 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 = []) } | val 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 = []) } | 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) | {
"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": 344,
"start_col": 0,
"start_line": 319
} | (*
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 | {
"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 | p: FStar.OrdSet.ordset a f -> q: FStar.OrdSet.ordset a f
-> z:
FStar.OrdSet.ordset a f
{ (forall (x: a).
FStar.OrdSet.mem x z =
(FStar.OrdSet.mem x p && Prims.op_Negation (FStar.OrdSet.mem x q))) /\
(match p, z with
| FStar.Pervasives.Native.Mktuple2 #_ #_ (Prims.Cons #_ ph _) (Prims.Cons #_ zh _) ->
f ph zh
| FStar.Pervasives.Native.Mktuple2 #_ #_ (Prims.Cons #_ _ _) (Prims.Nil #_) ->
FStar.OrdSet.subset p q
| FStar.Pervasives.Native.Mktuple2 #_ #_ (Prims.Nil #_) _ -> z = []) } | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.Nil",
"Prims.list",
"Prims.bool",
"Prims.unit",
"Prims.op_Equality",
"FStar.OrdSet.mem_implies_subset",
"FStar.OrdSet.subset_implies_mem",
"FStar.OrdSet.subset_transitivity",
"FStar.OrdSet.set_props",
"Prims.l_and",
"Prims.l_Forall",
"Prims.b2t",
"FStar.OrdSet.mem",
"Prims.op_AmpAmp",
"Prims.op_Negation",
"FStar.Pervasives.Native.Mktuple2",
"FStar.OrdSet.subset",
"FStar.OrdSet.smart_minus",
"Prims.Cons",
"Prims.logical",
"FStar.Classical.forall_intro",
"Prims.l_imp",
"Prims.__proj__Cons__item__hd",
"FStar.Classical.move_requires",
"FStar.OrdSet.mem_implies_f",
"FStar.Pervasives.Native.fst",
"FStar.OrdSet.remove_until_greater_than",
"Prims.op_disEquality",
"FStar.OrdSet.remove_until_gt_mem",
"FStar.Pervasives.Native.tuple2",
"Prims.op_LessThanOrEqual",
"FStar.OrdSet.size'",
"FStar.OrdSet.subset'",
"FStar.Pervasives.Native.snd",
"FStar.OrdSet.sorted"
] | [
"recursion"
] | false | false | false | false | false | let rec smart_minus #a #f (p: ordset a f) (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
let result = smart_minus pt q_after_ph in
set_props p;
if result = []
then
(subset_transitivity pt q_after_ph q;
subset_implies_mem pt q;
mem_implies_subset p q);
result
else ph :: (smart_minus pt q_after_ph) | false |
FStar.OrdSet.fst | FStar.OrdSet.lemma_strict_subset_size | val lemma_strict_subset_size (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f)
: Lemma (requires (strict_subset s1 s2))
(ensures (subset s1 s2 /\ size s1 < size s2))
[SMTPat (strict_subset s1 s2)] | val lemma_strict_subset_size (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f)
: Lemma (requires (strict_subset s1 s2))
(ensures (subset s1 s2 /\ size s1 < size s2))
[SMTPat (strict_subset s1 s2)] | 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 | {
"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": 475,
"start_col": 0,
"start_line": 464
} | (*
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 | {
"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.strict_subset s1 s2)
(ensures FStar.OrdSet.subset s1 s2 /\ FStar.OrdSet.size s1 < FStar.OrdSet.size s2)
[SMTPat (FStar.OrdSet.strict_subset s1 s2)] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.Classical.Sugar.exists_elim",
"Prims.b2t",
"Prims.op_AmpAmp",
"FStar.OrdSet.mem",
"Prims.op_Negation",
"Prims.op_GreaterThan",
"FStar.OrdSet.size",
"Prims.squash",
"Prims._assert",
"FStar.OrdSet.subset",
"FStar.OrdSet.insert'",
"Prims.unit",
"FStar.OrdSet.precise_size_insert",
"FStar.Classical.forall_intro",
"Prims.op_Equality",
"Prims.bool",
"Prims.op_BarBar",
"FStar.OrdSet.mem_insert",
"FStar.Classical.move_requires_2",
"Prims.l_Forall",
"Prims.Nil",
"FStar.Pervasives.pattern",
"FStar.OrdSet.eq_lemma"
] | [] | false | false | true | false | false | 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 _.
(Classical.forall_intro (mem_insert x s1);
precise_size_insert s1 x;
assert (subset (insert' x s1) s2)) | false |
FStar.OrdSet.fst | FStar.OrdSet.map | val map (#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 -> (size sb <= size sa) /\
(as_list sb == FStar.List.Tot.map g (as_list sa)) /\
(let sa = as_list sa in
let sb = as_list sb in
Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa)))) | val map (#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 -> (size sb <= size sa) /\
(as_list sb == FStar.List.Tot.map g (as_list sa)) /\
(let sa = as_list sa in
let sb = as_list sb in
Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa)))) | 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 | {
"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": 462,
"start_col": 0,
"start_line": 459
} | (*
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 | {
"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 | g: (_: a -> b) -> sa: FStar.OrdSet.ordset a fa -> Prims.Pure (FStar.OrdSet.ordset b fb) | Prims.Pure | [] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.map_internal",
"Prims.unit",
"FStar.OrdSet.map_as_list",
"FStar.OrdSet.map_size"
] | [] | false | false | false | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.map_internal | val 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))) | val 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))) | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 11,
"end_line": 445,
"start_col": 0,
"start_line": 434
} | (*
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 | {
"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 | g: (_: a -> b) -> sa: FStar.OrdSet.ordset a fa -> Prims.Pure (FStar.OrdSet.ordset b fb) | Prims.Pure | [] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.Nil",
"Prims.list",
"Prims.op_BarBar",
"Prims.op_Negation",
"Prims.uu___is_Cons",
"Prims.op_disEquality",
"Prims.__proj__Cons__item__hd",
"Prims.Cons",
"Prims.bool",
"FStar.OrdSet.map_internal",
"Prims.l_Forall",
"Prims.l_and",
"Prims.l_imp",
"Prims.b2t",
"Prims.l_iff",
"Prims.op_Equality",
"Prims.eq2"
] | [
"recursion"
] | false | false | false | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.count | val count (#a:eqtype) (#f: cmp a) (s: ordset a f) (c: condition a) : nat | val count (#a:eqtype) (#f: cmp a) (s: ordset a f) (c: condition a) : nat | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 30,
"end_line": 504,
"start_col": 0,
"start_line": 499
} | (*
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 | {
"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.nat | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.condition",
"Prims.list",
"Prims.op_Addition",
"FStar.OrdSet.count",
"Prims.bool",
"Prims.nat"
] | [
"recursion"
] | false | false | false | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.strict_subset_implies_diff_element | val 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))) | val 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))) | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 22,
"end_line": 488,
"start_col": 0,
"start_line": 479
} | (*
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 = () | {
"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.strict_subset s1 s2)
(ensures exists (x: a). FStar.OrdSet.mem x s2 /\ Prims.op_Negation (FStar.OrdSet.mem x s1)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.Pervasives.Native.Mktuple2",
"Prims.list",
"Prims.op_Equality",
"FStar.OrdSet.set_props",
"Prims.unit",
"FStar.OrdSet.strict_subset_implies_diff_element",
"Prims.bool",
"FStar.Classical.move_requires",
"Prims.b2t",
"FStar.OrdSet.mem",
"Prims.__proj__Cons__item__hd",
"FStar.OrdSet.mem_implies_f",
"FStar.OrdSet.strict_subset",
"Prims.squash",
"Prims.l_Exists",
"Prims.l_and",
"Prims.op_Negation",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec strict_subset_implies_diff_element #a #f (s1: ordset a f) (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
(strict_subset_implies_diff_element #a #f t1 t2;
set_props s2) | false |
FStar.OrdSet.fst | FStar.OrdSet.map_size | val 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) | val 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) | 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) | {
"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": 450,
"start_col": 0,
"start_line": 447
} | (*
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 | {
"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 | g: (_: a -> b) -> sa: FStar.OrdSet.ordset a fa
-> FStar.Pervasives.Lemma
(requires forall (x: a) (y: a). (fa x y ==> fb (g x) (g y)) /\ (x = y <==> g x = g y))
(ensures FStar.OrdSet.size (FStar.OrdSet.map_internal g sa) <= FStar.OrdSet.size sa) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.op_GreaterThan",
"FStar.OrdSet.size",
"FStar.OrdSet.map_size",
"FStar.OrdSet.tail",
"Prims.bool",
"Prims.unit",
"Prims.l_Forall",
"Prims.l_and",
"Prims.l_imp",
"Prims.b2t",
"Prims.l_iff",
"Prims.op_Equality",
"Prims.squash",
"Prims.op_LessThanOrEqual",
"FStar.OrdSet.map_internal",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | 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) | false |
FStar.OrdSet.fst | FStar.OrdSet.lemma_strict_subset_exists_diff | val lemma_strict_subset_exists_diff (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f)
: Lemma (requires subset s1 s2)
(ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) | val lemma_strict_subset_exists_diff (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f)
: Lemma (requires subset s1 s2)
(ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1)))) | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 70,
"end_line": 497,
"start_col": 0,
"start_line": 494
} | (*
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) = () | {
"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.strict_subset s1 s2 <==>
(exists (x: a). FStar.OrdSet.mem x s2 /\ Prims.op_Negation (FStar.OrdSet.mem x s1))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.Classical.move_requires_2",
"Prims.b2t",
"FStar.OrdSet.strict_subset",
"Prims.l_Exists",
"Prims.l_and",
"FStar.OrdSet.mem",
"Prims.op_Negation",
"FStar.OrdSet.strict_subset_implies_diff_element",
"Prims.unit",
"FStar.OrdSet.subset",
"Prims.squash",
"Prims.l_iff",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let lemma_strict_subset_exists_diff #a #f (s1: ordset a f) (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 | false |
FStar.OrdSet.fst | FStar.OrdSet.count_of_impossible | val count_of_impossible (#a:eqtype) (#f: cmp a) (s: ordset a f) (c: condition a{forall p. not (c p)})
: Lemma (count s c = 0) | val count_of_impossible (#a:eqtype) (#f: cmp a) (s: ordset a f) (c: condition a{forall p. not (c p)})
: Lemma (count s c = 0) | let count_of_impossible #a #f s c = simple_induction (fun p -> count p c = 0) s | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 79,
"end_line": 508,
"start_col": 0,
"start_line": 508
} | (*
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 #_ #_ _ _ = () | {
"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 {forall (p: a). Prims.op_Negation (c p)}
-> FStar.Pervasives.Lemma (ensures FStar.OrdSet.count s c = 0) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.condition",
"Prims.l_Forall",
"Prims.b2t",
"Prims.op_Negation",
"FStar.OrdSet.simple_induction",
"Prims.op_Equality",
"Prims.int",
"FStar.OrdSet.count",
"Prims.unit"
] | [] | false | false | true | false | false | let count_of_impossible #a #f s c =
| simple_induction (fun p -> count p c = 0) s | false |
FStar.OrdSet.fst | FStar.OrdSet.size_union | val size_union: #a:eqtype -> #f:cmp a -> s1:ordset a f -> s2:ordset a f
-> Lemma (requires True)
(ensures ((size #a #f (union #a #f s1 s2) >= size #a #f s1) &&
(size #a #f (union #a #f s1 s2) >= size #a #f s2)))
[SMTPat (size #a #f (union #a #f s1 s2))] | val size_union: #a:eqtype -> #f:cmp a -> s1:ordset a f -> s2:ordset a f
-> Lemma (requires True)
(ensures ((size #a #f (union #a #f s1 s2) >= size #a #f s1) &&
(size #a #f (union #a #f s1 s2) >= size #a #f s2)))
[SMTPat (size #a #f (union #a #f s1 s2))] | let size_union #a #f s1 s2 =
size_of_union_left s1 s2;
size_of_union_right 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": 27,
"end_line": 429,
"start_col": 0,
"start_line": 427
} | (*
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 | {
"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 (FStar.OrdSet.union s1 s2) >= FStar.OrdSet.size s2)
[SMTPat (FStar.OrdSet.size (FStar.OrdSet.union s1 s2))] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.size_of_union_right",
"Prims.unit",
"FStar.OrdSet.size_of_union_left"
] | [] | true | false | true | false | false | let size_union #a #f s1 s2 =
| size_of_union_left s1 s2;
size_of_union_right s1 s2 | false |
FStar.OrdSet.fst | FStar.OrdSet.map_as_list | val 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)) | val 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)) | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 55,
"end_line": 457,
"start_col": 0,
"start_line": 452
} | (*
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) | {
"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 | g: (_: a -> b) -> sa: FStar.OrdSet.ordset a fa
-> FStar.Pervasives.Lemma
(requires forall (x: a) (y: a). (fa x y ==> fb (g x) (g y)) /\ (x = y <==> g x = g y))
(ensures
FStar.OrdSet.as_list (FStar.OrdSet.map_internal g sa) ==
FStar.List.Tot.Base.map g (FStar.OrdSet.as_list sa)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.list",
"FStar.OrdSet.map_as_list",
"Prims.unit",
"Prims.l_Forall",
"Prims.l_and",
"Prims.l_imp",
"Prims.b2t",
"Prims.l_iff",
"Prims.op_Equality",
"Prims.squash",
"Prims.eq2",
"FStar.OrdSet.as_list",
"FStar.OrdSet.map_internal",
"FStar.List.Tot.Base.map",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.count_of_cons | val count_of_cons (#a:eqtype) (#f: cmp a) (s: ordset a f{size s > 0}) (c: condition a)
: Lemma (count s c = (count (tail s) c + (if (c (head s)) then 1 else 0))) | val count_of_cons (#a:eqtype) (#f: cmp a) (s: ordset a f{size s > 0}) (c: condition a)
: Lemma (count s c = (count (tail s) c + (if (c (head s)) then 1 else 0))) | let rec count_of_cons #a #f s c = if size s > 1 then count_of_cons (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": 77,
"end_line": 512,
"start_col": 0,
"start_line": 512
} | (*
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 | {
"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.OrdSet.size s > 0} -> c: FStar.OrdSet.condition a
-> FStar.Pervasives.Lemma
(ensures
FStar.OrdSet.count s c =
FStar.OrdSet.count (FStar.OrdSet.tail s) c +
(match c (FStar.OrdSet.head s) with
| true -> 1
| _ -> 0)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.b2t",
"Prims.op_GreaterThan",
"FStar.OrdSet.size",
"FStar.OrdSet.condition",
"FStar.OrdSet.count_of_cons",
"FStar.OrdSet.tail",
"Prims.bool",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec count_of_cons #a #f s c =
| if size s > 1 then count_of_cons (tail s) c | false |
FStar.OrdSet.fst | FStar.OrdSet.count_all | val count_all (#a:eqtype) (#f: cmp a) (s: ordset a f) (c: condition a{forall p. c p})
: Lemma (count s c = size s) | val count_all (#a:eqtype) (#f: cmp a) (s: ordset a f) (c: condition a{forall p. c p})
: Lemma (count s c = size s) | let count_all #a #f s c = simple_induction (fun p -> count p c = size p) s | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 74,
"end_line": 510,
"start_col": 0,
"start_line": 510
} | (*
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 | {
"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 {forall (p: a). c p}
-> FStar.Pervasives.Lemma (ensures FStar.OrdSet.count s c = FStar.OrdSet.size s) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.condition",
"Prims.l_Forall",
"Prims.b2t",
"FStar.OrdSet.simple_induction",
"Prims.op_Equality",
"Prims.nat",
"FStar.OrdSet.count",
"FStar.OrdSet.size",
"Prims.unit"
] | [] | false | false | true | false | false | let count_all #a #f s c =
| simple_induction (fun p -> count p c = size p) s | false |
FStar.OrdSet.fst | FStar.OrdSet.all | val all (#a:eqtype) (#f:cmp a) (s: ordset a f) (c: condition a) : Tot bool | val all (#a:eqtype) (#f:cmp a) (s: ordset a f) (c: condition a) : Tot bool | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 32,
"end_line": 517,
"start_col": 0,
"start_line": 514
} | (*
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 | {
"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.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.condition",
"Prims.list",
"Prims.op_AmpAmp",
"FStar.OrdSet.all",
"Prims.bool"
] | [
"recursion"
] | false | false | false | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.any | val any (#a:eqtype) (#f:cmp a) (s: ordset a f) (c: condition a) : Tot bool | val any (#a:eqtype) (#f:cmp a) (s: ordset a f) (c: condition a) : Tot bool | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 32,
"end_line": 522,
"start_col": 0,
"start_line": 519
} | (*
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 | {
"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.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.condition",
"Prims.list",
"Prims.op_BarBar",
"FStar.OrdSet.any",
"Prims.bool"
] | [
"recursion"
] | false | false | false | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.any_if_mem | val any_if_mem (#a #f: _) (s: ordset a f) (c: condition a) (x: _)
: Lemma (requires mem x s && c x) (ensures any s c) | val any_if_mem (#a #f: _) (s: ordset a f) (c: condition a) (x: _)
: Lemma (requires mem x s && c x) (ensures any s c) | 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 | {
"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": 528,
"start_col": 0,
"start_line": 526
} | (*
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 | {
"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.Pervasives.Lemma (requires FStar.OrdSet.mem x s && c x) (ensures 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.l_imp",
"Prims.b2t",
"Prims.op_AmpAmp",
"FStar.OrdSet.mem",
"FStar.OrdSet.any",
"Prims.unit",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.find_first | val find_first (#a:eqtype) (#f:cmp a) (s: ordset a f) (c: condition a) : option a | val find_first (#a:eqtype) (#f:cmp a) (s: ordset a f) (c: condition a) : option a | 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 | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 63,
"end_line": 534,
"start_col": 0,
"start_line": 532
} | (*
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 | {
"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.Native.option a | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.condition",
"FStar.Pervasives.Native.None",
"Prims.list",
"FStar.Pervasives.Native.Some",
"Prims.bool",
"FStar.OrdSet.find_first",
"FStar.Pervasives.Native.option"
] | [
"recursion"
] | false | false | false | false | false | 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 | false |
FStar.OrdSet.fst | FStar.OrdSet.mem_if_any | val mem_if_any (#a:eqtype) (#f:cmp a) (s:ordset a f) (c: condition a) (x:a{mem x s && c x})
: Lemma (any s c) | val mem_if_any (#a:eqtype) (#f:cmp a) (s:ordset a f) (c: condition a) (x:a{mem x s && c x})
: Lemma (any s c) | let rec mem_if_any #a #f s c x = if head s<>x then mem_if_any (tail s) c x | {
"file_name": "ulib/experimental/FStar.OrdSet.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 74,
"end_line": 524,
"start_col": 0,
"start_line": 524
} | (*
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 | {
"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 FStar.OrdSet.any 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",
"Prims.op_disEquality",
"FStar.OrdSet.head",
"FStar.OrdSet.mem_if_any",
"FStar.OrdSet.tail",
"Prims.bool",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec mem_if_any #a #f s c x =
| if head s <> x then mem_if_any (tail s) c x | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.