effect
stringclasses 48
values | original_source_type
stringlengths 0
23k
| opens_and_abbrevs
listlengths 2
92
| isa_cross_project_example
bool 1
class | source_definition
stringlengths 9
57.9k
| partial_definition
stringlengths 7
23.3k
| is_div
bool 2
classes | is_type
null | is_proof
bool 2
classes | completed_definiton
stringlengths 1
250k
| dependencies
dict | effect_flags
sequencelengths 0
2
| ideal_premises
sequencelengths 0
236
| mutual_with
sequencelengths 0
11
| file_context
stringlengths 0
407k
| interleaved
bool 1
class | is_simply_typed
bool 2
classes | file_name
stringlengths 5
48
| vconfig
dict | is_simple_lemma
null | source_type
stringlengths 10
23k
| proof_features
sequencelengths 0
1
| name
stringlengths 8
95
| source
dict | verbose_type
stringlengths 1
7.42k
| source_range
dict |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Prims.Tot | val update (#a: Type) (x: var) (xa: a) (vm: vmap a) : vmap a | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y | val update (#a: Type) (x: var) (xa: a) (vm: vmap a) : vmap a
let update (#a: Type) (x: var) (xa: a) (vm: vmap a) : vmap a = | false | null | false | let l, y = vm in
(x, xa) :: l, y | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"FStar.Reflection.V2.Data.var",
"FStar.Tactics.CanonCommSemiring.vmap",
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"FStar.Pervasives.Native.Mktuple2",
"Prims.Cons"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val update (#a: Type) (x: var) (xa: a) (vm: vmap a) : vmap a | [] | FStar.Tactics.CanonCommSemiring.update | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.Reflection.V2.Data.var -> xa: a -> vm: FStar.Tactics.CanonCommSemiring.vmap a
-> FStar.Tactics.CanonCommSemiring.vmap a | {
"end_col": 34,
"end_line": 375,
"start_col": 58,
"start_line": 374
} |
FStar.Pervasives.Lemma | val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)] | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let mult_one_l #a r x =
r.cm_mult.identity x | val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x = | false | null | true | r.cm_mult.identity x | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Algebra.CommMonoid.__proj__CM__item__identity",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"Prims.unit"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)] | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)] | [] | FStar.Tactics.CanonCommSemiring.mult_one_l | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | r: FStar.Tactics.CanonCommSemiring.cr a -> x: a
-> FStar.Pervasives.Lemma (ensures CM?.mult (CR?.cm_mult r) (CM?.unit (CR?.cm_mult r)) x == x)
[SMTPat (CM?.mult (CR?.cm_mult r) (CM?.unit (CR?.cm_mult r)) x)] | {
"end_col": 22,
"end_line": 470,
"start_col": 2,
"start_line": 470
} |
FStar.Pervasives.Lemma | val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)] | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let mult_zero_l #a r x =
r.mult_zero_l x | val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x = | false | null | true | r.mult_zero_l x | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__mult_zero_l",
"Prims.unit"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)] | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)] | [] | FStar.Tactics.CanonCommSemiring.mult_zero_l | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | r: FStar.Tactics.CanonCommSemiring.cr a -> x: a
-> FStar.Pervasives.Lemma
(ensures CM?.mult (CR?.cm_mult r) (CM?.unit (CR?.cm_add r)) x == CM?.unit (CR?.cm_add r))
[SMTPat (CM?.mult (CR?.cm_mult r) (CM?.unit (CR?.cm_add r)) x)] | {
"end_col": 17,
"end_line": 482,
"start_col": 2,
"start_line": 482
} |
Prims.Tot | val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p) | val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
let spolynomial_simplify #a r p = | false | null | false | canonical_sum_simplify r (spolynomial_normalize r p) | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.spolynomial",
"FStar.Tactics.CanonCommSemiring.canonical_sum_simplify",
"FStar.Tactics.CanonCommSemiring.spolynomial_normalize",
"FStar.Tactics.CanonCommSemiring.canonical_sum"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr] | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a | [] | FStar.Tactics.CanonCommSemiring.spolynomial_simplify | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | r: FStar.Tactics.CanonCommSemiring.cr a -> p: FStar.Tactics.CanonCommSemiring.spolynomial a
-> FStar.Tactics.CanonCommSemiring.canonical_sum a | {
"end_col": 31,
"end_line": 357,
"start_col": 2,
"start_line": 356
} |
Prims.Tot | val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p) | val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
let polynomial_simplify #a r p = | false | null | false | canonical_sum_simplify r (polynomial_normalize r p) | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.polynomial",
"FStar.Tactics.CanonCommSemiring.canonical_sum_simplify",
"FStar.Tactics.CanonCommSemiring.polynomial_normalize",
"FStar.Tactics.CanonCommSemiring.canonical_sum"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr] | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a | [] | FStar.Tactics.CanonCommSemiring.polynomial_simplify | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | r: FStar.Tactics.CanonCommSemiring.cr a -> p: FStar.Tactics.CanonCommSemiring.polynomial a
-> FStar.Tactics.CanonCommSemiring.canonical_sum a | {
"end_col": 30,
"end_line": 1390,
"start_col": 2,
"start_line": 1389
} |
FStar.Tactics.Effect.Tac | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let ddump m = if debugging () then dump m | let ddump m = | true | null | false | if debugging () then dump m | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [] | [
"Prims.string",
"FStar.Tactics.V2.Builtins.dump",
"Prims.unit",
"Prims.bool",
"FStar.Tactics.V2.Builtins.debugging"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr]
let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p)
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_p (#a:Type) (r:cr a) (vm:vmap a) (p:polynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p)
val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p))
let rec spolynomial_of_ok #a r vm p =
match p with
| Pconst c -> ()
| Pvar i -> ()
| Pplus p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Pmult p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Popp p ->
spolynomial_of_ok r vm p;
let x = interp_sp r vm (spolynomial_of r p) in
let y = r.cm_mult.mult (r.opp r.cm_mult.unit) x in
add_mult_opp r x;
opp_unique r x y
val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p)))
let rec polynomial_normalize_ok #a r vm p =
match p with
| Pvar _ -> ()
| Pconst _ -> ()
| Pplus l q ->
canonical_sum_merge_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_merge_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Pmult l q ->
canonical_sum_prod_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_prod_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Popp p1 ->
let l = SPconst (r.opp r.cm_mult.unit) in
polynomial_normalize_ok r vm p1;
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(polynomial_normalize r p1);
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(spolynomial_normalize r (spolynomial_of r p1))
val polynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_simplify r p) == interp_p r vm p)
let polynomial_simplify_ok #a r vm p =
calc (==) {
interp_cs r vm (polynomial_simplify r p);
== { }
interp_cs r vm (canonical_sum_simplify r (polynomial_normalize r p));
== { canonical_sum_simplify_ok r vm (polynomial_normalize r p) }
interp_cs r vm (polynomial_normalize r p);
== { polynomial_normalize_ok r vm p }
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p));
== { spolynomial_normalize_ok r vm (spolynomial_of r p) }
interp_sp r vm (spolynomial_of r p);
== { spolynomial_of_ok r vm p }
interp_p r vm p;
}
///
/// Tactic definition
/// | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val ddump : m: Prims.string -> FStar.Tactics.Effect.Tac Prims.unit | [] | FStar.Tactics.CanonCommSemiring.ddump | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | m: Prims.string -> FStar.Tactics.Effect.Tac Prims.unit | {
"end_col": 41,
"end_line": 1498,
"start_col": 14,
"start_line": 1498
} |
|
FStar.Tactics.Effect.Tac | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let find = find_aux 0 | let find = | true | null | false | find_aux 0 | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [] | [
"FStar.Tactics.CanonCommSemiring.find_aux"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr]
let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p)
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_p (#a:Type) (r:cr a) (vm:vmap a) (p:polynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p)
val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p))
let rec spolynomial_of_ok #a r vm p =
match p with
| Pconst c -> ()
| Pvar i -> ()
| Pplus p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Pmult p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Popp p ->
spolynomial_of_ok r vm p;
let x = interp_sp r vm (spolynomial_of r p) in
let y = r.cm_mult.mult (r.opp r.cm_mult.unit) x in
add_mult_opp r x;
opp_unique r x y
val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p)))
let rec polynomial_normalize_ok #a r vm p =
match p with
| Pvar _ -> ()
| Pconst _ -> ()
| Pplus l q ->
canonical_sum_merge_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_merge_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Pmult l q ->
canonical_sum_prod_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_prod_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Popp p1 ->
let l = SPconst (r.opp r.cm_mult.unit) in
polynomial_normalize_ok r vm p1;
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(polynomial_normalize r p1);
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(spolynomial_normalize r (spolynomial_of r p1))
val polynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_simplify r p) == interp_p r vm p)
let polynomial_simplify_ok #a r vm p =
calc (==) {
interp_cs r vm (polynomial_simplify r p);
== { }
interp_cs r vm (canonical_sum_simplify r (polynomial_normalize r p));
== { canonical_sum_simplify_ok r vm (polynomial_normalize r p) }
interp_cs r vm (polynomial_normalize r p);
== { polynomial_normalize_ok r vm p }
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p));
== { spolynomial_normalize_ok r vm (spolynomial_of r p) }
interp_sp r vm (spolynomial_of r p);
== { spolynomial_of_ok r vm p }
interp_p r vm p;
}
///
/// Tactic definition
///
(* Only dump when debugging is on *)
let ddump m = if debugging () then dump m
(**
* Finds the position of first occurrence of x in xs.
* This is specialized to terms and their funny term_eq.
**)
let rec find_aux (n:nat) (x:term) (xs:list term) : Tac (option nat) =
match xs with
| [] -> None
| x'::xs' -> if term_eq x x' then Some n else find_aux (n+1) x xs' | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val find : x: FStar.Tactics.NamedView.term -> xs: Prims.list FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac (FStar.Pervasives.Native.option Prims.nat) | [] | FStar.Tactics.CanonCommSemiring.find | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.Tactics.NamedView.term -> xs: Prims.list FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac (FStar.Pervasives.Native.option Prims.nat) | {
"end_col": 21,
"end_line": 1509,
"start_col": 11,
"start_line": 1509
} |
|
FStar.Tactics.Effect.Tac | val canon_norm: Prims.unit -> Tac unit | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let canon_norm () : Tac unit = norm steps | val canon_norm: Prims.unit -> Tac unit
let canon_norm () : Tac unit = | true | null | false | norm steps | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [] | [
"Prims.unit",
"FStar.Tactics.V2.Builtins.norm",
"FStar.Tactics.CanonCommSemiring.steps"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr]
let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p)
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_p (#a:Type) (r:cr a) (vm:vmap a) (p:polynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p)
val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p))
let rec spolynomial_of_ok #a r vm p =
match p with
| Pconst c -> ()
| Pvar i -> ()
| Pplus p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Pmult p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Popp p ->
spolynomial_of_ok r vm p;
let x = interp_sp r vm (spolynomial_of r p) in
let y = r.cm_mult.mult (r.opp r.cm_mult.unit) x in
add_mult_opp r x;
opp_unique r x y
val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p)))
let rec polynomial_normalize_ok #a r vm p =
match p with
| Pvar _ -> ()
| Pconst _ -> ()
| Pplus l q ->
canonical_sum_merge_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_merge_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Pmult l q ->
canonical_sum_prod_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_prod_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Popp p1 ->
let l = SPconst (r.opp r.cm_mult.unit) in
polynomial_normalize_ok r vm p1;
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(polynomial_normalize r p1);
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(spolynomial_normalize r (spolynomial_of r p1))
val polynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_simplify r p) == interp_p r vm p)
let polynomial_simplify_ok #a r vm p =
calc (==) {
interp_cs r vm (polynomial_simplify r p);
== { }
interp_cs r vm (canonical_sum_simplify r (polynomial_normalize r p));
== { canonical_sum_simplify_ok r vm (polynomial_normalize r p) }
interp_cs r vm (polynomial_normalize r p);
== { polynomial_normalize_ok r vm p }
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p));
== { spolynomial_normalize_ok r vm (spolynomial_of r p) }
interp_sp r vm (spolynomial_of r p);
== { spolynomial_of_ok r vm p }
interp_p r vm p;
}
///
/// Tactic definition
///
(* Only dump when debugging is on *)
let ddump m = if debugging () then dump m
(**
* Finds the position of first occurrence of x in xs.
* This is specialized to terms and their funny term_eq.
**)
let rec find_aux (n:nat) (x:term) (xs:list term) : Tac (option nat) =
match xs with
| [] -> None
| x'::xs' -> if term_eq x x' then Some n else find_aux (n+1) x xs'
let find = find_aux 0
let make_fvar (#a:Type) (t:term) (unquotea:term -> Tac a) (ts:list term)
(vm:vmap a) : Tac (polynomial a * list term * vmap a) =
match find t ts with
| Some v -> (Pvar v, ts, vm)
| None ->
let vfresh = length ts in
let z = unquotea t in
(Pvar vfresh, ts @ [t], update vfresh z vm)
(** This expects that add, opp, mone mult, and t have already been normalized *)
let rec reification_aux (#a:Type) (unquotea:term -> Tac a) (ts:list term) (vm:vmap a) (add opp mone mult t: term) : Tac (polynomial a * list term * vmap a) =
// ddump ("term = " ^ term_to_string t ^ "\n");
let hd, tl = collect_app_ref t in
match inspect hd, list_unref tl with
| Tv_FVar fv, [(t1, _) ; (t2, _)] ->
//ddump ("add = " ^ term_to_string add ^ "
// \nmul = " ^ term_to_string mult);
//ddump ("fv = " ^ term_to_string (pack (Tv_FVar fv)));
let binop (op:polynomial a -> polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e1, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
let (e2, ts, vm) = reification_aux unquotea ts vm add opp mone mult t2 in
(op e1 e2, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) add then binop Pplus else
if term_eq (pack (Tv_FVar fv)) mult then binop Pmult else
make_fvar t unquotea ts vm
| Tv_FVar fv, [(t1, _)] ->
let monop (op:polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
(op e, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) opp then monop Popp else
make_fvar t unquotea ts vm
| Tv_Const _, [] -> Pconst (unquotea t), ts, vm
| _, _ -> make_fvar t unquotea ts vm
(**
* How to normalize terms in the tactic.
* This is carefully tuned to unfold all and no more than required
**)
let steps =
[
primops;
iota;
zeta;
delta_attr [`%canon_attr];
delta_only [
`%FStar.Mul.op_Star; // For integer ring
`%FStar.Algebra.CommMonoid.int_plus_cm; // For integer ring
`%FStar.Algebra.CommMonoid.int_multiply_cm; // For integer ring
`%FStar.Algebra.CommMonoid.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.__proj__CM__item__unit;
`%__proj__CR__item__cm_add;
`%__proj__CR__item__opp;
`%__proj__CR__item__cm_mult;
`%FStar.List.Tot.assoc;
`%FStar.Pervasives.Native.fst;
`%FStar.Pervasives.Native.snd;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___1;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___2;
`%FStar.List.Tot.op_At;
`%FStar.List.Tot.append;
]
] | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val canon_norm: Prims.unit -> Tac unit | [] | FStar.Tactics.CanonCommSemiring.canon_norm | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | _: Prims.unit -> FStar.Tactics.Effect.Tac Prims.unit | {
"end_col": 41,
"end_line": 1576,
"start_col": 31,
"start_line": 1576
} |
FStar.Tactics.Effect.Tac | val canon_semiring_aux
(a: Type)
(ta: term)
(unquotea: (term -> Tac a))
(quotea: (a -> Tac term))
(tr tadd topp tmone tmult: term)
(munit: a)
: Tac unit | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let canon_semiring_aux
(a: Type) (ta: term) (unquotea: term -> Tac a) (quotea: a -> Tac term)
(tr tadd topp tmone tmult: term)
(munit: a)
: Tac unit
=
focus (fun () ->
norm []; // Do not normalize anything implicitly
let g = cur_goal () in
match term_as_formula g with
| Comp (Eq (Some t)) t1 t2 ->
begin
//ddump ("t1 = " ^ term_to_string t1 ^ "\nt2 = " ^ term_to_string t2);
if term_eq t ta then
begin
match reification unquotea quotea tadd topp tmone tmult munit [t1; t2] with
| ([e1; e2], vm) ->
(*
ddump (term_to_string t1);
ddump (term_to_string t2);
let r : cr a = unquote tr in
ddump ("vm = " ^ term_to_string (quote vm) ^ "\n" ^
"before = " ^ term_to_string (norm_term steps
(quote (interp_p r vm e1 == interp_p r vm e2))));
dump ("expected after = " ^ term_to_string (norm_term steps
(quote (
interp_cs r vm (polynomial_simplify r e1) ==
interp_cs r vm (polynomial_simplify r e2)))));
*)
let tvm = quote_vm ta quotea vm in
let te1 = quote_polynomial ta quotea e1 in
//ddump ("te1 = " ^ term_to_string te1);
let te2 = quote_polynomial ta quotea e2 in
//ddump ("te2 = " ^ term_to_string te2);
mapply (`(semiring_reflect
#(`#ta) (`#tr) (`#tvm) (`#te1) (`#te2) (`#t1) (`#t2)));
//ddump "Before canonization";
canon_norm ();
//ddump "After canonization";
later ();
//ddump "Before normalizing left-hand side";
canon_norm ();
//ddump "After normalizing left-hand side";
trefl ();
//ddump "Before normalizing right-hand side";
canon_norm ();
//ddump "After normalizing right-hand side";
trefl ()
| _ -> fail "Unexpected"
end
else fail "Found equality, but terms do not have the expected type"
end
| _ -> fail "Goal should be an equality") | val canon_semiring_aux
(a: Type)
(ta: term)
(unquotea: (term -> Tac a))
(quotea: (a -> Tac term))
(tr tadd topp tmone tmult: term)
(munit: a)
: Tac unit
let canon_semiring_aux
(a: Type)
(ta: term)
(unquotea: (term -> Tac a))
(quotea: (a -> Tac term))
(tr tadd topp tmone tmult: term)
(munit: a)
: Tac unit = | true | null | false | focus (fun () ->
norm [];
let g = cur_goal () in
match term_as_formula g with
| Comp (Eq (Some t)) t1 t2 ->
if term_eq t ta
then
match reification unquotea quotea tadd topp tmone tmult munit [t1; t2] with
| [e1 ; e2], vm ->
let tvm = quote_vm ta quotea vm in
let te1 = quote_polynomial ta quotea e1 in
let te2 = quote_polynomial ta quotea e2 in
mapply (`(semiring_reflect #(`#ta) (`#tr) (`#tvm) (`#te1) (`#te2) (`#t1) (`#t2)));
canon_norm ();
later ();
canon_norm ();
trefl ();
canon_norm ();
trefl ()
| _ -> fail "Unexpected"
else fail "Found equality, but terms do not have the expected type"
| _ -> fail "Goal should be an equality") | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [] | [
"FStar.Tactics.NamedView.term",
"FStar.Tactics.V2.Derived.focus",
"Prims.unit",
"FStar.Reflection.Types.typ",
"FStar.Tactics.CanonCommSemiring.polynomial",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.V2.Derived.trefl",
"FStar.Tactics.CanonCommSemiring.canon_norm",
"FStar.Tactics.V2.Derived.later",
"FStar.Tactics.MApply.mapply",
"FStar.Reflection.Types.term",
"FStar.Tactics.MApply.termable_term",
"FStar.Tactics.CanonCommSemiring.quote_polynomial",
"FStar.Tactics.CanonCommSemiring.quote_vm",
"FStar.Pervasives.Native.tuple2",
"Prims.list",
"FStar.Tactics.V2.Derived.fail",
"FStar.Tactics.CanonCommSemiring.reification",
"Prims.Cons",
"Prims.Nil",
"Prims.bool",
"FStar.Tactics.CanonCommSemiring.term_eq",
"FStar.Reflection.V2.Formula.formula",
"FStar.Reflection.V2.Formula.term_as_formula",
"FStar.Tactics.V2.Derived.cur_goal",
"FStar.Tactics.V2.Builtins.norm",
"FStar.Pervasives.norm_step"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr]
let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p)
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_p (#a:Type) (r:cr a) (vm:vmap a) (p:polynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p)
val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p))
let rec spolynomial_of_ok #a r vm p =
match p with
| Pconst c -> ()
| Pvar i -> ()
| Pplus p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Pmult p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Popp p ->
spolynomial_of_ok r vm p;
let x = interp_sp r vm (spolynomial_of r p) in
let y = r.cm_mult.mult (r.opp r.cm_mult.unit) x in
add_mult_opp r x;
opp_unique r x y
val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p)))
let rec polynomial_normalize_ok #a r vm p =
match p with
| Pvar _ -> ()
| Pconst _ -> ()
| Pplus l q ->
canonical_sum_merge_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_merge_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Pmult l q ->
canonical_sum_prod_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_prod_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Popp p1 ->
let l = SPconst (r.opp r.cm_mult.unit) in
polynomial_normalize_ok r vm p1;
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(polynomial_normalize r p1);
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(spolynomial_normalize r (spolynomial_of r p1))
val polynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_simplify r p) == interp_p r vm p)
let polynomial_simplify_ok #a r vm p =
calc (==) {
interp_cs r vm (polynomial_simplify r p);
== { }
interp_cs r vm (canonical_sum_simplify r (polynomial_normalize r p));
== { canonical_sum_simplify_ok r vm (polynomial_normalize r p) }
interp_cs r vm (polynomial_normalize r p);
== { polynomial_normalize_ok r vm p }
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p));
== { spolynomial_normalize_ok r vm (spolynomial_of r p) }
interp_sp r vm (spolynomial_of r p);
== { spolynomial_of_ok r vm p }
interp_p r vm p;
}
///
/// Tactic definition
///
(* Only dump when debugging is on *)
let ddump m = if debugging () then dump m
(**
* Finds the position of first occurrence of x in xs.
* This is specialized to terms and their funny term_eq.
**)
let rec find_aux (n:nat) (x:term) (xs:list term) : Tac (option nat) =
match xs with
| [] -> None
| x'::xs' -> if term_eq x x' then Some n else find_aux (n+1) x xs'
let find = find_aux 0
let make_fvar (#a:Type) (t:term) (unquotea:term -> Tac a) (ts:list term)
(vm:vmap a) : Tac (polynomial a * list term * vmap a) =
match find t ts with
| Some v -> (Pvar v, ts, vm)
| None ->
let vfresh = length ts in
let z = unquotea t in
(Pvar vfresh, ts @ [t], update vfresh z vm)
(** This expects that add, opp, mone mult, and t have already been normalized *)
let rec reification_aux (#a:Type) (unquotea:term -> Tac a) (ts:list term) (vm:vmap a) (add opp mone mult t: term) : Tac (polynomial a * list term * vmap a) =
// ddump ("term = " ^ term_to_string t ^ "\n");
let hd, tl = collect_app_ref t in
match inspect hd, list_unref tl with
| Tv_FVar fv, [(t1, _) ; (t2, _)] ->
//ddump ("add = " ^ term_to_string add ^ "
// \nmul = " ^ term_to_string mult);
//ddump ("fv = " ^ term_to_string (pack (Tv_FVar fv)));
let binop (op:polynomial a -> polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e1, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
let (e2, ts, vm) = reification_aux unquotea ts vm add opp mone mult t2 in
(op e1 e2, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) add then binop Pplus else
if term_eq (pack (Tv_FVar fv)) mult then binop Pmult else
make_fvar t unquotea ts vm
| Tv_FVar fv, [(t1, _)] ->
let monop (op:polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
(op e, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) opp then monop Popp else
make_fvar t unquotea ts vm
| Tv_Const _, [] -> Pconst (unquotea t), ts, vm
| _, _ -> make_fvar t unquotea ts vm
(**
* How to normalize terms in the tactic.
* This is carefully tuned to unfold all and no more than required
**)
let steps =
[
primops;
iota;
zeta;
delta_attr [`%canon_attr];
delta_only [
`%FStar.Mul.op_Star; // For integer ring
`%FStar.Algebra.CommMonoid.int_plus_cm; // For integer ring
`%FStar.Algebra.CommMonoid.int_multiply_cm; // For integer ring
`%FStar.Algebra.CommMonoid.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.__proj__CM__item__unit;
`%__proj__CR__item__cm_add;
`%__proj__CR__item__opp;
`%__proj__CR__item__cm_mult;
`%FStar.List.Tot.assoc;
`%FStar.Pervasives.Native.fst;
`%FStar.Pervasives.Native.snd;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___1;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___2;
`%FStar.List.Tot.op_At;
`%FStar.List.Tot.append;
]
]
let canon_norm () : Tac unit = norm steps
let reification (#a:Type)
(unquotea:term -> Tac a) (quotea:a -> Tac term) (tadd topp tmone tmult:term) (munit:a) (ts:list term) : Tac (list (polynomial a) * vmap a) =
// Be careful not to normalize operations too much
// E.g. we don't want to turn ( +% ) into (a + b) % prime
// or we won't be able to spot ring operations
let add = tadd in
let opp = topp in
let mone = tmone in
let mult = tmult in
let ts = Tactics.Util.map (norm_term steps) ts in
//ddump ("add = " ^ term_to_string add ^ "\nmult = " ^ term_to_string mult);
let (es, _, vm) =
Tactics.Util.fold_left
(fun (es, vs, vm) t ->
let (e, vs, vm) = reification_aux unquotea vs vm add opp mone mult t
in (e::es, vs, vm))
([],[], ([], munit)) ts
in (List.Tot.Base.rev es, vm)
(* The implicit argument in the application of `Pconst` is crucial *)
let rec quote_polynomial (#a:Type) (ta:term) (quotea:a -> Tac term) (e:polynomial a) : Tac term =
match e with
| Pconst c -> mk_app (`Pconst) [(ta, Q_Implicit); (quotea c, Q_Explicit)]
| Pvar x -> mk_e_app (`Pvar) [pack (Tv_Const (C_Int x))]
| Pplus e1 e2 ->
mk_e_app (`Pplus) [quote_polynomial ta quotea e1; quote_polynomial ta quotea e2]
| Pmult e1 e2 ->
mk_e_app (`Pmult) [quote_polynomial ta quotea e1; quote_polynomial ta quotea e2]
| Popp e -> mk_e_app (`Popp) [quote_polynomial ta quotea e]
(* Constructs the 3 main goals of the tactic *)
let semiring_reflect (#a:eqtype) (r:cr a) (vm:vmap a) (e1 e2:polynomial a) (a1 a2:a)
(_ : squash (
interp_cs r vm (polynomial_simplify r e1) ==
interp_cs r vm (polynomial_simplify r e2)))
(_ : squash (a1 == interp_p r vm e1))
(_ : squash (a2 == interp_p r vm e2)) :
squash (a1 == a2)
=
polynomial_simplify_ok r vm e1;
polynomial_simplify_ok r vm e2
(* [@@plugin] *)
let canon_semiring_aux
(a: Type) (ta: term) (unquotea: term -> Tac a) (quotea: a -> Tac term)
(tr tadd topp tmone tmult: term)
(munit: a)
: Tac unit | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val canon_semiring_aux
(a: Type)
(ta: term)
(unquotea: (term -> Tac a))
(quotea: (a -> Tac term))
(tr tadd topp tmone tmult: term)
(munit: a)
: Tac unit | [] | FStar.Tactics.CanonCommSemiring.canon_semiring_aux | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
a: Type ->
ta: FStar.Tactics.NamedView.term ->
unquotea: (_: FStar.Tactics.NamedView.term -> FStar.Tactics.Effect.Tac a) ->
quotea: (_: a -> FStar.Tactics.Effect.Tac FStar.Tactics.NamedView.term) ->
tr: FStar.Tactics.NamedView.term ->
tadd: FStar.Tactics.NamedView.term ->
topp: FStar.Tactics.NamedView.term ->
tmone: FStar.Tactics.NamedView.term ->
tmult: FStar.Tactics.NamedView.term ->
munit: a
-> FStar.Tactics.Effect.Tac Prims.unit | {
"end_col": 43,
"end_line": 1673,
"start_col": 2,
"start_line": 1627
} |
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z) | let distribute_left_lemma (a: Type) (cm_add cm_mult: cm a) = | false | null | false | let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x: a -> y: a -> z: a -> Lemma (x * (y + z) == x * y + x * z) | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"FStar.Algebra.CommMonoid.cm",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
/// | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val distribute_left_lemma : a: Type -> cm_add: FStar.Algebra.CommMonoid.cm a -> cm_mult: FStar.Algebra.CommMonoid.cm a -> Type | [] | FStar.Tactics.CanonCommSemiring.distribute_left_lemma | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: Type -> cm_add: FStar.Algebra.CommMonoid.cm a -> cm_mult: FStar.Algebra.CommMonoid.cm a -> Type | {
"end_col": 59,
"end_line": 63,
"start_col": 65,
"start_line": 60
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let vmap a = list (var * a) * a | let vmap a = | false | null | false | list (var * a) * a | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"FStar.Pervasives.Native.tuple2",
"Prims.list",
"FStar.Reflection.V2.Data.var"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure. | false | true | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val vmap : a: Type -> Type | [] | FStar.Tactics.CanonCommSemiring.vmap | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: Type -> Type | {
"end_col": 31,
"end_line": 371,
"start_col": 13,
"start_line": 371
} |
|
FStar.Pervasives.Lemma | val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)] | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let add_zero_l #a r x =
r.cm_add.identity x | val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x = | false | null | true | r.cm_add.identity x | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Algebra.CommMonoid.__proj__CM__item__identity",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add",
"Prims.unit"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)] | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)] | [] | FStar.Tactics.CanonCommSemiring.add_zero_l | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | r: FStar.Tactics.CanonCommSemiring.cr a -> x: a
-> FStar.Pervasives.Lemma (ensures CM?.mult (CR?.cm_add r) (CM?.unit (CR?.cm_add r)) x == x)
[SMTPat (CM?.mult (CR?.cm_add r) (CM?.unit (CR?.cm_add r)) x)] | {
"end_col": 22,
"end_line": 494,
"start_col": 2,
"start_line": 494
} |
FStar.Tactics.Effect.Tac | val quote_vm (#a: Type) (ta: term) (quotea: (a -> Tac term)) (vm: vmap a) : Tac term | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)] | val quote_vm (#a: Type) (ta: term) (quotea: (a -> Tac term)) (vm: vmap a) : Tac term
let quote_vm (#a: Type) (ta: term) (quotea: (a -> Tac term)) (vm: vmap a) : Tac term = | true | null | false | let quote_map_entry (p: (nat * a)) : Tac term =
mk_app (`Mktuple2)
[
((`nat), Q_Implicit);
(ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)
]
in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2)
[(tylist, Q_Implicit); (ta, Q_Implicit); (tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)] | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [] | [
"FStar.Tactics.NamedView.term",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Reflection.V2.Derived.mk_app",
"Prims.list",
"FStar.Reflection.V2.Data.argv",
"Prims.Cons",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Reflection.Types.term",
"FStar.Reflection.V2.Data.aqualv",
"FStar.Reflection.V2.Data.Q_Implicit",
"FStar.Reflection.V2.Data.Q_Explicit",
"Prims.Nil",
"FStar.Pervasives.Native.snd",
"FStar.Pervasives.Native.tuple2",
"FStar.Reflection.V2.Data.var",
"FStar.Reflection.V2.Derived.mk_e_app",
"FStar.Tactics.CanonCommSemiring.quote_list",
"FStar.Pervasives.Native.fst",
"Prims.nat",
"FStar.Tactics.NamedView.pack",
"FStar.Tactics.NamedView.Tv_Const",
"FStar.Reflection.V2.Data.C_Int"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)] | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val quote_vm (#a: Type) (ta: term) (quotea: (a -> Tac term)) (vm: vmap a) : Tac term | [] | FStar.Tactics.CanonCommSemiring.quote_vm | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
ta: FStar.Tactics.NamedView.term ->
quotea: (_: a -> FStar.Tactics.Effect.Tac FStar.Tactics.NamedView.term) ->
vm: FStar.Tactics.CanonCommSemiring.vmap a
-> FStar.Tactics.Effect.Tac FStar.Tactics.NamedView.term | {
"end_col": 73,
"end_line": 396,
"start_col": 81,
"start_line": 387
} |
Prims.Tot | val distribute_right (#a: Type) (r: cr a) : distribute_right_lemma a r.cm_add r.cm_mult | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z | val distribute_right (#a: Type) (r: cr a) : distribute_right_lemma a r.cm_add r.cm_mult
let distribute_right (#a: Type) (r: cr a) : distribute_right_lemma a r.cm_add r.cm_mult = | false | null | false | fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Algebra.CommMonoid.__proj__CM__item__commutativity",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"Prims.unit",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__distribute",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add",
"FStar.Tactics.CanonCommSemiring.distribute_right_lemma"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val distribute_right (#a: Type) (r: cr a) : distribute_right_lemma a r.cm_add r.cm_mult | [] | FStar.Tactics.CanonCommSemiring.distribute_right | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | r: FStar.Tactics.CanonCommSemiring.cr a
-> FStar.Tactics.CanonCommSemiring.distribute_right_lemma a (CR?.cm_add r) (CR?.cm_mult r) | {
"end_col": 31,
"end_line": 94,
"start_col": 2,
"start_line": 90
} |
Prims.Tot | val index:eqtype | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let index: eqtype = nat | val index:eqtype
let index:eqtype = | false | null | false | nat | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"Prims.nat"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x | false | true | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val index:eqtype | [] | FStar.Tactics.CanonCommSemiring.index | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | Prims.eqtype | {
"end_col": 23,
"end_line": 109,
"start_col": 20,
"start_line": 109
} |
Prims.Tot | val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom | val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
let rec canonical_sum_scalar #a r c0 s = | false | null | false | let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.canonical_sum",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Tactics.CanonCommSemiring.Cons_monom",
"FStar.Tactics.CanonCommSemiring.norm_fully",
"FStar.Tactics.CanonCommSemiring.canonical_sum_scalar",
"FStar.Tactics.CanonCommSemiring.Nil_monom",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.canonical_sum_scalar | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
c0: a ->
s: FStar.Tactics.CanonCommSemiring.canonical_sum a
-> FStar.Tactics.CanonCommSemiring.canonical_sum a | {
"end_col": 26,
"end_line": 256,
"start_col": 40,
"start_line": 251
} |
Prims.Tot | val ivl_aux (#a: Type) (r: cr a) (vm: vmap a) (x: index) (t: varlist) : Tot a (decreases t) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t') | val ivl_aux (#a: Type) (r: cr a) (vm: vmap a) (x: index) (t: varlist) : Tot a (decreases t)
let rec ivl_aux (#a: Type) (r: cr a) (vm: vmap a) (x: index) (t: varlist) : Tot a (decreases t) = | false | null | false | let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t') | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total",
""
] | [
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.index",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Tactics.CanonCommSemiring.interp_var",
"FStar.Tactics.CanonCommSemiring.ivl_aux",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val ivl_aux (#a: Type) (r: cr a) (vm: vmap a) (x: index) (t: varlist) : Tot a (decreases t) | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.ivl_aux | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
x: FStar.Tactics.CanonCommSemiring.index ->
t: FStar.Tactics.CanonCommSemiring.varlist
-> Prims.Tot a | {
"end_col": 66,
"end_line": 417,
"start_col": 25,
"start_line": 413
} |
Prims.Tot | val varlist_lt (x y: varlist) : bool | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false | val varlist_lt (x y: varlist) : bool
let rec varlist_lt (x y: varlist) : bool = | false | null | false | match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys -> if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Tactics.CanonCommSemiring.index",
"Prims.op_LessThan",
"Prims.bool",
"Prims.op_AmpAmp",
"Prims.op_Equality",
"FStar.Tactics.CanonCommSemiring.varlist_lt"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr] | false | true | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val varlist_lt (x y: varlist) : bool | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.varlist_lt | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.Tactics.CanonCommSemiring.varlist -> y: FStar.Tactics.CanonCommSemiring.varlist
-> Prims.bool | {
"end_col": 17,
"end_line": 141,
"start_col": 2,
"start_line": 137
} |
Prims.Tot | val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s | val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
let rec canonical_sum_scalar3 #a r c0 l0 s = | false | null | false | let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t -> monom_insert r c0 (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Tactics.CanonCommSemiring.canonical_sum",
"FStar.Tactics.CanonCommSemiring.monom_insert",
"FStar.Tactics.CanonCommSemiring.norm_fully",
"FStar.Tactics.CanonCommSemiring.varlist_merge",
"FStar.Tactics.CanonCommSemiring.canonical_sum_scalar3",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.canonical_sum_scalar3 | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
c0: a ->
l0: FStar.Tactics.CanonCommSemiring.varlist ->
s: FStar.Tactics.CanonCommSemiring.canonical_sum a
-> FStar.Tactics.CanonCommSemiring.canonical_sum a | {
"end_col": 18,
"end_line": 286,
"start_col": 44,
"start_line": 277
} |
FStar.Pervasives.Lemma | val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)] | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x | val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x = | false | null | true | r.cm_mult.commutativity r.cm_mult.unit x | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Algebra.CommMonoid.__proj__CM__item__commutativity",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit",
"Prims.unit"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)] | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)] | [] | FStar.Tactics.CanonCommSemiring.mult_one_r | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | r: FStar.Tactics.CanonCommSemiring.cr a -> x: a
-> FStar.Pervasives.Lemma (ensures CM?.mult (CR?.cm_mult r) x (CM?.unit (CR?.cm_mult r)) == x)
[SMTPat (CM?.mult (CR?.cm_mult r) x (CM?.unit (CR?.cm_mult r)))] | {
"end_col": 42,
"end_line": 476,
"start_col": 2,
"start_line": 476
} |
Prims.Tot | val interp_cs (#a: Type) (r: cr a) (vm: vmap a) (s: canonical_sum a) : a | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t | val interp_cs (#a: Type) (r: cr a) (vm: vmap a) (s: canonical_sum a) : a
let interp_cs (#a: Type) (r: cr a) (vm: vmap a) (s: canonical_sum a) : a = | false | null | false | let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.canonical_sum",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Tactics.CanonCommSemiring.ics_aux",
"FStar.Tactics.CanonCommSemiring.interp_vl",
"FStar.Tactics.CanonCommSemiring.interp_m",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *) | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val interp_cs (#a: Type) (r: cr a) (vm: vmap a) (s: canonical_sum a) : a | [] | FStar.Tactics.CanonCommSemiring.interp_cs | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
s: FStar.Tactics.CanonCommSemiring.canonical_sum a
-> a | {
"end_col": 58,
"end_line": 449,
"start_col": 70,
"start_line": 444
} |
FStar.Pervasives.Lemma | val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)] | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x | val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x = | false | null | true | r.cm_add.commutativity r.cm_add.unit x | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Algebra.CommMonoid.__proj__CM__item__commutativity",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit",
"Prims.unit"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)] | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)] | [] | FStar.Tactics.CanonCommSemiring.add_zero_r | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | r: FStar.Tactics.CanonCommSemiring.cr a -> x: a
-> FStar.Pervasives.Lemma (ensures CM?.mult (CR?.cm_add r) x (CM?.unit (CR?.cm_add r)) == x)
[SMTPat (CM?.mult (CR?.cm_add r) x (CM?.unit (CR?.cm_add r)))] | {
"end_col": 40,
"end_line": 500,
"start_col": 2,
"start_line": 500
} |
Prims.Tot | val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q) | val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
let rec spolynomial_normalize #a r p = | false | null | false | match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q -> canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q -> canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q) | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.spolynomial",
"FStar.Tactics.CanonCommSemiring.index",
"FStar.Tactics.CanonCommSemiring.Cons_varlist",
"FStar.Tactics.CanonCommSemiring.Cons_var",
"FStar.Tactics.CanonCommSemiring.Nil_var",
"FStar.Tactics.CanonCommSemiring.Nil_monom",
"FStar.Tactics.CanonCommSemiring.Cons_monom",
"FStar.Tactics.CanonCommSemiring.canonical_sum_merge",
"FStar.Tactics.CanonCommSemiring.spolynomial_normalize",
"FStar.Tactics.CanonCommSemiring.canonical_sum_prod",
"FStar.Tactics.CanonCommSemiring.canonical_sum"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr] | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.spolynomial_normalize | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | r: FStar.Tactics.CanonCommSemiring.cr a -> p: FStar.Tactics.CanonCommSemiring.spolynomial a
-> FStar.Tactics.CanonCommSemiring.canonical_sum a | {
"end_col": 80,
"end_line": 325,
"start_col": 2,
"start_line": 319
} |
Prims.Tot | val ics_aux (#a: Type) (r: cr a) (vm: vmap a) (x: a) (s: canonical_sum a) : Tot a (decreases s) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t) | val ics_aux (#a: Type) (r: cr a) (vm: vmap a) (x: a) (s: canonical_sum a) : Tot a (decreases s)
let rec ics_aux (#a: Type) (r: cr a) (vm: vmap a) (x: a) (s: canonical_sum a) : Tot a (decreases s) = | false | null | false | let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t) | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total",
""
] | [
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.canonical_sum",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Tactics.CanonCommSemiring.ics_aux",
"FStar.Tactics.CanonCommSemiring.interp_vl",
"FStar.Tactics.CanonCommSemiring.interp_m",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr] | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val ics_aux (#a: Type) (r: cr a) (vm: vmap a) (x: a) (s: canonical_sum a) : Tot a (decreases s) | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.ics_aux | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
x: a ->
s: FStar.Tactics.CanonCommSemiring.canonical_sum a
-> Prims.Tot a | {
"end_col": 68,
"end_line": 440,
"start_col": 25,
"start_line": 435
} |
FStar.Tactics.Effect.Tac | val quote_list (#a: Type) (ta: term) (quotea: (a -> Tac term)) (xs: list a) : Tac term | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)] | val quote_list (#a: Type) (ta: term) (quotea: (a -> Tac term)) (xs: list a) : Tac term
let rec quote_list (#a: Type) (ta: term) (quotea: (a -> Tac term)) (xs: list a) : Tac term = | true | null | false | match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x :: xs' ->
mk_app (`Cons) [(ta, Q_Implicit); (quotea x, Q_Explicit); (quote_list ta quotea xs', Q_Explicit)] | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [] | [
"FStar.Tactics.NamedView.term",
"Prims.list",
"FStar.Reflection.V2.Derived.mk_app",
"Prims.Cons",
"FStar.Reflection.V2.Data.argv",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Reflection.Types.term",
"FStar.Reflection.V2.Data.aqualv",
"FStar.Reflection.V2.Data.Q_Implicit",
"Prims.Nil",
"FStar.Reflection.V2.Data.Q_Explicit",
"FStar.Tactics.CanonCommSemiring.quote_list"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) : | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val quote_list (#a: Type) (ta: term) (quotea: (a -> Tac term)) (xs: list a) : Tac term | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.quote_list | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
ta: FStar.Tactics.NamedView.term ->
quotea: (_: a -> FStar.Tactics.Effect.Tac FStar.Tactics.NamedView.term) ->
xs: Prims.list a
-> FStar.Tactics.Effect.Tac FStar.Tactics.NamedView.term | {
"end_col": 68,
"end_line": 384,
"start_col": 2,
"start_line": 380
} |
Prims.Tot | val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s | val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
let rec canonical_sum_simplify #a r s = | false | null | false | let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero)
then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.canonical_sum",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Tactics.CanonCommSemiring.norm_fully",
"Prims.bool",
"Prims.op_Equality",
"FStar.Tactics.CanonCommSemiring.canonical_sum_simplify",
"FStar.Tactics.CanonCommSemiring.Cons_varlist",
"FStar.Tactics.CanonCommSemiring.Cons_monom",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.canonical_sum_simplify | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | r: FStar.Tactics.CanonCommSemiring.cr a -> s: FStar.Tactics.CanonCommSemiring.canonical_sum a
-> FStar.Tactics.CanonCommSemiring.canonical_sum a | {
"end_col": 18,
"end_line": 346,
"start_col": 39,
"start_line": 334
} |
FStar.Pervasives.Lemma | val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
} | val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y = | false | null | true | let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc ( == ) {
y;
( == ) { r.add_opp x }
y + (x + r.opp x);
( == ) { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
( == ) { r.cm_add.commutativity x y }
zero + r.opp x;
( == ) { () }
r.opp x;
} | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Calc.calc_finish",
"Prims.eq2",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__opp",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__add_opp",
"Prims.squash",
"FStar.Algebra.CommMonoid.__proj__CM__item__associativity",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add",
"FStar.Algebra.CommMonoid.__proj__CM__item__commutativity",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit) | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x) | [] | FStar.Tactics.CanonCommSemiring.opp_unique | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | r: FStar.Tactics.CanonCommSemiring.cr a -> x: a -> y: a
-> FStar.Pervasives.Lemma (requires CM?.mult (CR?.cm_add r) x y == CM?.unit (CR?.cm_add r))
(ensures y == CR?.opp r x) | {
"end_col": 3,
"end_line": 518,
"start_col": 25,
"start_line": 505
} |
FStar.Pervasives.Lemma | val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
} | val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x = | false | null | true | let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc ( == ) {
x + r.opp one * x;
( == ) { () }
one * x + r.opp one * x;
( == ) { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
( == ) { r.add_opp one }
zero * x;
( == ) { () }
zero;
} | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Calc.calc_finish",
"Prims.eq2",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__opp",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"FStar.Tactics.CanonCommSemiring.distribute_right",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__add_opp",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit) | [] | FStar.Tactics.CanonCommSemiring.add_mult_opp | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | r: FStar.Tactics.CanonCommSemiring.cr a -> x: a
-> FStar.Pervasives.Lemma
(ensures
CM?.mult (CR?.cm_add r) x (CM?.mult (CR?.cm_mult r) (CR?.opp r (CM?.unit (CR?.cm_mult r))) x) ==
CM?.unit (CR?.cm_add r)) | {
"end_col": 3,
"end_line": 537,
"start_col": 25,
"start_line": 522
} |
Prims.Tot | val interp_p (#a: Type) (r: cr a) (vm: vmap a) (p: polynomial a) : a | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec interp_p (#a:Type) (r:cr a) (vm:vmap a) (p:polynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p) | val interp_p (#a: Type) (r: cr a) (vm: vmap a) (p: polynomial a) : a
let rec interp_p (#a: Type) (r: cr a) (vm: vmap a) (p: polynomial a) : a = | false | null | false | let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p) | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.polynomial",
"FStar.Tactics.CanonCommSemiring.index",
"FStar.Tactics.CanonCommSemiring.interp_var",
"FStar.Tactics.CanonCommSemiring.interp_p",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__opp",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr]
let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p)
(** Interpretation of a polynomial *) | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val interp_p (#a: Type) (r: cr a) (vm: vmap a) (p: polynomial a) : a | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.interp_p | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
p: FStar.Tactics.CanonCommSemiring.polynomial a
-> a | {
"end_col": 37,
"end_line": 1414,
"start_col": 70,
"start_line": 1406
} |
FStar.Pervasives.Lemma | val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q | val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p = | false | null | true | match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm (spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm (spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.spolynomial",
"FStar.Tactics.CanonCommSemiring.index",
"FStar.Tactics.CanonCommSemiring.spolynomial_normalize_ok",
"Prims.unit",
"FStar.Tactics.CanonCommSemiring.canonical_sum_merge_ok",
"FStar.Tactics.CanonCommSemiring.spolynomial_normalize",
"FStar.Tactics.CanonCommSemiring.canonical_sum_prod_ok"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p) | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p) | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.spolynomial_normalize_ok | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
p: FStar.Tactics.CanonCommSemiring.spolynomial a
-> FStar.Pervasives.Lemma
(ensures
FStar.Tactics.CanonCommSemiring.interp_cs r
vm
(FStar.Tactics.CanonCommSemiring.spolynomial_normalize r p) ==
FStar.Tactics.CanonCommSemiring.interp_sp r vm p) | {
"end_col": 35,
"end_line": 1340,
"start_col": 2,
"start_line": 1328
} |
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let steps =
[
primops;
iota;
zeta;
delta_attr [`%canon_attr];
delta_only [
`%FStar.Mul.op_Star; // For integer ring
`%FStar.Algebra.CommMonoid.int_plus_cm; // For integer ring
`%FStar.Algebra.CommMonoid.int_multiply_cm; // For integer ring
`%FStar.Algebra.CommMonoid.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.__proj__CM__item__unit;
`%__proj__CR__item__cm_add;
`%__proj__CR__item__opp;
`%__proj__CR__item__cm_mult;
`%FStar.List.Tot.assoc;
`%FStar.Pervasives.Native.fst;
`%FStar.Pervasives.Native.snd;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___1;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___2;
`%FStar.List.Tot.op_At;
`%FStar.List.Tot.append;
]
] | let steps = | false | null | false | [
primops;
iota;
zeta;
delta_attr [`%canon_attr];
delta_only [
`%FStar.Mul.op_Star; `%FStar.Algebra.CommMonoid.int_plus_cm;
`%FStar.Algebra.CommMonoid.int_multiply_cm; `%FStar.Algebra.CommMonoid.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.__proj__CM__item__unit; `%__proj__CR__item__cm_add;
`%__proj__CR__item__opp; `%__proj__CR__item__cm_mult; `%FStar.List.Tot.assoc;
`%FStar.Pervasives.Native.fst; `%FStar.Pervasives.Native.snd;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___1;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___2; `%FStar.List.Tot.op_At;
`%FStar.List.Tot.append
]
] | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"Prims.Cons",
"FStar.Pervasives.norm_step",
"FStar.Pervasives.primops",
"FStar.Pervasives.iota",
"FStar.Pervasives.zeta",
"FStar.Pervasives.delta_attr",
"Prims.string",
"Prims.Nil",
"FStar.Pervasives.delta_only"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr]
let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p)
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_p (#a:Type) (r:cr a) (vm:vmap a) (p:polynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p)
val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p))
let rec spolynomial_of_ok #a r vm p =
match p with
| Pconst c -> ()
| Pvar i -> ()
| Pplus p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Pmult p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Popp p ->
spolynomial_of_ok r vm p;
let x = interp_sp r vm (spolynomial_of r p) in
let y = r.cm_mult.mult (r.opp r.cm_mult.unit) x in
add_mult_opp r x;
opp_unique r x y
val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p)))
let rec polynomial_normalize_ok #a r vm p =
match p with
| Pvar _ -> ()
| Pconst _ -> ()
| Pplus l q ->
canonical_sum_merge_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_merge_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Pmult l q ->
canonical_sum_prod_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_prod_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Popp p1 ->
let l = SPconst (r.opp r.cm_mult.unit) in
polynomial_normalize_ok r vm p1;
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(polynomial_normalize r p1);
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(spolynomial_normalize r (spolynomial_of r p1))
val polynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_simplify r p) == interp_p r vm p)
let polynomial_simplify_ok #a r vm p =
calc (==) {
interp_cs r vm (polynomial_simplify r p);
== { }
interp_cs r vm (canonical_sum_simplify r (polynomial_normalize r p));
== { canonical_sum_simplify_ok r vm (polynomial_normalize r p) }
interp_cs r vm (polynomial_normalize r p);
== { polynomial_normalize_ok r vm p }
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p));
== { spolynomial_normalize_ok r vm (spolynomial_of r p) }
interp_sp r vm (spolynomial_of r p);
== { spolynomial_of_ok r vm p }
interp_p r vm p;
}
///
/// Tactic definition
///
(* Only dump when debugging is on *)
let ddump m = if debugging () then dump m
(**
* Finds the position of first occurrence of x in xs.
* This is specialized to terms and their funny term_eq.
**)
let rec find_aux (n:nat) (x:term) (xs:list term) : Tac (option nat) =
match xs with
| [] -> None
| x'::xs' -> if term_eq x x' then Some n else find_aux (n+1) x xs'
let find = find_aux 0
let make_fvar (#a:Type) (t:term) (unquotea:term -> Tac a) (ts:list term)
(vm:vmap a) : Tac (polynomial a * list term * vmap a) =
match find t ts with
| Some v -> (Pvar v, ts, vm)
| None ->
let vfresh = length ts in
let z = unquotea t in
(Pvar vfresh, ts @ [t], update vfresh z vm)
(** This expects that add, opp, mone mult, and t have already been normalized *)
let rec reification_aux (#a:Type) (unquotea:term -> Tac a) (ts:list term) (vm:vmap a) (add opp mone mult t: term) : Tac (polynomial a * list term * vmap a) =
// ddump ("term = " ^ term_to_string t ^ "\n");
let hd, tl = collect_app_ref t in
match inspect hd, list_unref tl with
| Tv_FVar fv, [(t1, _) ; (t2, _)] ->
//ddump ("add = " ^ term_to_string add ^ "
// \nmul = " ^ term_to_string mult);
//ddump ("fv = " ^ term_to_string (pack (Tv_FVar fv)));
let binop (op:polynomial a -> polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e1, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
let (e2, ts, vm) = reification_aux unquotea ts vm add opp mone mult t2 in
(op e1 e2, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) add then binop Pplus else
if term_eq (pack (Tv_FVar fv)) mult then binop Pmult else
make_fvar t unquotea ts vm
| Tv_FVar fv, [(t1, _)] ->
let monop (op:polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
(op e, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) opp then monop Popp else
make_fvar t unquotea ts vm
| Tv_Const _, [] -> Pconst (unquotea t), ts, vm
| _, _ -> make_fvar t unquotea ts vm
(**
* How to normalize terms in the tactic.
* This is carefully tuned to unfold all and no more than required
**) | false | true | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val steps : Prims.list FStar.Pervasives.norm_step | [] | FStar.Tactics.CanonCommSemiring.steps | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | Prims.list FStar.Pervasives.norm_step | {
"end_col": 3,
"end_line": 1574,
"start_col": 2,
"start_line": 1552
} |
|
FStar.Pervasives.Lemma | val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> () | val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s = | false | null | true | let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> () | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.canonical_sum",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Tactics.CanonCommSemiring.canonical_sum_simplify_ok",
"Prims.unit",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a -> | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s) | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.canonical_sum_simplify_ok | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
s: FStar.Tactics.CanonCommSemiring.canonical_sum a
-> FStar.Pervasives.Lemma
(ensures
FStar.Tactics.CanonCommSemiring.interp_cs r
vm
(FStar.Tactics.CanonCommSemiring.canonical_sum_simplify r s) ==
FStar.Tactics.CanonCommSemiring.interp_cs r vm s) | {
"end_col": 19,
"end_line": 1350,
"start_col": 45,
"start_line": 1344
} |
Prims.Tot | val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p) | val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
let rec polynomial_normalize #a r p = | false | null | false | match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q -> canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q -> canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p -> canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p) | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.polynomial",
"FStar.Tactics.CanonCommSemiring.index",
"FStar.Tactics.CanonCommSemiring.Cons_varlist",
"FStar.Tactics.CanonCommSemiring.Cons_var",
"FStar.Tactics.CanonCommSemiring.Nil_var",
"FStar.Tactics.CanonCommSemiring.Nil_monom",
"FStar.Tactics.CanonCommSemiring.Cons_monom",
"FStar.Tactics.CanonCommSemiring.canonical_sum_merge",
"FStar.Tactics.CanonCommSemiring.polynomial_normalize",
"FStar.Tactics.CanonCommSemiring.canonical_sum_prod",
"FStar.Tactics.CanonCommSemiring.canonical_sum_scalar3",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__opp",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"FStar.Tactics.CanonCommSemiring.canonical_sum"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr] | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.polynomial_normalize | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | r: FStar.Tactics.CanonCommSemiring.cr a -> p: FStar.Tactics.CanonCommSemiring.polynomial a
-> FStar.Tactics.CanonCommSemiring.canonical_sum a | {
"end_col": 85,
"end_line": 1383,
"start_col": 2,
"start_line": 1375
} |
FStar.Tactics.Effect.Tac | val make_fvar (#a: Type) (t: term) (unquotea: (term -> Tac a)) (ts: list term) (vm: vmap a)
: Tac (polynomial a * list term * vmap a) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let make_fvar (#a:Type) (t:term) (unquotea:term -> Tac a) (ts:list term)
(vm:vmap a) : Tac (polynomial a * list term * vmap a) =
match find t ts with
| Some v -> (Pvar v, ts, vm)
| None ->
let vfresh = length ts in
let z = unquotea t in
(Pvar vfresh, ts @ [t], update vfresh z vm) | val make_fvar (#a: Type) (t: term) (unquotea: (term -> Tac a)) (ts: list term) (vm: vmap a)
: Tac (polynomial a * list term * vmap a)
let make_fvar (#a: Type) (t: term) (unquotea: (term -> Tac a)) (ts: list term) (vm: vmap a)
: Tac (polynomial a * list term * vmap a) = | true | null | false | match find t ts with
| Some v -> (Pvar v, ts, vm)
| None ->
let vfresh = length ts in
let z = unquotea t in
(Pvar vfresh, ts @ [t], update vfresh z vm) | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [] | [
"FStar.Tactics.NamedView.term",
"Prims.list",
"FStar.Tactics.CanonCommSemiring.vmap",
"Prims.nat",
"FStar.Pervasives.Native.Mktuple3",
"FStar.Tactics.CanonCommSemiring.polynomial",
"FStar.Tactics.CanonCommSemiring.Pvar",
"FStar.Pervasives.Native.tuple3",
"FStar.List.Tot.Base.op_At",
"Prims.Cons",
"Prims.Nil",
"FStar.Tactics.CanonCommSemiring.update",
"FStar.List.Tot.Base.length",
"FStar.Pervasives.Native.option",
"FStar.Tactics.CanonCommSemiring.find"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr]
let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p)
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_p (#a:Type) (r:cr a) (vm:vmap a) (p:polynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p)
val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p))
let rec spolynomial_of_ok #a r vm p =
match p with
| Pconst c -> ()
| Pvar i -> ()
| Pplus p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Pmult p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Popp p ->
spolynomial_of_ok r vm p;
let x = interp_sp r vm (spolynomial_of r p) in
let y = r.cm_mult.mult (r.opp r.cm_mult.unit) x in
add_mult_opp r x;
opp_unique r x y
val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p)))
let rec polynomial_normalize_ok #a r vm p =
match p with
| Pvar _ -> ()
| Pconst _ -> ()
| Pplus l q ->
canonical_sum_merge_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_merge_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Pmult l q ->
canonical_sum_prod_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_prod_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Popp p1 ->
let l = SPconst (r.opp r.cm_mult.unit) in
polynomial_normalize_ok r vm p1;
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(polynomial_normalize r p1);
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(spolynomial_normalize r (spolynomial_of r p1))
val polynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_simplify r p) == interp_p r vm p)
let polynomial_simplify_ok #a r vm p =
calc (==) {
interp_cs r vm (polynomial_simplify r p);
== { }
interp_cs r vm (canonical_sum_simplify r (polynomial_normalize r p));
== { canonical_sum_simplify_ok r vm (polynomial_normalize r p) }
interp_cs r vm (polynomial_normalize r p);
== { polynomial_normalize_ok r vm p }
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p));
== { spolynomial_normalize_ok r vm (spolynomial_of r p) }
interp_sp r vm (spolynomial_of r p);
== { spolynomial_of_ok r vm p }
interp_p r vm p;
}
///
/// Tactic definition
///
(* Only dump when debugging is on *)
let ddump m = if debugging () then dump m
(**
* Finds the position of first occurrence of x in xs.
* This is specialized to terms and their funny term_eq.
**)
let rec find_aux (n:nat) (x:term) (xs:list term) : Tac (option nat) =
match xs with
| [] -> None
| x'::xs' -> if term_eq x x' then Some n else find_aux (n+1) x xs'
let find = find_aux 0
let make_fvar (#a:Type) (t:term) (unquotea:term -> Tac a) (ts:list term) | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val make_fvar (#a: Type) (t: term) (unquotea: (term -> Tac a)) (ts: list term) (vm: vmap a)
: Tac (polynomial a * list term * vmap a) | [] | FStar.Tactics.CanonCommSemiring.make_fvar | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
t: FStar.Tactics.NamedView.term ->
unquotea: (_: FStar.Tactics.NamedView.term -> FStar.Tactics.Effect.Tac a) ->
ts: Prims.list FStar.Tactics.NamedView.term ->
vm: FStar.Tactics.CanonCommSemiring.vmap a
-> FStar.Tactics.Effect.Tac
((FStar.Tactics.CanonCommSemiring.polynomial a * Prims.list FStar.Tactics.NamedView.term) *
FStar.Tactics.CanonCommSemiring.vmap a) | {
"end_col": 47,
"end_line": 1518,
"start_col": 2,
"start_line": 1513
} |
FStar.Tactics.Effect.Tac | val reification
(#a: Type)
(unquotea: (term -> Tac a))
(quotea: (a -> Tac term))
(tadd topp tmone tmult: term)
(munit: a)
(ts: list term)
: Tac (list (polynomial a) * vmap a) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let reification (#a:Type)
(unquotea:term -> Tac a) (quotea:a -> Tac term) (tadd topp tmone tmult:term) (munit:a) (ts:list term) : Tac (list (polynomial a) * vmap a) =
// Be careful not to normalize operations too much
// E.g. we don't want to turn ( +% ) into (a + b) % prime
// or we won't be able to spot ring operations
let add = tadd in
let opp = topp in
let mone = tmone in
let mult = tmult in
let ts = Tactics.Util.map (norm_term steps) ts in
//ddump ("add = " ^ term_to_string add ^ "\nmult = " ^ term_to_string mult);
let (es, _, vm) =
Tactics.Util.fold_left
(fun (es, vs, vm) t ->
let (e, vs, vm) = reification_aux unquotea vs vm add opp mone mult t
in (e::es, vs, vm))
([],[], ([], munit)) ts
in (List.Tot.Base.rev es, vm) | val reification
(#a: Type)
(unquotea: (term -> Tac a))
(quotea: (a -> Tac term))
(tadd topp tmone tmult: term)
(munit: a)
(ts: list term)
: Tac (list (polynomial a) * vmap a)
let reification
(#a: Type)
(unquotea: (term -> Tac a))
(quotea: (a -> Tac term))
(tadd topp tmone tmult: term)
(munit: a)
(ts: list term)
: Tac (list (polynomial a) * vmap a) = | true | null | false | let add = tadd in
let opp = topp in
let mone = tmone in
let mult = tmult in
let ts = Tactics.Util.map (norm_term steps) ts in
let es, _, vm =
Tactics.Util.fold_left (fun (es, vs, vm) t ->
let e, vs, vm = reification_aux unquotea vs vm add opp mone mult t in
(e :: es, vs, vm))
([], [], ([], munit))
ts
in
(List.Tot.Base.rev es, vm) | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [] | [
"FStar.Tactics.NamedView.term",
"Prims.list",
"FStar.Tactics.CanonCommSemiring.polynomial",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Pervasives.Native.Mktuple2",
"FStar.List.Tot.Base.rev",
"FStar.Pervasives.Native.tuple2",
"FStar.Pervasives.Native.tuple3",
"FStar.Tactics.Util.fold_left",
"FStar.Pervasives.Native.Mktuple3",
"Prims.Cons",
"FStar.Tactics.CanonCommSemiring.reification_aux",
"Prims.Nil",
"FStar.Reflection.V2.Data.var",
"FStar.Tactics.Util.map",
"FStar.Tactics.V2.Derived.norm_term",
"FStar.Tactics.CanonCommSemiring.steps"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr]
let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p)
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_p (#a:Type) (r:cr a) (vm:vmap a) (p:polynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p)
val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p))
let rec spolynomial_of_ok #a r vm p =
match p with
| Pconst c -> ()
| Pvar i -> ()
| Pplus p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Pmult p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Popp p ->
spolynomial_of_ok r vm p;
let x = interp_sp r vm (spolynomial_of r p) in
let y = r.cm_mult.mult (r.opp r.cm_mult.unit) x in
add_mult_opp r x;
opp_unique r x y
val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p)))
let rec polynomial_normalize_ok #a r vm p =
match p with
| Pvar _ -> ()
| Pconst _ -> ()
| Pplus l q ->
canonical_sum_merge_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_merge_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Pmult l q ->
canonical_sum_prod_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_prod_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Popp p1 ->
let l = SPconst (r.opp r.cm_mult.unit) in
polynomial_normalize_ok r vm p1;
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(polynomial_normalize r p1);
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(spolynomial_normalize r (spolynomial_of r p1))
val polynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_simplify r p) == interp_p r vm p)
let polynomial_simplify_ok #a r vm p =
calc (==) {
interp_cs r vm (polynomial_simplify r p);
== { }
interp_cs r vm (canonical_sum_simplify r (polynomial_normalize r p));
== { canonical_sum_simplify_ok r vm (polynomial_normalize r p) }
interp_cs r vm (polynomial_normalize r p);
== { polynomial_normalize_ok r vm p }
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p));
== { spolynomial_normalize_ok r vm (spolynomial_of r p) }
interp_sp r vm (spolynomial_of r p);
== { spolynomial_of_ok r vm p }
interp_p r vm p;
}
///
/// Tactic definition
///
(* Only dump when debugging is on *)
let ddump m = if debugging () then dump m
(**
* Finds the position of first occurrence of x in xs.
* This is specialized to terms and their funny term_eq.
**)
let rec find_aux (n:nat) (x:term) (xs:list term) : Tac (option nat) =
match xs with
| [] -> None
| x'::xs' -> if term_eq x x' then Some n else find_aux (n+1) x xs'
let find = find_aux 0
let make_fvar (#a:Type) (t:term) (unquotea:term -> Tac a) (ts:list term)
(vm:vmap a) : Tac (polynomial a * list term * vmap a) =
match find t ts with
| Some v -> (Pvar v, ts, vm)
| None ->
let vfresh = length ts in
let z = unquotea t in
(Pvar vfresh, ts @ [t], update vfresh z vm)
(** This expects that add, opp, mone mult, and t have already been normalized *)
let rec reification_aux (#a:Type) (unquotea:term -> Tac a) (ts:list term) (vm:vmap a) (add opp mone mult t: term) : Tac (polynomial a * list term * vmap a) =
// ddump ("term = " ^ term_to_string t ^ "\n");
let hd, tl = collect_app_ref t in
match inspect hd, list_unref tl with
| Tv_FVar fv, [(t1, _) ; (t2, _)] ->
//ddump ("add = " ^ term_to_string add ^ "
// \nmul = " ^ term_to_string mult);
//ddump ("fv = " ^ term_to_string (pack (Tv_FVar fv)));
let binop (op:polynomial a -> polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e1, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
let (e2, ts, vm) = reification_aux unquotea ts vm add opp mone mult t2 in
(op e1 e2, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) add then binop Pplus else
if term_eq (pack (Tv_FVar fv)) mult then binop Pmult else
make_fvar t unquotea ts vm
| Tv_FVar fv, [(t1, _)] ->
let monop (op:polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
(op e, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) opp then monop Popp else
make_fvar t unquotea ts vm
| Tv_Const _, [] -> Pconst (unquotea t), ts, vm
| _, _ -> make_fvar t unquotea ts vm
(**
* How to normalize terms in the tactic.
* This is carefully tuned to unfold all and no more than required
**)
let steps =
[
primops;
iota;
zeta;
delta_attr [`%canon_attr];
delta_only [
`%FStar.Mul.op_Star; // For integer ring
`%FStar.Algebra.CommMonoid.int_plus_cm; // For integer ring
`%FStar.Algebra.CommMonoid.int_multiply_cm; // For integer ring
`%FStar.Algebra.CommMonoid.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.__proj__CM__item__unit;
`%__proj__CR__item__cm_add;
`%__proj__CR__item__opp;
`%__proj__CR__item__cm_mult;
`%FStar.List.Tot.assoc;
`%FStar.Pervasives.Native.fst;
`%FStar.Pervasives.Native.snd;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___1;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___2;
`%FStar.List.Tot.op_At;
`%FStar.List.Tot.append;
]
]
let canon_norm () : Tac unit = norm steps | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val reification
(#a: Type)
(unquotea: (term -> Tac a))
(quotea: (a -> Tac term))
(tadd topp tmone tmult: term)
(munit: a)
(ts: list term)
: Tac (list (polynomial a) * vmap a) | [] | FStar.Tactics.CanonCommSemiring.reification | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
unquotea: (_: FStar.Tactics.NamedView.term -> FStar.Tactics.Effect.Tac a) ->
quotea: (_: a -> FStar.Tactics.Effect.Tac FStar.Tactics.NamedView.term) ->
tadd: FStar.Tactics.NamedView.term ->
topp: FStar.Tactics.NamedView.term ->
tmone: FStar.Tactics.NamedView.term ->
tmult: FStar.Tactics.NamedView.term ->
munit: a ->
ts: Prims.list FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac
(Prims.list (FStar.Tactics.CanonCommSemiring.polynomial a) *
FStar.Tactics.CanonCommSemiring.vmap a) | {
"end_col": 31,
"end_line": 1595,
"start_col": 142,
"start_line": 1579
} |
FStar.Pervasives.Lemma | val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p))) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec polynomial_normalize_ok #a r vm p =
match p with
| Pvar _ -> ()
| Pconst _ -> ()
| Pplus l q ->
canonical_sum_merge_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_merge_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Pmult l q ->
canonical_sum_prod_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_prod_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Popp p1 ->
let l = SPconst (r.opp r.cm_mult.unit) in
polynomial_normalize_ok r vm p1;
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(polynomial_normalize r p1);
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(spolynomial_normalize r (spolynomial_of r p1)) | val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p)))
let rec polynomial_normalize_ok #a r vm p = | false | null | true | match p with
| Pvar _ -> ()
| Pconst _ -> ()
| Pplus l q ->
canonical_sum_merge_ok r vm (polynomial_normalize r l) (polynomial_normalize r q);
canonical_sum_merge_ok r
vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Pmult l q ->
canonical_sum_prod_ok r vm (polynomial_normalize r l) (polynomial_normalize r q);
canonical_sum_prod_ok r
vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Popp p1 ->
let l = SPconst (r.opp r.cm_mult.unit) in
polynomial_normalize_ok r vm p1;
canonical_sum_prod_ok r vm (spolynomial_normalize r l) (polynomial_normalize r p1);
canonical_sum_prod_ok r
vm
(spolynomial_normalize r l)
(spolynomial_normalize r (spolynomial_of r p1)) | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.polynomial",
"FStar.Tactics.CanonCommSemiring.index",
"FStar.Tactics.CanonCommSemiring.polynomial_normalize_ok",
"Prims.unit",
"FStar.Tactics.CanonCommSemiring.canonical_sum_merge_ok",
"FStar.Tactics.CanonCommSemiring.spolynomial_normalize",
"FStar.Tactics.CanonCommSemiring.spolynomial_of",
"FStar.Tactics.CanonCommSemiring.polynomial_normalize",
"FStar.Tactics.CanonCommSemiring.canonical_sum_prod_ok",
"FStar.Tactics.CanonCommSemiring.spolynomial",
"FStar.Tactics.CanonCommSemiring.SPconst",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__opp",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr]
let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p)
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_p (#a:Type) (r:cr a) (vm:vmap a) (p:polynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p)
val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p))
let rec spolynomial_of_ok #a r vm p =
match p with
| Pconst c -> ()
| Pvar i -> ()
| Pplus p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Pmult p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Popp p ->
spolynomial_of_ok r vm p;
let x = interp_sp r vm (spolynomial_of r p) in
let y = r.cm_mult.mult (r.opp r.cm_mult.unit) x in
add_mult_opp r x;
opp_unique r x y
val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p))) | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p))) | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.polynomial_normalize_ok | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
p: FStar.Tactics.CanonCommSemiring.polynomial a
-> FStar.Pervasives.Lemma
(ensures
FStar.Tactics.CanonCommSemiring.interp_cs r
vm
(FStar.Tactics.CanonCommSemiring.polynomial_normalize r p) ==
FStar.Tactics.CanonCommSemiring.interp_cs r
vm
(FStar.Tactics.CanonCommSemiring.spolynomial_normalize r
(FStar.Tactics.CanonCommSemiring.spolynomial_of r p))) | {
"end_col": 53,
"end_line": 1472,
"start_col": 2,
"start_line": 1441
} |
FStar.Pervasives.Lemma | val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p)) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec spolynomial_of_ok #a r vm p =
match p with
| Pconst c -> ()
| Pvar i -> ()
| Pplus p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Pmult p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Popp p ->
spolynomial_of_ok r vm p;
let x = interp_sp r vm (spolynomial_of r p) in
let y = r.cm_mult.mult (r.opp r.cm_mult.unit) x in
add_mult_opp r x;
opp_unique r x y | val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p))
let rec spolynomial_of_ok #a r vm p = | false | null | true | match p with
| Pconst c -> ()
| Pvar i -> ()
| Pplus p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Pmult p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Popp p ->
spolynomial_of_ok r vm p;
let x = interp_sp r vm (spolynomial_of r p) in
let y = r.cm_mult.mult (r.opp r.cm_mult.unit) x in
add_mult_opp r x;
opp_unique r x y | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.polynomial",
"FStar.Tactics.CanonCommSemiring.index",
"FStar.Tactics.CanonCommSemiring.spolynomial_of_ok",
"Prims.unit",
"FStar.Tactics.CanonCommSemiring.opp_unique",
"FStar.Tactics.CanonCommSemiring.add_mult_opp",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__opp",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit",
"FStar.Tactics.CanonCommSemiring.interp_sp",
"FStar.Tactics.CanonCommSemiring.spolynomial_of"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr]
let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p)
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_p (#a:Type) (r:cr a) (vm:vmap a) (p:polynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p)
val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p)) | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p)) | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.spolynomial_of_ok | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
p: FStar.Tactics.CanonCommSemiring.polynomial a
-> FStar.Pervasives.Lemma
(ensures
FStar.Tactics.CanonCommSemiring.interp_p r vm p ==
FStar.Tactics.CanonCommSemiring.interp_sp r
vm
(FStar.Tactics.CanonCommSemiring.spolynomial_of r p)) | {
"end_col": 20,
"end_line": 1434,
"start_col": 2,
"start_line": 1420
} |
FStar.Tactics.Effect.Tac | val quote_polynomial (#a: Type) (ta: term) (quotea: (a -> Tac term)) (e: polynomial a) : Tac term | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec quote_polynomial (#a:Type) (ta:term) (quotea:a -> Tac term) (e:polynomial a) : Tac term =
match e with
| Pconst c -> mk_app (`Pconst) [(ta, Q_Implicit); (quotea c, Q_Explicit)]
| Pvar x -> mk_e_app (`Pvar) [pack (Tv_Const (C_Int x))]
| Pplus e1 e2 ->
mk_e_app (`Pplus) [quote_polynomial ta quotea e1; quote_polynomial ta quotea e2]
| Pmult e1 e2 ->
mk_e_app (`Pmult) [quote_polynomial ta quotea e1; quote_polynomial ta quotea e2]
| Popp e -> mk_e_app (`Popp) [quote_polynomial ta quotea e] | val quote_polynomial (#a: Type) (ta: term) (quotea: (a -> Tac term)) (e: polynomial a) : Tac term
let rec quote_polynomial (#a: Type) (ta: term) (quotea: (a -> Tac term)) (e: polynomial a)
: Tac term = | true | null | false | match e with
| Pconst c -> mk_app (`Pconst) [(ta, Q_Implicit); (quotea c, Q_Explicit)]
| Pvar x -> mk_e_app (`Pvar) [pack (Tv_Const (C_Int x))]
| Pplus e1 e2 -> mk_e_app (`Pplus) [quote_polynomial ta quotea e1; quote_polynomial ta quotea e2]
| Pmult e1 e2 -> mk_e_app (`Pmult) [quote_polynomial ta quotea e1; quote_polynomial ta quotea e2]
| Popp e -> mk_e_app (`Popp) [quote_polynomial ta quotea e] | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [] | [
"FStar.Tactics.NamedView.term",
"FStar.Tactics.CanonCommSemiring.polynomial",
"FStar.Reflection.V2.Derived.mk_app",
"Prims.list",
"FStar.Reflection.V2.Data.argv",
"Prims.Cons",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Reflection.Types.term",
"FStar.Reflection.V2.Data.aqualv",
"FStar.Reflection.V2.Data.Q_Implicit",
"Prims.Nil",
"FStar.Reflection.V2.Data.Q_Explicit",
"FStar.Tactics.CanonCommSemiring.index",
"FStar.Reflection.V2.Derived.mk_e_app",
"FStar.Tactics.NamedView.pack",
"FStar.Tactics.NamedView.Tv_Const",
"FStar.Reflection.V2.Data.C_Int",
"FStar.Tactics.CanonCommSemiring.quote_polynomial"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr]
let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p)
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_p (#a:Type) (r:cr a) (vm:vmap a) (p:polynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p)
val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p))
let rec spolynomial_of_ok #a r vm p =
match p with
| Pconst c -> ()
| Pvar i -> ()
| Pplus p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Pmult p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Popp p ->
spolynomial_of_ok r vm p;
let x = interp_sp r vm (spolynomial_of r p) in
let y = r.cm_mult.mult (r.opp r.cm_mult.unit) x in
add_mult_opp r x;
opp_unique r x y
val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p)))
let rec polynomial_normalize_ok #a r vm p =
match p with
| Pvar _ -> ()
| Pconst _ -> ()
| Pplus l q ->
canonical_sum_merge_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_merge_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Pmult l q ->
canonical_sum_prod_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_prod_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Popp p1 ->
let l = SPconst (r.opp r.cm_mult.unit) in
polynomial_normalize_ok r vm p1;
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(polynomial_normalize r p1);
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(spolynomial_normalize r (spolynomial_of r p1))
val polynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_simplify r p) == interp_p r vm p)
let polynomial_simplify_ok #a r vm p =
calc (==) {
interp_cs r vm (polynomial_simplify r p);
== { }
interp_cs r vm (canonical_sum_simplify r (polynomial_normalize r p));
== { canonical_sum_simplify_ok r vm (polynomial_normalize r p) }
interp_cs r vm (polynomial_normalize r p);
== { polynomial_normalize_ok r vm p }
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p));
== { spolynomial_normalize_ok r vm (spolynomial_of r p) }
interp_sp r vm (spolynomial_of r p);
== { spolynomial_of_ok r vm p }
interp_p r vm p;
}
///
/// Tactic definition
///
(* Only dump when debugging is on *)
let ddump m = if debugging () then dump m
(**
* Finds the position of first occurrence of x in xs.
* This is specialized to terms and their funny term_eq.
**)
let rec find_aux (n:nat) (x:term) (xs:list term) : Tac (option nat) =
match xs with
| [] -> None
| x'::xs' -> if term_eq x x' then Some n else find_aux (n+1) x xs'
let find = find_aux 0
let make_fvar (#a:Type) (t:term) (unquotea:term -> Tac a) (ts:list term)
(vm:vmap a) : Tac (polynomial a * list term * vmap a) =
match find t ts with
| Some v -> (Pvar v, ts, vm)
| None ->
let vfresh = length ts in
let z = unquotea t in
(Pvar vfresh, ts @ [t], update vfresh z vm)
(** This expects that add, opp, mone mult, and t have already been normalized *)
let rec reification_aux (#a:Type) (unquotea:term -> Tac a) (ts:list term) (vm:vmap a) (add opp mone mult t: term) : Tac (polynomial a * list term * vmap a) =
// ddump ("term = " ^ term_to_string t ^ "\n");
let hd, tl = collect_app_ref t in
match inspect hd, list_unref tl with
| Tv_FVar fv, [(t1, _) ; (t2, _)] ->
//ddump ("add = " ^ term_to_string add ^ "
// \nmul = " ^ term_to_string mult);
//ddump ("fv = " ^ term_to_string (pack (Tv_FVar fv)));
let binop (op:polynomial a -> polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e1, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
let (e2, ts, vm) = reification_aux unquotea ts vm add opp mone mult t2 in
(op e1 e2, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) add then binop Pplus else
if term_eq (pack (Tv_FVar fv)) mult then binop Pmult else
make_fvar t unquotea ts vm
| Tv_FVar fv, [(t1, _)] ->
let monop (op:polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
(op e, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) opp then monop Popp else
make_fvar t unquotea ts vm
| Tv_Const _, [] -> Pconst (unquotea t), ts, vm
| _, _ -> make_fvar t unquotea ts vm
(**
* How to normalize terms in the tactic.
* This is carefully tuned to unfold all and no more than required
**)
let steps =
[
primops;
iota;
zeta;
delta_attr [`%canon_attr];
delta_only [
`%FStar.Mul.op_Star; // For integer ring
`%FStar.Algebra.CommMonoid.int_plus_cm; // For integer ring
`%FStar.Algebra.CommMonoid.int_multiply_cm; // For integer ring
`%FStar.Algebra.CommMonoid.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.__proj__CM__item__unit;
`%__proj__CR__item__cm_add;
`%__proj__CR__item__opp;
`%__proj__CR__item__cm_mult;
`%FStar.List.Tot.assoc;
`%FStar.Pervasives.Native.fst;
`%FStar.Pervasives.Native.snd;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___1;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___2;
`%FStar.List.Tot.op_At;
`%FStar.List.Tot.append;
]
]
let canon_norm () : Tac unit = norm steps
let reification (#a:Type)
(unquotea:term -> Tac a) (quotea:a -> Tac term) (tadd topp tmone tmult:term) (munit:a) (ts:list term) : Tac (list (polynomial a) * vmap a) =
// Be careful not to normalize operations too much
// E.g. we don't want to turn ( +% ) into (a + b) % prime
// or we won't be able to spot ring operations
let add = tadd in
let opp = topp in
let mone = tmone in
let mult = tmult in
let ts = Tactics.Util.map (norm_term steps) ts in
//ddump ("add = " ^ term_to_string add ^ "\nmult = " ^ term_to_string mult);
let (es, _, vm) =
Tactics.Util.fold_left
(fun (es, vs, vm) t ->
let (e, vs, vm) = reification_aux unquotea vs vm add opp mone mult t
in (e::es, vs, vm))
([],[], ([], munit)) ts
in (List.Tot.Base.rev es, vm)
(* The implicit argument in the application of `Pconst` is crucial *) | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val quote_polynomial (#a: Type) (ta: term) (quotea: (a -> Tac term)) (e: polynomial a) : Tac term | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.quote_polynomial | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
ta: FStar.Tactics.NamedView.term ->
quotea: (_: a -> FStar.Tactics.Effect.Tac FStar.Tactics.NamedView.term) ->
e: FStar.Tactics.CanonCommSemiring.polynomial a
-> FStar.Tactics.Effect.Tac FStar.Tactics.NamedView.term | {
"end_col": 61,
"end_line": 1606,
"start_col": 2,
"start_line": 1599
} |
FStar.Tactics.Effect.Tac | val canon_semiring (#a: eqtype) (r: cr a) : Tac unit | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let canon_semiring (#a:eqtype) (r:cr a) : Tac unit =
canon_semiring_aux a
(quote a) (unquote #a) (fun (x:a) -> quote x) (quote r)
(norm_term steps (quote r.cm_add.mult))
(norm_term steps (quote r.opp))
(norm_term steps (quote (r.opp r.cm_mult.unit)))
(norm_term steps (quote r.cm_mult.mult))
r.cm_add.unit | val canon_semiring (#a: eqtype) (r: cr a) : Tac unit
let canon_semiring (#a: eqtype) (r: cr a) : Tac unit = | true | null | false | canon_semiring_aux a (quote a) (unquote #a) (fun (x: a) -> quote x) (quote r)
(norm_term steps (quote r.cm_add.mult)) (norm_term steps (quote r.opp))
(norm_term steps (quote (r.opp r.cm_mult.unit))) (norm_term steps (quote r.cm_mult.mult))
r.cm_add.unit | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.canon_semiring_aux",
"FStar.Tactics.V2.Builtins.unquote",
"FStar.Reflection.Types.term",
"FStar.Tactics.NamedView.term",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add",
"Prims.unit",
"FStar.Tactics.V2.Derived.norm_term",
"FStar.Tactics.CanonCommSemiring.steps",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__opp"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr]
let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p)
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_p (#a:Type) (r:cr a) (vm:vmap a) (p:polynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p)
val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p))
let rec spolynomial_of_ok #a r vm p =
match p with
| Pconst c -> ()
| Pvar i -> ()
| Pplus p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Pmult p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Popp p ->
spolynomial_of_ok r vm p;
let x = interp_sp r vm (spolynomial_of r p) in
let y = r.cm_mult.mult (r.opp r.cm_mult.unit) x in
add_mult_opp r x;
opp_unique r x y
val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p)))
let rec polynomial_normalize_ok #a r vm p =
match p with
| Pvar _ -> ()
| Pconst _ -> ()
| Pplus l q ->
canonical_sum_merge_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_merge_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Pmult l q ->
canonical_sum_prod_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_prod_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Popp p1 ->
let l = SPconst (r.opp r.cm_mult.unit) in
polynomial_normalize_ok r vm p1;
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(polynomial_normalize r p1);
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(spolynomial_normalize r (spolynomial_of r p1))
val polynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_simplify r p) == interp_p r vm p)
let polynomial_simplify_ok #a r vm p =
calc (==) {
interp_cs r vm (polynomial_simplify r p);
== { }
interp_cs r vm (canonical_sum_simplify r (polynomial_normalize r p));
== { canonical_sum_simplify_ok r vm (polynomial_normalize r p) }
interp_cs r vm (polynomial_normalize r p);
== { polynomial_normalize_ok r vm p }
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p));
== { spolynomial_normalize_ok r vm (spolynomial_of r p) }
interp_sp r vm (spolynomial_of r p);
== { spolynomial_of_ok r vm p }
interp_p r vm p;
}
///
/// Tactic definition
///
(* Only dump when debugging is on *)
let ddump m = if debugging () then dump m
(**
* Finds the position of first occurrence of x in xs.
* This is specialized to terms and their funny term_eq.
**)
let rec find_aux (n:nat) (x:term) (xs:list term) : Tac (option nat) =
match xs with
| [] -> None
| x'::xs' -> if term_eq x x' then Some n else find_aux (n+1) x xs'
let find = find_aux 0
let make_fvar (#a:Type) (t:term) (unquotea:term -> Tac a) (ts:list term)
(vm:vmap a) : Tac (polynomial a * list term * vmap a) =
match find t ts with
| Some v -> (Pvar v, ts, vm)
| None ->
let vfresh = length ts in
let z = unquotea t in
(Pvar vfresh, ts @ [t], update vfresh z vm)
(** This expects that add, opp, mone mult, and t have already been normalized *)
let rec reification_aux (#a:Type) (unquotea:term -> Tac a) (ts:list term) (vm:vmap a) (add opp mone mult t: term) : Tac (polynomial a * list term * vmap a) =
// ddump ("term = " ^ term_to_string t ^ "\n");
let hd, tl = collect_app_ref t in
match inspect hd, list_unref tl with
| Tv_FVar fv, [(t1, _) ; (t2, _)] ->
//ddump ("add = " ^ term_to_string add ^ "
// \nmul = " ^ term_to_string mult);
//ddump ("fv = " ^ term_to_string (pack (Tv_FVar fv)));
let binop (op:polynomial a -> polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e1, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
let (e2, ts, vm) = reification_aux unquotea ts vm add opp mone mult t2 in
(op e1 e2, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) add then binop Pplus else
if term_eq (pack (Tv_FVar fv)) mult then binop Pmult else
make_fvar t unquotea ts vm
| Tv_FVar fv, [(t1, _)] ->
let monop (op:polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
(op e, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) opp then monop Popp else
make_fvar t unquotea ts vm
| Tv_Const _, [] -> Pconst (unquotea t), ts, vm
| _, _ -> make_fvar t unquotea ts vm
(**
* How to normalize terms in the tactic.
* This is carefully tuned to unfold all and no more than required
**)
let steps =
[
primops;
iota;
zeta;
delta_attr [`%canon_attr];
delta_only [
`%FStar.Mul.op_Star; // For integer ring
`%FStar.Algebra.CommMonoid.int_plus_cm; // For integer ring
`%FStar.Algebra.CommMonoid.int_multiply_cm; // For integer ring
`%FStar.Algebra.CommMonoid.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.__proj__CM__item__unit;
`%__proj__CR__item__cm_add;
`%__proj__CR__item__opp;
`%__proj__CR__item__cm_mult;
`%FStar.List.Tot.assoc;
`%FStar.Pervasives.Native.fst;
`%FStar.Pervasives.Native.snd;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___1;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___2;
`%FStar.List.Tot.op_At;
`%FStar.List.Tot.append;
]
]
let canon_norm () : Tac unit = norm steps
let reification (#a:Type)
(unquotea:term -> Tac a) (quotea:a -> Tac term) (tadd topp tmone tmult:term) (munit:a) (ts:list term) : Tac (list (polynomial a) * vmap a) =
// Be careful not to normalize operations too much
// E.g. we don't want to turn ( +% ) into (a + b) % prime
// or we won't be able to spot ring operations
let add = tadd in
let opp = topp in
let mone = tmone in
let mult = tmult in
let ts = Tactics.Util.map (norm_term steps) ts in
//ddump ("add = " ^ term_to_string add ^ "\nmult = " ^ term_to_string mult);
let (es, _, vm) =
Tactics.Util.fold_left
(fun (es, vs, vm) t ->
let (e, vs, vm) = reification_aux unquotea vs vm add opp mone mult t
in (e::es, vs, vm))
([],[], ([], munit)) ts
in (List.Tot.Base.rev es, vm)
(* The implicit argument in the application of `Pconst` is crucial *)
let rec quote_polynomial (#a:Type) (ta:term) (quotea:a -> Tac term) (e:polynomial a) : Tac term =
match e with
| Pconst c -> mk_app (`Pconst) [(ta, Q_Implicit); (quotea c, Q_Explicit)]
| Pvar x -> mk_e_app (`Pvar) [pack (Tv_Const (C_Int x))]
| Pplus e1 e2 ->
mk_e_app (`Pplus) [quote_polynomial ta quotea e1; quote_polynomial ta quotea e2]
| Pmult e1 e2 ->
mk_e_app (`Pmult) [quote_polynomial ta quotea e1; quote_polynomial ta quotea e2]
| Popp e -> mk_e_app (`Popp) [quote_polynomial ta quotea e]
(* Constructs the 3 main goals of the tactic *)
let semiring_reflect (#a:eqtype) (r:cr a) (vm:vmap a) (e1 e2:polynomial a) (a1 a2:a)
(_ : squash (
interp_cs r vm (polynomial_simplify r e1) ==
interp_cs r vm (polynomial_simplify r e2)))
(_ : squash (a1 == interp_p r vm e1))
(_ : squash (a2 == interp_p r vm e2)) :
squash (a1 == a2)
=
polynomial_simplify_ok r vm e1;
polynomial_simplify_ok r vm e2
(* [@@plugin] *)
let canon_semiring_aux
(a: Type) (ta: term) (unquotea: term -> Tac a) (quotea: a -> Tac term)
(tr tadd topp tmone tmult: term)
(munit: a)
: Tac unit
=
focus (fun () ->
norm []; // Do not normalize anything implicitly
let g = cur_goal () in
match term_as_formula g with
| Comp (Eq (Some t)) t1 t2 ->
begin
//ddump ("t1 = " ^ term_to_string t1 ^ "\nt2 = " ^ term_to_string t2);
if term_eq t ta then
begin
match reification unquotea quotea tadd topp tmone tmult munit [t1; t2] with
| ([e1; e2], vm) ->
(*
ddump (term_to_string t1);
ddump (term_to_string t2);
let r : cr a = unquote tr in
ddump ("vm = " ^ term_to_string (quote vm) ^ "\n" ^
"before = " ^ term_to_string (norm_term steps
(quote (interp_p r vm e1 == interp_p r vm e2))));
dump ("expected after = " ^ term_to_string (norm_term steps
(quote (
interp_cs r vm (polynomial_simplify r e1) ==
interp_cs r vm (polynomial_simplify r e2)))));
*)
let tvm = quote_vm ta quotea vm in
let te1 = quote_polynomial ta quotea e1 in
//ddump ("te1 = " ^ term_to_string te1);
let te2 = quote_polynomial ta quotea e2 in
//ddump ("te2 = " ^ term_to_string te2);
mapply (`(semiring_reflect
#(`#ta) (`#tr) (`#tvm) (`#te1) (`#te2) (`#t1) (`#t2)));
//ddump "Before canonization";
canon_norm ();
//ddump "After canonization";
later ();
//ddump "Before normalizing left-hand side";
canon_norm ();
//ddump "After normalizing left-hand side";
trefl ();
//ddump "Before normalizing right-hand side";
canon_norm ();
//ddump "After normalizing right-hand side";
trefl ()
| _ -> fail "Unexpected"
end
else fail "Found equality, but terms do not have the expected type"
end
| _ -> fail "Goal should be an equality") | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val canon_semiring (#a: eqtype) (r: cr a) : Tac unit | [] | FStar.Tactics.CanonCommSemiring.canon_semiring | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | r: FStar.Tactics.CanonCommSemiring.cr a -> FStar.Tactics.Effect.Tac Prims.unit | {
"end_col": 17,
"end_line": 1682,
"start_col": 2,
"start_line": 1676
} |
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t) | let interp_m (#a: Type) (r: cr a) (vm: vmap a) (c: a) (l: varlist) = | false | null | false | let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t) | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Tactics.CanonCommSemiring.index",
"FStar.Tactics.CanonCommSemiring.ivl_aux",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val interp_m : r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
c: a ->
l: FStar.Tactics.CanonCommSemiring.varlist
-> a | [] | FStar.Tactics.CanonCommSemiring.interp_m | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
c: a ->
l: FStar.Tactics.CanonCommSemiring.varlist
-> a | {
"end_col": 46,
"end_line": 431,
"start_col": 63,
"start_line": 427
} |
|
FStar.Pervasives.Lemma | val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s)) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> () | val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s = | false | null | true | let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc ( == ) {
interp_cs r vm (canonical_sum_scalar r c0 s);
( == ) { () }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
( == ) { () }
aplus (amult (amult c0 c) (interp_vl r vm l)) (interp_cs r vm (canonical_sum_scalar r c0 t));
( == ) { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l))) (interp_cs r vm (canonical_sum_scalar r c0 t));
( == ) { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l))) (amult c0 (interp_cs r vm t));
( == ) { r.distribute c0 (amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
( == ) { () }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t ->
let c = aone in
calc ( == ) {
interp_cs r vm (canonical_sum_scalar r c0 s);
( == ) { () }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
( == ) { () }
aplus (amult (amult c0 c) (interp_vl r vm l)) (interp_cs r vm (canonical_sum_scalar r c0 t));
( == ) { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l))) (interp_cs r vm (canonical_sum_scalar r c0 t));
( == ) { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l))) (amult c0 (interp_cs r vm t));
( == ) { r.distribute c0 (amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
( == ) { () }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> () | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.canonical_sum",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Calc.calc_finish",
"Prims.eq2",
"FStar.Tactics.CanonCommSemiring.interp_cs",
"FStar.Tactics.CanonCommSemiring.canonical_sum_scalar",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"FStar.Tactics.CanonCommSemiring.interp_vl",
"FStar.Tactics.CanonCommSemiring.Cons_monom",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"FStar.Algebra.CommMonoid.__proj__CM__item__associativity",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"FStar.Tactics.CanonCommSemiring.canonical_sum_scalar_ok",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__distribute",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) == | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s)) | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.canonical_sum_scalar_ok | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
c0: a ->
s: FStar.Tactics.CanonCommSemiring.canonical_sum a
-> FStar.Pervasives.Lemma
(ensures
FStar.Tactics.CanonCommSemiring.interp_cs r
vm
(FStar.Tactics.CanonCommSemiring.canonical_sum_scalar r c0 s) ==
CM?.mult (CR?.cm_mult r) c0 (FStar.Tactics.CanonCommSemiring.interp_cs r vm s)) | {
"end_col": 19,
"end_line": 1098,
"start_col": 46,
"start_line": 1052
} |
Prims.Tot | val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom | val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
let rec canonical_sum_scalar2 #a r l0 s = | false | null | false | match s with
| Cons_monom c l t -> monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t -> varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Tactics.CanonCommSemiring.canonical_sum",
"FStar.Tactics.CanonCommSemiring.monom_insert",
"FStar.Tactics.CanonCommSemiring.varlist_merge",
"FStar.Tactics.CanonCommSemiring.canonical_sum_scalar2",
"FStar.Tactics.CanonCommSemiring.varlist_insert",
"FStar.Tactics.CanonCommSemiring.Nil_monom"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr] | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.canonical_sum_scalar2 | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
l0: FStar.Tactics.CanonCommSemiring.varlist ->
s: FStar.Tactics.CanonCommSemiring.canonical_sum a
-> FStar.Tactics.CanonCommSemiring.canonical_sum a | {
"end_col": 26,
"end_line": 269,
"start_col": 2,
"start_line": 264
} |
Prims.Tot | val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1 | val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
let rec canonical_sum_prod #a r s1 s2 = | false | null | false | match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2) (canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2) (canonical_sum_prod r t1 s2)
| Nil_monom -> s1 | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.canonical_sum",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Tactics.CanonCommSemiring.canonical_sum_merge",
"FStar.Tactics.CanonCommSemiring.canonical_sum_scalar3",
"FStar.Tactics.CanonCommSemiring.canonical_sum_prod",
"FStar.Tactics.CanonCommSemiring.canonical_sum_scalar2"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr] | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.canonical_sum_prod | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
s1: FStar.Tactics.CanonCommSemiring.canonical_sum a ->
s2: FStar.Tactics.CanonCommSemiring.canonical_sum a
-> FStar.Tactics.CanonCommSemiring.canonical_sum a | {
"end_col": 19,
"end_line": 301,
"start_col": 2,
"start_line": 294
} |
Prims.Tot | val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom | val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
let rec monom_insert #a r c1 l1 s2 = | false | null | false | let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else if varlist_lt l1 l2 then Cons_monom c1 l1 s2 else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else if varlist_lt l1 l2 then Cons_monom c1 l1 s2 else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom -> if c1 = aone then Cons_varlist l1 Nil_monom else Cons_monom c1 l1 Nil_monom | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Tactics.CanonCommSemiring.canonical_sum",
"Prims.op_Equality",
"FStar.Tactics.CanonCommSemiring.Cons_monom",
"FStar.Tactics.CanonCommSemiring.norm_fully",
"Prims.bool",
"FStar.Tactics.CanonCommSemiring.varlist_lt",
"FStar.Tactics.CanonCommSemiring.monom_insert",
"FStar.Tactics.CanonCommSemiring.Cons_varlist",
"FStar.Tactics.CanonCommSemiring.Nil_monom",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.monom_insert | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
c1: a ->
l1: FStar.Tactics.CanonCommSemiring.varlist ->
s2: FStar.Tactics.CanonCommSemiring.canonical_sum a
-> FStar.Tactics.CanonCommSemiring.canonical_sum a | {
"end_col": 35,
"end_line": 237,
"start_col": 36,
"start_line": 216
} |
FStar.Pervasives.Lemma | val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)] | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit | val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x = | false | null | true | r.cm_mult.commutativity x r.cm_add.unit | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Algebra.CommMonoid.__proj__CM__item__commutativity",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add",
"Prims.unit"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)] | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)] | [] | FStar.Tactics.CanonCommSemiring.mult_zero_r | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | r: FStar.Tactics.CanonCommSemiring.cr a -> x: a
-> FStar.Pervasives.Lemma
(ensures CM?.mult (CR?.cm_mult r) x (CM?.unit (CR?.cm_add r)) == CM?.unit (CR?.cm_add r))
[SMTPat (CM?.mult (CR?.cm_mult r) x (CM?.unit (CR?.cm_add r)))] | {
"end_col": 41,
"end_line": 488,
"start_col": 2,
"start_line": 488
} |
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t | let interp_vl (#a: Type) (r: cr a) (vm: vmap a) (l: varlist) = | false | null | false | let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Tactics.CanonCommSemiring.index",
"FStar.Tactics.CanonCommSemiring.ivl_aux",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t') | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val interp_vl : r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
l: FStar.Tactics.CanonCommSemiring.varlist
-> a | [] | FStar.Tactics.CanonCommSemiring.interp_vl | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
l: FStar.Tactics.CanonCommSemiring.varlist
-> a | {
"end_col": 36,
"end_line": 424,
"start_col": 58,
"start_line": 420
} |
|
Prims.Tot | val interp_sp (#a: Type) (r: cr a) (vm: vmap a) (p: spolynomial a) : a | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2) | val interp_sp (#a: Type) (r: cr a) (vm: vmap a) (p: spolynomial a) : a
let rec interp_sp (#a: Type) (r: cr a) (vm: vmap a) (p: spolynomial a) : a = | false | null | false | let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2) | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.spolynomial",
"FStar.Tactics.CanonCommSemiring.index",
"FStar.Tactics.CanonCommSemiring.interp_var",
"FStar.Tactics.CanonCommSemiring.interp_sp",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *) | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val interp_sp (#a: Type) (r: cr a) (vm: vmap a) (p: spolynomial a) : a | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.interp_sp | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
p: FStar.Tactics.CanonCommSemiring.spolynomial a
-> a | {
"end_col": 65,
"end_line": 460,
"start_col": 72,
"start_line": 453
} |
FStar.Pervasives.Lemma | val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t | val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s = | false | null | true | match s with
| Nil_monom -> ()
| Cons_varlist l t -> ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux_ok r vm (interp_m r vm c l) t | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma",
""
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.canonical_sum",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Tactics.CanonCommSemiring.ics_aux_ok",
"FStar.Tactics.CanonCommSemiring.interp_vl",
"FStar.Tactics.CanonCommSemiring.interp_m",
"Prims.unit"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s) | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s) | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.ics_aux_ok | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
x: a ->
s: FStar.Tactics.CanonCommSemiring.canonical_sum a
-> FStar.Pervasives.Lemma
(ensures
FStar.Tactics.CanonCommSemiring.ics_aux r vm x s ==
CM?.mult (CR?.cm_add r) x (FStar.Tactics.CanonCommSemiring.interp_cs r vm s)) (decreases s) | {
"end_col": 41,
"end_line": 622,
"start_col": 2,
"start_line": 617
} |
FStar.Tactics.Effect.Tac | val int_semiring: Prims.unit -> Tac unit | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let int_semiring () : Tac unit =
(* Check to see if goal is a `nat` equality, change the equality to `int` beforehand *)
match term_as_formula (cur_goal ()) with
| Comp (Eq (Some t)) _ _ ->
if term_eq t (`Prims.nat)
then (apply_lemma (`eq_nat_via_int); canon_semiring int_cr)
else canon_semiring int_cr
| _ ->
canon_semiring int_cr | val int_semiring: Prims.unit -> Tac unit
let int_semiring () : Tac unit = | true | null | false | match term_as_formula (cur_goal ()) with
| Comp (Eq (Some t)) _ _ ->
if term_eq t (`Prims.nat)
then
(apply_lemma (`eq_nat_via_int);
canon_semiring int_cr)
else canon_semiring int_cr
| _ -> canon_semiring int_cr | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [] | [
"Prims.unit",
"FStar.Reflection.Types.typ",
"FStar.Tactics.NamedView.term",
"FStar.Tactics.CanonCommSemiring.canon_semiring",
"Prims.int",
"FStar.Tactics.CanonCommSemiring.int_cr",
"FStar.Tactics.V2.Derived.apply_lemma",
"Prims.bool",
"FStar.Tactics.CanonCommSemiring.term_eq",
"FStar.Reflection.V2.Formula.formula",
"FStar.Reflection.V2.Formula.term_as_formula",
"FStar.Tactics.V2.Derived.cur_goal"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr]
let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p)
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_p (#a:Type) (r:cr a) (vm:vmap a) (p:polynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p)
val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p))
let rec spolynomial_of_ok #a r vm p =
match p with
| Pconst c -> ()
| Pvar i -> ()
| Pplus p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Pmult p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Popp p ->
spolynomial_of_ok r vm p;
let x = interp_sp r vm (spolynomial_of r p) in
let y = r.cm_mult.mult (r.opp r.cm_mult.unit) x in
add_mult_opp r x;
opp_unique r x y
val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p)))
let rec polynomial_normalize_ok #a r vm p =
match p with
| Pvar _ -> ()
| Pconst _ -> ()
| Pplus l q ->
canonical_sum_merge_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_merge_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Pmult l q ->
canonical_sum_prod_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_prod_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Popp p1 ->
let l = SPconst (r.opp r.cm_mult.unit) in
polynomial_normalize_ok r vm p1;
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(polynomial_normalize r p1);
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(spolynomial_normalize r (spolynomial_of r p1))
val polynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_simplify r p) == interp_p r vm p)
let polynomial_simplify_ok #a r vm p =
calc (==) {
interp_cs r vm (polynomial_simplify r p);
== { }
interp_cs r vm (canonical_sum_simplify r (polynomial_normalize r p));
== { canonical_sum_simplify_ok r vm (polynomial_normalize r p) }
interp_cs r vm (polynomial_normalize r p);
== { polynomial_normalize_ok r vm p }
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p));
== { spolynomial_normalize_ok r vm (spolynomial_of r p) }
interp_sp r vm (spolynomial_of r p);
== { spolynomial_of_ok r vm p }
interp_p r vm p;
}
///
/// Tactic definition
///
(* Only dump when debugging is on *)
let ddump m = if debugging () then dump m
(**
* Finds the position of first occurrence of x in xs.
* This is specialized to terms and their funny term_eq.
**)
let rec find_aux (n:nat) (x:term) (xs:list term) : Tac (option nat) =
match xs with
| [] -> None
| x'::xs' -> if term_eq x x' then Some n else find_aux (n+1) x xs'
let find = find_aux 0
let make_fvar (#a:Type) (t:term) (unquotea:term -> Tac a) (ts:list term)
(vm:vmap a) : Tac (polynomial a * list term * vmap a) =
match find t ts with
| Some v -> (Pvar v, ts, vm)
| None ->
let vfresh = length ts in
let z = unquotea t in
(Pvar vfresh, ts @ [t], update vfresh z vm)
(** This expects that add, opp, mone mult, and t have already been normalized *)
let rec reification_aux (#a:Type) (unquotea:term -> Tac a) (ts:list term) (vm:vmap a) (add opp mone mult t: term) : Tac (polynomial a * list term * vmap a) =
// ddump ("term = " ^ term_to_string t ^ "\n");
let hd, tl = collect_app_ref t in
match inspect hd, list_unref tl with
| Tv_FVar fv, [(t1, _) ; (t2, _)] ->
//ddump ("add = " ^ term_to_string add ^ "
// \nmul = " ^ term_to_string mult);
//ddump ("fv = " ^ term_to_string (pack (Tv_FVar fv)));
let binop (op:polynomial a -> polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e1, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
let (e2, ts, vm) = reification_aux unquotea ts vm add opp mone mult t2 in
(op e1 e2, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) add then binop Pplus else
if term_eq (pack (Tv_FVar fv)) mult then binop Pmult else
make_fvar t unquotea ts vm
| Tv_FVar fv, [(t1, _)] ->
let monop (op:polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
(op e, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) opp then monop Popp else
make_fvar t unquotea ts vm
| Tv_Const _, [] -> Pconst (unquotea t), ts, vm
| _, _ -> make_fvar t unquotea ts vm
(**
* How to normalize terms in the tactic.
* This is carefully tuned to unfold all and no more than required
**)
let steps =
[
primops;
iota;
zeta;
delta_attr [`%canon_attr];
delta_only [
`%FStar.Mul.op_Star; // For integer ring
`%FStar.Algebra.CommMonoid.int_plus_cm; // For integer ring
`%FStar.Algebra.CommMonoid.int_multiply_cm; // For integer ring
`%FStar.Algebra.CommMonoid.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.__proj__CM__item__unit;
`%__proj__CR__item__cm_add;
`%__proj__CR__item__opp;
`%__proj__CR__item__cm_mult;
`%FStar.List.Tot.assoc;
`%FStar.Pervasives.Native.fst;
`%FStar.Pervasives.Native.snd;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___1;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___2;
`%FStar.List.Tot.op_At;
`%FStar.List.Tot.append;
]
]
let canon_norm () : Tac unit = norm steps
let reification (#a:Type)
(unquotea:term -> Tac a) (quotea:a -> Tac term) (tadd topp tmone tmult:term) (munit:a) (ts:list term) : Tac (list (polynomial a) * vmap a) =
// Be careful not to normalize operations too much
// E.g. we don't want to turn ( +% ) into (a + b) % prime
// or we won't be able to spot ring operations
let add = tadd in
let opp = topp in
let mone = tmone in
let mult = tmult in
let ts = Tactics.Util.map (norm_term steps) ts in
//ddump ("add = " ^ term_to_string add ^ "\nmult = " ^ term_to_string mult);
let (es, _, vm) =
Tactics.Util.fold_left
(fun (es, vs, vm) t ->
let (e, vs, vm) = reification_aux unquotea vs vm add opp mone mult t
in (e::es, vs, vm))
([],[], ([], munit)) ts
in (List.Tot.Base.rev es, vm)
(* The implicit argument in the application of `Pconst` is crucial *)
let rec quote_polynomial (#a:Type) (ta:term) (quotea:a -> Tac term) (e:polynomial a) : Tac term =
match e with
| Pconst c -> mk_app (`Pconst) [(ta, Q_Implicit); (quotea c, Q_Explicit)]
| Pvar x -> mk_e_app (`Pvar) [pack (Tv_Const (C_Int x))]
| Pplus e1 e2 ->
mk_e_app (`Pplus) [quote_polynomial ta quotea e1; quote_polynomial ta quotea e2]
| Pmult e1 e2 ->
mk_e_app (`Pmult) [quote_polynomial ta quotea e1; quote_polynomial ta quotea e2]
| Popp e -> mk_e_app (`Popp) [quote_polynomial ta quotea e]
(* Constructs the 3 main goals of the tactic *)
let semiring_reflect (#a:eqtype) (r:cr a) (vm:vmap a) (e1 e2:polynomial a) (a1 a2:a)
(_ : squash (
interp_cs r vm (polynomial_simplify r e1) ==
interp_cs r vm (polynomial_simplify r e2)))
(_ : squash (a1 == interp_p r vm e1))
(_ : squash (a2 == interp_p r vm e2)) :
squash (a1 == a2)
=
polynomial_simplify_ok r vm e1;
polynomial_simplify_ok r vm e2
(* [@@plugin] *)
let canon_semiring_aux
(a: Type) (ta: term) (unquotea: term -> Tac a) (quotea: a -> Tac term)
(tr tadd topp tmone tmult: term)
(munit: a)
: Tac unit
=
focus (fun () ->
norm []; // Do not normalize anything implicitly
let g = cur_goal () in
match term_as_formula g with
| Comp (Eq (Some t)) t1 t2 ->
begin
//ddump ("t1 = " ^ term_to_string t1 ^ "\nt2 = " ^ term_to_string t2);
if term_eq t ta then
begin
match reification unquotea quotea tadd topp tmone tmult munit [t1; t2] with
| ([e1; e2], vm) ->
(*
ddump (term_to_string t1);
ddump (term_to_string t2);
let r : cr a = unquote tr in
ddump ("vm = " ^ term_to_string (quote vm) ^ "\n" ^
"before = " ^ term_to_string (norm_term steps
(quote (interp_p r vm e1 == interp_p r vm e2))));
dump ("expected after = " ^ term_to_string (norm_term steps
(quote (
interp_cs r vm (polynomial_simplify r e1) ==
interp_cs r vm (polynomial_simplify r e2)))));
*)
let tvm = quote_vm ta quotea vm in
let te1 = quote_polynomial ta quotea e1 in
//ddump ("te1 = " ^ term_to_string te1);
let te2 = quote_polynomial ta quotea e2 in
//ddump ("te2 = " ^ term_to_string te2);
mapply (`(semiring_reflect
#(`#ta) (`#tr) (`#tvm) (`#te1) (`#te2) (`#t1) (`#t2)));
//ddump "Before canonization";
canon_norm ();
//ddump "After canonization";
later ();
//ddump "Before normalizing left-hand side";
canon_norm ();
//ddump "After normalizing left-hand side";
trefl ();
//ddump "Before normalizing right-hand side";
canon_norm ();
//ddump "After normalizing right-hand side";
trefl ()
| _ -> fail "Unexpected"
end
else fail "Found equality, but terms do not have the expected type"
end
| _ -> fail "Goal should be an equality")
let canon_semiring (#a:eqtype) (r:cr a) : Tac unit =
canon_semiring_aux a
(quote a) (unquote #a) (fun (x:a) -> quote x) (quote r)
(norm_term steps (quote r.cm_add.mult))
(norm_term steps (quote r.opp))
(norm_term steps (quote (r.opp r.cm_mult.unit)))
(norm_term steps (quote r.cm_mult.mult))
r.cm_add.unit
/// Ring of integers
[@@canon_attr]
let int_cr : cr int =
CR int_plus_cm int_multiply_cm op_Minus (fun x -> ()) (fun x y z -> ()) (fun x -> ())
private
let eq_nat_via_int (a b : nat) (eq : squash (eq2 #int a b)) : Lemma (eq2 #nat a b) = ()
let int_semiring () : Tac unit = | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val int_semiring: Prims.unit -> Tac unit | [] | FStar.Tactics.CanonCommSemiring.int_semiring | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | _: Prims.unit -> FStar.Tactics.Effect.Tac Prims.unit | {
"end_col": 29,
"end_line": 1701,
"start_col": 4,
"start_line": 1695
} |
FStar.Pervasives.Lemma | val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z)) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
} | val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z = | false | null | true | let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc ( == ) {
aplus (aplus w x) (aplus y z);
( == ) { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
( == ) { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
( == ) { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
( == ) { assoc w y z }
aplus (aplus (aplus w y) z) x;
( == ) { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
( == ) { comm z x }
aplus (aplus w y) (aplus x z);
} | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Calc.calc_finish",
"Prims.eq2",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"Prims.l_True",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add",
"FStar.Pervasives.pattern",
"FStar.Algebra.CommMonoid.__proj__CM__item__commutativity",
"FStar.Algebra.CommMonoid.__proj__CM__item__associativity"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z)) | [] | FStar.Tactics.CanonCommSemiring.aplus_assoc_4 | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | r: FStar.Tactics.CanonCommSemiring.cr a -> w: a -> x: a -> y: a -> z: a
-> FStar.Pervasives.Lemma
(ensures
(let aplus = CM?.mult (CR?.cm_add r) in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))) | {
"end_col": 3,
"end_line": 649,
"start_col": 32,
"start_line": 631
} |
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let test (a:int) =
let open FStar.Mul in
assert (a + - a + 2 * a + - a == -a + 2 * a) by (int_semiring ()) | let test (a: int) = | false | null | false | let open FStar.Mul in
FStar.Tactics.Effect.assert_by_tactic (a + - a + 2 * a + - a == - a + 2 * a)
(fun _ ->
();
(int_semiring ())) | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"Prims.int",
"FStar.Tactics.Effect.assert_by_tactic",
"Prims.eq2",
"Prims.op_Addition",
"Prims.op_Minus",
"FStar.Mul.op_Star",
"Prims.unit",
"FStar.Tactics.CanonCommSemiring.int_semiring"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr]
let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p)
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_p (#a:Type) (r:cr a) (vm:vmap a) (p:polynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p)
val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p))
let rec spolynomial_of_ok #a r vm p =
match p with
| Pconst c -> ()
| Pvar i -> ()
| Pplus p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Pmult p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Popp p ->
spolynomial_of_ok r vm p;
let x = interp_sp r vm (spolynomial_of r p) in
let y = r.cm_mult.mult (r.opp r.cm_mult.unit) x in
add_mult_opp r x;
opp_unique r x y
val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p)))
let rec polynomial_normalize_ok #a r vm p =
match p with
| Pvar _ -> ()
| Pconst _ -> ()
| Pplus l q ->
canonical_sum_merge_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_merge_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Pmult l q ->
canonical_sum_prod_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_prod_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Popp p1 ->
let l = SPconst (r.opp r.cm_mult.unit) in
polynomial_normalize_ok r vm p1;
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(polynomial_normalize r p1);
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(spolynomial_normalize r (spolynomial_of r p1))
val polynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_simplify r p) == interp_p r vm p)
let polynomial_simplify_ok #a r vm p =
calc (==) {
interp_cs r vm (polynomial_simplify r p);
== { }
interp_cs r vm (canonical_sum_simplify r (polynomial_normalize r p));
== { canonical_sum_simplify_ok r vm (polynomial_normalize r p) }
interp_cs r vm (polynomial_normalize r p);
== { polynomial_normalize_ok r vm p }
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p));
== { spolynomial_normalize_ok r vm (spolynomial_of r p) }
interp_sp r vm (spolynomial_of r p);
== { spolynomial_of_ok r vm p }
interp_p r vm p;
}
///
/// Tactic definition
///
(* Only dump when debugging is on *)
let ddump m = if debugging () then dump m
(**
* Finds the position of first occurrence of x in xs.
* This is specialized to terms and their funny term_eq.
**)
let rec find_aux (n:nat) (x:term) (xs:list term) : Tac (option nat) =
match xs with
| [] -> None
| x'::xs' -> if term_eq x x' then Some n else find_aux (n+1) x xs'
let find = find_aux 0
let make_fvar (#a:Type) (t:term) (unquotea:term -> Tac a) (ts:list term)
(vm:vmap a) : Tac (polynomial a * list term * vmap a) =
match find t ts with
| Some v -> (Pvar v, ts, vm)
| None ->
let vfresh = length ts in
let z = unquotea t in
(Pvar vfresh, ts @ [t], update vfresh z vm)
(** This expects that add, opp, mone mult, and t have already been normalized *)
let rec reification_aux (#a:Type) (unquotea:term -> Tac a) (ts:list term) (vm:vmap a) (add opp mone mult t: term) : Tac (polynomial a * list term * vmap a) =
// ddump ("term = " ^ term_to_string t ^ "\n");
let hd, tl = collect_app_ref t in
match inspect hd, list_unref tl with
| Tv_FVar fv, [(t1, _) ; (t2, _)] ->
//ddump ("add = " ^ term_to_string add ^ "
// \nmul = " ^ term_to_string mult);
//ddump ("fv = " ^ term_to_string (pack (Tv_FVar fv)));
let binop (op:polynomial a -> polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e1, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
let (e2, ts, vm) = reification_aux unquotea ts vm add opp mone mult t2 in
(op e1 e2, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) add then binop Pplus else
if term_eq (pack (Tv_FVar fv)) mult then binop Pmult else
make_fvar t unquotea ts vm
| Tv_FVar fv, [(t1, _)] ->
let monop (op:polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
(op e, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) opp then monop Popp else
make_fvar t unquotea ts vm
| Tv_Const _, [] -> Pconst (unquotea t), ts, vm
| _, _ -> make_fvar t unquotea ts vm
(**
* How to normalize terms in the tactic.
* This is carefully tuned to unfold all and no more than required
**)
let steps =
[
primops;
iota;
zeta;
delta_attr [`%canon_attr];
delta_only [
`%FStar.Mul.op_Star; // For integer ring
`%FStar.Algebra.CommMonoid.int_plus_cm; // For integer ring
`%FStar.Algebra.CommMonoid.int_multiply_cm; // For integer ring
`%FStar.Algebra.CommMonoid.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.__proj__CM__item__unit;
`%__proj__CR__item__cm_add;
`%__proj__CR__item__opp;
`%__proj__CR__item__cm_mult;
`%FStar.List.Tot.assoc;
`%FStar.Pervasives.Native.fst;
`%FStar.Pervasives.Native.snd;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___1;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___2;
`%FStar.List.Tot.op_At;
`%FStar.List.Tot.append;
]
]
let canon_norm () : Tac unit = norm steps
let reification (#a:Type)
(unquotea:term -> Tac a) (quotea:a -> Tac term) (tadd topp tmone tmult:term) (munit:a) (ts:list term) : Tac (list (polynomial a) * vmap a) =
// Be careful not to normalize operations too much
// E.g. we don't want to turn ( +% ) into (a + b) % prime
// or we won't be able to spot ring operations
let add = tadd in
let opp = topp in
let mone = tmone in
let mult = tmult in
let ts = Tactics.Util.map (norm_term steps) ts in
//ddump ("add = " ^ term_to_string add ^ "\nmult = " ^ term_to_string mult);
let (es, _, vm) =
Tactics.Util.fold_left
(fun (es, vs, vm) t ->
let (e, vs, vm) = reification_aux unquotea vs vm add opp mone mult t
in (e::es, vs, vm))
([],[], ([], munit)) ts
in (List.Tot.Base.rev es, vm)
(* The implicit argument in the application of `Pconst` is crucial *)
let rec quote_polynomial (#a:Type) (ta:term) (quotea:a -> Tac term) (e:polynomial a) : Tac term =
match e with
| Pconst c -> mk_app (`Pconst) [(ta, Q_Implicit); (quotea c, Q_Explicit)]
| Pvar x -> mk_e_app (`Pvar) [pack (Tv_Const (C_Int x))]
| Pplus e1 e2 ->
mk_e_app (`Pplus) [quote_polynomial ta quotea e1; quote_polynomial ta quotea e2]
| Pmult e1 e2 ->
mk_e_app (`Pmult) [quote_polynomial ta quotea e1; quote_polynomial ta quotea e2]
| Popp e -> mk_e_app (`Popp) [quote_polynomial ta quotea e]
(* Constructs the 3 main goals of the tactic *)
let semiring_reflect (#a:eqtype) (r:cr a) (vm:vmap a) (e1 e2:polynomial a) (a1 a2:a)
(_ : squash (
interp_cs r vm (polynomial_simplify r e1) ==
interp_cs r vm (polynomial_simplify r e2)))
(_ : squash (a1 == interp_p r vm e1))
(_ : squash (a2 == interp_p r vm e2)) :
squash (a1 == a2)
=
polynomial_simplify_ok r vm e1;
polynomial_simplify_ok r vm e2
(* [@@plugin] *)
let canon_semiring_aux
(a: Type) (ta: term) (unquotea: term -> Tac a) (quotea: a -> Tac term)
(tr tadd topp tmone tmult: term)
(munit: a)
: Tac unit
=
focus (fun () ->
norm []; // Do not normalize anything implicitly
let g = cur_goal () in
match term_as_formula g with
| Comp (Eq (Some t)) t1 t2 ->
begin
//ddump ("t1 = " ^ term_to_string t1 ^ "\nt2 = " ^ term_to_string t2);
if term_eq t ta then
begin
match reification unquotea quotea tadd topp tmone tmult munit [t1; t2] with
| ([e1; e2], vm) ->
(*
ddump (term_to_string t1);
ddump (term_to_string t2);
let r : cr a = unquote tr in
ddump ("vm = " ^ term_to_string (quote vm) ^ "\n" ^
"before = " ^ term_to_string (norm_term steps
(quote (interp_p r vm e1 == interp_p r vm e2))));
dump ("expected after = " ^ term_to_string (norm_term steps
(quote (
interp_cs r vm (polynomial_simplify r e1) ==
interp_cs r vm (polynomial_simplify r e2)))));
*)
let tvm = quote_vm ta quotea vm in
let te1 = quote_polynomial ta quotea e1 in
//ddump ("te1 = " ^ term_to_string te1);
let te2 = quote_polynomial ta quotea e2 in
//ddump ("te2 = " ^ term_to_string te2);
mapply (`(semiring_reflect
#(`#ta) (`#tr) (`#tvm) (`#te1) (`#te2) (`#t1) (`#t2)));
//ddump "Before canonization";
canon_norm ();
//ddump "After canonization";
later ();
//ddump "Before normalizing left-hand side";
canon_norm ();
//ddump "After normalizing left-hand side";
trefl ();
//ddump "Before normalizing right-hand side";
canon_norm ();
//ddump "After normalizing right-hand side";
trefl ()
| _ -> fail "Unexpected"
end
else fail "Found equality, but terms do not have the expected type"
end
| _ -> fail "Goal should be an equality")
let canon_semiring (#a:eqtype) (r:cr a) : Tac unit =
canon_semiring_aux a
(quote a) (unquote #a) (fun (x:a) -> quote x) (quote r)
(norm_term steps (quote r.cm_add.mult))
(norm_term steps (quote r.opp))
(norm_term steps (quote (r.opp r.cm_mult.unit)))
(norm_term steps (quote r.cm_mult.mult))
r.cm_add.unit
/// Ring of integers
[@@canon_attr]
let int_cr : cr int =
CR int_plus_cm int_multiply_cm op_Minus (fun x -> ()) (fun x y z -> ()) (fun x -> ())
private
let eq_nat_via_int (a b : nat) (eq : squash (eq2 #int a b)) : Lemma (eq2 #nat a b) = ()
let int_semiring () : Tac unit =
(* Check to see if goal is a `nat` equality, change the equality to `int` beforehand *)
match term_as_formula (cur_goal ()) with
| Comp (Eq (Some t)) _ _ ->
if term_eq t (`Prims.nat)
then (apply_lemma (`eq_nat_via_int); canon_semiring int_cr)
else canon_semiring int_cr
| _ ->
canon_semiring int_cr
#set-options "--tactic_trace_d 0 --no_smt" | false | true | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": true,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val test : a: Prims.int -> Prims.unit | [] | FStar.Tactics.CanonCommSemiring.test | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: Prims.int -> Prims.unit | {
"end_col": 67,
"end_line": 1707,
"start_col": 2,
"start_line": 1706
} |
|
FStar.Pervasives.Lemma | val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2)) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2 | val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 = | false | null | true | let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2 | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Tactics.CanonCommSemiring.canonical_sum",
"FStar.Tactics.CanonCommSemiring.monom_insert_ok",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"Prims.unit"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) == | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2)) | [] | FStar.Tactics.CanonCommSemiring.varlist_insert_ok | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
l1: FStar.Tactics.CanonCommSemiring.varlist ->
s2: FStar.Tactics.CanonCommSemiring.canonical_sum a
-> FStar.Pervasives.Lemma
(ensures
FStar.Tactics.CanonCommSemiring.interp_cs r
vm
(FStar.Tactics.CanonCommSemiring.varlist_insert r l1 s2) ==
CM?.mult (CR?.cm_add r)
(FStar.Tactics.CanonCommSemiring.interp_vl r vm l1)
(FStar.Tactics.CanonCommSemiring.interp_cs r vm s2)) | {
"end_col": 33,
"end_line": 1045,
"start_col": 37,
"start_line": 1043
} |
FStar.Pervasives.Lemma | val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p | val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p = | false | null | true | canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.spolynomial",
"FStar.Tactics.CanonCommSemiring.spolynomial_normalize_ok",
"Prims.unit",
"FStar.Tactics.CanonCommSemiring.canonical_sum_simplify_ok",
"FStar.Tactics.CanonCommSemiring.spolynomial_normalize"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p) | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p) | [] | FStar.Tactics.CanonCommSemiring.spolynomial_simplify_ok | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
p: FStar.Tactics.CanonCommSemiring.spolynomial a
-> FStar.Pervasives.Lemma
(ensures
FStar.Tactics.CanonCommSemiring.interp_cs r
vm
(FStar.Tactics.CanonCommSemiring.spolynomial_simplify r p) ==
FStar.Tactics.CanonCommSemiring.interp_sp r vm p) | {
"end_col": 33,
"end_line": 1356,
"start_col": 2,
"start_line": 1355
} |
FStar.Pervasives.Lemma | val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2)) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> () | val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 = | false | null | true | let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc ( == ) {
interp_cs r vm (canonical_sum_prod r s1 s2);
( == ) { () }
interp_cs r
vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2) (canonical_sum_prod r t1 s2));
( == ) { canonical_sum_merge_ok r
vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
( == ) { (canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2) }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
( == ) { distribute_right r
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1)) (interp_cs r vm s2);
( == ) { () }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc ( == ) {
interp_cs r vm (canonical_sum_prod r s1 s2);
( == ) { () }
interp_cs r
vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2) (canonical_sum_prod r t1 s2));
( == ) { canonical_sum_merge_ok r
vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
( == ) { (canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2) }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
( == ) { distribute_right r (interp_vl r vm l1) (interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1)) (interp_cs r vm s2);
( == ) { () }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> () | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.canonical_sum",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Calc.calc_finish",
"Prims.eq2",
"FStar.Tactics.CanonCommSemiring.interp_cs",
"FStar.Tactics.CanonCommSemiring.canonical_sum_prod",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"FStar.Tactics.CanonCommSemiring.interp_vl",
"FStar.Tactics.CanonCommSemiring.canonical_sum_scalar3",
"FStar.Tactics.CanonCommSemiring.canonical_sum_merge",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"FStar.Tactics.CanonCommSemiring.canonical_sum_merge_ok",
"FStar.Tactics.CanonCommSemiring.canonical_sum_prod_ok",
"FStar.Tactics.CanonCommSemiring.canonical_sum_scalar3_ok",
"FStar.Tactics.CanonCommSemiring.distribute_right",
"FStar.Tactics.CanonCommSemiring.canonical_sum_scalar2",
"FStar.Tactics.CanonCommSemiring.canonical_sum_scalar2_ok",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) == | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2)) | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.canonical_sum_prod_ok | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
s1: FStar.Tactics.CanonCommSemiring.canonical_sum a ->
s2: FStar.Tactics.CanonCommSemiring.canonical_sum a
-> FStar.Pervasives.Lemma
(ensures
FStar.Tactics.CanonCommSemiring.interp_cs r
vm
(FStar.Tactics.CanonCommSemiring.canonical_sum_prod r s1 s2) ==
CM?.mult (CR?.cm_mult r)
(FStar.Tactics.CanonCommSemiring.interp_cs r vm s1)
(FStar.Tactics.CanonCommSemiring.interp_cs r vm s2)) | {
"end_col": 19,
"end_line": 1323,
"start_col": 45,
"start_line": 1272
} |
Prims.Tot | val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p) | val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
let rec spolynomial_of #a r p = | false | null | false | match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p) | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.polynomial",
"FStar.Tactics.CanonCommSemiring.index",
"FStar.Tactics.CanonCommSemiring.SPvar",
"FStar.Tactics.CanonCommSemiring.SPconst",
"FStar.Tactics.CanonCommSemiring.SPplus",
"FStar.Tactics.CanonCommSemiring.spolynomial_of",
"FStar.Tactics.CanonCommSemiring.SPmult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__opp",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"FStar.Tactics.CanonCommSemiring.spolynomial"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr] | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.spolynomial_of | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | r: FStar.Tactics.CanonCommSemiring.cr a -> p: FStar.Tactics.CanonCommSemiring.polynomial a
-> FStar.Tactics.CanonCommSemiring.spolynomial a | {
"end_col": 74,
"end_line": 1402,
"start_col": 2,
"start_line": 1397
} |
FStar.Pervasives.Lemma | val polynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_simplify r p) == interp_p r vm p) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let polynomial_simplify_ok #a r vm p =
calc (==) {
interp_cs r vm (polynomial_simplify r p);
== { }
interp_cs r vm (canonical_sum_simplify r (polynomial_normalize r p));
== { canonical_sum_simplify_ok r vm (polynomial_normalize r p) }
interp_cs r vm (polynomial_normalize r p);
== { polynomial_normalize_ok r vm p }
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p));
== { spolynomial_normalize_ok r vm (spolynomial_of r p) }
interp_sp r vm (spolynomial_of r p);
== { spolynomial_of_ok r vm p }
interp_p r vm p;
} | val polynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_simplify r p) == interp_p r vm p)
let polynomial_simplify_ok #a r vm p = | false | null | true | calc ( == ) {
interp_cs r vm (polynomial_simplify r p);
( == ) { () }
interp_cs r vm (canonical_sum_simplify r (polynomial_normalize r p));
( == ) { canonical_sum_simplify_ok r vm (polynomial_normalize r p) }
interp_cs r vm (polynomial_normalize r p);
( == ) { polynomial_normalize_ok r vm p }
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p));
( == ) { spolynomial_normalize_ok r vm (spolynomial_of r p) }
interp_sp r vm (spolynomial_of r p);
( == ) { spolynomial_of_ok r vm p }
interp_p r vm p;
} | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.polynomial",
"FStar.Calc.calc_finish",
"Prims.eq2",
"FStar.Tactics.CanonCommSemiring.interp_cs",
"FStar.Tactics.CanonCommSemiring.polynomial_simplify",
"FStar.Tactics.CanonCommSemiring.interp_p",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"FStar.Tactics.CanonCommSemiring.interp_sp",
"FStar.Tactics.CanonCommSemiring.spolynomial_of",
"FStar.Tactics.CanonCommSemiring.spolynomial_normalize",
"FStar.Tactics.CanonCommSemiring.polynomial_normalize",
"FStar.Tactics.CanonCommSemiring.canonical_sum_simplify",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"FStar.Tactics.CanonCommSemiring.canonical_sum_simplify_ok",
"FStar.Tactics.CanonCommSemiring.polynomial_normalize_ok",
"FStar.Tactics.CanonCommSemiring.spolynomial_normalize_ok",
"FStar.Tactics.CanonCommSemiring.spolynomial_of_ok"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr]
let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p)
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_p (#a:Type) (r:cr a) (vm:vmap a) (p:polynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p)
val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p))
let rec spolynomial_of_ok #a r vm p =
match p with
| Pconst c -> ()
| Pvar i -> ()
| Pplus p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Pmult p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Popp p ->
spolynomial_of_ok r vm p;
let x = interp_sp r vm (spolynomial_of r p) in
let y = r.cm_mult.mult (r.opp r.cm_mult.unit) x in
add_mult_opp r x;
opp_unique r x y
val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p)))
let rec polynomial_normalize_ok #a r vm p =
match p with
| Pvar _ -> ()
| Pconst _ -> ()
| Pplus l q ->
canonical_sum_merge_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_merge_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Pmult l q ->
canonical_sum_prod_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_prod_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Popp p1 ->
let l = SPconst (r.opp r.cm_mult.unit) in
polynomial_normalize_ok r vm p1;
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(polynomial_normalize r p1);
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(spolynomial_normalize r (spolynomial_of r p1))
val polynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_simplify r p) == interp_p r vm p) | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val polynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_simplify r p) == interp_p r vm p) | [] | FStar.Tactics.CanonCommSemiring.polynomial_simplify_ok | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
p: FStar.Tactics.CanonCommSemiring.polynomial a
-> FStar.Pervasives.Lemma
(ensures
FStar.Tactics.CanonCommSemiring.interp_cs r
vm
(FStar.Tactics.CanonCommSemiring.polynomial_simplify r p) ==
FStar.Tactics.CanonCommSemiring.interp_p r vm p) | {
"end_col": 3,
"end_line": 1490,
"start_col": 2,
"start_line": 1478
} |
FStar.Tactics.Effect.Tac | val find_aux (n: nat) (x: term) (xs: list term) : Tac (option nat) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec find_aux (n:nat) (x:term) (xs:list term) : Tac (option nat) =
match xs with
| [] -> None
| x'::xs' -> if term_eq x x' then Some n else find_aux (n+1) x xs' | val find_aux (n: nat) (x: term) (xs: list term) : Tac (option nat)
let rec find_aux (n: nat) (x: term) (xs: list term) : Tac (option nat) = | true | null | false | match xs with
| [] -> None
| x' :: xs' -> if term_eq x x' then Some n else find_aux (n + 1) x xs' | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [] | [
"Prims.nat",
"FStar.Tactics.NamedView.term",
"Prims.list",
"FStar.Pervasives.Native.None",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.Some",
"Prims.bool",
"FStar.Tactics.CanonCommSemiring.find_aux",
"Prims.op_Addition",
"FStar.Tactics.CanonCommSemiring.term_eq"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr]
let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p)
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_p (#a:Type) (r:cr a) (vm:vmap a) (p:polynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p)
val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p))
let rec spolynomial_of_ok #a r vm p =
match p with
| Pconst c -> ()
| Pvar i -> ()
| Pplus p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Pmult p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Popp p ->
spolynomial_of_ok r vm p;
let x = interp_sp r vm (spolynomial_of r p) in
let y = r.cm_mult.mult (r.opp r.cm_mult.unit) x in
add_mult_opp r x;
opp_unique r x y
val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p)))
let rec polynomial_normalize_ok #a r vm p =
match p with
| Pvar _ -> ()
| Pconst _ -> ()
| Pplus l q ->
canonical_sum_merge_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_merge_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Pmult l q ->
canonical_sum_prod_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_prod_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Popp p1 ->
let l = SPconst (r.opp r.cm_mult.unit) in
polynomial_normalize_ok r vm p1;
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(polynomial_normalize r p1);
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(spolynomial_normalize r (spolynomial_of r p1))
val polynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_simplify r p) == interp_p r vm p)
let polynomial_simplify_ok #a r vm p =
calc (==) {
interp_cs r vm (polynomial_simplify r p);
== { }
interp_cs r vm (canonical_sum_simplify r (polynomial_normalize r p));
== { canonical_sum_simplify_ok r vm (polynomial_normalize r p) }
interp_cs r vm (polynomial_normalize r p);
== { polynomial_normalize_ok r vm p }
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p));
== { spolynomial_normalize_ok r vm (spolynomial_of r p) }
interp_sp r vm (spolynomial_of r p);
== { spolynomial_of_ok r vm p }
interp_p r vm p;
}
///
/// Tactic definition
///
(* Only dump when debugging is on *)
let ddump m = if debugging () then dump m
(**
* Finds the position of first occurrence of x in xs.
* This is specialized to terms and their funny term_eq.
**) | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val find_aux (n: nat) (x: term) (xs: list term) : Tac (option nat) | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.find_aux | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | n: Prims.nat -> x: FStar.Tactics.NamedView.term -> xs: Prims.list FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac (FStar.Pervasives.Native.option Prims.nat) | {
"end_col": 68,
"end_line": 1507,
"start_col": 2,
"start_line": 1505
} |
Prims.Tot | val int_cr:cr int | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let int_cr : cr int =
CR int_plus_cm int_multiply_cm op_Minus (fun x -> ()) (fun x y z -> ()) (fun x -> ()) | val int_cr:cr int
let int_cr:cr int = | false | null | false | CR int_plus_cm int_multiply_cm op_Minus (fun x -> ()) (fun x y z -> ()) (fun x -> ()) | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"FStar.Tactics.CanonCommSemiring.CR",
"Prims.int",
"FStar.Algebra.CommMonoid.int_plus_cm",
"FStar.Algebra.CommMonoid.int_multiply_cm",
"Prims.op_Minus",
"Prims.unit"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr]
let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p)
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_p (#a:Type) (r:cr a) (vm:vmap a) (p:polynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p)
val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p))
let rec spolynomial_of_ok #a r vm p =
match p with
| Pconst c -> ()
| Pvar i -> ()
| Pplus p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Pmult p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Popp p ->
spolynomial_of_ok r vm p;
let x = interp_sp r vm (spolynomial_of r p) in
let y = r.cm_mult.mult (r.opp r.cm_mult.unit) x in
add_mult_opp r x;
opp_unique r x y
val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p)))
let rec polynomial_normalize_ok #a r vm p =
match p with
| Pvar _ -> ()
| Pconst _ -> ()
| Pplus l q ->
canonical_sum_merge_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_merge_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Pmult l q ->
canonical_sum_prod_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_prod_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Popp p1 ->
let l = SPconst (r.opp r.cm_mult.unit) in
polynomial_normalize_ok r vm p1;
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(polynomial_normalize r p1);
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(spolynomial_normalize r (spolynomial_of r p1))
val polynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_simplify r p) == interp_p r vm p)
let polynomial_simplify_ok #a r vm p =
calc (==) {
interp_cs r vm (polynomial_simplify r p);
== { }
interp_cs r vm (canonical_sum_simplify r (polynomial_normalize r p));
== { canonical_sum_simplify_ok r vm (polynomial_normalize r p) }
interp_cs r vm (polynomial_normalize r p);
== { polynomial_normalize_ok r vm p }
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p));
== { spolynomial_normalize_ok r vm (spolynomial_of r p) }
interp_sp r vm (spolynomial_of r p);
== { spolynomial_of_ok r vm p }
interp_p r vm p;
}
///
/// Tactic definition
///
(* Only dump when debugging is on *)
let ddump m = if debugging () then dump m
(**
* Finds the position of first occurrence of x in xs.
* This is specialized to terms and their funny term_eq.
**)
let rec find_aux (n:nat) (x:term) (xs:list term) : Tac (option nat) =
match xs with
| [] -> None
| x'::xs' -> if term_eq x x' then Some n else find_aux (n+1) x xs'
let find = find_aux 0
let make_fvar (#a:Type) (t:term) (unquotea:term -> Tac a) (ts:list term)
(vm:vmap a) : Tac (polynomial a * list term * vmap a) =
match find t ts with
| Some v -> (Pvar v, ts, vm)
| None ->
let vfresh = length ts in
let z = unquotea t in
(Pvar vfresh, ts @ [t], update vfresh z vm)
(** This expects that add, opp, mone mult, and t have already been normalized *)
let rec reification_aux (#a:Type) (unquotea:term -> Tac a) (ts:list term) (vm:vmap a) (add opp mone mult t: term) : Tac (polynomial a * list term * vmap a) =
// ddump ("term = " ^ term_to_string t ^ "\n");
let hd, tl = collect_app_ref t in
match inspect hd, list_unref tl with
| Tv_FVar fv, [(t1, _) ; (t2, _)] ->
//ddump ("add = " ^ term_to_string add ^ "
// \nmul = " ^ term_to_string mult);
//ddump ("fv = " ^ term_to_string (pack (Tv_FVar fv)));
let binop (op:polynomial a -> polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e1, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
let (e2, ts, vm) = reification_aux unquotea ts vm add opp mone mult t2 in
(op e1 e2, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) add then binop Pplus else
if term_eq (pack (Tv_FVar fv)) mult then binop Pmult else
make_fvar t unquotea ts vm
| Tv_FVar fv, [(t1, _)] ->
let monop (op:polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
(op e, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) opp then monop Popp else
make_fvar t unquotea ts vm
| Tv_Const _, [] -> Pconst (unquotea t), ts, vm
| _, _ -> make_fvar t unquotea ts vm
(**
* How to normalize terms in the tactic.
* This is carefully tuned to unfold all and no more than required
**)
let steps =
[
primops;
iota;
zeta;
delta_attr [`%canon_attr];
delta_only [
`%FStar.Mul.op_Star; // For integer ring
`%FStar.Algebra.CommMonoid.int_plus_cm; // For integer ring
`%FStar.Algebra.CommMonoid.int_multiply_cm; // For integer ring
`%FStar.Algebra.CommMonoid.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.__proj__CM__item__unit;
`%__proj__CR__item__cm_add;
`%__proj__CR__item__opp;
`%__proj__CR__item__cm_mult;
`%FStar.List.Tot.assoc;
`%FStar.Pervasives.Native.fst;
`%FStar.Pervasives.Native.snd;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___1;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___2;
`%FStar.List.Tot.op_At;
`%FStar.List.Tot.append;
]
]
let canon_norm () : Tac unit = norm steps
let reification (#a:Type)
(unquotea:term -> Tac a) (quotea:a -> Tac term) (tadd topp tmone tmult:term) (munit:a) (ts:list term) : Tac (list (polynomial a) * vmap a) =
// Be careful not to normalize operations too much
// E.g. we don't want to turn ( +% ) into (a + b) % prime
// or we won't be able to spot ring operations
let add = tadd in
let opp = topp in
let mone = tmone in
let mult = tmult in
let ts = Tactics.Util.map (norm_term steps) ts in
//ddump ("add = " ^ term_to_string add ^ "\nmult = " ^ term_to_string mult);
let (es, _, vm) =
Tactics.Util.fold_left
(fun (es, vs, vm) t ->
let (e, vs, vm) = reification_aux unquotea vs vm add opp mone mult t
in (e::es, vs, vm))
([],[], ([], munit)) ts
in (List.Tot.Base.rev es, vm)
(* The implicit argument in the application of `Pconst` is crucial *)
let rec quote_polynomial (#a:Type) (ta:term) (quotea:a -> Tac term) (e:polynomial a) : Tac term =
match e with
| Pconst c -> mk_app (`Pconst) [(ta, Q_Implicit); (quotea c, Q_Explicit)]
| Pvar x -> mk_e_app (`Pvar) [pack (Tv_Const (C_Int x))]
| Pplus e1 e2 ->
mk_e_app (`Pplus) [quote_polynomial ta quotea e1; quote_polynomial ta quotea e2]
| Pmult e1 e2 ->
mk_e_app (`Pmult) [quote_polynomial ta quotea e1; quote_polynomial ta quotea e2]
| Popp e -> mk_e_app (`Popp) [quote_polynomial ta quotea e]
(* Constructs the 3 main goals of the tactic *)
let semiring_reflect (#a:eqtype) (r:cr a) (vm:vmap a) (e1 e2:polynomial a) (a1 a2:a)
(_ : squash (
interp_cs r vm (polynomial_simplify r e1) ==
interp_cs r vm (polynomial_simplify r e2)))
(_ : squash (a1 == interp_p r vm e1))
(_ : squash (a2 == interp_p r vm e2)) :
squash (a1 == a2)
=
polynomial_simplify_ok r vm e1;
polynomial_simplify_ok r vm e2
(* [@@plugin] *)
let canon_semiring_aux
(a: Type) (ta: term) (unquotea: term -> Tac a) (quotea: a -> Tac term)
(tr tadd topp tmone tmult: term)
(munit: a)
: Tac unit
=
focus (fun () ->
norm []; // Do not normalize anything implicitly
let g = cur_goal () in
match term_as_formula g with
| Comp (Eq (Some t)) t1 t2 ->
begin
//ddump ("t1 = " ^ term_to_string t1 ^ "\nt2 = " ^ term_to_string t2);
if term_eq t ta then
begin
match reification unquotea quotea tadd topp tmone tmult munit [t1; t2] with
| ([e1; e2], vm) ->
(*
ddump (term_to_string t1);
ddump (term_to_string t2);
let r : cr a = unquote tr in
ddump ("vm = " ^ term_to_string (quote vm) ^ "\n" ^
"before = " ^ term_to_string (norm_term steps
(quote (interp_p r vm e1 == interp_p r vm e2))));
dump ("expected after = " ^ term_to_string (norm_term steps
(quote (
interp_cs r vm (polynomial_simplify r e1) ==
interp_cs r vm (polynomial_simplify r e2)))));
*)
let tvm = quote_vm ta quotea vm in
let te1 = quote_polynomial ta quotea e1 in
//ddump ("te1 = " ^ term_to_string te1);
let te2 = quote_polynomial ta quotea e2 in
//ddump ("te2 = " ^ term_to_string te2);
mapply (`(semiring_reflect
#(`#ta) (`#tr) (`#tvm) (`#te1) (`#te2) (`#t1) (`#t2)));
//ddump "Before canonization";
canon_norm ();
//ddump "After canonization";
later ();
//ddump "Before normalizing left-hand side";
canon_norm ();
//ddump "After normalizing left-hand side";
trefl ();
//ddump "Before normalizing right-hand side";
canon_norm ();
//ddump "After normalizing right-hand side";
trefl ()
| _ -> fail "Unexpected"
end
else fail "Found equality, but terms do not have the expected type"
end
| _ -> fail "Goal should be an equality")
let canon_semiring (#a:eqtype) (r:cr a) : Tac unit =
canon_semiring_aux a
(quote a) (unquote #a) (fun (x:a) -> quote x) (quote r)
(norm_term steps (quote r.cm_add.mult))
(norm_term steps (quote r.opp))
(norm_term steps (quote (r.opp r.cm_mult.unit)))
(norm_term steps (quote r.cm_mult.mult))
r.cm_add.unit
/// Ring of integers
[@@canon_attr] | false | true | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val int_cr:cr int | [] | FStar.Tactics.CanonCommSemiring.int_cr | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | FStar.Tactics.CanonCommSemiring.cr Prims.int | {
"end_col": 87,
"end_line": 1688,
"start_col": 2,
"start_line": 1688
} |
FStar.Pervasives.Lemma | val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s)) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> () | val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s = | false | null | true | let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc ( == ) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
( == ) { () }
interp_cs r vm (monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
( == ) { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
( == ) { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
( == ) { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
( == ) { r.cm_mult.associativity c (interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
( == ) { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
( == ) { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
( == ) { r.distribute (interp_vl r vm l0) (amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0) (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
( == ) { () }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t ->
let c = aone in
calc ( == ) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
( == ) { () }
interp_cs r vm (monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
( == ) { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
( == ) { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
( == ) { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
( == ) { r.cm_mult.associativity c (interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
( == ) { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
( == ) { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
( == ) { r.distribute (interp_vl r vm l0) (amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0) (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
( == ) { () }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> () | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Tactics.CanonCommSemiring.canonical_sum",
"FStar.Calc.calc_finish",
"Prims.eq2",
"FStar.Tactics.CanonCommSemiring.interp_cs",
"FStar.Tactics.CanonCommSemiring.canonical_sum_scalar2",
"FStar.Tactics.CanonCommSemiring.interp_vl",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"FStar.Tactics.CanonCommSemiring.varlist_merge",
"FStar.Tactics.CanonCommSemiring.monom_insert",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"FStar.Tactics.CanonCommSemiring.monom_insert_ok",
"FStar.Tactics.CanonCommSemiring.varlist_merge_ok",
"FStar.Tactics.CanonCommSemiring.canonical_sum_scalar2_ok",
"FStar.Algebra.CommMonoid.__proj__CM__item__associativity",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"FStar.Algebra.CommMonoid.__proj__CM__item__commutativity",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__distribute",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) == | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s)) | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.canonical_sum_scalar2_ok | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
l0: FStar.Tactics.CanonCommSemiring.varlist ->
s: FStar.Tactics.CanonCommSemiring.canonical_sum a
-> FStar.Pervasives.Lemma
(ensures
FStar.Tactics.CanonCommSemiring.interp_cs r
vm
(FStar.Tactics.CanonCommSemiring.canonical_sum_scalar2 r l0 s) ==
CM?.mult (CR?.cm_mult r)
(FStar.Tactics.CanonCommSemiring.interp_vl r vm l0)
(FStar.Tactics.CanonCommSemiring.interp_cs r vm s)) | {
"end_col": 19,
"end_line": 1175,
"start_col": 47,
"start_line": 1105
} |
Prims.Tot | val semiring_reflect:
#a: eqtype ->
r: cr a ->
vm: vmap a ->
e1: polynomial a ->
e2: polynomial a ->
a1: a ->
a2: a ->
squash (interp_cs r vm (polynomial_simplify r e1) == interp_cs r vm (polynomial_simplify r e2)) ->
squash (a1 == interp_p r vm e1) ->
squash (a2 == interp_p r vm e2)
-> squash (a1 == a2) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let semiring_reflect (#a:eqtype) (r:cr a) (vm:vmap a) (e1 e2:polynomial a) (a1 a2:a)
(_ : squash (
interp_cs r vm (polynomial_simplify r e1) ==
interp_cs r vm (polynomial_simplify r e2)))
(_ : squash (a1 == interp_p r vm e1))
(_ : squash (a2 == interp_p r vm e2)) :
squash (a1 == a2)
=
polynomial_simplify_ok r vm e1;
polynomial_simplify_ok r vm e2 | val semiring_reflect:
#a: eqtype ->
r: cr a ->
vm: vmap a ->
e1: polynomial a ->
e2: polynomial a ->
a1: a ->
a2: a ->
squash (interp_cs r vm (polynomial_simplify r e1) == interp_cs r vm (polynomial_simplify r e2)) ->
squash (a1 == interp_p r vm e1) ->
squash (a2 == interp_p r vm e2)
-> squash (a1 == a2)
let semiring_reflect
(#a: eqtype)
(r: cr a)
(vm: vmap a)
(e1: polynomial a)
(e2: polynomial a)
(a1: a)
(a2: a)
(_:
squash (interp_cs r vm (polynomial_simplify r e1) ==
interp_cs r vm (polynomial_simplify r e2)))
(_: squash (a1 == interp_p r vm e1))
(_: squash (a2 == interp_p r vm e2))
: squash (a1 == a2) = | false | null | true | polynomial_simplify_ok r vm e1;
polynomial_simplify_ok r vm e2 | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"total"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.polynomial",
"Prims.squash",
"Prims.eq2",
"FStar.Tactics.CanonCommSemiring.interp_cs",
"FStar.Tactics.CanonCommSemiring.polynomial_simplify",
"FStar.Tactics.CanonCommSemiring.interp_p",
"FStar.Tactics.CanonCommSemiring.polynomial_simplify_ok",
"Prims.unit"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_prod_ok: #a:eqtype -> r:cr a -> vm:vmap a ->
s1:canonical_sum a -> s2:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_prod r s1 s2) ==
r.cm_mult.mult (interp_cs r vm s1) (interp_cs r vm s2))
let rec canonical_sum_prod_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar3 r c1 l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar3_ok r vm c1 l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_prod r s1 s2);
== { }
interp_cs r vm
(canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2));
== { canonical_sum_merge_ok r vm
(canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2) }
aplus (interp_cs r vm (canonical_sum_scalar2 r l1 s2))
(interp_cs r vm (canonical_sum_prod r t1 s2));
== { canonical_sum_scalar2_ok r vm l1 s2;
canonical_sum_prod_ok r vm t1 s2 }
aplus (amult (interp_vl r vm l1) (interp_cs r vm s2))
(amult (interp_cs r vm t1) (interp_cs r vm s2));
== { distribute_right r (interp_vl r vm l1)
(interp_cs r vm t1) (interp_cs r vm s2) }
amult (aplus (interp_vl r vm l1) (interp_cs r vm t1))
(interp_cs r vm s2);
== { }
amult (interp_cs r vm s1) (interp_cs r vm s2);
}
| Nil_monom -> ()
val spolynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_normalize r p) == interp_sp r vm p)
let rec spolynomial_normalize_ok #a r vm p =
match p with
| SPvar _ -> ()
| SPconst _ -> ()
| SPplus l q ->
canonical_sum_merge_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
| SPmult l q ->
canonical_sum_prod_ok r vm
(spolynomial_normalize r l) (spolynomial_normalize r q);
spolynomial_normalize_ok r vm l;
spolynomial_normalize_ok r vm q
val canonical_sum_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> s:canonical_sum a ->
Lemma (interp_cs r vm (canonical_sum_simplify r s) == interp_cs r vm s)
let rec canonical_sum_simplify_ok #a r vm s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
match s with
| Cons_monom c _ t -> canonical_sum_simplify_ok r vm t
| Cons_varlist _ t -> canonical_sum_simplify_ok r vm t
| Nil_monom -> ()
val spolynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:spolynomial a ->
Lemma (interp_cs r vm (spolynomial_simplify r p) == interp_sp r vm p)
let spolynomial_simplify_ok #a r vm p =
canonical_sum_simplify_ok r vm (spolynomial_normalize r p);
spolynomial_normalize_ok r vm p
(**
* This is the type where we first reflect expressions,
* before eliminating additive inverses
**)
type polynomial a =
| Pvar : index -> polynomial a
| Pconst : a -> polynomial a
| Pplus : polynomial a -> polynomial a -> polynomial a
| Pmult : polynomial a -> polynomial a -> polynomial a
| Popp : polynomial a -> polynomial a
(** Canonize a reflected expression *)
val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let rec polynomial_normalize #a r p =
match p with
| Pvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| Pconst c -> Cons_monom c Nil_var Nil_monom
| Pplus l q ->
canonical_sum_merge r (polynomial_normalize r l) (polynomial_normalize r q)
| Pmult l q ->
canonical_sum_prod r (polynomial_normalize r l) (polynomial_normalize r q)
| Popp p ->
canonical_sum_scalar3 r (r.opp r.cm_mult.unit) Nil_var (polynomial_normalize r p)
val polynomial_simplify: #a:eqtype -> cr a -> polynomial a -> canonical_sum a
[@@canon_attr]
let polynomial_simplify #a r p =
canonical_sum_simplify r
(polynomial_normalize r p)
(** Translate to a representation without additive inverses *)
val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a
[@@canon_attr]
let rec spolynomial_of #a r p =
match p with
| Pvar i -> SPvar i
| Pconst c -> SPconst c
| Pplus l q -> SPplus (spolynomial_of r l) (spolynomial_of r q)
| Pmult l q -> SPmult (spolynomial_of r l) (spolynomial_of r q)
| Popp p -> SPmult (SPconst (r.opp r.cm_mult.unit)) (spolynomial_of r p)
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_p (#a:Type) (r:cr a) (vm:vmap a) (p:polynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| Pconst c -> c
| Pvar i -> interp_var vm i
| Pplus p1 p2 -> aplus (interp_p r vm p1) (interp_p r vm p2)
| Pmult p1 p2 -> amult (interp_p r vm p1) (interp_p r vm p2)
| Popp p -> r.opp (interp_p r vm p)
val spolynomial_of_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_p r vm p == interp_sp r vm (spolynomial_of r p))
let rec spolynomial_of_ok #a r vm p =
match p with
| Pconst c -> ()
| Pvar i -> ()
| Pplus p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Pmult p1 p2 ->
spolynomial_of_ok r vm p1;
spolynomial_of_ok r vm p2
| Popp p ->
spolynomial_of_ok r vm p;
let x = interp_sp r vm (spolynomial_of r p) in
let y = r.cm_mult.mult (r.opp r.cm_mult.unit) x in
add_mult_opp r x;
opp_unique r x y
val polynomial_normalize_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_normalize r p) ==
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p)))
let rec polynomial_normalize_ok #a r vm p =
match p with
| Pvar _ -> ()
| Pconst _ -> ()
| Pplus l q ->
canonical_sum_merge_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_merge_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Pmult l q ->
canonical_sum_prod_ok r vm
(polynomial_normalize r l)
(polynomial_normalize r q);
canonical_sum_prod_ok r vm
(spolynomial_normalize r (spolynomial_of r l))
(spolynomial_normalize r (spolynomial_of r q));
polynomial_normalize_ok r vm l;
polynomial_normalize_ok r vm q
| Popp p1 ->
let l = SPconst (r.opp r.cm_mult.unit) in
polynomial_normalize_ok r vm p1;
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(polynomial_normalize r p1);
canonical_sum_prod_ok r vm
(spolynomial_normalize r l)
(spolynomial_normalize r (spolynomial_of r p1))
val polynomial_simplify_ok: #a:eqtype -> r:cr a -> vm:vmap a -> p:polynomial a ->
Lemma (interp_cs r vm (polynomial_simplify r p) == interp_p r vm p)
let polynomial_simplify_ok #a r vm p =
calc (==) {
interp_cs r vm (polynomial_simplify r p);
== { }
interp_cs r vm (canonical_sum_simplify r (polynomial_normalize r p));
== { canonical_sum_simplify_ok r vm (polynomial_normalize r p) }
interp_cs r vm (polynomial_normalize r p);
== { polynomial_normalize_ok r vm p }
interp_cs r vm (spolynomial_normalize r (spolynomial_of r p));
== { spolynomial_normalize_ok r vm (spolynomial_of r p) }
interp_sp r vm (spolynomial_of r p);
== { spolynomial_of_ok r vm p }
interp_p r vm p;
}
///
/// Tactic definition
///
(* Only dump when debugging is on *)
let ddump m = if debugging () then dump m
(**
* Finds the position of first occurrence of x in xs.
* This is specialized to terms and their funny term_eq.
**)
let rec find_aux (n:nat) (x:term) (xs:list term) : Tac (option nat) =
match xs with
| [] -> None
| x'::xs' -> if term_eq x x' then Some n else find_aux (n+1) x xs'
let find = find_aux 0
let make_fvar (#a:Type) (t:term) (unquotea:term -> Tac a) (ts:list term)
(vm:vmap a) : Tac (polynomial a * list term * vmap a) =
match find t ts with
| Some v -> (Pvar v, ts, vm)
| None ->
let vfresh = length ts in
let z = unquotea t in
(Pvar vfresh, ts @ [t], update vfresh z vm)
(** This expects that add, opp, mone mult, and t have already been normalized *)
let rec reification_aux (#a:Type) (unquotea:term -> Tac a) (ts:list term) (vm:vmap a) (add opp mone mult t: term) : Tac (polynomial a * list term * vmap a) =
// ddump ("term = " ^ term_to_string t ^ "\n");
let hd, tl = collect_app_ref t in
match inspect hd, list_unref tl with
| Tv_FVar fv, [(t1, _) ; (t2, _)] ->
//ddump ("add = " ^ term_to_string add ^ "
// \nmul = " ^ term_to_string mult);
//ddump ("fv = " ^ term_to_string (pack (Tv_FVar fv)));
let binop (op:polynomial a -> polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e1, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
let (e2, ts, vm) = reification_aux unquotea ts vm add opp mone mult t2 in
(op e1 e2, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) add then binop Pplus else
if term_eq (pack (Tv_FVar fv)) mult then binop Pmult else
make_fvar t unquotea ts vm
| Tv_FVar fv, [(t1, _)] ->
let monop (op:polynomial a -> polynomial a) : Tac (polynomial a * list term * vmap a) =
let (e, ts, vm) = reification_aux unquotea ts vm add opp mone mult t1 in
(op e, ts, vm)
in
if term_eq (pack (Tv_FVar fv)) opp then monop Popp else
make_fvar t unquotea ts vm
| Tv_Const _, [] -> Pconst (unquotea t), ts, vm
| _, _ -> make_fvar t unquotea ts vm
(**
* How to normalize terms in the tactic.
* This is carefully tuned to unfold all and no more than required
**)
let steps =
[
primops;
iota;
zeta;
delta_attr [`%canon_attr];
delta_only [
`%FStar.Mul.op_Star; // For integer ring
`%FStar.Algebra.CommMonoid.int_plus_cm; // For integer ring
`%FStar.Algebra.CommMonoid.int_multiply_cm; // For integer ring
`%FStar.Algebra.CommMonoid.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.__proj__CM__item__unit;
`%__proj__CR__item__cm_add;
`%__proj__CR__item__opp;
`%__proj__CR__item__cm_mult;
`%FStar.List.Tot.assoc;
`%FStar.Pervasives.Native.fst;
`%FStar.Pervasives.Native.snd;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___1;
`%FStar.Pervasives.Native.__proj__Mktuple2__item___2;
`%FStar.List.Tot.op_At;
`%FStar.List.Tot.append;
]
]
let canon_norm () : Tac unit = norm steps
let reification (#a:Type)
(unquotea:term -> Tac a) (quotea:a -> Tac term) (tadd topp tmone tmult:term) (munit:a) (ts:list term) : Tac (list (polynomial a) * vmap a) =
// Be careful not to normalize operations too much
// E.g. we don't want to turn ( +% ) into (a + b) % prime
// or we won't be able to spot ring operations
let add = tadd in
let opp = topp in
let mone = tmone in
let mult = tmult in
let ts = Tactics.Util.map (norm_term steps) ts in
//ddump ("add = " ^ term_to_string add ^ "\nmult = " ^ term_to_string mult);
let (es, _, vm) =
Tactics.Util.fold_left
(fun (es, vs, vm) t ->
let (e, vs, vm) = reification_aux unquotea vs vm add opp mone mult t
in (e::es, vs, vm))
([],[], ([], munit)) ts
in (List.Tot.Base.rev es, vm)
(* The implicit argument in the application of `Pconst` is crucial *)
let rec quote_polynomial (#a:Type) (ta:term) (quotea:a -> Tac term) (e:polynomial a) : Tac term =
match e with
| Pconst c -> mk_app (`Pconst) [(ta, Q_Implicit); (quotea c, Q_Explicit)]
| Pvar x -> mk_e_app (`Pvar) [pack (Tv_Const (C_Int x))]
| Pplus e1 e2 ->
mk_e_app (`Pplus) [quote_polynomial ta quotea e1; quote_polynomial ta quotea e2]
| Pmult e1 e2 ->
mk_e_app (`Pmult) [quote_polynomial ta quotea e1; quote_polynomial ta quotea e2]
| Popp e -> mk_e_app (`Popp) [quote_polynomial ta quotea e]
(* Constructs the 3 main goals of the tactic *)
let semiring_reflect (#a:eqtype) (r:cr a) (vm:vmap a) (e1 e2:polynomial a) (a1 a2:a)
(_ : squash (
interp_cs r vm (polynomial_simplify r e1) ==
interp_cs r vm (polynomial_simplify r e2)))
(_ : squash (a1 == interp_p r vm e1))
(_ : squash (a2 == interp_p r vm e2)) :
squash (a1 == a2) | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val semiring_reflect:
#a: eqtype ->
r: cr a ->
vm: vmap a ->
e1: polynomial a ->
e2: polynomial a ->
a1: a ->
a2: a ->
squash (interp_cs r vm (polynomial_simplify r e1) == interp_cs r vm (polynomial_simplify r e2)) ->
squash (a1 == interp_p r vm e1) ->
squash (a2 == interp_p r vm e2)
-> squash (a1 == a2) | [] | FStar.Tactics.CanonCommSemiring.semiring_reflect | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
e1: FStar.Tactics.CanonCommSemiring.polynomial a ->
e2: FStar.Tactics.CanonCommSemiring.polynomial a ->
a1: a ->
a2: a ->
_:
Prims.squash (FStar.Tactics.CanonCommSemiring.interp_cs r
vm
(FStar.Tactics.CanonCommSemiring.polynomial_simplify r e1) ==
FStar.Tactics.CanonCommSemiring.interp_cs r
vm
(FStar.Tactics.CanonCommSemiring.polynomial_simplify r e2)) ->
_: Prims.squash (a1 == FStar.Tactics.CanonCommSemiring.interp_p r vm e1) ->
_: Prims.squash (a2 == FStar.Tactics.CanonCommSemiring.interp_p r vm e2)
-> Prims.squash (a1 == a2) | {
"end_col": 32,
"end_line": 1618,
"start_col": 2,
"start_line": 1617
} |
FStar.Pervasives.Lemma | val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2)) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> () | val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 = | false | null | true | let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc ( == ) {
interp_cs r vm (monom_insert r c1 l1 s2);
( == ) { () }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
( == ) { () }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
( == ) { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
( == ) { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
( == ) { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
( == ) { r.cm_add.associativity (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
( == ) { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2
then ()
else
calc ( == ) {
interp_cs r vm (monom_insert r c1 l1 s2);
( == ) { () }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
( == ) { () }
aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm (monom_insert r c1 l1 t2));
( == ) { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t2));
( == ) { r.cm_add.commutativity (amult c1 (interp_vl r vm l1)) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (amult c1 (interp_vl r vm l1)));
( == ) { r.cm_add.associativity (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
( == ) { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
( == ) { r.cm_add.commutativity (interp_cs r vm s2) (amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 ->
let c2 = aone in
if l1 = l2
then
calc ( == ) {
interp_cs r vm (monom_insert r c1 l1 s2);
( == ) { () }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
( == ) { () }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
( == ) { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
( == ) { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
( == ) { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
( == ) { r.cm_add.associativity (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
( == ) { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2
then ()
else
calc ( == ) {
interp_cs r vm (monom_insert r c1 l1 s2);
( == ) { () }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
( == ) { () }
aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm (monom_insert r c1 l1 t2));
( == ) { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t2));
( == ) { r.cm_add.commutativity (amult c1 (interp_vl r vm l1)) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (amult c1 (interp_vl r vm l1)));
( == ) { r.cm_add.associativity (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
( == ) { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
( == ) { r.cm_add.commutativity (interp_cs r vm s2) (amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> () | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Tactics.CanonCommSemiring.canonical_sum",
"Prims.op_Equality",
"FStar.Calc.calc_finish",
"Prims.eq2",
"FStar.Tactics.CanonCommSemiring.interp_cs",
"FStar.Tactics.CanonCommSemiring.monom_insert",
"FStar.Tactics.CanonCommSemiring.interp_vl",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"FStar.Tactics.CanonCommSemiring.interp_m",
"FStar.Tactics.CanonCommSemiring.ics_aux",
"FStar.Tactics.CanonCommSemiring.Cons_monom",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"FStar.Tactics.CanonCommSemiring.ics_aux_ok",
"FStar.Tactics.CanonCommSemiring.interp_m_ok",
"FStar.Tactics.CanonCommSemiring.distribute_right",
"FStar.Algebra.CommMonoid.__proj__CM__item__associativity",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add",
"Prims.bool",
"FStar.Tactics.CanonCommSemiring.varlist_lt",
"FStar.Tactics.CanonCommSemiring.monom_insert_ok",
"FStar.Algebra.CommMonoid.__proj__CM__item__commutativity",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) == | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2)) | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.monom_insert_ok | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
c1: a ->
l1: FStar.Tactics.CanonCommSemiring.varlist ->
s2: FStar.Tactics.CanonCommSemiring.canonical_sum a
-> FStar.Pervasives.Lemma
(ensures
FStar.Tactics.CanonCommSemiring.interp_cs r
vm
(FStar.Tactics.CanonCommSemiring.monom_insert r c1 l1 s2) ==
CM?.mult (CR?.cm_add r)
(CM?.mult (CR?.cm_mult r) c1 (FStar.Tactics.CanonCommSemiring.interp_vl r vm l1))
(FStar.Tactics.CanonCommSemiring.interp_cs r vm s2)) | {
"end_col": 19,
"end_line": 1037,
"start_col": 42,
"start_line": 909
} |
FStar.Pervasives.Lemma | val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s)) | [
{
"abbrev": false,
"full_module": "FStar.Algebra.CommMonoid",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec canonical_sum_scalar3_ok #a r vm c0 l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
== { }
interp_cs r vm
(monom_insert r (amult c0 c) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t));
== { monom_insert_ok r vm (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
== { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 c)
(interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
== { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0))
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> () | val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s))
let rec canonical_sum_scalar3_ok #a r vm c0 l0 s = | false | null | true | let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc ( == ) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
( == ) { () }
interp_cs r
vm
(monom_insert r (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t));
( == ) { monom_insert_ok r
vm
(amult c0 c)
(varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
( == ) { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
( == ) { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
( == ) { r.cm_mult.associativity (amult c0 c) (interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
( == ) { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
( == ) { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
( == ) { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
( == ) { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
( == ) { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0)) (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
( == ) { () }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Cons_varlist l t ->
let c = aone in
calc ( == ) {
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s);
( == ) { () }
interp_cs r
vm
(monom_insert r (amult c0 c) (varlist_merge l0 l) (canonical_sum_scalar3 r c0 l0 t));
( == ) { monom_insert_ok r
vm
(amult c0 c)
(varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t) }
aplus (amult (amult c0 c) (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
( == ) { varlist_merge_ok r vm l0 l }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar3 r c0 l0 t));
( == ) { canonical_sum_scalar3_ok r vm c0 l0 t }
aplus (amult (amult c0 c) (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
( == ) { r.cm_mult.associativity (amult c0 c) (interp_vl r vm l0) (interp_vl r vm l) }
aplus (amult (amult (amult c0 c) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
( == ) { r.cm_mult.commutativity c0 c }
aplus (amult (amult (amult c c0) (interp_vl r vm l0)) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
( == ) { r.cm_mult.associativity c c0 (interp_vl r vm l0) }
aplus (amult (amult c (amult c0 (interp_vl r vm l0))) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
( == ) { r.cm_mult.commutativity c (amult c0 (interp_vl r vm l0)) }
aplus (amult (amult (amult c0 (interp_vl r vm l0)) c) (interp_vl r vm l))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
( == ) { r.cm_mult.associativity (amult c0 (interp_vl r vm l0)) c (interp_vl r vm l) }
aplus (amult (amult c0 (interp_vl r vm l0)) (amult c (interp_vl r vm l)))
(amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm t));
( == ) { r.distribute (amult c0 (interp_vl r vm l0))
(amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult (amult c0 (interp_vl r vm l0)) (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
( == ) { () }
amult (amult c0 (interp_vl r vm l0)) (interp_cs r vm s);
}
| Nil_monom -> () | {
"checked_file": "FStar.Tactics.CanonCommSemiring.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Util.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.fst.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked",
"FStar.Algebra.CommMonoid.fst.checked"
],
"interface_file": false,
"source_file": "FStar.Tactics.CanonCommSemiring.fst"
} | [
"lemma"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Tactics.CanonCommSemiring.canonical_sum",
"FStar.Calc.calc_finish",
"Prims.eq2",
"FStar.Tactics.CanonCommSemiring.interp_cs",
"FStar.Tactics.CanonCommSemiring.canonical_sum_scalar3",
"FStar.Tactics.CanonCommSemiring.interp_vl",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"FStar.Tactics.CanonCommSemiring.varlist_merge",
"FStar.Tactics.CanonCommSemiring.monom_insert",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"FStar.Tactics.CanonCommSemiring.monom_insert_ok",
"FStar.Tactics.CanonCommSemiring.varlist_merge_ok",
"FStar.Tactics.CanonCommSemiring.canonical_sum_scalar3_ok",
"FStar.Algebra.CommMonoid.__proj__CM__item__associativity",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"FStar.Algebra.CommMonoid.__proj__CM__item__commutativity",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__distribute",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.Tactics.CanonCommSemiring
/// A tactic to solve equalities on a commutative semiring (a, +, *, 0, 1)
///
/// The tactic [canon_semiring] is parameterized by the base type [a] and
/// a semiring theory [cr a]. This requires:
///
/// - A commutative monoid (a, +, 0) for addition
/// That is, + is associative, commutative and has identity element 0
/// - An additive inverse operator for (a, +, 0), making it an Abelian group
/// That is, a + -a = 0
/// - A commutative monoid (a, *, 1) for multiplication
/// That is, * is associative, commutative and has identity element 1
/// - Multiplication left-distributes over addition
/// That is, a * (b + c) == a * b + a * c
/// - 0 is an absorbing element of multiplication
/// That is, 0 * a = 0
///
/// In contrast to the previous version of FStar.Tactics.CanonCommSemiring,
/// the tactic defined here canonizes products, additions and additive inverses,
/// collects coefficients in monomials, and eliminates trivial expressions.
///
/// This is based on the legacy (second) version of Coq's ring tactic:
/// - https://github.com/coq-contribs/legacy-ring/
///
/// See also the newest ring tactic in Coq, which is even more general
/// and efficient:
/// - https://coq.inria.fr/refman/addendum/ring.html
/// - http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf
open FStar.List
open FStar.Reflection.V2
open FStar.Tactics.V2
open FStar.Algebra.CommMonoid
let term_eq = FStar.Tactics.term_eq_old
(** An attribute for marking definitions to unfold by the tactic *)
irreducible let canon_attr = ()
///
/// Commutative semiring theory
///
let distribute_left_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma (x * (y + z) == x * y + x * z)
let distribute_right_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
let ( + ) = cm_add.mult in
let ( * ) = cm_mult.mult in
x:a -> y:a -> z:a -> Lemma ((x + y) * z == x * z + y * z)
let mult_zero_l_lemma (a:Type) (cm_add:cm a) (cm_mult:cm a) =
x:a -> Lemma (cm_mult.mult cm_add.unit x == cm_add.unit)
let add_opp_r_lemma (a:Type) (cm_add:cm a) (opp:(a -> a)) =
let ( + ) = cm_add.mult in
x:a -> Lemma (x + opp x == cm_add.unit)
[@@canon_attr]
unopteq
type cr (a:Type) =
| CR :
cm_add: cm a ->
cm_mult: cm a ->
opp: (a -> a) ->
add_opp: add_opp_r_lemma a cm_add opp ->
distribute: distribute_left_lemma a cm_add cm_mult ->
mult_zero_l: mult_zero_l_lemma a cm_add cm_mult ->
cr a
let distribute_right (#a:Type) (r:cr a) : distribute_right_lemma a r.cm_add r.cm_mult =
fun x y z ->
r.cm_mult.commutativity (r.cm_add.mult x y) z;
r.distribute z x y;
r.cm_mult.commutativity x z;
r.cm_mult.commutativity y z
///
/// Syntax of canonical ring expressions
///
(**
* Marking expressions we would like to normalize fully.
* This does not do anything at the moment, but it would be nice
* to have a cheap mechanism to make this work without traversing the
* whole goal.
**)
[@@canon_attr]
unfold let norm_fully (#a:Type) (x:a) = x
let index: eqtype = nat
(*
* A list of variables represents a sorted product of one or more variables.
* We do not need to prove sortedness to prove correctness, so we never
* make it explicit.
*)
type varlist =
| Nil_var : varlist
| Cons_var : index -> varlist -> varlist
(*
* A canonical expression represents an ordered sum of monomials.
* Each monomial is either:
* - a varlist (a product of variables): x1 * ... * xk
* - a product of a scalar and a varlist: c * x1 * ... * xk
*
* The order on monomials is the lexicographic order on varlist, with the
* additional convention that monomials with a scalar are less than monomials
* without a scalar.
*)
type canonical_sum a =
| Nil_monom : canonical_sum a
| Cons_monom : a -> varlist -> canonical_sum a -> canonical_sum a
| Cons_varlist : varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec varlist_lt (x y:varlist) : bool =
match x, y with
| Nil_var, Cons_var _ _ -> true
| Cons_var i xs, Cons_var j ys ->
if i < j then true else i = j && varlist_lt xs ys
| _, _ -> false
[@@canon_attr]
val varlist_merge: l1:varlist -> l2:varlist -> Tot varlist (decreases %[l1; l2; 0])
[@@canon_attr]
val vm_aux: index -> t1:varlist -> l2:varlist -> Tot varlist (decreases %[t1; l2; 1])
(* Merges two lists of variables, preserving sortedness *)
[@@canon_attr]
let rec varlist_merge l1 l2 =
match l1, l2 with
| _, Nil_var -> l1
| Nil_var, _ -> l2
| Cons_var v1 t1, Cons_var v2 t2 -> vm_aux v1 t1 l2
and vm_aux v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then Cons_var v1 (varlist_merge t1 l2)
else Cons_var v2 (vm_aux v1 t1 t2)
| _ -> Cons_var v1 t1
(*
* Merges two canonical expressions
*
* We require that [a] is eqtype for better reasons later.
* Here it is convenient to fix the universe of [a] in
* mutually recursive functions.
*)
[@@canon_attr]
val canonical_sum_merge : #a:eqtype -> cr a
-> s1:canonical_sum a -> s2:canonical_sum a
-> Tot (canonical_sum a) (decreases %[s1; s2; 0])
[@@canon_attr]
val csm_aux: #a:eqtype -> r:cr a -> c1:a -> l1:varlist -> t1:canonical_sum a
-> s2:canonical_sum a -> Tot (canonical_sum a) (decreases %[t1; s2; 1])
[@@canon_attr]
let rec canonical_sum_merge #a r s1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux r c1 l1 t1 s2
| Cons_varlist l1 t1 -> csm_aux r aone l1 t1 s2
| Nil_monom -> s2
and csm_aux #a r c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_monom c2 l2 (csm_aux #a r c1 l1 t1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 (canonical_sum_merge r t1 t2)
else
if varlist_lt l1 l2
then Cons_monom c1 l1 (canonical_sum_merge r t1 s2)
else Cons_varlist l2 (csm_aux r c1 l1 t1 t2)
| Nil_monom ->
//if c1 = aone then Cons_varlist l1 t1 else
Cons_monom c1 l1 t1
(* Inserts a monomial into the appropriate position in a canonical sum *)
val monom_insert: #a:eqtype -> r:cr a
-> c1:a -> l1:varlist -> s2:canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec monom_insert #a r c1 l1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 c2)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_monom c2 l2 (monom_insert r c1 l1 t2)
| Cons_varlist l2 t2 ->
if l1 = l2
then Cons_monom (norm_fully (aplus c1 aone)) l1 t2
else
if varlist_lt l1 l2
then Cons_monom c1 l1 s2
else Cons_varlist l2 (monom_insert r c1 l1 t2)
| Nil_monom ->
if c1 = aone
then Cons_varlist l1 Nil_monom
else Cons_monom c1 l1 Nil_monom
(* Inserts a monomial without scalar into a canonical sum *)
val varlist_insert: #a:eqtype -> cr a -> varlist -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let varlist_insert #a r l1 s2 =
let aone = r.cm_mult.unit in
monom_insert r aone l1 s2
(* Multiplies a sum by a scalar c0 *)
val canonical_sum_scalar: #a:Type -> cr a -> a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar #a r c0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t -> Cons_monom (norm_fully (amult c0 c)) l (canonical_sum_scalar r c0 t)
| Cons_varlist l t -> Cons_monom c0 l (canonical_sum_scalar r c0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial without scalar *)
val canonical_sum_scalar2: #a:eqtype -> cr a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar2 #a r l0 s =
match s with
| Cons_monom c l t ->
monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Cons_varlist l t ->
varlist_insert r (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t)
| Nil_monom -> Nil_monom
(* Multiplies a sum by a monomial with scalar *)
val canonical_sum_scalar3: #a:eqtype -> cr a -> a -> varlist
-> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_scalar3 #a r c0 l0 s =
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
monom_insert r (norm_fully (amult c0 c)) (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Cons_varlist l t ->
monom_insert r c0 (varlist_merge l0 l)
(canonical_sum_scalar3 r c0 l0 t)
| Nil_monom -> s
(* Multiplies two canonical sums *)
val canonical_sum_prod: #a:eqtype -> cr a
-> canonical_sum a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_prod #a r s1 s2 =
match s1 with
| Cons_monom c1 l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar3 r c1 l1 s2)
(canonical_sum_prod r t1 s2)
| Cons_varlist l1 t1 ->
canonical_sum_merge r (canonical_sum_scalar2 r l1 s2)
(canonical_sum_prod r t1 s2)
| Nil_monom -> s1
///
/// Syntax of concrete semiring polynomials
///
(* This is the type where we reflect expressions before normalization *)
type spolynomial a =
| SPvar : index -> spolynomial a
| SPconst : a -> spolynomial a
| SPplus : spolynomial a -> spolynomial a -> spolynomial a
| SPmult : spolynomial a -> spolynomial a -> spolynomial a
(** Canonize a reflected expression *)
val spolynomial_normalize: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let rec spolynomial_normalize #a r p =
match p with
| SPvar i -> Cons_varlist (Cons_var i Nil_var) Nil_monom
| SPconst c -> Cons_monom c Nil_var Nil_monom
| SPplus l q ->
canonical_sum_merge r (spolynomial_normalize r l) (spolynomial_normalize r q)
| SPmult l q ->
canonical_sum_prod r (spolynomial_normalize r l) (spolynomial_normalize r q)
(**
* Simplify a canonical sum.
* Removes 0 * x1 * ... * xk and turns 1 * x1 * ... * xk into x1 * ... * xk
**)
val canonical_sum_simplify: #a:eqtype -> cr a -> canonical_sum a -> canonical_sum a
[@@canon_attr]
let rec canonical_sum_simplify #a r s =
let azero = r.cm_add.unit in
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
match s with
| Cons_monom c l t ->
if norm_fully (c = azero) then canonical_sum_simplify r t
else
if norm_fully (c = aone)
then Cons_varlist l (canonical_sum_simplify r t)
else Cons_monom c l (canonical_sum_simplify r t)
| Cons_varlist l t -> Cons_varlist l (canonical_sum_simplify r t)
| Nil_monom -> s
(**
* The main canonization algorithm: turn an expression into a sum and
* simplify it.
**)
val spolynomial_simplify: #a:eqtype -> cr a -> spolynomial a -> canonical_sum a
[@@canon_attr]
let spolynomial_simplify #a r p =
canonical_sum_simplify r
(spolynomial_normalize r p)
///
/// Interpretation of varlists, monomials and canonical sums
///
(**
* The variable map:
* This maps polynomial variables to ring expressions. That is, any term
* that is not an addition or a multiplication is turned into a variable
*
* The representation is inefficient. For large terms it might be worthwhile
* using a better data structure.
**)
let vmap a = list (var * a) * a
(** Add a new entry in a variable map *)
let update (#a:Type) (x:var) (xa:a) (vm:vmap a) : vmap a =
let l, y = vm in (x, xa) :: l, y
(** Quotes a list *)
let rec quote_list (#a:Type) (ta:term) (quotea:a -> Tac term) (xs:list a) :
Tac term =
match xs with
| [] -> mk_app (`Nil) [(ta, Q_Implicit)]
| x::xs' -> mk_app (`Cons) [(ta, Q_Implicit);
(quotea x, Q_Explicit);
(quote_list ta quotea xs', Q_Explicit)]
(** Quotes a variable map *)
let quote_vm (#a:Type) (ta: term) (quotea:a -> Tac term) (vm:vmap a) : Tac term =
let quote_map_entry (p:(nat * a)) : Tac term =
mk_app (`Mktuple2) [(`nat, Q_Implicit); (ta, Q_Implicit);
(pack (Tv_Const (C_Int (fst p))), Q_Explicit);
(quotea (snd p), Q_Explicit)] in
let tyentry = mk_e_app (`tuple2) [(`nat); ta] in
let tlist = quote_list tyentry quote_map_entry (fst vm) in
let tylist = mk_e_app (`list) [tyentry] in
mk_app (`Mktuple2) [(tylist, Q_Implicit); (ta, Q_Implicit);
(tlist, Q_Explicit); (quotea (snd vm), Q_Explicit)]
(**
* A varlist is interpreted as the product of the entries in the variable map
*
* Unbound variables are mapped to the default value according to the map.
* This would normally never occur, but it makes it easy to prove correctness.
*)
[@@canon_attr]
let interp_var (#a:Type) (vm:vmap a) (i:index) =
match List.Tot.Base.assoc i (fst vm) with
| Some x -> x
| _ -> snd vm
[@@canon_attr]
private
let rec ivl_aux (#a:Type) (r:cr a) (vm:vmap a) (x:index) (t:varlist)
: Tot a (decreases t) =
let amult = r.cm_mult.mult in
match t with
| Nil_var -> interp_var vm x
| Cons_var x' t' -> amult (interp_var vm x) (ivl_aux r vm x' t')
[@@canon_attr]
let interp_vl (#a:Type) (r:cr a) (vm:vmap a) (l:varlist) =
let aone = r.cm_mult.unit in
match l with
| Nil_var -> aone
| Cons_var x t -> ivl_aux r vm x t
[@@canon_attr]
let interp_m (#a:Type) (r:cr a) (vm:vmap a) (c:a) (l:varlist) =
let amult = r.cm_mult.mult in
match l with
| Nil_var -> c
| Cons_var x t -> amult c (ivl_aux r vm x t)
[@@canon_attr]
let rec ics_aux (#a:Type) (r:cr a) (vm:vmap a) (x:a) (s:canonical_sum a)
: Tot a (decreases s) =
let aplus = r.cm_add.mult in
match s with
| Nil_monom -> x
| Cons_varlist l t -> aplus x (ics_aux r vm (interp_vl r vm l) t)
| Cons_monom c l t -> aplus x (ics_aux r vm (interp_m r vm c l) t)
(** Interpretation of a canonical sum *)
[@@canon_attr]
let interp_cs (#a:Type) (r:cr a) (vm:vmap a) (s:canonical_sum a) : a =
let azero = r.cm_add.unit in
match s with
| Nil_monom -> azero
| Cons_varlist l t -> ics_aux r vm (interp_vl r vm l) t
| Cons_monom c l t -> ics_aux r vm (interp_m r vm c l) t
(** Interpretation of a polynomial *)
[@@canon_attr]
let rec interp_sp (#a:Type) (r:cr a) (vm:vmap a) (p:spolynomial a) : a =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match p with
| SPconst c -> c
| SPvar i -> interp_var vm i
| SPplus p1 p2 -> aplus (interp_sp r vm p1) (interp_sp r vm p2)
| SPmult p1 p2 -> amult (interp_sp r vm p1) (interp_sp r vm p2)
///
/// Proof of correctness
///
val mult_one_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_mult.unit x == x)
[SMTPat (r.cm_mult.mult r.cm_mult.unit x)]
let mult_one_l #a r x =
r.cm_mult.identity x
val mult_one_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_mult.unit == x)
[SMTPat (r.cm_mult.mult x r.cm_mult.unit)]
let mult_one_r #a r x =
r.cm_mult.commutativity r.cm_mult.unit x
val mult_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult r.cm_add.unit x == r.cm_add.unit)
[SMTPat (r.cm_mult.mult r.cm_add.unit x)]
let mult_zero_l #a r x =
r.mult_zero_l x
val mult_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_mult.mult x r.cm_add.unit == r.cm_add.unit)
[SMTPat (r.cm_mult.mult x r.cm_add.unit)]
let mult_zero_r #a r x =
r.cm_mult.commutativity x r.cm_add.unit
val add_zero_l (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult r.cm_add.unit x == x)
[SMTPat (r.cm_add.mult r.cm_add.unit x)]
let add_zero_l #a r x =
r.cm_add.identity x
val add_zero_r (#a:Type) (r:cr a) (x:a) :
Lemma (r.cm_add.mult x r.cm_add.unit == x)
[SMTPat (r.cm_add.mult x r.cm_add.unit)]
let add_zero_r #a r x =
r.cm_add.commutativity r.cm_add.unit x
val opp_unique (#a:Type) (r:cr a) (x y:a) : Lemma
(requires r.cm_add.mult x y == r.cm_add.unit)
(ensures y == r.opp x)
let opp_unique #a r x y =
let ( + ) = r.cm_add.mult in
let zero = r.cm_add.unit in
calc (==) {
y;
== { r.add_opp x }
y + (x + r.opp x);
== { r.cm_add.associativity y x (r.opp x) }
(y + x) + r.opp x;
== { r.cm_add.commutativity x y }
zero + r.opp x;
== { }
r.opp x;
}
val add_mult_opp (#a:Type) (r:cr a) (x:a) : Lemma
(r.cm_add.mult x (r.cm_mult.mult (r.opp r.cm_mult.unit) x) == r.cm_add.unit)
let add_mult_opp #a r x =
let ( + ) = r.cm_add.mult in
let ( * ) = r.cm_mult.mult in
let zero = r.cm_add.unit in
let one = r.cm_mult.unit in
calc (==) {
x + r.opp one * x;
== { }
one * x + r.opp one * x;
== { distribute_right r one (r.opp one) x }
(one + r.opp one) * x;
== { r.add_opp one }
zero * x;
== { }
zero;
}
val ivl_aux_ok (#a:Type) (r:cr a) (vm:vmap a) (v:varlist) (i:index) : Lemma
(ivl_aux r vm i v == r.cm_mult.mult (interp_var vm i) (interp_vl r vm v))
let ivl_aux_ok #a r vm v i = ()
val vm_aux_ok (#a:eqtype) (r:cr a) (vm:vmap a) (v:index) (t l:varlist) :
Lemma
(ensures
interp_vl r vm (vm_aux v t l) ==
r.cm_mult.mult (interp_vl r vm (Cons_var v t)) (interp_vl r vm l))
(decreases %[t; l; 1])
val varlist_merge_ok (#a:eqtype) (r:cr a) (vm:vmap a) (x y:varlist) :
Lemma
(ensures
interp_vl r vm (varlist_merge x y) ==
r.cm_mult.mult (interp_vl r vm x) (interp_vl r vm y))
(decreases %[x; y; 0])
let rec varlist_merge_ok #a r vm x y =
let amult = r.cm_mult.mult in
match x, y with
| Cons_var v1 t1, Nil_var -> ()
| Cons_var v1 t1, Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 y;
assert (
interp_vl r vm (varlist_merge x y) ==
amult (interp_var vm v1) (amult (interp_vl r vm t1) (interp_vl r vm y)));
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm y)
end
else
vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> ()
and vm_aux_ok #a r vm v1 t1 l2 =
match l2 with
| Cons_var v2 t2 ->
if v1 < v2
then
begin
varlist_merge_ok r vm t1 l2;
r.cm_mult.associativity
(interp_var vm v1) (interp_vl r vm t1) (interp_vl r vm l2)
end
else
begin
vm_aux_ok r vm v1 t1 t2;
calc (==) {
interp_vl r vm (Cons_var v2 (vm_aux v1 t1 t2));
== { }
ivl_aux r vm v2 (vm_aux v1 t1 t2);
== { }
r.cm_mult.mult (interp_var vm v2) (interp_vl r vm (vm_aux v1 t1 t2));
== { }
r.cm_mult.mult (interp_var vm v2) (r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm t2) }
r.cm_mult.mult (interp_var vm v2)
(r.cm_mult.mult (interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) );
== { r.cm_mult.associativity
(interp_var vm v2)
(interp_vl r vm t2) (interp_vl r vm (Cons_var v1 t1)) }
r.cm_mult.mult
(r.cm_mult.mult (interp_var vm v2) (interp_vl r vm t2))
(interp_vl r vm (Cons_var v1 t1));
== { r.cm_mult.commutativity
(interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2)) }
r.cm_mult.mult (interp_vl r vm (Cons_var v1 t1)) (interp_vl r vm (Cons_var v2 t2));
}
end
| _ -> ()
val ics_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> s:canonical_sum a ->
Lemma (ensures ics_aux r vm x s == r.cm_add.mult x (interp_cs r vm s))
(decreases s)
let rec ics_aux_ok #a r vm x s =
match s with
| Nil_monom -> ()
| Cons_varlist l t ->
ics_aux_ok r vm (interp_vl r vm l) t
| Cons_monom c l t ->
ics_aux_ok r vm (interp_m r vm c l) t
val interp_m_ok: #a:eqtype -> r:cr a -> vm:vmap a -> x:a -> l:varlist ->
Lemma (interp_m r vm x l == r.cm_mult.mult x (interp_vl r vm l))
let interp_m_ok #a r vm x l = ()
val aplus_assoc_4: #a:Type -> r:cr a -> w:a -> x:a -> y:a -> z:a -> Lemma
(let aplus = r.cm_add.mult in
aplus (aplus w x) (aplus y z) == aplus (aplus w y) (aplus x z))
let aplus_assoc_4 #a r w x y z =
let aplus = r.cm_add.mult in
let assoc = r.cm_add.associativity in
let comm = r.cm_add.commutativity in
calc (==) {
aplus (aplus w x) (aplus y z);
== { assoc w x (aplus y z) }
aplus w (aplus x (aplus y z));
== { comm x (aplus y z) }
aplus w (aplus (aplus y z) x);
== { assoc w (aplus y z) x }
aplus (aplus w (aplus y z)) x;
== { assoc w y z }
aplus (aplus (aplus w y) z) x;
== { assoc (aplus w y) z x }
aplus (aplus w y) (aplus z x);
== { comm z x }
aplus (aplus w y) (aplus x z);
}
val canonical_sum_merge_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> s1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (canonical_sum_merge r s1 s2) ==
r.cm_add.mult (interp_cs r vm s1) (interp_cs r vm s2))
(decreases %[s1; s2; 0])
val csm_aux_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> t1:canonical_sum a -> s2:canonical_sum a ->
Lemma
(ensures
interp_cs r vm (csm_aux r c1 l1 t1 s2) ==
r.cm_add.mult (interp_cs r vm (Cons_monom c1 l1 t1)) (interp_cs r vm s2))
(decreases %[t1; s2; 1])
let rec canonical_sum_merge_ok #a r vm s1 s2 =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s1 with
| Cons_monom c1 l1 t1 -> csm_aux_ok #a r vm c1 l1 t1 s2
| Cons_varlist l1 t1 ->
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
interp_cs r vm (csm_aux r aone l1 t1 s2);
== { csm_aux_ok #a r vm aone l1 t1 s2 }
aplus (interp_cs r vm (Cons_monom aone l1 t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (interp_vl r vm l1) t1 }
aplus (interp_cs r vm (Cons_varlist l1 t1))
(interp_cs r vm s2);
}
| Nil_monom -> ()
and csm_aux_ok #a r vm c1 l1 t1 s2 =
let aplus = r.cm_add.mult in
let aone = r.cm_mult.unit in
let amult = r.cm_mult.mult in
match s2 with
| Nil_monom -> ()
| Cons_monom c2 l2 t2 ->
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2 then
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2);
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1)
(canonical_sum_merge r t1 t2) }
aplus (interp_m r vm (aplus c1 c2) l1)
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 t2));
== { canonical_sum_merge_ok r vm t1 t2 }
aplus (amult (aplus c1 c2) (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm t2));
== { distribute_right r c1 c2 (interp_vl r vm l1) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(aplus (interp_cs r vm t1)
(interp_cs r vm t2));
== { aplus_assoc_4 r
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t1)
(interp_cs r vm t2) }
aplus (aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm t1))
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1)
(aplus (amult c2 (interp_vl r vm l2)) (interp_cs r vm t2));
== { ics_aux_ok r vm (amult c2 (interp_vl r vm l2)) t2;
interp_m_ok r vm c2 l2 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else if varlist_lt l1 l2 then
begin
calc (==) {
interp_cs r vm (canonical_sum_merge r s1 s2);
== { }
ics_aux r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2);
== { ics_aux_ok r vm (interp_m r vm c1 l1)
(canonical_sum_merge r t1 s2) }
aplus (interp_m r vm c1 l1)
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { interp_m_ok r vm c1 l1 }
aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm (canonical_sum_merge r t1 s2));
== { canonical_sum_merge_ok r vm t1 s2 }
aplus (amult c1 (interp_vl r vm l1))
(aplus (interp_cs r vm t1) (interp_cs r vm s2));
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t1)
(interp_cs r vm s2)
}
aplus (aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t1))
(interp_cs r vm s2);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
else
begin
calc (==) {
interp_cs r vm (csm_aux r c1 l1 t1 s2);
== { }
ics_aux r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2);
== { ics_aux_ok r vm (interp_m r vm c2 l2)
(csm_aux r c1 l1 t1 t2) }
aplus (interp_m r vm c2 l2)
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { interp_m_ok r vm c2 l2 }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (csm_aux r c1 l1 t1 t2));
== { csm_aux_ok r vm c1 l1 t1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm s1) (interp_cs r vm t2));
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2) (interp_cs r vm s1));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(interp_cs r vm s1)
}
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(interp_cs r vm s1);
== { ics_aux_ok r vm (amult c1 (interp_vl r vm l1)) t1;
interp_m_ok r vm c1 l1 }
aplus (interp_cs r vm s2) (interp_cs r vm s1);
== { r.cm_add.commutativity (interp_cs r vm s1) (interp_cs r vm s2) }
aplus (interp_cs r vm s1) (interp_cs r vm s2);
}
end
val monom_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c1:a -> l1:varlist -> s2:canonical_sum a ->
Lemma
(interp_cs r vm (monom_insert r c1 l1 s2) ==
r.cm_add.mult (r.cm_mult.mult c1 (interp_vl r vm l1)) (interp_cs r vm s2))
let rec monom_insert_ok #a r vm c1 l1 s2 =
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
let aone = r.cm_mult.unit in
match s2 with
| Cons_monom c2 l2 t2 ->
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Cons_varlist l2 t2 -> // Same as Cons_monom with c2 = aone
let c2 = aone in
if l1 = l2
then
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom (aplus c1 c2) l1 t2);
== { }
ics_aux r vm (interp_m r vm (aplus c1 c2) l1) t2;
== { ics_aux_ok r vm (interp_m r vm (aplus c1 c2) l1) t2 }
aplus (interp_m r vm (aplus c1 c2) l1) (interp_cs r vm t2);
== { interp_m_ok r vm (aplus c1 c2) l1 }
aplus (amult (aplus c1 c2) (interp_vl r vm l2)) (interp_cs r vm t2);
== { distribute_right r c1 c2 (interp_vl r vm l2) }
aplus (aplus (amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2)))
(interp_cs r vm t2);
== { r.cm_add.associativity
(amult c1 (interp_vl r vm l1))
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2) }
aplus (amult c1 (interp_vl r vm l1))
(aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
else
if varlist_lt l1 l2 then ()
else
calc (==) {
interp_cs r vm (monom_insert r c1 l1 s2);
== { }
interp_cs r vm (Cons_monom c2 l2 (monom_insert r c1 l1 t2));
== { }
aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm (monom_insert r c1 l1 t2));
== { monom_insert_ok r vm c1 l1 t2 }
aplus (amult c2 (interp_vl r vm l2))
(aplus (amult c1 (interp_vl r vm l1))
(interp_cs r vm t2));
== { r.cm_add.commutativity
(amult c1 (interp_vl r vm l1))
(interp_cs r vm t2) }
aplus (amult c2 (interp_vl r vm l2))
(aplus (interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)));
== { r.cm_add.associativity
(amult c2 (interp_vl r vm l2))
(interp_cs r vm t2)
(amult c1 (interp_vl r vm l1)) }
aplus (aplus (amult c2 (interp_vl r vm l2))
(interp_cs r vm t2))
(amult c1 (interp_vl r vm l1));
== { ics_aux_ok r vm (interp_m r vm c2 l2) t2 }
aplus (interp_cs r vm s2) (amult c1 (interp_vl r vm l1));
== { r.cm_add.commutativity
(interp_cs r vm s2)
(amult c1 (interp_vl r vm l1)) }
aplus (amult c1 (interp_vl r vm l1)) (interp_cs r vm s2);
}
| Nil_monom -> ()
val varlist_insert_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l1:varlist -> s2:canonical_sum a ->
Lemma (interp_cs r vm (varlist_insert r l1 s2) ==
r.cm_add.mult (interp_vl r vm l1) (interp_cs r vm s2))
let varlist_insert_ok #a r vm l1 s2 =
let aone = r.cm_mult.unit in
monom_insert_ok r vm aone l1 s2
val canonical_sum_scalar_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar r c0 s) ==
r.cm_mult.mult c0 (interp_cs r vm s))
let rec canonical_sum_scalar_ok #a r vm c0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = r.cm_mult.unit
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar r c0 s);
== { }
interp_cs r vm (Cons_monom (amult c0 c) l (canonical_sum_scalar r c0 t));
== { }
aplus (amult (amult c0 c) (interp_vl r vm l))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { r.cm_mult.associativity c0 c (interp_vl r vm l) }
aplus (amult c0 (amult c (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar r c0 t));
== { canonical_sum_scalar_ok r vm c0 t }
aplus (amult c0 (amult c (interp_vl r vm l)))
(amult c0 (interp_cs r vm t));
== { r.distribute c0 (amult c (interp_vl r vm l))
(interp_cs r vm t) }
amult c0 (aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult c0 (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar2_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar2 r l0 s) ==
r.cm_mult.mult (interp_vl r vm l0) (interp_cs r vm s))
let rec canonical_sum_scalar2_ok #a r vm l0 s =
let aone = r.cm_mult.unit in
let aplus = r.cm_add.mult in
let amult = r.cm_mult.mult in
match s with
| Cons_monom c l t ->
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Cons_varlist l t -> // Same as Cons_monom c l t with c = aone
let c = aone in
calc (==) {
interp_cs r vm (canonical_sum_scalar2 r l0 s);
== { }
interp_cs r vm
(monom_insert r c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t));
== { monom_insert_ok r vm c (varlist_merge l0 l) (canonical_sum_scalar2 r l0 t) }
aplus (amult c (interp_vl r vm (varlist_merge l0 l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { varlist_merge_ok r vm l0 l }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(interp_cs r vm (canonical_sum_scalar2 r l0 t));
== { canonical_sum_scalar2_ok r vm l0 t }
aplus (amult c (amult (interp_vl r vm l0) (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity c (interp_vl r vm l0)
(interp_vl r vm l) }
aplus (amult (amult c (interp_vl r vm l0)) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.commutativity (interp_vl r vm l0) c }
aplus (amult (amult (interp_vl r vm l0) c) (interp_vl r vm l))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.cm_mult.associativity (interp_vl r vm l0) c (interp_vl r vm l) }
aplus (amult (interp_vl r vm l0) (amult c (interp_vl r vm l)))
(amult (interp_vl r vm l0) (interp_cs r vm t));
== { r.distribute (interp_vl r vm l0)
(amult c (interp_vl r vm l)) (interp_cs r vm t) }
amult (interp_vl r vm l0)
(aplus (amult c (interp_vl r vm l)) (interp_cs r vm t));
== { }
amult (interp_vl r vm l0) (interp_cs r vm s);
}
| Nil_monom -> ()
val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) == | false | false | FStar.Tactics.CanonCommSemiring.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val canonical_sum_scalar3_ok: #a:eqtype -> r:cr a -> vm:vmap a
-> c0:a -> l0:varlist -> s:canonical_sum a ->
Lemma (
interp_cs r vm (canonical_sum_scalar3 r c0 l0 s) ==
r.cm_mult.mult (r.cm_mult.mult c0 (interp_vl r vm l0)) (interp_cs r vm s)) | [
"recursion"
] | FStar.Tactics.CanonCommSemiring.canonical_sum_scalar3_ok | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
c0: a ->
l0: FStar.Tactics.CanonCommSemiring.varlist ->
s: FStar.Tactics.CanonCommSemiring.canonical_sum a
-> FStar.Pervasives.Lemma
(ensures
FStar.Tactics.CanonCommSemiring.interp_cs r
vm
(FStar.Tactics.CanonCommSemiring.canonical_sum_scalar3 r c0 l0 s) ==
CM?.mult (CR?.cm_mult r)
(CM?.mult (CR?.cm_mult r) c0 (FStar.Tactics.CanonCommSemiring.interp_vl r vm l0))
(FStar.Tactics.CanonCommSemiring.interp_cs r vm s)) | {
"end_col": 19,
"end_line": 1266,
"start_col": 50,
"start_line": 1182
} |
Prims.Tot | val gt (a b: t) : Tot bool | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b) | val gt (a b: t) : Tot bool
let gt (a b: t) : Tot bool = | false | null | false | gt #n (v a) (v b) | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [
"total"
] | [
"FStar.UInt16.t",
"FStar.UInt.gt",
"FStar.UInt16.n",
"FStar.UInt16.v",
"Prims.bool"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b) | false | true | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val gt (a b: t) : Tot bool | [] | FStar.UInt16.gt | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | {
"end_col": 49,
"end_line": 226,
"start_col": 32,
"start_line": 226
} |
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Plus_Question_Hat = add_underspec | let op_Plus_Question_Hat = | false | null | false | add_underspec | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [] | [
"FStar.UInt16.add_underspec"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *) | false | false | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Plus_Question_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | [] | FStar.UInt16.op_Plus_Question_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 47,
"end_line": 316,
"start_col": 34,
"start_line": 316
} |
|
Prims.Tot | val lt (a b: t) : Tot bool | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b) | val lt (a b: t) : Tot bool
let lt (a b: t) : Tot bool = | false | null | false | lt #n (v a) (v b) | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [
"total"
] | [
"FStar.UInt16.t",
"FStar.UInt.lt",
"FStar.UInt16.n",
"FStar.UInt16.v",
"Prims.bool"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b) | false | true | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val lt (a b: t) : Tot bool | [] | FStar.UInt16.lt | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | {
"end_col": 49,
"end_line": 232,
"start_col": 32,
"start_line": 232
} |
Prims.Tot | val gte (a b: t) : Tot bool | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b) | val gte (a b: t) : Tot bool
let gte (a b: t) : Tot bool = | false | null | false | gte #n (v a) (v b) | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [
"total"
] | [
"FStar.UInt16.t",
"FStar.UInt.gte",
"FStar.UInt16.n",
"FStar.UInt16.v",
"Prims.bool"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b) | false | true | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val gte (a b: t) : Tot bool | [] | FStar.UInt16.gte | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | {
"end_col": 51,
"end_line": 229,
"start_col": 33,
"start_line": 229
} |
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Subtraction_Question_Hat = sub_underspec | let op_Subtraction_Question_Hat = | false | null | false | sub_underspec | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [] | [
"FStar.UInt16.sub_underspec"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod | false | false | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Subtraction_Question_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | [] | FStar.UInt16.op_Subtraction_Question_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 54,
"end_line": 319,
"start_col": 41,
"start_line": 319
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Star_Percent_Hat = mul_mod | let op_Star_Percent_Hat = | false | null | false | mul_mod | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [] | [
"FStar.UInt16.mul_mod"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul | false | false | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Star_Percent_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | [] | FStar.UInt16.op_Star_Percent_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 40,
"end_line": 323,
"start_col": 33,
"start_line": 323
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Greater_Equals_Hat = gte | let op_Greater_Equals_Hat = | false | null | false | gte | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [
"total"
] | [
"FStar.UInt16.gte"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec
unfold let op_Star_Percent_Hat = mul_mod
unfold let op_Slash_Hat = div
unfold let op_Percent_Hat = rem
unfold let op_Hat_Hat = logxor
unfold let op_Amp_Hat = logand
unfold let op_Bar_Hat = logor
unfold let op_Less_Less_Hat = shift_left
unfold let op_Greater_Greater_Hat = shift_right
unfold let op_Equals_Hat = eq | false | true | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Greater_Equals_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | [] | FStar.UInt16.op_Greater_Equals_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | {
"end_col": 38,
"end_line": 333,
"start_col": 35,
"start_line": 333
} |
|
Prims.Tot | [
{
"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
}
] | false | let n = 16 | let n = | false | null | false | 16 | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [
"total"
] | [] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****) | false | true | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val n : Prims.int | [] | FStar.UInt16.n | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | Prims.int | {
"end_col": 17,
"end_line": 20,
"start_col": 15,
"start_line": 20
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Hat_Hat = logxor | let op_Hat_Hat = | false | null | false | logxor | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [] | [
"FStar.UInt16.logxor"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec
unfold let op_Star_Percent_Hat = mul_mod
unfold let op_Slash_Hat = div | false | false | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Hat_Hat : x: FStar.UInt16.t -> y: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | [] | FStar.UInt16.op_Hat_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt16.t -> y: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 30,
"end_line": 326,
"start_col": 24,
"start_line": 326
} |
|
Prims.Tot | val eq (a b: t) : Tot bool | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b) | val eq (a b: t) : Tot bool
let eq (a b: t) : Tot bool = | false | null | false | eq #n (v a) (v b) | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [
"total"
] | [
"FStar.UInt16.t",
"FStar.UInt.eq",
"FStar.UInt16.n",
"FStar.UInt16.v",
"Prims.bool"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality | false | true | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val eq (a b: t) : Tot bool | [] | FStar.UInt16.eq | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | {
"end_col": 49,
"end_line": 223,
"start_col": 32,
"start_line": 223
} |
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Bar_Hat = logor | let op_Bar_Hat = | false | null | false | logor | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [] | [
"FStar.UInt16.logor"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec
unfold let op_Star_Percent_Hat = mul_mod
unfold let op_Slash_Hat = div
unfold let op_Percent_Hat = rem
unfold let op_Hat_Hat = logxor | false | false | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Bar_Hat : x: FStar.UInt16.t -> y: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | [] | FStar.UInt16.op_Bar_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt16.t -> y: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 29,
"end_line": 328,
"start_col": 24,
"start_line": 328
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Plus_Hat = add | let op_Plus_Hat = | false | null | false | add | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [] | [
"FStar.UInt16.add"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options | false | false | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Plus_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | [] | FStar.UInt16.op_Plus_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 28,
"end_line": 315,
"start_col": 25,
"start_line": 315
} |
|
Prims.Tot | val lte (a b: t) : Tot bool | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b) | val lte (a b: t) : Tot bool
let lte (a b: t) : Tot bool = | false | null | false | lte #n (v a) (v b) | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [
"total"
] | [
"FStar.UInt16.t",
"FStar.UInt.lte",
"FStar.UInt16.n",
"FStar.UInt16.v",
"Prims.bool"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b) | false | true | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val lte (a b: t) : Tot bool | [] | FStar.UInt16.lte | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | {
"end_col": 51,
"end_line": 235,
"start_col": 33,
"start_line": 235
} |
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Subtraction_Hat = sub | let op_Subtraction_Hat = | false | null | false | sub | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [] | [
"FStar.UInt16.sub"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec | false | false | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Subtraction_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | [] | FStar.UInt16.op_Subtraction_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 35,
"end_line": 318,
"start_col": 32,
"start_line": 318
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Percent_Hat = rem | let op_Percent_Hat = | false | null | false | rem | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [] | [
"FStar.UInt16.rem"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec
unfold let op_Star_Percent_Hat = mul_mod | false | false | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Percent_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t{FStar.UInt16.v b <> 0} -> Prims.Pure FStar.UInt16.t | [] | FStar.UInt16.op_Percent_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t{FStar.UInt16.v b <> 0} -> Prims.Pure FStar.UInt16.t | {
"end_col": 31,
"end_line": 325,
"start_col": 28,
"start_line": 325
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Less_Less_Hat = shift_left | let op_Less_Less_Hat = | false | null | false | shift_left | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [] | [
"FStar.UInt16.shift_left"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec
unfold let op_Star_Percent_Hat = mul_mod
unfold let op_Slash_Hat = div
unfold let op_Percent_Hat = rem
unfold let op_Hat_Hat = logxor
unfold let op_Amp_Hat = logand | false | false | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Less_Less_Hat : a: FStar.UInt16.t -> s: FStar.UInt32.t -> Prims.Pure FStar.UInt16.t | [] | FStar.UInt16.op_Less_Less_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> s: FStar.UInt32.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 40,
"end_line": 329,
"start_col": 30,
"start_line": 329
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Greater_Hat = gt | let op_Greater_Hat = | false | null | false | gt | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [
"total"
] | [
"FStar.UInt16.gt"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec
unfold let op_Star_Percent_Hat = mul_mod
unfold let op_Slash_Hat = div
unfold let op_Percent_Hat = rem
unfold let op_Hat_Hat = logxor
unfold let op_Amp_Hat = logand
unfold let op_Bar_Hat = logor
unfold let op_Less_Less_Hat = shift_left
unfold let op_Greater_Greater_Hat = shift_right | false | true | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Greater_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | [] | FStar.UInt16.op_Greater_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | {
"end_col": 30,
"end_line": 332,
"start_col": 28,
"start_line": 332
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Amp_Hat = logand | let op_Amp_Hat = | false | null | false | logand | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [] | [
"FStar.UInt16.logand"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec
unfold let op_Star_Percent_Hat = mul_mod
unfold let op_Slash_Hat = div
unfold let op_Percent_Hat = rem | false | false | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Amp_Hat : x: FStar.UInt16.t -> y: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | [] | FStar.UInt16.op_Amp_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt16.t -> y: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 30,
"end_line": 327,
"start_col": 24,
"start_line": 327
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Slash_Hat = div | let op_Slash_Hat = | false | null | false | div | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [] | [
"FStar.UInt16.div"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec | false | false | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Slash_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t{FStar.UInt16.v b <> 0} -> Prims.Pure FStar.UInt16.t | [] | FStar.UInt16.op_Slash_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t{FStar.UInt16.v b <> 0} -> Prims.Pure FStar.UInt16.t | {
"end_col": 29,
"end_line": 324,
"start_col": 26,
"start_line": 324
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Greater_Greater_Hat = shift_right | let op_Greater_Greater_Hat = | false | null | false | shift_right | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [] | [
"FStar.UInt16.shift_right"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec
unfold let op_Star_Percent_Hat = mul_mod
unfold let op_Slash_Hat = div
unfold let op_Percent_Hat = rem
unfold let op_Hat_Hat = logxor
unfold let op_Amp_Hat = logand
unfold let op_Bar_Hat = logor | false | false | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Greater_Greater_Hat : a: FStar.UInt16.t -> s: FStar.UInt32.t -> Prims.Pure FStar.UInt16.t | [] | FStar.UInt16.op_Greater_Greater_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> s: FStar.UInt32.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 47,
"end_line": 330,
"start_col": 36,
"start_line": 330
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Equals_Hat = eq | let op_Equals_Hat = | false | null | false | eq | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [
"total"
] | [
"FStar.UInt16.eq"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec
unfold let op_Star_Percent_Hat = mul_mod
unfold let op_Slash_Hat = div
unfold let op_Percent_Hat = rem
unfold let op_Hat_Hat = logxor
unfold let op_Amp_Hat = logand
unfold let op_Bar_Hat = logor
unfold let op_Less_Less_Hat = shift_left | false | true | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Equals_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | [] | FStar.UInt16.op_Equals_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | {
"end_col": 29,
"end_line": 331,
"start_col": 27,
"start_line": 331
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Star_Hat = mul | let op_Star_Hat = | false | null | false | mul | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [] | [
"FStar.UInt16.mul"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec | false | false | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Star_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | [] | FStar.UInt16.op_Star_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 28,
"end_line": 321,
"start_col": 25,
"start_line": 321
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Subtraction_Percent_Hat = sub_mod | let op_Subtraction_Percent_Hat = | false | null | false | sub_mod | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [] | [
"FStar.UInt16.sub_mod"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub | false | false | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Subtraction_Percent_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | [] | FStar.UInt16.op_Subtraction_Percent_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 47,
"end_line": 320,
"start_col": 40,
"start_line": 320
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Less_Hat = lt | let op_Less_Hat = | false | null | false | lt | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [
"total"
] | [
"FStar.UInt16.lt"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec
unfold let op_Star_Percent_Hat = mul_mod
unfold let op_Slash_Hat = div
unfold let op_Percent_Hat = rem
unfold let op_Hat_Hat = logxor
unfold let op_Amp_Hat = logand
unfold let op_Bar_Hat = logor
unfold let op_Less_Less_Hat = shift_left
unfold let op_Greater_Greater_Hat = shift_right
unfold let op_Equals_Hat = eq
unfold let op_Greater_Hat = gt | false | true | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Less_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | [] | FStar.UInt16.op_Less_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | {
"end_col": 27,
"end_line": 334,
"start_col": 25,
"start_line": 334
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Less_Equals_Hat = lte | let op_Less_Equals_Hat = | false | null | false | lte | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [
"total"
] | [
"FStar.UInt16.lte"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec
unfold let op_Star_Percent_Hat = mul_mod
unfold let op_Slash_Hat = div
unfold let op_Percent_Hat = rem
unfold let op_Hat_Hat = logxor
unfold let op_Amp_Hat = logand
unfold let op_Bar_Hat = logor
unfold let op_Less_Less_Hat = shift_left
unfold let op_Greater_Greater_Hat = shift_right
unfold let op_Equals_Hat = eq
unfold let op_Greater_Hat = gt
unfold let op_Greater_Equals_Hat = gte | false | true | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Less_Equals_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | [] | FStar.UInt16.op_Less_Equals_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | {
"end_col": 35,
"end_line": 335,
"start_col": 32,
"start_line": 335
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Star_Question_Hat = mul_underspec | let op_Star_Question_Hat = | false | null | false | mul_underspec | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [] | [
"FStar.UInt16.mul_underspec"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod | false | false | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Star_Question_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | [] | FStar.UInt16.op_Star_Question_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 47,
"end_line": 322,
"start_col": 34,
"start_line": 322
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let op_Plus_Percent_Hat = add_mod | let op_Plus_Percent_Hat = | false | null | false | add_mod | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [] | [
"FStar.UInt16.add_mod"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add | false | false | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Plus_Percent_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | [] | FStar.UInt16.op_Plus_Percent_Hat | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 40,
"end_line": 317,
"start_col": 33,
"start_line": 317
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let minus (a:t) = add_mod (lognot a) (uint_to_t 1) | let minus (a: t) = | false | null | false | add_mod (lognot a) (uint_to_t 1) | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [
"total"
] | [
"FStar.UInt16.t",
"FStar.UInt16.add_mod",
"FStar.UInt16.lognot",
"FStar.UInt16.uint_to_t"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *) | false | true | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val minus : a: FStar.UInt16.t -> FStar.UInt16.t | [] | FStar.UInt16.minus | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> FStar.UInt16.t | {
"end_col": 50,
"end_line": 239,
"start_col": 18,
"start_line": 239
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let n_minus_one = UInt32.uint_to_t (n - 1) | let n_minus_one = | false | null | false | UInt32.uint_to_t (n - 1) | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [
"total"
] | [
"FStar.UInt32.uint_to_t",
"Prims.op_Subtraction",
"FStar.UInt16.n"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *) | false | true | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val n_minus_one : FStar.UInt32.t | [] | FStar.UInt16.n_minus_one | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | FStar.UInt32.t | {
"end_col": 42,
"end_line": 243,
"start_col": 18,
"start_line": 243
} |
|
Prims.Pure | val gte_mask (a b: t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\ (v a < v b ==> v c = 0))) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c | val gte_mask (a b: t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\ (v a < v b ==> v c = 0)))
let gte_mask (a b: t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\ (v a < v b ==> v c = 0))) = | false | null | false | let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [] | [
"FStar.UInt16.t",
"Prims.unit",
"FStar.UInt.lemma_msb_gte",
"FStar.UInt16.n",
"FStar.UInt16.v",
"FStar.UInt16.lemma_sub_msbs",
"FStar.UInt16.sub_mod",
"FStar.UInt16.uint_to_t",
"FStar.UInt16.shift_right",
"FStar.UInt16.n_minus_one",
"FStar.UInt16.logxor",
"FStar.UInt16.logor",
"Prims.l_True",
"Prims.l_and",
"Prims.l_imp",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.op_LessThan"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\ | false | false | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 1,
"initial_ifuel": 1,
"max_fuel": 1,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 80,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val gte_mask (a b: t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\ (v a < v b ==> v c = 0))) | [] | FStar.UInt16.gte_mask | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 5,
"end_line": 311,
"start_col": 3,
"start_line": 299
} |
Prims.Pure | val eq_mask (a b: t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\ (v a <> v b ==> v c = 0))) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"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
}
] | false | let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c | val eq_mask (a b: t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\ (v a <> v b ==> v c = 0)))
let eq_mask (a b: t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\ (v a <> v b ==> v c = 0))) = | false | null | false | let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b
then
(logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\ v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n))
else
(logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n));
c | {
"checked_file": "FStar.UInt16.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt16.fsti"
} | [] | [
"FStar.UInt16.t",
"Prims.unit",
"Prims.op_Equality",
"Prims._assert",
"Prims.b2t",
"FStar.UInt.uint_t",
"FStar.UInt16.n",
"FStar.UInt16.v",
"FStar.UInt.ones",
"Prims.l_and",
"Prims.int",
"FStar.UInt.logor_lemma_1",
"FStar.UInt.lognot_lemma_1",
"FStar.UInt.logxor_self",
"Prims.bool",
"FStar.UInt.zero",
"FStar.UInt.lemma_minus_zero",
"FStar.UInt.lemma_msb_pow2",
"FStar.UInt16.lognot",
"FStar.UInt.logxor_neq_nonzero",
"FStar.UInt16.sub_mod",
"FStar.UInt16.uint_to_t",
"FStar.UInt16.shift_right",
"FStar.UInt16.n_minus_one",
"FStar.UInt16.logor",
"FStar.UInt16.minus",
"FStar.UInt16.logxor",
"Prims.l_True",
"Prims.l_imp",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.op_disEquality"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 16
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\ | false | false | FStar.UInt16.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 1,
"initial_ifuel": 1,
"max_fuel": 1,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 80,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val eq_mask (a b: t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\ (v a <> v b ==> v c = 0))) | [] | FStar.UInt16.eq_mask | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 5,
"end_line": 282,
"start_col": 3,
"start_line": 260
} |
Prims.Tot | val bn_field_check_modulus: #t:limb_t -> km:BM.mont t -> bn_field_check_modulus_st t km.BM.bn.BN.len | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.MontExponentiation",
"short_module": "ME"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Lib",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Base",
"short_module": "BB"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.Definitions",
"short_module": "BD"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontArithmetic",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontArithmetic",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "FStar.Math.Euclid",
"short_module": "Euclid"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let bn_field_check_modulus #t km n =
let m = km.BM.mont_check n in
BB.unsafe_bool_of_limb m | val bn_field_check_modulus: #t:limb_t -> km:BM.mont t -> bn_field_check_modulus_st t km.BM.bn.BN.len
let bn_field_check_modulus #t km n = | false | null | false | let m = km.BM.mont_check n in
BB.unsafe_bool_of_limb m | {
"checked_file": "Hacl.Bignum.MontArithmetic.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Bignum.MontArithmetic.fst.checked",
"Hacl.Spec.Bignum.MontArithmetic.fst.checked",
"Hacl.Spec.Bignum.Definitions.fst.checked",
"Hacl.Bignum.Montgomery.fsti.checked",
"Hacl.Bignum.MontExponentiation.fst.checked",
"Hacl.Bignum.ModInv.fst.checked",
"Hacl.Bignum.Lib.fst.checked",
"Hacl.Bignum.Exponentiation.fsti.checked",
"Hacl.Bignum.Definitions.fst.checked",
"Hacl.Bignum.Base.fst.checked",
"Hacl.Bignum.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": true,
"source_file": "Hacl.Bignum.MontArithmetic.fst"
} | [
"total"
] | [
"Hacl.Bignum.Definitions.limb_t",
"Hacl.Bignum.Montgomery.mont",
"Hacl.Bignum.Definitions.lbignum",
"Hacl.Bignum.__proj__Mkbn__item__len",
"Hacl.Bignum.Montgomery.__proj__Mkmont__item__bn",
"Hacl.Spec.Bignum.Base.unsafe_bool_of_limb",
"Prims.bool",
"Hacl.Bignum.Definitions.limb",
"Hacl.Bignum.Montgomery.__proj__Mkmont__item__mont_check"
] | [] | module Hacl.Bignum.MontArithmetic
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum.Definitions
module B = LowStar.Buffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module S = Hacl.Spec.Bignum.MontArithmetic
module BD = Hacl.Spec.Bignum.Definitions
module BB = Hacl.Bignum.Base
module BL = Hacl.Bignum.Lib
module ME = Hacl.Bignum.MontExponentiation
module BE = Hacl.Bignum.Exponentiation
module BN = Hacl.Bignum
module BM = Hacl.Bignum.Montgomery
module BI = Hacl.Bignum.ModInv
friend Hacl.Spec.Bignum.MontArithmetic
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
let _align_fsti = ()
let bn_field_get_len #t k =
let open LowStar.BufferOps in
let k1 = !*k in
k1.len | false | false | Hacl.Bignum.MontArithmetic.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val bn_field_check_modulus: #t:limb_t -> km:BM.mont t -> bn_field_check_modulus_st t km.BM.bn.BN.len | [] | Hacl.Bignum.MontArithmetic.bn_field_check_modulus | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | km: Hacl.Bignum.Montgomery.mont t
-> Hacl.Bignum.MontArithmetic.bn_field_check_modulus_st t (Mkbn?.len (Mkmont?.bn km)) | {
"end_col": 26,
"end_line": 41,
"start_col": 36,
"start_line": 39
} |
Prims.Tot | val bn_field_get_len: #t:limb_t -> bn_field_get_len_st t | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.MontExponentiation",
"short_module": "ME"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Lib",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Base",
"short_module": "BB"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.Definitions",
"short_module": "BD"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontArithmetic",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontArithmetic",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "FStar.Math.Euclid",
"short_module": "Euclid"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let bn_field_get_len #t k =
let open LowStar.BufferOps in
let k1 = !*k in
k1.len | val bn_field_get_len: #t:limb_t -> bn_field_get_len_st t
let bn_field_get_len #t k = | false | null | false | let open LowStar.BufferOps in
let k1 = !*k in
k1.len | {
"checked_file": "Hacl.Bignum.MontArithmetic.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Bignum.MontArithmetic.fst.checked",
"Hacl.Spec.Bignum.MontArithmetic.fst.checked",
"Hacl.Spec.Bignum.Definitions.fst.checked",
"Hacl.Bignum.Montgomery.fsti.checked",
"Hacl.Bignum.MontExponentiation.fst.checked",
"Hacl.Bignum.ModInv.fst.checked",
"Hacl.Bignum.Lib.fst.checked",
"Hacl.Bignum.Exponentiation.fsti.checked",
"Hacl.Bignum.Definitions.fst.checked",
"Hacl.Bignum.Base.fst.checked",
"Hacl.Bignum.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": true,
"source_file": "Hacl.Bignum.MontArithmetic.fst"
} | [
"total"
] | [
"Hacl.Bignum.Definitions.limb_t",
"Hacl.Bignum.MontArithmetic.pbn_mont_ctx",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__len",
"Hacl.Bignum.MontArithmetic.lb",
"Hacl.Bignum.MontArithmetic.ll",
"Hacl.Bignum.meta_len",
"Hacl.Bignum.MontArithmetic.bn_mont_ctx",
"LowStar.BufferOps.op_Bang_Star",
"LowStar.Buffer.trivial_preorder"
] | [] | module Hacl.Bignum.MontArithmetic
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum.Definitions
module B = LowStar.Buffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module S = Hacl.Spec.Bignum.MontArithmetic
module BD = Hacl.Spec.Bignum.Definitions
module BB = Hacl.Bignum.Base
module BL = Hacl.Bignum.Lib
module ME = Hacl.Bignum.MontExponentiation
module BE = Hacl.Bignum.Exponentiation
module BN = Hacl.Bignum
module BM = Hacl.Bignum.Montgomery
module BI = Hacl.Bignum.ModInv
friend Hacl.Spec.Bignum.MontArithmetic
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
let _align_fsti = () | false | false | Hacl.Bignum.MontArithmetic.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val bn_field_get_len: #t:limb_t -> bn_field_get_len_st t | [] | Hacl.Bignum.MontArithmetic.bn_field_get_len | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | Hacl.Bignum.MontArithmetic.bn_field_get_len_st t | {
"end_col": 8,
"end_line": 36,
"start_col": 2,
"start_line": 34
} |
Prims.Tot | val bn_to_field: #t:limb_t -> km:BM.mont t -> bn_to_field_st t km.BM.bn.BN.len | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.MontExponentiation",
"short_module": "ME"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Lib",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Base",
"short_module": "BB"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.Definitions",
"short_module": "BD"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontArithmetic",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontArithmetic",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "FStar.Math.Euclid",
"short_module": "Euclid"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let bn_to_field #t km k a aM =
let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
km.BM.to k1.n k1.mu k1.r2 a aM;
let h1 = ST.get () in
assert (as_seq h1 aM == S.bn_to_field (as_pctx h0 k) (as_seq h0 a)) | val bn_to_field: #t:limb_t -> km:BM.mont t -> bn_to_field_st t km.BM.bn.BN.len
let bn_to_field #t km k a aM = | false | null | false | let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
km.BM.to k1.n k1.mu k1.r2 a aM;
let h1 = ST.get () in
assert (as_seq h1 aM == S.bn_to_field (as_pctx h0 k) (as_seq h0 a)) | {
"checked_file": "Hacl.Bignum.MontArithmetic.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Bignum.MontArithmetic.fst.checked",
"Hacl.Spec.Bignum.MontArithmetic.fst.checked",
"Hacl.Spec.Bignum.Definitions.fst.checked",
"Hacl.Bignum.Montgomery.fsti.checked",
"Hacl.Bignum.MontExponentiation.fst.checked",
"Hacl.Bignum.ModInv.fst.checked",
"Hacl.Bignum.Lib.fst.checked",
"Hacl.Bignum.Exponentiation.fsti.checked",
"Hacl.Bignum.Definitions.fst.checked",
"Hacl.Bignum.Base.fst.checked",
"Hacl.Bignum.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": true,
"source_file": "Hacl.Bignum.MontArithmetic.fst"
} | [
"total"
] | [
"Hacl.Bignum.Definitions.limb_t",
"Hacl.Bignum.Montgomery.mont",
"Hacl.Bignum.MontArithmetic.pbn_mont_ctx",
"Hacl.Bignum.Definitions.lbignum",
"Hacl.Bignum.__proj__Mkbn__item__len",
"Hacl.Bignum.Montgomery.__proj__Mkmont__item__bn",
"Prims._assert",
"Prims.eq2",
"Lib.Sequence.seq",
"Hacl.Bignum.Definitions.limb",
"Prims.l_or",
"Prims.nat",
"FStar.Seq.Base.length",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Prims.l_and",
"Hacl.Spec.Bignum.Definitions.limb",
"Hacl.Spec.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx__item__len",
"Hacl.Bignum.MontArithmetic.as_pctx",
"Prims.b2t",
"Prims.op_LessThan",
"Hacl.Spec.Bignum.Definitions.bn_v",
"Hacl.Spec.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx__item__n",
"Lib.Buffer.as_seq",
"Lib.Buffer.MUT",
"Hacl.Spec.Bignum.MontArithmetic.bn_to_field",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Hacl.Bignum.Montgomery.__proj__Mkmont__item__to",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__n",
"Hacl.Bignum.MontArithmetic.lb",
"Hacl.Bignum.MontArithmetic.ll",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__mu",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__r2",
"Hacl.Bignum.MontArithmetic.bn_mont_ctx",
"LowStar.BufferOps.op_Bang_Star",
"LowStar.Buffer.trivial_preorder"
] | [] | module Hacl.Bignum.MontArithmetic
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum.Definitions
module B = LowStar.Buffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module S = Hacl.Spec.Bignum.MontArithmetic
module BD = Hacl.Spec.Bignum.Definitions
module BB = Hacl.Bignum.Base
module BL = Hacl.Bignum.Lib
module ME = Hacl.Bignum.MontExponentiation
module BE = Hacl.Bignum.Exponentiation
module BN = Hacl.Bignum
module BM = Hacl.Bignum.Montgomery
module BI = Hacl.Bignum.ModInv
friend Hacl.Spec.Bignum.MontArithmetic
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
let _align_fsti = ()
let bn_field_get_len #t k =
let open LowStar.BufferOps in
let k1 = !*k in
k1.len
let bn_field_check_modulus #t km n =
let m = km.BM.mont_check n in
BB.unsafe_bool_of_limb m
let bn_field_init #t len precomp_r2 r n =
let h0 = ST.get () in
let r2 : buffer (limb t) = B.mmalloc r (uint #t #SEC 0) len in
let n1 : buffer (limb t) = B.mmalloc r (uint #t #SEC 0) len in
let h1 = ST.get () in
B.(modifies_only_not_unused_in loc_none h0 h1);
assert (B.length r2 == FStar.UInt32.v len);
assert (B.length n1 == FStar.UInt32.v len);
let r2 : lbignum t len = r2 in
let n1 : lbignum t len = n1 in
copy n1 n;
let nBits = size (bits t) *! BB.unsafe_size_from_limb (BL.bn_get_top_index len n) in
precomp_r2 nBits n r2;
let mu = BM.mod_inv_limb n.(0ul) in
let res : bn_mont_ctx t = { len = len; n = n1; mu = mu; r2 = r2 } in
let h2 = ST.get () in
assert (as_ctx h2 res == S.bn_field_init (as_seq h0 n));
assert (S.bn_mont_ctx_inv (as_ctx h2 res));
B.(modifies_only_not_unused_in loc_none h0 h2);
B.malloc r res 1ul
let bn_field_free #t k =
let open LowStar.BufferOps in
let k1 = !*k in
let n : buffer (limb t) = k1.n in
let r2 : buffer (limb t) = k1.r2 in
B.free n;
B.freeable_disjoint n r2;
B.free r2;
B.free k | false | false | Hacl.Bignum.MontArithmetic.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val bn_to_field: #t:limb_t -> km:BM.mont t -> bn_to_field_st t km.BM.bn.BN.len | [] | Hacl.Bignum.MontArithmetic.bn_to_field | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | km: Hacl.Bignum.Montgomery.mont t
-> Hacl.Bignum.MontArithmetic.bn_to_field_st t (Mkbn?.len (Mkmont?.bn km)) | {
"end_col": 69,
"end_line": 85,
"start_col": 2,
"start_line": 80
} |
Prims.Tot | val bn_from_field: #t:limb_t -> km:BM.mont t -> bn_from_field_st t km.BM.bn.BN.len | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.MontExponentiation",
"short_module": "ME"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Lib",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Base",
"short_module": "BB"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.Definitions",
"short_module": "BD"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontArithmetic",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontArithmetic",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "FStar.Math.Euclid",
"short_module": "Euclid"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let bn_from_field #t km k aM a =
let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
km.BM.from k1.n k1.mu aM a;
let h1 = ST.get () in
assert (as_seq h1 a == S.bn_from_field (as_pctx h0 k) (as_seq h0 aM)) | val bn_from_field: #t:limb_t -> km:BM.mont t -> bn_from_field_st t km.BM.bn.BN.len
let bn_from_field #t km k aM a = | false | null | false | let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
km.BM.from k1.n k1.mu aM a;
let h1 = ST.get () in
assert (as_seq h1 a == S.bn_from_field (as_pctx h0 k) (as_seq h0 aM)) | {
"checked_file": "Hacl.Bignum.MontArithmetic.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Bignum.MontArithmetic.fst.checked",
"Hacl.Spec.Bignum.MontArithmetic.fst.checked",
"Hacl.Spec.Bignum.Definitions.fst.checked",
"Hacl.Bignum.Montgomery.fsti.checked",
"Hacl.Bignum.MontExponentiation.fst.checked",
"Hacl.Bignum.ModInv.fst.checked",
"Hacl.Bignum.Lib.fst.checked",
"Hacl.Bignum.Exponentiation.fsti.checked",
"Hacl.Bignum.Definitions.fst.checked",
"Hacl.Bignum.Base.fst.checked",
"Hacl.Bignum.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": true,
"source_file": "Hacl.Bignum.MontArithmetic.fst"
} | [
"total"
] | [
"Hacl.Bignum.Definitions.limb_t",
"Hacl.Bignum.Montgomery.mont",
"Hacl.Bignum.MontArithmetic.pbn_mont_ctx",
"Hacl.Bignum.Definitions.lbignum",
"Hacl.Bignum.__proj__Mkbn__item__len",
"Hacl.Bignum.Montgomery.__proj__Mkmont__item__bn",
"Prims._assert",
"Prims.eq2",
"Lib.Sequence.seq",
"Hacl.Bignum.Definitions.limb",
"Prims.l_or",
"Prims.nat",
"FStar.Seq.Base.length",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Hacl.Spec.Bignum.Definitions.limb",
"Hacl.Spec.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx__item__len",
"Hacl.Bignum.MontArithmetic.as_pctx",
"Lib.Buffer.as_seq",
"Lib.Buffer.MUT",
"Hacl.Spec.Bignum.MontArithmetic.bn_from_field",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Hacl.Bignum.Montgomery.__proj__Mkmont__item__from",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__n",
"Hacl.Bignum.MontArithmetic.lb",
"Hacl.Bignum.MontArithmetic.ll",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__mu",
"Hacl.Bignum.MontArithmetic.bn_mont_ctx",
"LowStar.BufferOps.op_Bang_Star",
"LowStar.Buffer.trivial_preorder"
] | [] | module Hacl.Bignum.MontArithmetic
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum.Definitions
module B = LowStar.Buffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module S = Hacl.Spec.Bignum.MontArithmetic
module BD = Hacl.Spec.Bignum.Definitions
module BB = Hacl.Bignum.Base
module BL = Hacl.Bignum.Lib
module ME = Hacl.Bignum.MontExponentiation
module BE = Hacl.Bignum.Exponentiation
module BN = Hacl.Bignum
module BM = Hacl.Bignum.Montgomery
module BI = Hacl.Bignum.ModInv
friend Hacl.Spec.Bignum.MontArithmetic
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
let _align_fsti = ()
let bn_field_get_len #t k =
let open LowStar.BufferOps in
let k1 = !*k in
k1.len
let bn_field_check_modulus #t km n =
let m = km.BM.mont_check n in
BB.unsafe_bool_of_limb m
let bn_field_init #t len precomp_r2 r n =
let h0 = ST.get () in
let r2 : buffer (limb t) = B.mmalloc r (uint #t #SEC 0) len in
let n1 : buffer (limb t) = B.mmalloc r (uint #t #SEC 0) len in
let h1 = ST.get () in
B.(modifies_only_not_unused_in loc_none h0 h1);
assert (B.length r2 == FStar.UInt32.v len);
assert (B.length n1 == FStar.UInt32.v len);
let r2 : lbignum t len = r2 in
let n1 : lbignum t len = n1 in
copy n1 n;
let nBits = size (bits t) *! BB.unsafe_size_from_limb (BL.bn_get_top_index len n) in
precomp_r2 nBits n r2;
let mu = BM.mod_inv_limb n.(0ul) in
let res : bn_mont_ctx t = { len = len; n = n1; mu = mu; r2 = r2 } in
let h2 = ST.get () in
assert (as_ctx h2 res == S.bn_field_init (as_seq h0 n));
assert (S.bn_mont_ctx_inv (as_ctx h2 res));
B.(modifies_only_not_unused_in loc_none h0 h2);
B.malloc r res 1ul
let bn_field_free #t k =
let open LowStar.BufferOps in
let k1 = !*k in
let n : buffer (limb t) = k1.n in
let r2 : buffer (limb t) = k1.r2 in
B.free n;
B.freeable_disjoint n r2;
B.free r2;
B.free k
let bn_to_field #t km k a aM =
let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
km.BM.to k1.n k1.mu k1.r2 a aM;
let h1 = ST.get () in
assert (as_seq h1 aM == S.bn_to_field (as_pctx h0 k) (as_seq h0 a)) | false | false | Hacl.Bignum.MontArithmetic.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val bn_from_field: #t:limb_t -> km:BM.mont t -> bn_from_field_st t km.BM.bn.BN.len | [] | Hacl.Bignum.MontArithmetic.bn_from_field | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | km: Hacl.Bignum.Montgomery.mont t
-> Hacl.Bignum.MontArithmetic.bn_from_field_st t (Mkbn?.len (Mkmont?.bn km)) | {
"end_col": 71,
"end_line": 94,
"start_col": 2,
"start_line": 89
} |
Prims.Tot | val bn_field_one: #t:limb_t -> km:BM.mont t -> bn_field_one_st t km.BM.bn.BN.len | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.MontExponentiation",
"short_module": "ME"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Lib",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Base",
"short_module": "BB"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.Definitions",
"short_module": "BD"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontArithmetic",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontArithmetic",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "FStar.Math.Euclid",
"short_module": "Euclid"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let bn_field_one #t km k oneM =
let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
BM.bn_mont_one km.BM.bn.BN.len km.BM.from k1.n k1.mu k1.r2 oneM;
let h1 = ST.get () in
assert (as_seq h1 oneM == S.bn_field_one (as_pctx h0 k)) | val bn_field_one: #t:limb_t -> km:BM.mont t -> bn_field_one_st t km.BM.bn.BN.len
let bn_field_one #t km k oneM = | false | null | false | let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
BM.bn_mont_one km.BM.bn.BN.len km.BM.from k1.n k1.mu k1.r2 oneM;
let h1 = ST.get () in
assert (as_seq h1 oneM == S.bn_field_one (as_pctx h0 k)) | {
"checked_file": "Hacl.Bignum.MontArithmetic.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Bignum.MontArithmetic.fst.checked",
"Hacl.Spec.Bignum.MontArithmetic.fst.checked",
"Hacl.Spec.Bignum.Definitions.fst.checked",
"Hacl.Bignum.Montgomery.fsti.checked",
"Hacl.Bignum.MontExponentiation.fst.checked",
"Hacl.Bignum.ModInv.fst.checked",
"Hacl.Bignum.Lib.fst.checked",
"Hacl.Bignum.Exponentiation.fsti.checked",
"Hacl.Bignum.Definitions.fst.checked",
"Hacl.Bignum.Base.fst.checked",
"Hacl.Bignum.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": true,
"source_file": "Hacl.Bignum.MontArithmetic.fst"
} | [
"total"
] | [
"Hacl.Bignum.Definitions.limb_t",
"Hacl.Bignum.Montgomery.mont",
"Hacl.Bignum.MontArithmetic.pbn_mont_ctx",
"Hacl.Bignum.Definitions.lbignum",
"Hacl.Bignum.__proj__Mkbn__item__len",
"Hacl.Bignum.Montgomery.__proj__Mkmont__item__bn",
"Prims._assert",
"Prims.eq2",
"Lib.Sequence.seq",
"Hacl.Bignum.Definitions.limb",
"Prims.l_or",
"Prims.nat",
"FStar.Seq.Base.length",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Prims.l_and",
"Hacl.Spec.Bignum.Definitions.limb",
"Hacl.Spec.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx__item__len",
"Hacl.Bignum.MontArithmetic.as_pctx",
"Prims.b2t",
"Prims.op_LessThan",
"Hacl.Spec.Bignum.Definitions.bn_v",
"Hacl.Spec.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx__item__n",
"Lib.Buffer.as_seq",
"Lib.Buffer.MUT",
"Hacl.Spec.Bignum.MontArithmetic.bn_field_one",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Hacl.Bignum.Montgomery.bn_mont_one",
"Hacl.Bignum.Montgomery.__proj__Mkmont__item__from",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__n",
"Hacl.Bignum.MontArithmetic.lb",
"Hacl.Bignum.MontArithmetic.ll",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__mu",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__r2",
"Hacl.Bignum.MontArithmetic.bn_mont_ctx",
"LowStar.BufferOps.op_Bang_Star",
"LowStar.Buffer.trivial_preorder"
] | [] | module Hacl.Bignum.MontArithmetic
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum.Definitions
module B = LowStar.Buffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module S = Hacl.Spec.Bignum.MontArithmetic
module BD = Hacl.Spec.Bignum.Definitions
module BB = Hacl.Bignum.Base
module BL = Hacl.Bignum.Lib
module ME = Hacl.Bignum.MontExponentiation
module BE = Hacl.Bignum.Exponentiation
module BN = Hacl.Bignum
module BM = Hacl.Bignum.Montgomery
module BI = Hacl.Bignum.ModInv
friend Hacl.Spec.Bignum.MontArithmetic
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
let _align_fsti = ()
let bn_field_get_len #t k =
let open LowStar.BufferOps in
let k1 = !*k in
k1.len
let bn_field_check_modulus #t km n =
let m = km.BM.mont_check n in
BB.unsafe_bool_of_limb m
let bn_field_init #t len precomp_r2 r n =
let h0 = ST.get () in
let r2 : buffer (limb t) = B.mmalloc r (uint #t #SEC 0) len in
let n1 : buffer (limb t) = B.mmalloc r (uint #t #SEC 0) len in
let h1 = ST.get () in
B.(modifies_only_not_unused_in loc_none h0 h1);
assert (B.length r2 == FStar.UInt32.v len);
assert (B.length n1 == FStar.UInt32.v len);
let r2 : lbignum t len = r2 in
let n1 : lbignum t len = n1 in
copy n1 n;
let nBits = size (bits t) *! BB.unsafe_size_from_limb (BL.bn_get_top_index len n) in
precomp_r2 nBits n r2;
let mu = BM.mod_inv_limb n.(0ul) in
let res : bn_mont_ctx t = { len = len; n = n1; mu = mu; r2 = r2 } in
let h2 = ST.get () in
assert (as_ctx h2 res == S.bn_field_init (as_seq h0 n));
assert (S.bn_mont_ctx_inv (as_ctx h2 res));
B.(modifies_only_not_unused_in loc_none h0 h2);
B.malloc r res 1ul
let bn_field_free #t k =
let open LowStar.BufferOps in
let k1 = !*k in
let n : buffer (limb t) = k1.n in
let r2 : buffer (limb t) = k1.r2 in
B.free n;
B.freeable_disjoint n r2;
B.free r2;
B.free k
let bn_to_field #t km k a aM =
let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
km.BM.to k1.n k1.mu k1.r2 a aM;
let h1 = ST.get () in
assert (as_seq h1 aM == S.bn_to_field (as_pctx h0 k) (as_seq h0 a))
let bn_from_field #t km k aM a =
let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
km.BM.from k1.n k1.mu aM a;
let h1 = ST.get () in
assert (as_seq h1 a == S.bn_from_field (as_pctx h0 k) (as_seq h0 aM))
let bn_field_add #t km k aM bM cM =
let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
km.BM.bn.BN.add_mod_n k1.n aM bM cM;
let h1 = ST.get () in
assert (as_seq h1 cM == S.bn_field_add (as_pctx h0 k) (as_seq h0 aM) (as_seq h0 bM))
let bn_field_sub #t km k aM bM cM =
let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
km.BM.bn.BN.sub_mod_n k1.n aM bM cM;
let h1 = ST.get () in
assert (as_seq h1 cM == S.bn_field_sub (as_pctx h0 k) (as_seq h0 aM) (as_seq h0 bM))
let bn_field_mul #t km k aM bM cM =
let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
km.BM.mul k1.n k1.mu aM bM cM;
let h1 = ST.get () in
assert (as_seq h1 cM == S.bn_field_mul (as_pctx h0 k) (as_seq h0 aM) (as_seq h0 bM))
let bn_field_sqr #t km k aM cM =
let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
km.BM.sqr k1.n k1.mu aM cM;
let h1 = ST.get () in
assert (as_seq h1 cM == S.bn_field_sqr (as_pctx h0 k) (as_seq h0 aM)) | false | false | Hacl.Bignum.MontArithmetic.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val bn_field_one: #t:limb_t -> km:BM.mont t -> bn_field_one_st t km.BM.bn.BN.len | [] | Hacl.Bignum.MontArithmetic.bn_field_one | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | km: Hacl.Bignum.Montgomery.mont t
-> Hacl.Bignum.MontArithmetic.bn_field_one_st t (Mkbn?.len (Mkmont?.bn km)) | {
"end_col": 58,
"end_line": 139,
"start_col": 2,
"start_line": 134
} |
Prims.Tot | val bn_field_sqr: #t:limb_t -> km:BM.mont t -> bn_field_sqr_st t km.BM.bn.BN.len | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.MontExponentiation",
"short_module": "ME"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Lib",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Base",
"short_module": "BB"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.Definitions",
"short_module": "BD"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontArithmetic",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontArithmetic",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "FStar.Math.Euclid",
"short_module": "Euclid"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let bn_field_sqr #t km k aM cM =
let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
km.BM.sqr k1.n k1.mu aM cM;
let h1 = ST.get () in
assert (as_seq h1 cM == S.bn_field_sqr (as_pctx h0 k) (as_seq h0 aM)) | val bn_field_sqr: #t:limb_t -> km:BM.mont t -> bn_field_sqr_st t km.BM.bn.BN.len
let bn_field_sqr #t km k aM cM = | false | null | false | let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
km.BM.sqr k1.n k1.mu aM cM;
let h1 = ST.get () in
assert (as_seq h1 cM == S.bn_field_sqr (as_pctx h0 k) (as_seq h0 aM)) | {
"checked_file": "Hacl.Bignum.MontArithmetic.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Bignum.MontArithmetic.fst.checked",
"Hacl.Spec.Bignum.MontArithmetic.fst.checked",
"Hacl.Spec.Bignum.Definitions.fst.checked",
"Hacl.Bignum.Montgomery.fsti.checked",
"Hacl.Bignum.MontExponentiation.fst.checked",
"Hacl.Bignum.ModInv.fst.checked",
"Hacl.Bignum.Lib.fst.checked",
"Hacl.Bignum.Exponentiation.fsti.checked",
"Hacl.Bignum.Definitions.fst.checked",
"Hacl.Bignum.Base.fst.checked",
"Hacl.Bignum.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": true,
"source_file": "Hacl.Bignum.MontArithmetic.fst"
} | [
"total"
] | [
"Hacl.Bignum.Definitions.limb_t",
"Hacl.Bignum.Montgomery.mont",
"Hacl.Bignum.MontArithmetic.pbn_mont_ctx",
"Hacl.Bignum.Definitions.lbignum",
"Hacl.Bignum.__proj__Mkbn__item__len",
"Hacl.Bignum.Montgomery.__proj__Mkmont__item__bn",
"Prims._assert",
"Prims.eq2",
"Lib.Sequence.seq",
"Hacl.Bignum.Definitions.limb",
"Prims.l_or",
"Prims.nat",
"FStar.Seq.Base.length",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Prims.l_and",
"Hacl.Spec.Bignum.Definitions.limb",
"Hacl.Spec.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx__item__len",
"Hacl.Bignum.MontArithmetic.as_pctx",
"Prims.b2t",
"Prims.op_LessThan",
"Hacl.Spec.Bignum.Definitions.bn_v",
"Hacl.Spec.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx__item__n",
"Lib.Buffer.as_seq",
"Lib.Buffer.MUT",
"Hacl.Spec.Bignum.MontArithmetic.bn_field_sqr",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Hacl.Bignum.Montgomery.__proj__Mkmont__item__sqr",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__n",
"Hacl.Bignum.MontArithmetic.lb",
"Hacl.Bignum.MontArithmetic.ll",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__mu",
"Hacl.Bignum.MontArithmetic.bn_mont_ctx",
"LowStar.BufferOps.op_Bang_Star",
"LowStar.Buffer.trivial_preorder"
] | [] | module Hacl.Bignum.MontArithmetic
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum.Definitions
module B = LowStar.Buffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module S = Hacl.Spec.Bignum.MontArithmetic
module BD = Hacl.Spec.Bignum.Definitions
module BB = Hacl.Bignum.Base
module BL = Hacl.Bignum.Lib
module ME = Hacl.Bignum.MontExponentiation
module BE = Hacl.Bignum.Exponentiation
module BN = Hacl.Bignum
module BM = Hacl.Bignum.Montgomery
module BI = Hacl.Bignum.ModInv
friend Hacl.Spec.Bignum.MontArithmetic
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
let _align_fsti = ()
let bn_field_get_len #t k =
let open LowStar.BufferOps in
let k1 = !*k in
k1.len
let bn_field_check_modulus #t km n =
let m = km.BM.mont_check n in
BB.unsafe_bool_of_limb m
let bn_field_init #t len precomp_r2 r n =
let h0 = ST.get () in
let r2 : buffer (limb t) = B.mmalloc r (uint #t #SEC 0) len in
let n1 : buffer (limb t) = B.mmalloc r (uint #t #SEC 0) len in
let h1 = ST.get () in
B.(modifies_only_not_unused_in loc_none h0 h1);
assert (B.length r2 == FStar.UInt32.v len);
assert (B.length n1 == FStar.UInt32.v len);
let r2 : lbignum t len = r2 in
let n1 : lbignum t len = n1 in
copy n1 n;
let nBits = size (bits t) *! BB.unsafe_size_from_limb (BL.bn_get_top_index len n) in
precomp_r2 nBits n r2;
let mu = BM.mod_inv_limb n.(0ul) in
let res : bn_mont_ctx t = { len = len; n = n1; mu = mu; r2 = r2 } in
let h2 = ST.get () in
assert (as_ctx h2 res == S.bn_field_init (as_seq h0 n));
assert (S.bn_mont_ctx_inv (as_ctx h2 res));
B.(modifies_only_not_unused_in loc_none h0 h2);
B.malloc r res 1ul
let bn_field_free #t k =
let open LowStar.BufferOps in
let k1 = !*k in
let n : buffer (limb t) = k1.n in
let r2 : buffer (limb t) = k1.r2 in
B.free n;
B.freeable_disjoint n r2;
B.free r2;
B.free k
let bn_to_field #t km k a aM =
let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
km.BM.to k1.n k1.mu k1.r2 a aM;
let h1 = ST.get () in
assert (as_seq h1 aM == S.bn_to_field (as_pctx h0 k) (as_seq h0 a))
let bn_from_field #t km k aM a =
let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
km.BM.from k1.n k1.mu aM a;
let h1 = ST.get () in
assert (as_seq h1 a == S.bn_from_field (as_pctx h0 k) (as_seq h0 aM))
let bn_field_add #t km k aM bM cM =
let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
km.BM.bn.BN.add_mod_n k1.n aM bM cM;
let h1 = ST.get () in
assert (as_seq h1 cM == S.bn_field_add (as_pctx h0 k) (as_seq h0 aM) (as_seq h0 bM))
let bn_field_sub #t km k aM bM cM =
let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
km.BM.bn.BN.sub_mod_n k1.n aM bM cM;
let h1 = ST.get () in
assert (as_seq h1 cM == S.bn_field_sub (as_pctx h0 k) (as_seq h0 aM) (as_seq h0 bM))
let bn_field_mul #t km k aM bM cM =
let open LowStar.BufferOps in
let k1 = !*k in
let h0 = ST.get () in
km.BM.mul k1.n k1.mu aM bM cM;
let h1 = ST.get () in
assert (as_seq h1 cM == S.bn_field_mul (as_pctx h0 k) (as_seq h0 aM) (as_seq h0 bM)) | false | false | Hacl.Bignum.MontArithmetic.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val bn_field_sqr: #t:limb_t -> km:BM.mont t -> bn_field_sqr_st t km.BM.bn.BN.len | [] | Hacl.Bignum.MontArithmetic.bn_field_sqr | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | km: Hacl.Bignum.Montgomery.mont t
-> Hacl.Bignum.MontArithmetic.bn_field_sqr_st t (Mkbn?.len (Mkmont?.bn km)) | {
"end_col": 71,
"end_line": 130,
"start_col": 2,
"start_line": 125
} |
Prims.Tot | val bn_field_free: #t:limb_t -> bn_field_free_st t | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.MontExponentiation",
"short_module": "ME"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Lib",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Base",
"short_module": "BB"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.Definitions",
"short_module": "BD"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontArithmetic",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontArithmetic",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "FStar.Math.Euclid",
"short_module": "Euclid"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let bn_field_free #t k =
let open LowStar.BufferOps in
let k1 = !*k in
let n : buffer (limb t) = k1.n in
let r2 : buffer (limb t) = k1.r2 in
B.free n;
B.freeable_disjoint n r2;
B.free r2;
B.free k | val bn_field_free: #t:limb_t -> bn_field_free_st t
let bn_field_free #t k = | false | null | false | let open LowStar.BufferOps in
let k1 = !*k in
let n:buffer (limb t) = k1.n in
let r2:buffer (limb t) = k1.r2 in
B.free n;
B.freeable_disjoint n r2;
B.free r2;
B.free k | {
"checked_file": "Hacl.Bignum.MontArithmetic.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Bignum.MontArithmetic.fst.checked",
"Hacl.Spec.Bignum.MontArithmetic.fst.checked",
"Hacl.Spec.Bignum.Definitions.fst.checked",
"Hacl.Bignum.Montgomery.fsti.checked",
"Hacl.Bignum.MontExponentiation.fst.checked",
"Hacl.Bignum.ModInv.fst.checked",
"Hacl.Bignum.Lib.fst.checked",
"Hacl.Bignum.Exponentiation.fsti.checked",
"Hacl.Bignum.Definitions.fst.checked",
"Hacl.Bignum.Base.fst.checked",
"Hacl.Bignum.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": true,
"source_file": "Hacl.Bignum.MontArithmetic.fst"
} | [
"total"
] | [
"Hacl.Bignum.Definitions.limb_t",
"Hacl.Bignum.MontArithmetic.pbn_mont_ctx",
"LowStar.Monotonic.Buffer.free",
"Hacl.Bignum.MontArithmetic.bn_mont_ctx",
"LowStar.Buffer.trivial_preorder",
"Prims.unit",
"Hacl.Bignum.Definitions.limb",
"LowStar.Monotonic.Buffer.freeable_disjoint",
"Lib.Buffer.buffer_t",
"Lib.Buffer.MUT",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__r2",
"Hacl.Bignum.MontArithmetic.lb",
"Hacl.Bignum.MontArithmetic.ll",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__n",
"LowStar.BufferOps.op_Bang_Star"
] | [] | module Hacl.Bignum.MontArithmetic
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum.Definitions
module B = LowStar.Buffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module S = Hacl.Spec.Bignum.MontArithmetic
module BD = Hacl.Spec.Bignum.Definitions
module BB = Hacl.Bignum.Base
module BL = Hacl.Bignum.Lib
module ME = Hacl.Bignum.MontExponentiation
module BE = Hacl.Bignum.Exponentiation
module BN = Hacl.Bignum
module BM = Hacl.Bignum.Montgomery
module BI = Hacl.Bignum.ModInv
friend Hacl.Spec.Bignum.MontArithmetic
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
let _align_fsti = ()
let bn_field_get_len #t k =
let open LowStar.BufferOps in
let k1 = !*k in
k1.len
let bn_field_check_modulus #t km n =
let m = km.BM.mont_check n in
BB.unsafe_bool_of_limb m
let bn_field_init #t len precomp_r2 r n =
let h0 = ST.get () in
let r2 : buffer (limb t) = B.mmalloc r (uint #t #SEC 0) len in
let n1 : buffer (limb t) = B.mmalloc r (uint #t #SEC 0) len in
let h1 = ST.get () in
B.(modifies_only_not_unused_in loc_none h0 h1);
assert (B.length r2 == FStar.UInt32.v len);
assert (B.length n1 == FStar.UInt32.v len);
let r2 : lbignum t len = r2 in
let n1 : lbignum t len = n1 in
copy n1 n;
let nBits = size (bits t) *! BB.unsafe_size_from_limb (BL.bn_get_top_index len n) in
precomp_r2 nBits n r2;
let mu = BM.mod_inv_limb n.(0ul) in
let res : bn_mont_ctx t = { len = len; n = n1; mu = mu; r2 = r2 } in
let h2 = ST.get () in
assert (as_ctx h2 res == S.bn_field_init (as_seq h0 n));
assert (S.bn_mont_ctx_inv (as_ctx h2 res));
B.(modifies_only_not_unused_in loc_none h0 h2);
B.malloc r res 1ul | false | false | Hacl.Bignum.MontArithmetic.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val bn_field_free: #t:limb_t -> bn_field_free_st t | [] | Hacl.Bignum.MontArithmetic.bn_field_free | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | Hacl.Bignum.MontArithmetic.bn_field_free_st t | {
"end_col": 10,
"end_line": 76,
"start_col": 2,
"start_line": 69
} |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.