file_name
stringlengths 5
52
| name
stringlengths 4
95
| original_source_type
stringlengths 0
23k
| source_type
stringlengths 9
23k
| source_definition
stringlengths 9
57.9k
| source
dict | source_range
dict | file_context
stringlengths 0
721k
| dependencies
dict | opens_and_abbrevs
listlengths 2
94
| vconfig
dict | interleaved
bool 1
class | verbose_type
stringlengths 1
7.42k
| effect
stringclasses 118
values | effect_flags
sequencelengths 0
2
| mutual_with
sequencelengths 0
11
| ideal_premises
sequencelengths 0
236
| proof_features
sequencelengths 0
1
| is_simple_lemma
bool 2
classes | is_div
bool 2
classes | is_proof
bool 2
classes | is_simply_typed
bool 2
classes | is_type
bool 2
classes | partial_definition
stringlengths 5
3.99k
| completed_definiton
stringlengths 1
1.63M
| isa_cross_project_example
bool 1
class |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
FStar.UInt16.fsti | FStar.UInt16.op_Plus_Question_Hat | val op_Plus_Question_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | let op_Plus_Question_Hat = add_underspec | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 47,
"end_line": 317,
"start_col": 7,
"start_line": 317
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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 *) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | Prims.Pure | [] | [] | [
"FStar.UInt16.add_underspec"
] | [] | false | false | false | false | false | let op_Plus_Question_Hat =
| add_underspec | false |
|
FStar.UInt16.fsti | FStar.UInt16.op_Plus_Hat | val op_Plus_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | let op_Plus_Hat = add | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 28,
"end_line": 316,
"start_col": 7,
"start_line": 316
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | Prims.Pure | [] | [] | [
"FStar.UInt16.add"
] | [] | false | false | false | false | false | let op_Plus_Hat =
| add | false |
|
FStar.UInt16.fsti | FStar.UInt16.op_Star_Hat | val op_Star_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | let op_Star_Hat = mul | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 28,
"end_line": 322,
"start_col": 7,
"start_line": 322
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | Prims.Pure | [] | [] | [
"FStar.UInt16.mul"
] | [] | false | false | false | false | false | let op_Star_Hat =
| mul | false |
|
FStar.UInt16.fsti | FStar.UInt16.op_Subtraction_Question_Hat | val op_Subtraction_Question_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | let op_Subtraction_Question_Hat = sub_underspec | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 54,
"end_line": 320,
"start_col": 7,
"start_line": 320
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | Prims.Pure | [] | [] | [
"FStar.UInt16.sub_underspec"
] | [] | false | false | false | false | false | let op_Subtraction_Question_Hat =
| sub_underspec | false |
|
FStar.UInt16.fsti | FStar.UInt16.op_Percent_Hat | val op_Percent_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t{FStar.UInt16.v b <> 0} -> Prims.Pure FStar.UInt16.t | let op_Percent_Hat = rem | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 31,
"end_line": 326,
"start_col": 7,
"start_line": 326
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt16.t -> b: FStar.UInt16.t{FStar.UInt16.v b <> 0} -> Prims.Pure FStar.UInt16.t | Prims.Pure | [] | [] | [
"FStar.UInt16.rem"
] | [] | false | false | false | false | false | let op_Percent_Hat =
| rem | false |
|
FStar.UInt16.fsti | FStar.UInt16.op_Star_Question_Hat | val op_Star_Question_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | let op_Star_Question_Hat = mul_underspec | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 47,
"end_line": 323,
"start_col": 7,
"start_line": 323
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | Prims.Pure | [] | [] | [
"FStar.UInt16.mul_underspec"
] | [] | false | false | false | false | false | let op_Star_Question_Hat =
| mul_underspec | false |
|
FStar.UInt16.fsti | FStar.UInt16.op_Amp_Hat | val op_Amp_Hat : x: FStar.UInt16.t -> y: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | let op_Amp_Hat = logand | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 30,
"end_line": 328,
"start_col": 7,
"start_line": 328
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt16.t -> y: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | Prims.Pure | [] | [] | [
"FStar.UInt16.logand"
] | [] | false | false | false | false | false | let op_Amp_Hat =
| logand | false |
|
FStar.UInt16.fsti | FStar.UInt16.op_Star_Percent_Hat | val op_Star_Percent_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | let op_Star_Percent_Hat = mul_mod | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 40,
"end_line": 324,
"start_col": 7,
"start_line": 324
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | Prims.Pure | [] | [] | [
"FStar.UInt16.mul_mod"
] | [] | false | false | false | false | false | let op_Star_Percent_Hat =
| mul_mod | false |
|
FStar.UInt16.fsti | FStar.UInt16.op_Bar_Hat | val op_Bar_Hat : x: FStar.UInt16.t -> y: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | let op_Bar_Hat = logor | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 29,
"end_line": 329,
"start_col": 7,
"start_line": 329
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt16.t -> y: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | Prims.Pure | [] | [] | [
"FStar.UInt16.logor"
] | [] | false | false | false | false | false | let op_Bar_Hat =
| logor | false |
|
FStar.UInt16.fsti | FStar.UInt16.op_Less_Less_Hat | val op_Less_Less_Hat : a: FStar.UInt16.t -> s: FStar.UInt32.t -> Prims.Pure FStar.UInt16.t | let op_Less_Less_Hat = shift_left | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 40,
"end_line": 330,
"start_col": 7,
"start_line": 330
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt16.t -> s: FStar.UInt32.t -> Prims.Pure FStar.UInt16.t | Prims.Pure | [] | [] | [
"FStar.UInt16.shift_left"
] | [] | false | false | false | false | false | let op_Less_Less_Hat =
| shift_left | false |
|
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.reification | 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) | 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) =
// 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) | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 31,
"end_line": 1595,
"start_col": 0,
"start_line": 1578
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
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) | FStar.Tactics.Effect.Tac | [] | [] | [
"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.Stubs.Reflection.V2.Data.var",
"FStar.Tactics.Util.map",
"FStar.Tactics.V2.Derived.norm_term",
"FStar.Tactics.CanonCommSemiring.steps"
] | [] | false | true | false | false | 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) =
| 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) | false |
FStar.UInt16.fsti | FStar.UInt16.op_Hat_Hat | val op_Hat_Hat : x: FStar.UInt16.t -> y: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | let op_Hat_Hat = logxor | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 30,
"end_line": 327,
"start_col": 7,
"start_line": 327
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt16.t -> y: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | Prims.Pure | [] | [] | [
"FStar.UInt16.logxor"
] | [] | false | false | false | false | false | let op_Hat_Hat =
| logxor | false |
|
FStar.UInt16.fsti | FStar.UInt16.op_Greater_Greater_Hat | val op_Greater_Greater_Hat : a: FStar.UInt16.t -> s: FStar.UInt32.t -> Prims.Pure FStar.UInt16.t | let op_Greater_Greater_Hat = shift_right | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 47,
"end_line": 331,
"start_col": 7,
"start_line": 331
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt16.t -> s: FStar.UInt32.t -> Prims.Pure FStar.UInt16.t | Prims.Pure | [] | [] | [
"FStar.UInt16.shift_right"
] | [] | false | false | false | false | false | let op_Greater_Greater_Hat =
| shift_right | false |
|
FStar.UInt16.fsti | FStar.UInt16.op_Slash_Hat | val op_Slash_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t{FStar.UInt16.v b <> 0} -> Prims.Pure FStar.UInt16.t | let op_Slash_Hat = div | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 29,
"end_line": 325,
"start_col": 7,
"start_line": 325
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt16.t -> b: FStar.UInt16.t{FStar.UInt16.v b <> 0} -> Prims.Pure FStar.UInt16.t | Prims.Pure | [] | [] | [
"FStar.UInt16.div"
] | [] | false | false | false | false | false | let op_Slash_Hat =
| div | false |
|
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.ddump | val ddump : m: Prims.string -> FStar.Tactics.Effect.Tac Prims.unit | let ddump m = if debugging () then dump m | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 41,
"end_line": 1498,
"start_col": 0,
"start_line": 1498
} | (*
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
/// | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | m: Prims.string -> FStar.Tactics.Effect.Tac Prims.unit | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.string",
"FStar.Stubs.Tactics.V2.Builtins.dump",
"Prims.unit",
"Prims.bool",
"FStar.Stubs.Tactics.V2.Builtins.debugging"
] | [] | false | true | false | false | false | let ddump m =
| if debugging () then dump m | false |
|
FStar.UInt16.fsti | FStar.UInt16.op_Greater_Equals_Hat | val op_Greater_Equals_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | let op_Greater_Equals_Hat = gte | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 38,
"end_line": 334,
"start_col": 7,
"start_line": 334
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt16.gte"
] | [] | false | false | false | true | false | let op_Greater_Equals_Hat =
| gte | false |
|
FStar.UInt16.fsti | FStar.UInt16.op_Greater_Hat | val op_Greater_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | let op_Greater_Hat = gt | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 30,
"end_line": 333,
"start_col": 7,
"start_line": 333
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt16.gt"
] | [] | false | false | false | true | false | let op_Greater_Hat =
| gt | false |
|
FStar.UInt16.fsti | FStar.UInt16.op_Less_Equals_Hat | val op_Less_Equals_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | let op_Less_Equals_Hat = lte | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 35,
"end_line": 336,
"start_col": 7,
"start_line": 336
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt16.lte"
] | [] | false | false | false | true | false | let op_Less_Equals_Hat =
| lte | false |
|
FStar.UInt16.fsti | FStar.UInt16.op_Less_Hat | val op_Less_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | let op_Less_Hat = lt | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 27,
"end_line": 335,
"start_col": 7,
"start_line": 335
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt16.lt"
] | [] | false | false | false | true | false | let op_Less_Hat =
| lt | false |
|
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.steps | val steps : Prims.list FStar.Pervasives.norm_step | 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;
]
] | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 3,
"end_line": 1574,
"start_col": 0,
"start_line": 1551
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.list FStar.Pervasives.norm_step | Prims.Tot | [
"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"
] | [] | false | false | false | true | false | let steps =
| [
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
]
] | false |
|
FStar.UInt16.fsti | FStar.UInt16.minus | val minus : a: FStar.UInt16.t -> FStar.UInt16.t | let minus (a:t) = add_mod (lognot a) (uint_to_t 1) | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 50,
"end_line": 239,
"start_col": 0,
"start_line": 239
} | (*
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 *) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false | a: FStar.UInt16.t -> FStar.UInt16.t | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt16.t",
"FStar.UInt16.add_mod",
"FStar.UInt16.lognot",
"FStar.UInt16.uint_to_t"
] | [] | false | false | false | true | false | let minus (a: t) =
| add_mod (lognot a) (uint_to_t 1) | false |
|
FStar.UInt16.fsti | FStar.UInt16.op_Equals_Hat | val op_Equals_Hat : a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | let op_Equals_Hat = eq | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 29,
"end_line": 332,
"start_col": 7,
"start_line": 332
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt16.eq"
] | [] | false | false | false | true | false | let op_Equals_Hat =
| eq | false |
|
FStar.UInt16.fsti | FStar.UInt16.n_minus_one | val n_minus_one : FStar.UInt32.t | let n_minus_one = UInt32.uint_to_t (n - 1) | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 42,
"end_line": 244,
"start_col": 0,
"start_line": 244
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false | FStar.UInt32.t | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt32.uint_to_t",
"Prims.op_Subtraction",
"FStar.UInt16.n"
] | [] | false | false | false | true | false | let n_minus_one =
| UInt32.uint_to_t (n - 1) | false |
|
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.varlist_insert_ok | 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)) | 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 | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 33,
"end_line": 1045,
"start_col": 0,
"start_line": 1043
} | (*
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) == | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
r: 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)) | FStar.Pervasives.Lemma | [
"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"
] | [] | true | false | true | false | 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 | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.canon_norm | val canon_norm: Prims.unit -> Tac unit | val canon_norm: Prims.unit -> Tac unit | let canon_norm () : Tac unit = norm steps | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 41,
"end_line": 1576,
"start_col": 0,
"start_line": 1576
} | (*
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;
]
] | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.unit -> FStar.Tactics.Effect.Tac Prims.unit | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.unit",
"FStar.Stubs.Tactics.V2.Builtins.norm",
"FStar.Tactics.CanonCommSemiring.steps"
] | [] | false | true | false | false | false | let canon_norm () : Tac unit =
| norm steps | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.canon_semiring | val canon_semiring (#a: eqtype) (r: cr a) : Tac unit | val canon_semiring (#a: eqtype) (r: cr a) : Tac unit | 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 | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 17,
"end_line": 1682,
"start_col": 0,
"start_line": 1675
} | (*
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") | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | r: FStar.Tactics.CanonCommSemiring.cr a -> FStar.Tactics.Effect.Tac Prims.unit | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.canon_semiring_aux",
"FStar.Stubs.Tactics.V2.Builtins.unquote",
"FStar.Stubs.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"
] | [] | false | true | false | false | 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 | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.varlist_merge_ok | 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]) | 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
| _ -> () | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 11,
"end_line": 611,
"start_col": 0,
"start_line": 557
} | (*
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]) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
x: FStar.Tactics.CanonCommSemiring.varlist ->
y: FStar.Tactics.CanonCommSemiring.varlist
-> FStar.Pervasives.Lemma
(ensures
FStar.Tactics.CanonCommSemiring.interp_vl r
vm
(FStar.Tactics.CanonCommSemiring.varlist_merge x y) ==
CM?.mult (CR?.cm_mult r)
(FStar.Tactics.CanonCommSemiring.interp_vl r vm x)
(FStar.Tactics.CanonCommSemiring.interp_vl r vm y)) (decreases %[x;y;0]) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [
"varlist_merge_ok",
"vm_aux_ok"
] | [
"Prims.eqtype",
"FStar.Tactics.CanonCommSemiring.cr",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Tactics.CanonCommSemiring.varlist",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Tactics.CanonCommSemiring.index",
"Prims.op_LessThan",
"FStar.Algebra.CommMonoid.__proj__CM__item__associativity",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"FStar.Tactics.CanonCommSemiring.interp_var",
"FStar.Tactics.CanonCommSemiring.interp_vl",
"Prims.unit",
"Prims._assert",
"Prims.eq2",
"FStar.Tactics.CanonCommSemiring.varlist_merge",
"FStar.Tactics.CanonCommSemiring.varlist_merge_ok",
"Prims.bool",
"FStar.Tactics.CanonCommSemiring.vm_aux_ok",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult"
] | [
"mutual recursion"
] | false | false | true | false | false | 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
(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))
else vm_aux_ok r vm v1 t1 y
| Nil_var, _ -> () | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.int_semiring | val int_semiring: Prims.unit -> Tac unit | val int_semiring: Prims.unit -> Tac unit | 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 | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 29,
"end_line": 1701,
"start_col": 0,
"start_line": 1693
} | (*
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) = () | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.unit -> FStar.Tactics.Effect.Tac Prims.unit | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.unit",
"FStar.Stubs.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"
] | [] | false | true | false | false | false | let int_semiring () : Tac unit =
| 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 | false |
Vale.SHA.SHA_helpers.fst | Vale.SHA.SHA_helpers.lemma_endian_relation | val lemma_endian_relation (quads qs: seq quad32) (input2: seq UInt8.t)
: Lemma
(requires
length qs == 4 /\ length input2 == 64 /\ qs == reverse_bytes_nat32_quad32_seq quads /\
input2 == seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes quads))
(ensures quads_to_block qs == words_of_bytes SHA2_256 #(block_word_length SHA2_256) input2) | val lemma_endian_relation (quads qs: seq quad32) (input2: seq UInt8.t)
: Lemma
(requires
length qs == 4 /\ length input2 == 64 /\ qs == reverse_bytes_nat32_quad32_seq quads /\
input2 == seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes quads))
(ensures quads_to_block qs == words_of_bytes SHA2_256 #(block_word_length SHA2_256) input2) | let lemma_endian_relation (quads qs:seq quad32) (input2:seq UInt8.t) : Lemma
(requires length qs == 4 /\ length input2 == 64 /\
qs == reverse_bytes_nat32_quad32_seq quads /\
input2 == seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes quads))
(ensures quads_to_block qs == words_of_bytes SHA2_256 #(block_word_length SHA2_256) input2)
=
let fi (i:nat{i < length (quads_to_block qs)}) : Lemma
((quads_to_block qs).[i] == (words_of_bytes SHA2_256 #(block_word_length SHA2_256) input2).[i])
=
let open Vale.Def.Words.Four_s in
let open Vale.Lib.Seqs_s in
let ni = (seq_four_to_seq_LE quads).[i] in
let b = slice input2 (4 * i) (4 * i + 4) in
// FStar.Krml.Endianness.lemma_be_to_n_is_bounded b;
calc (==) {
b;
== {}
slice input2 (4 * i) (4 * i + 4);
== {}
slice (seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes quads)) (4 * i) (4 * i + 4);
== {le_seq_quad32_to_bytes_reveal ()}
slice (seq_nat8_to_seq_uint8 (seq_nat32_to_seq_nat8_LE (seq_four_to_seq_LE quads))) (4 * i) (4 * i + 4);
equal {}
seq_nat8_to_seq_uint8 (slice (seq_nat32_to_seq_nat8_LE (seq_four_to_seq_LE quads)) (4 * i) (4 * i + 4));
== {}
seq_nat8_to_seq_uint8 (slice (seq_four_to_seq_LE (seq_map (nat_to_four 8) (seq_four_to_seq_LE quads))) (4 * i) (4 * i + 4));
== {slice_commutes_seq_four_to_seq_LE (seq_map (nat_to_four 8) (seq_four_to_seq_LE quads)) i (i + 1)}
seq_nat8_to_seq_uint8 (seq_four_to_seq_LE (slice (seq_map (nat_to_four 8) (seq_four_to_seq_LE quads)) i (i + 1)));
equal {reveal_opaque (`%seq_four_to_seq_LE) (seq_four_to_seq_LE #nat8)}
seq_nat8_to_seq_uint8 (four_to_seq_LE (nat_to_four 8 (seq_four_to_seq_LE quads).[i]));
};
let open Lib.IntTypes in
calc (==) {
(words_of_bytes SHA2_256 #(block_word_length SHA2_256) input2).[i];
== { }
(Lib.ByteSequence.uints_from_bytes_be #U32 #SEC #(block_word_length SHA2_256) input2).[i];
== { Lib.ByteSequence.index_uints_from_bytes_be #U32 #SEC #(block_word_length SHA2_256) input2 i }
Lib.ByteSequence.uint_from_bytes_be (Lib.Sequence.sub #uint8 #64 input2 (i * 4) 4);
== { let open Lib.Sequence in
calc (==) {
sub #uint8 #64 input2 (i * 4) 4;
== { }
Seq.slice input2 (4 * i) (4 * i + 4);
}
}
Lib.ByteSequence.uint_from_bytes_be #U32 #SEC b;
== { calc (==) {
Lib.ByteSequence.nat_from_bytes_be #SEC b;
(==) { }
Lib.ByteSequence.nat_from_bytes_be #SEC (seq_nat8_to_seq_uint8 (four_to_seq_LE (nat_to_four 8 ni)));
(==) { lemma_be_to_n_4 (four_to_seq_LE (nat_to_four 8 ni)) }
be_bytes_to_nat32 (four_to_seq_LE (nat_to_four 8 ni));
};
v_inj (Lib.ByteSequence.uint_from_bytes_be #U32 #SEC b)
(u32 (be_bytes_to_nat32 (four_to_seq_LE (nat_to_four 8 ni))))
}
nat32_to_word (be_bytes_to_nat32 (four_to_seq_LE (nat_to_four 8 ni)));
== {}
nat32_to_word (be_bytes_to_nat32 (reverse_seq (nat32_to_be_bytes ni)));
== {reverse_bytes_nat32_reveal ()}
nat32_to_word (reverse_bytes_nat32 ni);
== {}
nat32_to_word (reverse_bytes_nat32 (seq_four_to_seq_LE quads).[i]);
== {reveal_opaque (`%seq_four_to_seq_LE) (seq_four_to_seq_LE #nat32)}
nat32_to_word (seq_four_to_seq_LE qs).[i];
== {}
(quads_to_block qs).[i];
}
in
FStar.Classical.forall_intro fi;
assert (equal (quads_to_block qs) (words_of_bytes SHA2_256 #(block_word_length SHA2_256) input2)) | {
"file_name": "vale/code/crypto/sha/Vale.SHA.SHA_helpers.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 99,
"end_line": 870,
"start_col": 0,
"start_line": 800
} | module Vale.SHA.SHA_helpers
open FStar.Mul
open Vale.Def.Prop_s
open Vale.Def.Opaque_s
open Spec.SHA2
open Spec.SHA2.Lemmas
open Spec.Agile.Hash
open Spec.Hash.Definitions
open Spec.Hash.Lemmas
open Vale.X64.CryptoInstructions_s
open Vale.Def.Types_s
open Vale.Def.Words_s
open FStar.Seq
open FStar.UInt32 // Interop with UInt-based SHA spec
open Vale.Arch.Types
friend Spec.SHA2
friend Spec.SHA2.Lemmas
friend Vale.X64.CryptoInstructions_s
#reset-options "--max_fuel 0 --max_ifuel 0"
// Define these specific converters here, so that F* only reasons about
// the correctness of the conversion once, rather that at every call site
let vv (u:Lib.IntTypes.uint32) : nat32 = Lib.IntTypes.v u
let to_uint32 (n:nat32) : Lib.IntTypes.uint32 = Lib.IntTypes.u32 n
let word = Lib.IntTypes.uint32
let k = (Spec.SHA2.k0 SHA2_256)
let reveal_word () = ()
val add_mod_lemma:x:Lib.IntTypes.uint32 -> y:Lib.IntTypes.uint32 ->
Lemma (add_mod x y == Lib.IntTypes.(x +. y))
[SMTPat (Lib.IntTypes.(x +. y))]
let add_mod_lemma x y = ()
unfold let ws_opaque_aux = ws
let ws_opaque (b:block_w) (t:counter{t < size_k_w_256}) : nat32 =
vv (ws_opaque_aux SHA2_256 b t)
unfold let shuffle_core_opaque_aux = shuffle_core
let shuffle_core_opaque (block:block_w) (hash:hash256) (t:counter{t < size_k_w_256}):hash256 =
shuffle_core_opaque_aux SHA2_256 block hash t
[@"opaque_to_smt"] let update_multi_opaque_aux = opaque_make update_multi
irreducible let update_multi_reveal = opaque_revealer (`%update_multi_opaque_aux) update_multi_opaque_aux update_multi
let update_multi_opaque (hash:hash256) (blocks:bytes_blocks):hash256 =
(update_multi_opaque_aux SHA2_256 hash () blocks)
let update_multi_transparent (hash:hash256) (blocks:bytes_blocks) =
update_multi SHA2_256 hash () blocks
let add_mod32 (x:word) (y:nat32) : nat32 = vv (add_mod x (to_uint32 y))
let word_to_nat32 = vv
let nat32_to_word = to_uint32
let byte_to_nat8 = UInt8.v
let nat8_to_byte = UInt8.uint_to_t
let make_hash_def (abef cdgh:quad32) :
(hash:words_state SHA2_256 {
length hash == 8 /\
hash.[0] == to_uint32 abef.hi3 /\
hash.[1] == to_uint32 abef.hi2 /\
hash.[2] == to_uint32 cdgh.hi3 /\
hash.[3] == to_uint32 cdgh.hi2 /\
hash.[4] == to_uint32 abef.lo1 /\
hash.[5] == to_uint32 abef.lo0 /\
hash.[6] == to_uint32 cdgh.lo1 /\
hash.[7] == to_uint32 cdgh.lo0
} ) =
let a = to_uint32 abef.hi3 in
let b = to_uint32 abef.hi2 in
let c = to_uint32 cdgh.hi3 in
let d = to_uint32 cdgh.hi2 in
let e = to_uint32 abef.lo1 in
let f = to_uint32 abef.lo0 in
let g = to_uint32 cdgh.lo1 in
let h = to_uint32 cdgh.lo0 in
let l = [a; b; c; d; e; f; g; h] in
assert_norm (List.length l == 8);
let hash = seq_of_list l in
assert (length hash == 8);
elim_of_list l;
//assert_norm (index hash 2 == c);
hash
[@"opaque_to_smt"] let make_hash = opaque_make make_hash_def
irreducible let make_hash_reveal = opaque_revealer (`%make_hash) make_hash make_hash_def
let make_ordered_hash_def (abcd efgh:quad32) :
(hash:words_state SHA2_256 {
length hash == 8 /\
hash.[0] == to_uint32 abcd.lo0 /\
hash.[1] == to_uint32 abcd.lo1 /\
hash.[2] == to_uint32 abcd.hi2 /\
hash.[3] == to_uint32 abcd.hi3 /\
hash.[4] == to_uint32 efgh.lo0 /\
hash.[5] == to_uint32 efgh.lo1 /\
hash.[6] == to_uint32 efgh.hi2 /\
hash.[7] == to_uint32 efgh.hi3
})
=
let a = to_uint32 abcd.lo0 in
let b = to_uint32 abcd.lo1 in
let c = to_uint32 abcd.hi2 in
let d = to_uint32 abcd.hi3 in
let e = to_uint32 efgh.lo0 in
let f = to_uint32 efgh.lo1 in
let g = to_uint32 efgh.hi2 in
let h = to_uint32 efgh.hi3 in
let l = [a; b; c; d; e; f; g; h] in
assert_norm (List.length l == 8);
let hash = seq_of_list l in
assert (length hash == 8);
elim_of_list l;
hash
[@"opaque_to_smt"] let make_ordered_hash = opaque_make make_ordered_hash_def
irreducible let make_ordered_hash_reveal = opaque_revealer (`%make_ordered_hash) make_ordered_hash make_ordered_hash_def
let shuffle_core_properties (block:block_w) (hash:hash256) (t:counter{t < size_k_w_256}) :
Lemma(let h = shuffle_core_opaque block hash t in
let open Lib.IntTypes in
let a0 = hash.[0] in
let b0 = hash.[1] in
let c0 = hash.[2] in
let d0 = hash.[3] in
let e0 = hash.[4] in
let f0 = hash.[5] in
let g0 = hash.[6] in
let h0 = hash.[7] in
let t1 = h0 +. (_Sigma1 SHA2_256 e0) +. (_Ch SHA2_256 e0 f0 g0) +. (k0 SHA2_256).[t] +. (ws SHA2_256 block t) in
let t2 = (_Sigma0 SHA2_256 a0) +. (_Maj SHA2_256 a0 b0 c0) in
h.[0] == t1 +. t2 /\
h.[1] == a0 /\
h.[2] == b0 /\
h.[3] == c0 /\
h.[4] == d0 +. t1 /\
h.[5] == e0 /\
h.[6] == f0 /\
h.[7] == g0)
=
Pervasives.reveal_opaque (`%shuffle_core) shuffle_core;
let h = shuffle_core SHA2_256 block hash t in
let a0 = hash.[0] in
let b0 = hash.[1] in
let c0 = hash.[2] in
let d0 = hash.[3] in
let e0 = hash.[4] in
let f0 = hash.[5] in
let g0 = hash.[6] in
let h0 = hash.[7] in
let t1 = h0 +. (_Sigma1 SHA2_256 e0) +. (_Ch SHA2_256 e0 f0 g0) +. (k0 SHA2_256).[t] +. (ws SHA2_256 block t) in
let t2 = (_Sigma0 SHA2_256 a0) +. (_Maj SHA2_256 a0 b0 c0) in
let l = [ t1 +. t2; a0; b0; c0; d0 +. t1; e0; f0; g0 ] in
assert (h == seq_of_list l);
elim_of_list l;
()
let sha256_rnds2_spec_update_quad32 (abef cdgh:quad32) (wk:UInt32.t) : (quad32 & quad32) =
let hash0 = make_hash abef cdgh in
let a', b', c', d', e', f', g', h' = sha256_rnds2_spec_update hash0.[0] hash0.[1] hash0.[2] hash0.[3] hash0.[4] hash0.[5] hash0.[6] hash0.[7] wk in
let abef' = Mkfour (vv f') (vv e') (vv b') (vv a') in
let cdgh' = Mkfour (vv h') (vv g') (vv d') (vv c') in
(abef', cdgh')
let sha256_rnds2_spec_quad32 (src1 src2 wk:quad32) : quad32 =
let abef', cdgh' = sha256_rnds2_spec_update_quad32 src2 src1 (to_uint32 wk.lo0) in
let abef'', cdgh'' = sha256_rnds2_spec_update_quad32 abef' cdgh' (to_uint32 wk.lo1) in
abef''
let lemma_sha256_rnds2_spec_quad32 (src1 src2 wk:quad32) :
Lemma (sha256_rnds2_spec src1 src2 wk == sha256_rnds2_spec_quad32 src1 src2 wk)
=
sha256_rnds2_spec_reveal ();
()
let lemma_add_mod_commutes (x y:UInt32.t) :
Lemma (add_mod x y == add_mod y x)
=
()
(*
let lemma_add_mod32_associates (x y z:int) :
Lemma ( ((x + y % pow2_32) + z) % pow2_32 == (x + ((y + z) % pow2_32)) % pow2_32 )
=
()
*)
let lemma_add_mod_associates_U32 (x y z:UInt32.t) :
Lemma (add_mod x (add_mod y z) == add_mod (add_mod x y) z)
=
let open Lib.IntTypes in
calc (==) {
v (x +. (y +. z));
(==) { }
(v x + (v y + v z) % pow2 32) % pow2 32;
(==) { FStar.Math.Lemmas.lemma_mod_add_distr (v x) (v y + v z) (pow2 32) }
((v x + v y) + v z) % pow2 32;
(==) { FStar.Math.Lemmas.lemma_mod_add_distr (v z) (v x + v y) (pow2 32) }
((v x + v y) % pow2 32 + v z) % pow2 32;
(==) { }
v ((x +. y) +. z);
};
v_inj (x +. (y +. z)) ((x +. y) +. z)
(*
assert_norm (pow2 32 == pow2_32);
//assert (add_mod y z == to_uint32 ((vv y + vv z) % pow2_32));
//assert (add_mod x (add_mod y z) == to_uint32 ((vv x + vv (to_uint32 ((vv y + vv z) % pow2_32))) % pow2_32));
lemma_add_mod32_associates (vv x) (vv y) (vv z);
//assert (to_uint32 ((vv x + vv (to_uint32 ((vv y + vv z) % pow2_32))) % pow2_32) ==
// to_uint32 (((vv x + vv y % pow2_32) + vv z) % pow2_32));
*)
#reset-options "--max_fuel 0 --max_ifuel 0"
let lemma_add_wrap_is_add_mod (n0 n1:nat32) :
Lemma (add_wrap n0 n1 == vv (add_mod (to_uint32 n0) (to_uint32 n1)))
=
assert_norm (pow2 32 == pow2_32);
()
let add_wrap_commutes (x y:nat32) :
Lemma(add_wrap x y == add_wrap y x)
=
()
// Walk F* through the math steps required to rearrange all of the add_mods
let lemma_add_mod_a (a b c d e f g h wk:UInt32.t) :
Lemma ( let u = add_mod (_Ch SHA2_256 e f g)
(add_mod (_Sigma1 SHA2_256 e)
(add_mod wk
(add_mod h
(add_mod (_Maj SHA2_256 a b c)
(_Sigma0 SHA2_256 a))))) in
let t1 = add_mod h (add_mod (_Sigma1 SHA2_256 e) (add_mod (_Ch SHA2_256 e f g) wk)) in
let t2 = add_mod (_Sigma0 SHA2_256 a) (_Maj SHA2_256 a b c) in
let core = add_mod t1 t2 in
u == core)
=
let t1 = add_mod h (add_mod (_Sigma1 SHA2_256 e) (add_mod (_Ch SHA2_256 e f g) wk)) in
let t2 = add_mod (_Sigma0 SHA2_256 a) (_Maj SHA2_256 a b c) in
let core = add_mod t1 t2 in
lemma_add_mod_commutes (_Sigma0 SHA2_256 a) (_Maj SHA2_256 a b c);
assert (t2 == add_mod (_Maj SHA2_256 a b c) (_Sigma0 SHA2_256 a));
lemma_add_mod_commutes h (add_mod (_Sigma1 SHA2_256 e) (add_mod (_Ch SHA2_256 e f g) wk));
assert (t1 == add_mod (add_mod (_Sigma1 SHA2_256 e) (add_mod (_Ch SHA2_256 e f g) wk)) h);
lemma_add_mod_associates_U32 (add_mod (_Sigma1 SHA2_256 e) (add_mod (_Ch SHA2_256 e f g) wk)) h (add_mod (_Maj SHA2_256 a b c) (_Sigma0 SHA2_256 a));
assert (core == add_mod (add_mod (_Sigma1 SHA2_256 e) (add_mod (_Ch SHA2_256 e f g) wk)) (add_mod h (add_mod (_Maj SHA2_256 a b c) (_Sigma0 SHA2_256 a))));
lemma_add_mod_associates_U32 (_Sigma1 SHA2_256 e) (_Ch SHA2_256 e f g) wk;
// ==> add_mod (add_mod (_Sigma1 SHA2_256 e) (_Ch SHA2_256 e f g)) wk)
lemma_add_mod_commutes (_Sigma1 SHA2_256 e) (_Ch SHA2_256 e f g);
// ==> add_mod (add_mod (_Ch SHA2_256 e f g) (_Sigma1 SHA2_256 e)) wk)
lemma_add_mod_associates_U32 (add_mod (_Ch SHA2_256 e f g) (_Sigma1 SHA2_256 e)) wk (add_mod h (add_mod (_Maj SHA2_256 a b c) (_Sigma0 SHA2_256 a)));
assert (core == add_mod (add_mod (_Ch SHA2_256 e f g) (_Sigma1 SHA2_256 e)) (add_mod wk (add_mod h (add_mod (_Maj SHA2_256 a b c) (_Sigma0 SHA2_256 a)))));
lemma_add_mod_associates_U32 (_Ch SHA2_256 e f g) (_Sigma1 SHA2_256 e) (add_mod wk (add_mod h (add_mod (_Maj SHA2_256 a b c) (_Sigma0 SHA2_256 a))));
()
let lemma_add_mod_e (a b c d e f g h wk:UInt32.t) :
Lemma ( let u = add_mod (_Ch SHA2_256 e f g)
(add_mod (_Sigma1 SHA2_256 e)
(add_mod wk
(add_mod h
d))) in
let t1 = add_mod h (add_mod (_Sigma1 SHA2_256 e) (add_mod (_Ch SHA2_256 e f g) wk)) in
let core = add_mod d t1 in
u == core)
=
let t1 = add_mod h (add_mod (_Sigma1 SHA2_256 e) (add_mod (_Ch SHA2_256 e f g) wk)) in
let core = add_mod d t1 in
lemma_add_mod_commutes h (add_mod (_Sigma1 SHA2_256 e) (add_mod (_Ch SHA2_256 e f g) wk));
// t1 == add_mod (add_mod (_Sigma1 SHA2_256 e) (add_mod (_Ch SHA2_256 e f g) wk)) h);
lemma_add_mod_commutes d t1;
// core == add_mod t1 d
lemma_add_mod_associates_U32 (_Sigma1 SHA2_256 e) (_Ch SHA2_256 e f g) wk;
assert (t1 == add_mod (add_mod (add_mod (_Sigma1 SHA2_256 e) (_Ch SHA2_256 e f g)) wk) h);
lemma_add_mod_associates_U32 (add_mod (add_mod (_Sigma1 SHA2_256 e) (_Ch SHA2_256 e f g)) wk) h d;
assert (core == add_mod (add_mod (add_mod (_Sigma1 SHA2_256 e) (_Ch SHA2_256 e f g)) wk) (add_mod h d));
lemma_add_mod_associates_U32 (add_mod (_Sigma1 SHA2_256 e) (_Ch SHA2_256 e f g)) wk (add_mod h d);
assert (core == add_mod (add_mod (_Sigma1 SHA2_256 e) (_Ch SHA2_256 e f g)) (add_mod wk (add_mod h d)));
lemma_add_mod_associates_U32 (_Ch SHA2_256 e f g) (_Sigma1 SHA2_256 e) (add_mod wk (add_mod h d));
assert (core == add_mod (_Ch SHA2_256 e f g) (add_mod (_Sigma1 SHA2_256 e) (add_mod wk (add_mod h d))));
()
let lemma_sha256_rnds2_spec_update_is_shuffle_core (hash:hash256) (wk:UInt32.t) (t:counter) (block:block_w) : Lemma
(requires t < size_k_w SHA2_256 /\ wk == to_uint32 (add_mod32 (k0 SHA2_256).[t] (ws_opaque block t)))
(ensures (let a', b', c', d', e', f', g', h' =
sha256_rnds2_spec_update hash.[0] hash.[1] hash.[2] hash.[3]
hash.[4] hash.[5] hash.[6] hash.[7] wk in
let u = seq_of_list [a'; b'; c'; d'; e'; f'; g'; h'] in
let c = shuffle_core_opaque block hash t in
u == c))
=
let a', b', c', d', e', f', g', h' =
sha256_rnds2_spec_update hash.[0] hash.[1] hash.[2] hash.[3]
hash.[4] hash.[5] hash.[6] hash.[7] wk in
let l:list Lib.IntTypes.uint32 = [a'; b'; c'; d'; e'; f'; g'; h'] in
let u:Seq.seq UInt32.t = seq_of_list l in
assert_norm (List.length l == 8);
let c = shuffle_core_opaque block hash t in
Pervasives.reveal_opaque (`%shuffle_core) shuffle_core;
Pervasives.reveal_opaque (`%ws) ws;
shuffle_core_properties block hash t;
elim_of_list l;
Classical.forall_intro_3 lemma_add_mod_associates_U32;
lemma_add_mod_a hash.[0] hash.[1] hash.[2] hash.[3] hash.[4] hash.[5] hash.[6] hash.[7] wk;
lemma_add_mod_e hash.[0] hash.[1] hash.[2] hash.[3] hash.[4] hash.[5] hash.[6] hash.[7] wk
let sha256_rnds2_spec_update_core_quad32 (abef cdgh:quad32) (wk:UInt32.t) (block:block_w) (t:counter{t < size_k_w_256}) : (quad32 & quad32) =
let hash0 = make_hash abef cdgh in
let hash1 = shuffle_core_opaque block hash0 t in
let abef' = Mkfour (vv hash1.[5]) (vv hash1.[4]) (vv hash1.[1]) (vv hash1.[0]) in
let cdgh' = Mkfour (vv hash1.[7]) (vv hash1.[6]) (vv hash1.[3]) (vv hash1.[2]) in
(abef', cdgh')
let lemma_rnds_quad32 (abef cdgh:quad32) (wk:UInt32.t) (block:block_w) (t:counter{t < size_k_w_256}) : Lemma
(requires wk == to_uint32 (add_mod32 (k0 SHA2_256).[t] (ws_opaque block t)))
(ensures sha256_rnds2_spec_update_core_quad32 abef cdgh wk block t == sha256_rnds2_spec_update_quad32 abef cdgh wk)
=
let hash0 = make_hash abef cdgh in
let hash1 = shuffle_core_opaque block hash0 t in
let a', b', c', d', e', f', g', h' =
sha256_rnds2_spec_update hash0.[0] hash0.[1] hash0.[2] hash0.[3]
hash0.[4] hash0.[5] hash0.[6] hash0.[7] wk in
let l = [a'; b'; c'; d'; e'; f'; g'; h'] in
elim_of_list l;
lemma_sha256_rnds2_spec_update_is_shuffle_core hash0 wk t block;
()
#push-options "--z3rlimit 30"
let lemma_rnds2_spec_quad32_is_shuffle_core_x2 (abef cdgh:quad32) (wk0 wk1:UInt32.t) (block:block_w) (t:counter{t < size_k_w_256 - 1}) : Lemma
(requires vv wk0 == add_mod32 (k0 SHA2_256).[t] (ws_opaque block t) /\
vv wk1 == add_mod32 (k0 SHA2_256).[t+1] (ws_opaque block (t+1)))
(ensures (let hash0 = make_hash abef cdgh in
let hash1 = shuffle_core_opaque block hash0 t in
let hash2 = shuffle_core_opaque block hash1 (t + 1) in
let abef', cdgh' = sha256_rnds2_spec_update_quad32 abef cdgh wk0 in
let abef'', cdgh'' = sha256_rnds2_spec_update_quad32 abef' cdgh' wk1 in
hash2 == make_hash abef'' cdgh''))
=
let hash0 = make_hash abef cdgh in
let hash1 = shuffle_core_opaque block hash0 t in
let hash2 = shuffle_core_opaque block hash1 (t + 1) in
let abef', cdgh' = sha256_rnds2_spec_update_quad32 abef cdgh wk0 in
let abef'', cdgh'' = sha256_rnds2_spec_update_quad32 abef' cdgh' wk1 in
lemma_rnds_quad32 abef cdgh wk0 block t;
lemma_rnds_quad32 abef' cdgh' wk1 block (t+1);
assert (equal (make_hash abef' cdgh') hash1);
assert (equal (make_hash abef'' cdgh'') hash2);
()
#pop-options
let sha256_rnds2_spec_update_quad32_x2_shifts (abef cdgh:quad32) (wk0 wk1:UInt32.t) :
Lemma ((let abef', cdgh' = sha256_rnds2_spec_update_quad32 abef cdgh wk0 in
let abef'', cdgh'' = sha256_rnds2_spec_update_quad32 abef' cdgh' wk1 in
cdgh'' == abef))
=
()
let sha256_rnds2_spec_quad32_is_shuffle_core_x2 (abef cdgh wk:quad32) (block:block_w) (t:counter{t < size_k_w_256 - 1}) : Lemma
(requires wk.lo0 == add_mod32 (k0 SHA2_256).[t] (ws_opaque block t) /\
wk.lo1 == add_mod32 (k0 SHA2_256).[t+1] (ws_opaque block (t+1)))
(ensures (let hash0 = make_hash abef cdgh in
let hash1 = shuffle_core_opaque block hash0 t in
let hash2 = shuffle_core_opaque block hash1 (t + 1) in
let abef' = sha256_rnds2_spec_quad32 cdgh abef wk in
hash2 == make_hash abef' abef))
=
lemma_rnds2_spec_quad32_is_shuffle_core_x2 abef cdgh (to_uint32 wk.lo0) (to_uint32 wk.lo1) block t;
sha256_rnds2_spec_update_quad32_x2_shifts abef cdgh (to_uint32 wk.lo0) (to_uint32 wk.lo1);
()
let lemma_sha256_rnds2_two_steps (abef cdgh xmm0:quad32) (t:counter) (block:block_w) : Lemma
(requires t + 1 < size_k_w_256 /\
xmm0.lo0 == add_wrap (vv (k0 SHA2_256).[t] ) (ws_opaque block t) /\
xmm0.lo1 == add_wrap (vv (k0 SHA2_256).[t+1]) (ws_opaque block (t+1)) )
(ensures (let hash0 = make_hash abef cdgh in
let hash1 = shuffle_core_opaque block hash0 t in
let hash2 = shuffle_core_opaque block hash1 (t + 1) in
hash2 == make_hash (sha256_rnds2_spec cdgh abef xmm0) abef))
=
let hash0 = make_hash abef cdgh in
let hash1 = shuffle_core_opaque block hash0 t in
let hash2 = shuffle_core_opaque block hash1 (t + 1) in
lemma_add_wrap_is_add_mod (vv (k0 SHA2_256).[t] ) (ws_opaque block t);
lemma_add_wrap_is_add_mod (vv (k0 SHA2_256).[t+1]) (ws_opaque block (t+1));
sha256_rnds2_spec_quad32_is_shuffle_core_x2 abef cdgh xmm0 block t;
lemma_sha256_rnds2_spec_quad32 cdgh abef xmm0;
()
// Top-level proof for the SHA256_rnds2 instruction
let lemma_sha256_rnds2 (abef cdgh xmm0:quad32) (t:counter) (block:block_w) (hash_in:hash256) : Lemma
(requires t + 1 < size_k_w_256 /\
xmm0.lo0 == add_wrap (word_to_nat32 k.[t]) (ws_opaque block t) /\
xmm0.lo1 == add_wrap (word_to_nat32 k.[t+1]) (ws_opaque block (t+1)) /\
make_hash abef cdgh == Spec.Loops.repeat_range 0 t (shuffle_core_opaque block) hash_in
)
(ensures make_hash (sha256_rnds2_spec cdgh abef xmm0) abef ==
Spec.Loops.repeat_range 0 (t+2) (shuffle_core_opaque block) hash_in)
=
lemma_add_wrap_is_add_mod (vv (k0 SHA2_256).[t] ) (ws_opaque block t);
lemma_add_wrap_is_add_mod (vv (k0 SHA2_256).[t+1]) (ws_opaque block (t+1));
lemma_sha256_rnds2_two_steps abef cdgh xmm0 t block;
Spec.Loops.repeat_range_induction 0 (t + 1) (shuffle_core_opaque block) hash_in;
Spec.Loops.repeat_range_induction 0 (t + 2) (shuffle_core_opaque block) hash_in;
()
(* Proof work for the SHA256_msg* instructions *)
let _sigma0_quad32 (q:quad32) : quad32 =
Mkfour (vv (_sigma0 SHA2_256 (to_uint32 q.lo0)))
(vv (_sigma0 SHA2_256 (to_uint32 q.lo1)))
(vv (_sigma0 SHA2_256 (to_uint32 q.hi2)))
(vv (_sigma0 SHA2_256 (to_uint32 q.hi3)))
let _sigma1_quad32 (q:quad32) : quad32 =
Mkfour (vv (_sigma1 SHA2_256 (to_uint32 q.lo0)))
(vv (_sigma1 SHA2_256 (to_uint32 q.lo1)))
(vv (_sigma1 SHA2_256 (to_uint32 q.hi2)))
(vv (_sigma1 SHA2_256 (to_uint32 q.hi3)))
let ws_partial_def (t:counter) (block:block_w) : quad32 =
if 16 <= t && t < size_k_w_256 then
(let init = ws_quad32 (t-16) block in
let sigma0_in = ws_quad32 (t-15) block in
let sigma0_out = _sigma0_quad32 sigma0_in in
add_wrap_quad32 init sigma0_out)
else
Mkfour 0 0 0 0
let add_mod_quad32 (q0 q1:quad32) : quad32 =
Mkfour (vv (add_mod (to_uint32 q0.lo0) (to_uint32 q1.lo0)))
(vv (add_mod (to_uint32 q0.lo1) (to_uint32 q1.lo1)))
(vv (add_mod (to_uint32 q0.hi2) (to_uint32 q1.hi2)))
(vv (add_mod (to_uint32 q0.hi3) (to_uint32 q1.hi3)))
let lemma_add_wrap_quad32_is_add_mod_quad32 (q0 q1:quad32) :
Lemma (add_mod_quad32 q0 q1 == add_wrap_quad32 q0 q1)
=
FStar.Classical.forall_intro_2 lemma_add_wrap_is_add_mod;
()
#reset-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 30"
// Top-level proof for the SHA256_msg1 instruction
let lemma_sha256_msg1 (dst src:quad32) (t:counter) (block:block_w) : Lemma
(requires 16 <= t /\ t < size_k_w(SHA2_256) /\
dst == ws_quad32 (t-16) block /\
src.lo0 == ws_opaque block (t-12))
(ensures sha256_msg1_spec dst src == ws_partial t block)
=
sha256_msg1_spec_reveal ();
let init = ws_quad32 (t-16) block in
let sigma0_in = ws_quad32 (t-15) block in
let sigma0_out = _sigma0_quad32 sigma0_in in
lemma_add_wrap_quad32_is_add_mod_quad32 init sigma0_out;
ws_partial_reveal ();
()
#reset-options "--max_fuel 0 --max_ifuel 0"
let lemma_add_mod_ws_rearrangement (a b c d:UInt32.t) :
Lemma (let open Lib.IntTypes in
a +. b +. c +. d == d +. c +. b +. a)
=
let open Lib.IntTypes in
calc (==) {
a +. b +. c +. d;
(==) {}
(((a +. b) +. c) +. d);
(==) { lemma_add_mod_commutes ((a +. b) +. c) d;
lemma_add_mod_commutes (a +. b) c;
lemma_add_mod_commutes a b
}
d +. (c +. (b +. a));
(==) { lemma_add_mod_associates_U32 d c (b +. a);
lemma_add_mod_associates_U32 (d +. c) b a}
(((d +. c) +. b) +. a);
}
let ws_computed (b:block_w) (t:counter{t < size_k_w_256}): Tot (UInt32.t) =
if t < block_word_length SHA2_256 then to_uint32 (ws_opaque b t)
else
let t16 = to_uint32 (ws_opaque b (t - 16)) in
let t15 = to_uint32 (ws_opaque b (t - 15)) in
let t7 = to_uint32 (ws_opaque b (t - 7)) in
let t2 = to_uint32 (ws_opaque b (t - 2)) in
let s1 = _sigma1 SHA2_256 t2 in
let s0 = _sigma0 SHA2_256 t15 in
let open Lib.IntTypes in
(t16 +. s0 +. t7 +. s1)
#push-options "--max_fuel 1"
let lemma_ws_computed_is_ws (b:block_w) (t:counter{t < size_k_w_256}) :
Lemma (ws_computed b t == ws SHA2_256 b t)
=
Pervasives.reveal_opaque (`%ws) ws;
if t < block_word_length SHA2_256 then (
assert (vv (ws_computed b t) == ws_opaque b t);
assert (to_uint32 (ws_opaque b t) == ws SHA2_256 b t);
()
) else (
let t16 = to_uint32 (ws_opaque b (t - 16)) in
let t15 = to_uint32 (ws_opaque b (t - 15)) in
let t7 = to_uint32 (ws_opaque b (t - 7)) in
let t2 = to_uint32 (ws_opaque b (t - 2)) in
let s1 = _sigma1 SHA2_256 t2 in
let s0 = _sigma0 SHA2_256 t15 in
lemma_add_mod_ws_rearrangement s1 t7 s0 t16;
()
)
#pop-options
let lemma_ws_computed_is_ws_opaque (b:block_w) (t:counter{t < size_k_w_256}) :
Lemma (vv (ws_computed b t) == ws_opaque b t)
=
lemma_ws_computed_is_ws b t;
Pervasives.reveal_opaque (`%ws) ws;
()
let ws_computed_quad32 (t:counter{t < size_k_w_256 - 3}) (block:block_w) : quad32 =
Mkfour (vv (ws_computed block t))
(vv (ws_computed block (t+1)))
(vv (ws_computed block (t+2)))
(vv (ws_computed block (t+3)))
let lemma_ws_computed_is_ws_quad32 (b:block_w) (t:counter{t < size_k_w_256 - 3}) :
Lemma (ws_computed_quad32 t b == ws_quad32 t b)
=
let w = ws_computed_quad32 t b in
let w' = ws_quad32 t b in
lemma_ws_computed_is_ws_opaque b t;
lemma_ws_computed_is_ws_opaque b (t+1);
lemma_ws_computed_is_ws_opaque b (t+2);
lemma_ws_computed_is_ws_opaque b (t+3);
()
#push-options "--z3rlimit 30"
let lemma_ws_computed_quad32 (t:counter{16 <= t /\ t < size_k_w_256 - 4}) (block:block_w) :
Lemma (let t_minus_16 = ws_quad32 (t-16) block in
let t_minus_15 = ws_quad32 (t-15) block in
let t_minus_7 = ws_quad32 (t - 7) block in
let t_minus_2 = ws_quad32 (t - 2) block in
let m1 = add_mod_quad32 t_minus_16 (_sigma0_quad32 t_minus_15) in
let m2 = add_mod_quad32 m1 t_minus_7 in
let m3 = add_mod_quad32 m2 (_sigma1_quad32 t_minus_2) in
m3 == ws_computed_quad32 t block )
=
()
#pop-options
let sha256_msg1_spec_t (t:counter{t < size_k_w_256 - 1}) (block:block_w) : quad32 =
let init = ws_quad32 t block in
let next = ws_quad32 (t + 1) block in
let msg1 = add_mod_quad32 init (_sigma0_quad32 next) in
msg1
#push-options "--ifuel 1"
let lemma_sha256_msg1_spec_t_partial (t:counter) (block:block_w) : Lemma
(requires 16 <= t /\ t < size_k_w_256 - 3)
(ensures ws_partial t block == sha256_msg1_spec_t (t-16) block)
=
ws_partial_reveal ();
let init = ws_quad32 (t-16) block in
let next = ws_quad32 (t-15) block in
lemma_add_wrap_quad32_is_add_mod_quad32 init (_sigma0_quad32 next);
()
#pop-options
let lemma_sha256_msg1_spec_t (src1 src2:quad32) (t:counter) (block:block_w) : Lemma
(requires t < size_k_w_256 - 4 /\
src1 == ws_quad32 t block /\
src2.lo0 == ws_opaque block (t+4))
(ensures sha256_msg1_spec_t t block == sha256_msg1_spec src1 src2)
=
sha256_msg1_spec_reveal ();
()
#push-options "--z3rlimit 70"
let lemma_sha256_step2 (src1 src2:quad32) (t:counter) (block:block_w) : Lemma
(requires 16 <= t /\ t < size_k_w(SHA2_256) - 3 /\
src2.hi2 == ws_opaque block (t-2) /\
src2.hi3 == ws_opaque block (t-1) /\
(let w = sha256_msg1_spec_t (t-16) block in
let mid = ws_quad32 (t-7) block in
src1 == add_mod_quad32 w mid))
(ensures sha256_msg2_spec src1 src2 == ws_computed_quad32 t block)
=
sha256_msg2_spec_reveal ();
let w = sha256_msg1_spec_t (t-16) block in
let mid = ws_quad32 (t-7) block in
let final = sha256_msg2_spec src1 src2 in
lemma_ws_computed_is_ws_opaque block (t);
lemma_ws_computed_is_ws_opaque block (t+1);
()
#pop-options
// Top-level proof for the SHA256_msg2 instruction
let lemma_sha256_msg2 (src1 src2:quad32) (t:counter) (block:block_w) : Lemma
(requires 16 <= t /\ t < size_k_w(SHA2_256) - 3 /\
(let step1 = ws_partial t block in
let t_minus_7 = ws_quad32 (t-7) block in
src1 == add_wrap_quad32 step1 t_minus_7 /\
src2.hi2 == ws_opaque block (t-2) /\
src2.hi3 == ws_opaque block (t-1)))
(ensures sha256_msg2_spec src1 src2 == ws_quad32 t block)
=
let step1 = ws_partial t block in
let t_minus_7 = ws_quad32 (t-7) block in
lemma_sha256_msg1_spec_t_partial t block;
// ==> step1 == sha256_msg1_spec_t (t-16) block
lemma_add_wrap_quad32_is_add_mod_quad32 step1 t_minus_7;
lemma_sha256_step2 src1 src2 t block;
lemma_ws_computed_is_ws_quad32 block t;
()
(* Abbreviations and lemmas for the code itself *)
#reset-options "--z3rlimit 20 --max_fuel 1"
let lemma_quads_to_block qs
=
reveal_opaque (`%seq_four_to_seq_LE) (seq_four_to_seq_LE #nat32);
reveal_opaque (`%ws) ws
#reset-options "--max_fuel 0 --max_ifuel 0"
let translate_hash_update (h0 h1 h0' h1' a0 a1:quad32) : Lemma
(requires h0' == add_wrap_quad32 a0 h0 /\
h1' == add_wrap_quad32 a1 h1)
(ensures (
let h = make_hash h0 h1 in
let a = make_hash a0 a1 in
let h' = make_hash h0' h1' in
let open Lib.IntTypes in
let mapped = Spec.Loops.seq_map2 ( +. ) h a in
mapped == h'))
=
let h = make_hash h0 h1 in
let a = make_hash a0 a1 in
let h' = make_hash h0' h1' in
let open Lib.IntTypes in
let mapped = Spec.Loops.seq_map2 ( +. ) h a in
FStar.Classical.forall_intro_2 lemma_add_wrap_is_add_mod;
assert (equal mapped h');
()
unfold let shuffle_opaque = shuffle
let update_block (hash:hash256) (block:block_w): Tot (hash256) =
let hash_1 = shuffle_opaque SHA2_256 hash block in
let open Lib.IntTypes in
Spec.Loops.seq_map2 ( +. ) hash hash_1
#push-options "--z3cliopt smt.arith.nl=true" (* FIXME: Seemingly needed after fix to #2894 in F*, but should not be *)
let lemma_update_block_equiv (hash:hash256) (block:bytes{length block = block_length}) :
Lemma (update_block hash (words_of_bytes SHA2_256 #(block_word_length SHA2_256) block) == update SHA2_256 hash block)
=
Pervasives.reveal_opaque (`%Spec.SHA2.update) Spec.SHA2.update;
Pervasives.reveal_opaque (`%Spec.SHA2.shuffle) Spec.SHA2.shuffle;
assert (equal (update_block hash (words_of_bytes SHA2_256 #(block_word_length SHA2_256) block)) (update SHA2_256 hash block));
()
#pop-options
let update_lemma (src1 src2 src1' src2' h0 h1:quad32) (block:block_w) : Lemma
(requires (let hash_orig = make_hash h0 h1 in
make_hash src1 src2 ==
Spec.Loops.repeat_range 0 64 (shuffle_core_opaque block) hash_orig /\
src1' == add_wrap_quad32 src1 h0 /\
src2' == add_wrap_quad32 src2 h1))
(ensures (let hash_orig = make_hash h0 h1 in
make_hash src1' src2' == update_block hash_orig block))
=
let hash_orig = make_hash h0 h1 in
let hash_1 = shuffle_opaque SHA2_256 hash_orig block in
Pervasives.reveal_opaque (`%shuffle) shuffle;
Pervasives.reveal_opaque (`%shuffle_core) shuffle_core;
let rec f (i:nat{i <= 64}) : Lemma (
Spec.Loops.repeat_range 0 i (shuffle_core_opaque block) hash_orig ==
Spec.Loops.repeat_range 0 i (shuffle_core SHA2_256 block) hash_orig)
=
if i = 0 then (
Spec.Loops.repeat_range_base 0 (shuffle_core_opaque block) hash_orig;
Spec.Loops.repeat_range_base 0 (shuffle_core SHA2_256 block) hash_orig
) else (
f (i - 1);
Spec.Loops.repeat_range_induction 0 i (shuffle_core_opaque block) hash_orig;
Spec.Loops.repeat_range_induction 0 i (shuffle_core SHA2_256 block) hash_orig
)
in
f 64;
(*
let h = make_hash src1 src2 in
assert (forall (block:block_w) (hash:hash256) . FStar.FunctionalExtensionality.feq (shuffle_core_opaque block hash) (shuffle_core_opaque_aux SHA2_256 block hash));
//assert (forall (block:block_w) . (shuffle_core_opaque block) == (shuffle_core_opaque_aux SHA2_256 block));
assert (shuffle_core_opaque == shuffle_core_opaque_aux SHA2_256);
assert (shuffle_core_opaque == shuffle_core SHA2_256);
assert (shuffle_core_opaque block == shuffle_core SHA2_256 block);
assert (Spec.Loops.repeat_range 0 64 (shuffle_core_opaque block) hash_orig ==
Spec.Loops.repeat_range 0 64 (shuffle_core SHA2_256 block) hash_orig);
assert (make_hash src1 src2 == shuffle SHA2_256 hash_orig block);
assert (make_hash src1 src2 == shuffle_opaque SHA2_256 hash_orig block);
*)
translate_hash_update src1 src2 src1' src2' h0 h1;
shuffle_is_shuffle_pre SHA2_256 hash_orig block;
assert (equal (make_hash src1' src2') (update_block hash_orig block));
()
let lemma_le_bytes_to_seq_quad32_empty (b:seq nat8) : Lemma
(requires b == empty)
(ensures le_bytes_to_seq_quad32 b == empty)
=
reveal_opaque (`%le_bytes_to_seq_quad32) le_bytes_to_seq_quad32;
assert (equal (le_bytes_to_seq_quad32 b) empty)
let lemma_le_bytes_to_seq_quad32_length (b:seq nat8) : Lemma
(requires length b % 16 == 0)
(ensures length (le_bytes_to_seq_quad32 b) == length b / 16)
=
reveal_opaque (`%le_bytes_to_seq_quad32) le_bytes_to_seq_quad32;
()
#push-options "--max_fuel 1" // Without this, F* refuses to do even one unfolding of recursive functions :(
let lemma_slice_commutes_reverse_bytes_nat32_quad32_seq (s:seq quad32) (pivot:nat) : Lemma
(requires pivot <= length s)
(ensures slice (reverse_bytes_nat32_quad32_seq s) 0 pivot == reverse_bytes_nat32_quad32_seq (slice s 0 pivot))
=
let rs = reverse_bytes_nat32_quad32_seq s in
let srs = slice (reverse_bytes_nat32_quad32_seq s) 0 pivot in
let ss = slice s 0 pivot in
let rss = reverse_bytes_nat32_quad32_seq ss in
if pivot = 0 then (
assert (equal ss empty);
assert (equal srs empty);
assert (equal empty (reverse_bytes_nat32_quad32_seq empty));
()
) else (
assert (equal srs rss)
)
// One level of expansion that we can use in places that can't use fuel
let lemma_update_multi_quads_unfold (s:seq quad32) (hash_orig:hash256) : Lemma
(requires length s >= 4)
(ensures (let prefix, qs = split s (length s - 4) in
let h_prefix = update_multi_quads prefix hash_orig in
let hash = update_block h_prefix (quads_to_block qs) in
update_multi_quads s hash_orig == hash))
=
()
let lemma_update_multi_quads_short (s:seq quad32) (hash_orig:hash256) : Lemma
(requires length s < 4)
(ensures update_multi_quads s hash_orig == hash_orig)
=
()
let update_multi_one (h:hash256) (b:bytes_blocks {length b = block_length}) : Lemma
(ensures (update_multi SHA2_256 h () b == update SHA2_256 h b)) =
update_multi_update SHA2_256 h b
#pop-options
friend Lib.ByteSequence
#reset-options "--z3rlimit 50 --max_fuel 1 --max_ifuel 0 --z3cliopt smt.arith.nl=true"
let lemma_be_to_n_4 (s:seq4 nat8) : Lemma
(Lib.ByteSequence.nat_from_bytes_be #Lib.IntTypes.SEC (seq_nat8_to_seq_uint8 s) == be_bytes_to_nat32 s)
=
let open Lib.IntTypes in
let open Vale.Def.Words.Four_s in
assert (pow2 8 = 0x100);
assert (pow2 16 = 0x10000);
assert_norm (pow2 24 = 0x1000000);
let x = seq_nat8_to_seq_uint8 s in
let f = Lib.ByteSequence.nat_from_intseq_be_ #U8 #SEC in
calc (==) {
f x <: nat ;
== { }
FStar.UInt8.v (last x) + pow2 8 * f (slice x 0 3);
== {}
index s 3 + pow2 8 * f (slice x 0 3);
== {}
index s 3 + pow2 8 * index s 2 + pow2 16 * f (slice x 0 2);
== {}
index s 3 + pow2 8 * index s 2 + pow2 16 * index s 1 + pow2 24 * f (slice x 0 1);
== {}
index s 3 + pow2 8 * index s 2 + pow2 16 * index s 1 + pow2 24 * index s 0 + pow2 32 * f (slice x 0 0);
== {}
index s 3 + pow2 8 * index s 2 + pow2 16 * index s 1 + pow2 24 * index s 0;
== {}
four_to_nat_unfold 8 (seq_to_four_BE s);
== {reveal_opaque (`%four_to_nat) four_to_nat}
be_bytes_to_nat32 s;
}
#reset-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 40" | {
"checked_file": "/",
"dependencies": [
"Vale.X64.CryptoInstructions_s.fst.checked",
"Vale.X64.CryptoInstructions_s.fst.checked",
"Vale.Lib.Seqs_s.fst.checked",
"Vale.Lib.Seqs.fsti.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Words.Seq_s.fsti.checked",
"Vale.Def.Words.Seq.fsti.checked",
"Vale.Def.Words.Four_s.fsti.checked",
"Vale.Def.Types_s.fst.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Def.Opaque_s.fsti.checked",
"Vale.Arch.Types.fsti.checked",
"Spec.SHA2.Lemmas.fst.checked",
"Spec.SHA2.Lemmas.fst.checked",
"Spec.SHA2.fst.checked",
"Spec.SHA2.fst.checked",
"Spec.Loops.fst.checked",
"Spec.Hash.Lemmas.fsti.checked",
"Spec.Hash.Definitions.fst.checked",
"Spec.Agile.Hash.fsti.checked",
"prims.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.SHA.SHA_helpers.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.UInt32 // Interop with UInt-based SHA spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt32",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Def.Words_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Def.Types_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.CryptoInstructions_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Lemmas",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile.Hash",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.SHA2.Lemmas",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.SHA2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Def.Opaque_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Def.Prop_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Def.Words.Seq_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Def.Words_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Def.Types_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.CryptoInstructions_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Def.Opaque_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Def.Prop_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.SHA",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.SHA",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 0,
"max_fuel": 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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 40,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
quads: FStar.Seq.Base.seq Vale.Def.Types_s.quad32 ->
qs: FStar.Seq.Base.seq Vale.Def.Types_s.quad32 ->
input2: FStar.Seq.Base.seq FStar.UInt8.t
-> FStar.Pervasives.Lemma
(requires
FStar.Seq.Base.length qs == 4 /\ FStar.Seq.Base.length input2 == 64 /\
qs == Vale.Arch.Types.reverse_bytes_nat32_quad32_seq quads /\
input2 ==
Vale.Def.Words.Seq_s.seq_nat8_to_seq_uint8 (Vale.Def.Types_s.le_seq_quad32_to_bytes quads))
(ensures
Vale.SHA.SHA_helpers.quads_to_block qs ==
Spec.Hash.Definitions.words_of_bytes Spec.Hash.Definitions.SHA2_256 input2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"FStar.Seq.Base.seq",
"Vale.Def.Types_s.quad32",
"FStar.UInt8.t",
"Prims._assert",
"FStar.Seq.Base.equal",
"Vale.SHA.SHA_helpers.word",
"Vale.SHA.SHA_helpers.quads_to_block",
"Spec.Hash.Definitions.words_of_bytes",
"Spec.Hash.Definitions.SHA2_256",
"Spec.Hash.Definitions.block_word_length",
"Prims.unit",
"FStar.Classical.forall_intro",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.Seq.Base.length",
"Prims.eq2",
"FStar.UInt32.t",
"Spec.SHA2.op_String_Access",
"Spec.Hash.Definitions.word",
"Prims.l_True",
"Prims.squash",
"FStar.Seq.Base.index",
"Prims.Nil",
"FStar.Pervasives.pattern",
"FStar.Calc.calc_finish",
"Prims.Cons",
"FStar.Preorder.relation",
"FStar.Calc.calc_step",
"Vale.SHA.SHA_helpers.nat32_to_word",
"Vale.Def.Types_s.nat32",
"Vale.Def.Words.Seq_s.seq_four_to_seq_LE",
"Vale.Def.Types_s.reverse_bytes_nat32",
"Vale.Def.Types_s.be_bytes_to_nat32",
"Vale.Lib.Seqs_s.reverse_seq",
"Vale.Def.Types_s.nat8",
"Vale.Def.Types_s.nat32_to_be_bytes",
"Vale.Def.Words.Seq_s.four_to_seq_LE",
"Vale.Def.Words_s.natN",
"Prims.pow2",
"Vale.Def.Words.Four_s.nat_to_four",
"Lib.ByteSequence.uint_from_bytes_be",
"Lib.IntTypes.U32",
"Lib.IntTypes.SEC",
"Lib.Sequence.sub",
"Lib.IntTypes.uint8",
"FStar.Mul.op_Star",
"Lib.IntTypes.uint_t",
"Lib.ByteSequence.uints_from_bytes_be",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Lib.ByteSequence.index_uints_from_bytes_be",
"Lib.Sequence.lseq",
"Prims.l_and",
"Lib.Sequence.to_seq",
"FStar.Seq.Base.slice",
"Prims.op_Addition",
"Prims.l_Forall",
"Prims.l_or",
"Lib.Sequence.index",
"FStar.UInt32.v_inj",
"Lib.IntTypes.u32",
"Lib.Sequence.length",
"Lib.IntTypes.U8",
"Lib.ByteSequence.nat_from_bytes_be",
"Vale.Def.Words.Seq_s.seq_nat8_to_seq_uint8",
"Vale.SHA.SHA_helpers.lemma_be_to_n_4",
"Vale.Def.Types_s.reverse_bytes_nat32_reveal",
"FStar.Pervasives.reveal_opaque",
"Vale.Def.Words_s.four",
"Vale.Def.Words_s.nat32",
"Prims.int",
"Vale.Def.Words_s.nat8",
"Vale.Lib.Seqs_s.seq_map",
"Vale.Def.Words.Seq_s.seq_nat32_to_seq_nat8_LE",
"Vale.Def.Types_s.le_seq_quad32_to_bytes",
"Vale.Def.Types_s.le_seq_quad32_to_bytes_reveal",
"Vale.Arch.Types.slice_commutes_seq_four_to_seq_LE",
"Vale.Arch.Types.reverse_bytes_nat32_quad32_seq",
"Prims.op_Equality",
"Vale.SHA.SHA_helpers.size_block_w_256"
] | [] | false | false | true | false | false | let lemma_endian_relation (quads qs: seq quad32) (input2: seq UInt8.t)
: Lemma
(requires
length qs == 4 /\ length input2 == 64 /\ qs == reverse_bytes_nat32_quad32_seq quads /\
input2 == seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes quads))
(ensures quads_to_block qs == words_of_bytes SHA2_256 #(block_word_length SHA2_256) input2) =
| let fi (i: nat{i < length (quads_to_block qs)})
: Lemma
((quads_to_block qs).[ i ] ==
(words_of_bytes SHA2_256 #(block_word_length SHA2_256) input2).[ i ]) =
let open Vale.Def.Words.Four_s in
let open Vale.Lib.Seqs_s in
let ni = (seq_four_to_seq_LE quads).[ i ] in
let b = slice input2 (4 * i) (4 * i + 4) in
calc ( == ) {
b;
( == ) { () }
slice input2 (4 * i) (4 * i + 4);
( == ) { () }
slice (seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes quads)) (4 * i) (4 * i + 4);
( == ) { le_seq_quad32_to_bytes_reveal () }
slice (seq_nat8_to_seq_uint8 (seq_nat32_to_seq_nat8_LE (seq_four_to_seq_LE quads)))
(4 * i)
(4 * i + 4);
equal { () }
seq_nat8_to_seq_uint8 (slice (seq_nat32_to_seq_nat8_LE (seq_four_to_seq_LE quads))
(4 * i)
(4 * i + 4));
( == ) { () }
seq_nat8_to_seq_uint8 (slice (seq_four_to_seq_LE (seq_map (nat_to_four 8)
(seq_four_to_seq_LE quads)))
(4 * i)
(4 * i + 4));
( == ) { slice_commutes_seq_four_to_seq_LE (seq_map (nat_to_four 8) (seq_four_to_seq_LE quads))
i
(i + 1) }
seq_nat8_to_seq_uint8 (seq_four_to_seq_LE (slice (seq_map (nat_to_four 8)
(seq_four_to_seq_LE quads))
i
(i + 1)));
equal { reveal_opaque (`%seq_four_to_seq_LE) (seq_four_to_seq_LE #nat8) }
seq_nat8_to_seq_uint8 (four_to_seq_LE (nat_to_four 8 (seq_four_to_seq_LE quads).[ i ]));
};
let open Lib.IntTypes in
calc ( == ) {
(words_of_bytes SHA2_256 #(block_word_length SHA2_256) input2).[ i ];
( == ) { () }
(Lib.ByteSequence.uints_from_bytes_be #U32 #SEC #(block_word_length SHA2_256) input2).[ i ];
( == ) { Lib.ByteSequence.index_uints_from_bytes_be #U32
#SEC
#(block_word_length SHA2_256)
input2
i }
Lib.ByteSequence.uint_from_bytes_be (Lib.Sequence.sub #uint8 #64 input2 (i * 4) 4);
( == ) { let open Lib.Sequence in
calc ( == ) {
sub #uint8 #64 input2 (i * 4) 4;
( == ) { () }
Seq.slice input2 (4 * i) (4 * i + 4);
} }
Lib.ByteSequence.uint_from_bytes_be #U32 #SEC b;
( == ) { (calc ( == ) {
Lib.ByteSequence.nat_from_bytes_be #SEC b;
( == ) { () }
Lib.ByteSequence.nat_from_bytes_be #SEC
(seq_nat8_to_seq_uint8 (four_to_seq_LE (nat_to_four 8 ni)));
( == ) { lemma_be_to_n_4 (four_to_seq_LE (nat_to_four 8 ni)) }
be_bytes_to_nat32 (four_to_seq_LE (nat_to_four 8 ni));
};
v_inj (Lib.ByteSequence.uint_from_bytes_be #U32 #SEC b)
(u32 (be_bytes_to_nat32 (four_to_seq_LE (nat_to_four 8 ni))))) }
nat32_to_word (be_bytes_to_nat32 (four_to_seq_LE (nat_to_four 8 ni)));
( == ) { () }
nat32_to_word (be_bytes_to_nat32 (reverse_seq (nat32_to_be_bytes ni)));
( == ) { reverse_bytes_nat32_reveal () }
nat32_to_word (reverse_bytes_nat32 ni);
( == ) { () }
nat32_to_word (reverse_bytes_nat32 (seq_four_to_seq_LE quads).[ i ]);
( == ) { reveal_opaque (`%seq_four_to_seq_LE) (seq_four_to_seq_LE #nat32) }
nat32_to_word (seq_four_to_seq_LE qs).[ i ];
( == ) { () }
(quads_to_block qs).[ i ];
}
in
FStar.Classical.forall_intro fi;
assert (equal (quads_to_block qs) (words_of_bytes SHA2_256 #(block_word_length SHA2_256) input2)) | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.spolynomial_simplify_ok | 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) | 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 | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 33,
"end_line": 1356,
"start_col": 0,
"start_line": 1354
} | (*
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 -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
r: 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) | FStar.Pervasives.Lemma | [
"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"
] | [] | true | false | true | false | 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 | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.canon_semiring_aux | 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 | 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
=
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") | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 43,
"end_line": 1673,
"start_col": 0,
"start_line": 1621
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Type ->
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 | FStar.Tactics.Effect.Tac | [] | [] | [
"FStar.Tactics.NamedView.term",
"FStar.Tactics.V2.Derived.focus",
"Prims.unit",
"FStar.Stubs.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.Stubs.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.Stubs.Tactics.V2.Builtins.norm",
"FStar.Pervasives.norm_step"
] | [] | false | true | false | false | 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 [];
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") | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.canonical_sum_simplify_ok | 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) | 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 -> () | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 19,
"end_line": 1350,
"start_col": 0,
"start_line": 1344
} | (*
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 -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
r: 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) | FStar.Pervasives.Lemma | [
"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"
] | [
"recursion"
] | false | false | true | false | 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 -> () | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.reification_aux | val 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) | val 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) | 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 | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 38,
"end_line": 1545,
"start_col": 0,
"start_line": 1521
} | (*
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) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
unquotea: (_: FStar.Tactics.NamedView.term -> FStar.Tactics.Effect.Tac a) ->
ts: Prims.list FStar.Tactics.NamedView.term ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
add: FStar.Tactics.NamedView.term ->
opp: FStar.Tactics.NamedView.term ->
mone: FStar.Tactics.NamedView.term ->
mult: FStar.Tactics.NamedView.term ->
t: FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac
((FStar.Tactics.CanonCommSemiring.polynomial a * Prims.list FStar.Tactics.NamedView.term) *
FStar.Tactics.CanonCommSemiring.vmap a) | FStar.Tactics.Effect.Tac | [] | [] | [
"FStar.Tactics.NamedView.term",
"Prims.list",
"FStar.Tactics.CanonCommSemiring.vmap",
"FStar.Stubs.Reflection.Types.term",
"Prims.l_or",
"Prims.eq2",
"Prims.precedes",
"FStar.Stubs.Reflection.V2.Data.argv",
"FStar.Pervasives.Native.fst",
"FStar.Stubs.Reflection.V2.Data.aqualv",
"FStar.Stubs.Reflection.Types.fv",
"FStar.Tactics.CanonCommSemiring.Pplus",
"FStar.Pervasives.Native.tuple3",
"FStar.Tactics.CanonCommSemiring.polynomial",
"Prims.bool",
"FStar.Tactics.CanonCommSemiring.Pmult",
"FStar.Tactics.CanonCommSemiring.make_fvar",
"FStar.Tactics.CanonCommSemiring.term_eq",
"FStar.Tactics.NamedView.pack",
"FStar.Tactics.NamedView.Tv_FVar",
"FStar.Pervasives.Native.Mktuple3",
"FStar.Tactics.CanonCommSemiring.reification_aux",
"FStar.Tactics.CanonCommSemiring.Popp",
"FStar.Stubs.Reflection.V2.Data.vconst",
"FStar.Tactics.CanonCommSemiring.Pconst",
"FStar.Tactics.NamedView.named_term_view",
"FStar.Pervasives.Native.tuple2",
"FStar.Pervasives.Native.Mktuple2",
"FStar.List.Tot.Base.list_unref",
"FStar.Tactics.NamedView.inspect",
"FStar.Reflection.V2.Derived.Lemmas.collect_app_ref"
] | [
"recursion"
] | false | true | false | false | false | 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) =
| let hd, tl = collect_app_ref t in
match inspect hd, list_unref tl with
| Tv_FVar fv, [t1, _ ; t2, _] ->
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 | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.spolynomial_normalize_ok | 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) | 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 | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 35,
"end_line": 1340,
"start_col": 0,
"start_line": 1327
} | (*
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 -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
r: 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) | FStar.Pervasives.Lemma | [
"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"
] | [
"recursion"
] | false | false | true | false | 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 | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.test | val test : a: Prims.int -> Prims.unit | let test (a:int) =
let open FStar.Mul in
assert (a + - a + 2 * a + - a == -a + 2 * a) by (int_semiring ()) | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 67,
"end_line": 1707,
"start_col": 0,
"start_line": 1705
} | (*
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" | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false | a: Prims.int -> Prims.unit | Prims.Tot | [
"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"
] | [] | false | false | false | true | false | let test (a: int) =
| let open FStar.Mul in
FStar.Tactics.Effect.assert_by_tactic (a + - a + 2 * a + - a == - a + 2 * a)
(fun _ ->
();
(int_semiring ())) | false |
|
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.polynomial_normalize | val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a | val polynomial_normalize: #a:eqtype -> cr a -> polynomial a -> canonical_sum a | 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) | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 85,
"end_line": 1383,
"start_col": 0,
"start_line": 1374
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | r: FStar.Tactics.CanonCommSemiring.cr a -> p: FStar.Tactics.CanonCommSemiring.polynomial a
-> FStar.Tactics.CanonCommSemiring.canonical_sum a | Prims.Tot | [
"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"
] | [
"recursion"
] | false | false | false | false | 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) | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.make_fvar | val make_fvar (#a: Type) (t: term) (unquotea: (term -> Tac a)) (ts: list term) (vm: vmap a)
: Tac (polynomial a * list term * vmap a) | 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) =
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) | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 47,
"end_line": 1518,
"start_col": 0,
"start_line": 1511
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
t: FStar.Tactics.NamedView.term ->
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) | FStar.Tactics.Effect.Tac | [] | [] | [
"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"
] | [] | false | true | false | false | 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) | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.spolynomial_of_ok | 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)) | 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 | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 20,
"end_line": 1434,
"start_col": 0,
"start_line": 1419
} | (*
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 -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
r: 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)) | FStar.Pervasives.Lemma | [
"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"
] | [
"recursion"
] | false | false | true | false | 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 | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.spolynomial_of | val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a | val spolynomial_of: #a:eqtype -> cr a -> polynomial a -> spolynomial a | 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) | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 74,
"end_line": 1402,
"start_col": 0,
"start_line": 1396
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | r: FStar.Tactics.CanonCommSemiring.cr a -> p: FStar.Tactics.CanonCommSemiring.polynomial a
-> FStar.Tactics.CanonCommSemiring.spolynomial a | Prims.Tot | [
"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"
] | [
"recursion"
] | false | false | false | false | 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) | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.canonical_sum_prod_ok | 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)) | 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 -> () | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 19,
"end_line": 1323,
"start_col": 0,
"start_line": 1272
} | (*
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) == | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
r: 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)) | FStar.Pervasives.Lemma | [
"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"
] | [
"recursion"
] | false | false | true | false | 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 -> () | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.canonical_sum_scalar_ok | 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)) | 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 -> () | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 19,
"end_line": 1098,
"start_col": 0,
"start_line": 1052
} | (*
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) == | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
r: 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)) | FStar.Pervasives.Lemma | [
"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"
] | [
"recursion"
] | false | false | true | false | 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 ->
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 -> () | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.canonical_sum_scalar2_ok | 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)) | 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 -> () | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 19,
"end_line": 1175,
"start_col": 0,
"start_line": 1105
} | (*
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) == | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
r: 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)) | FStar.Pervasives.Lemma | [
"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"
] | [
"recursion"
] | false | false | true | false | 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 ->
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 -> () | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.int_cr | val int_cr:cr int | val int_cr:cr int | let int_cr : cr int =
CR int_plus_cm int_multiply_cm op_Minus (fun x -> ()) (fun x y z -> ()) (fun x -> ()) | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 87,
"end_line": 1688,
"start_col": 0,
"start_line": 1687
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | FStar.Tactics.CanonCommSemiring.cr Prims.int | Prims.Tot | [
"total"
] | [] | [
"FStar.Tactics.CanonCommSemiring.CR",
"Prims.int",
"FStar.Algebra.CommMonoid.int_plus_cm",
"FStar.Algebra.CommMonoid.int_multiply_cm",
"Prims.op_Minus",
"Prims.unit"
] | [] | false | false | false | true | false | let int_cr:cr int =
| CR int_plus_cm int_multiply_cm op_Minus (fun x -> ()) (fun x y z -> ()) (fun x -> ()) | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.polynomial_simplify_ok | 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) | 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;
} | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 3,
"end_line": 1490,
"start_col": 0,
"start_line": 1477
} | (*
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 -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
r: 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) | FStar.Pervasives.Lemma | [
"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"
] | [] | false | false | true | false | 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;
} | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.interp_p | val interp_p (#a: Type) (r: cr a) (vm: vmap a) (p: polynomial a) : a | 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 =
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) | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 37,
"end_line": 1414,
"start_col": 0,
"start_line": 1406
} | (*
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 *) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
p: FStar.Tactics.CanonCommSemiring.polynomial a
-> a | Prims.Tot | [
"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"
] | [
"recursion"
] | false | false | false | true | 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) | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.find_aux | val find_aux (n: nat) (x: term) (xs: list term) : Tac (option nat) | 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) =
match xs with
| [] -> None
| x'::xs' -> if term_eq x x' then Some n else find_aux (n+1) x xs' | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 68,
"end_line": 1507,
"start_col": 0,
"start_line": 1504
} | (*
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. | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | n: Prims.nat -> x: FStar.Tactics.NamedView.term -> xs: Prims.list FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac (FStar.Pervasives.Native.option Prims.nat) | FStar.Tactics.Effect.Tac | [] | [] | [
"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"
] | [
"recursion"
] | false | true | false | false | 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' | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.semiring_reflect | 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) | 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 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 | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 32,
"end_line": 1618,
"start_col": 0,
"start_line": 1609
} | (*
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] | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
r: 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) | Prims.Tot | [
"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"
] | [] | false | false | true | false | false | 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) =
| polynomial_simplify_ok r vm e1;
polynomial_simplify_ok r vm e2 | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.polynomial_normalize_ok | 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))) | 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)) | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 53,
"end_line": 1472,
"start_col": 0,
"start_line": 1440
} | (*
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) == | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
r: 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))) | FStar.Pervasives.Lemma | [
"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"
] | [
"recursion"
] | false | false | true | false | 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)) | false |
FStar.UInt16.fsti | FStar.UInt16.gte_mask | 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))) | 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: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 | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 5,
"end_line": 312,
"start_col": 0,
"start_line": 295
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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
*) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | Prims.Pure | [] | [] | [
"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"
] | [] | false | false | false | false | false | 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))) =
| 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 | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.monom_insert_ok | 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)) | 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 -> () | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 19,
"end_line": 1037,
"start_col": 0,
"start_line": 909
} | (*
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) == | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
r: 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)) | FStar.Pervasives.Lemma | [
"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"
] | [
"recursion"
] | false | false | true | false | 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 ->
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 -> () | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.canonical_sum_scalar3_ok | 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)) | 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 -> () | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 19,
"end_line": 1266,
"start_col": 0,
"start_line": 1182
} | (*
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) == | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
r: 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)) | FStar.Pervasives.Lemma | [
"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"
] | [
"recursion"
] | false | false | true | false | 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 ->
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 -> () | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.quote_polynomial | val quote_polynomial (#a: Type) (ta: term) (quotea: (a -> Tac term)) (e: polynomial a) : Tac term | 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 =
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] | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 61,
"end_line": 1606,
"start_col": 0,
"start_line": 1598
} | (*
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) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
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 | FStar.Tactics.Effect.Tac | [] | [] | [
"FStar.Tactics.NamedView.term",
"FStar.Tactics.CanonCommSemiring.polynomial",
"FStar.Reflection.V2.Derived.mk_app",
"Prims.list",
"FStar.Stubs.Reflection.V2.Data.argv",
"Prims.Cons",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Stubs.Reflection.Types.term",
"FStar.Stubs.Reflection.V2.Data.aqualv",
"FStar.Stubs.Reflection.V2.Data.Q_Implicit",
"Prims.Nil",
"FStar.Stubs.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.Stubs.Reflection.V2.Data.C_Int",
"FStar.Tactics.CanonCommSemiring.quote_polynomial"
] | [
"recursion"
] | false | true | false | false | 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] | false |
FStar.Tactics.CanonCommSemiring.fst | FStar.Tactics.CanonCommSemiring.csm_aux_ok | 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]) | 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 | {
"file_name": "ulib/FStar.Tactics.CanonCommSemiring.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 7,
"end_line": 902,
"start_col": 0,
"start_line": 667
} | (*
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]) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
r: FStar.Tactics.CanonCommSemiring.cr a ->
vm: FStar.Tactics.CanonCommSemiring.vmap a ->
c1: a ->
l1: FStar.Tactics.CanonCommSemiring.varlist ->
t1: 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.csm_aux r c1 l1 t1 s2) ==
CM?.mult (CR?.cm_add r)
(FStar.Tactics.CanonCommSemiring.interp_cs r
vm
(FStar.Tactics.CanonCommSemiring.Cons_monom c1 l1 t1))
(FStar.Tactics.CanonCommSemiring.interp_cs r vm s2)) (decreases %[t1;s2;1]) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [
"canonical_sum_merge_ok",
"csm_aux_ok"
] | [
"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.csm_aux",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"FStar.Tactics.CanonCommSemiring.interp_vl",
"FStar.Tactics.CanonCommSemiring.canonical_sum_merge",
"FStar.Tactics.CanonCommSemiring.interp_m",
"FStar.Tactics.CanonCommSemiring.ics_aux",
"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.canonical_sum_merge_ok",
"FStar.Tactics.CanonCommSemiring.distribute_right",
"FStar.Tactics.CanonCommSemiring.aplus_assoc_4",
"Prims.bool",
"FStar.Tactics.CanonCommSemiring.varlist_lt",
"FStar.Algebra.CommMonoid.__proj__CM__item__associativity",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_add",
"FStar.Tactics.CanonCommSemiring.csm_aux_ok",
"FStar.Algebra.CommMonoid.__proj__CM__item__commutativity",
"FStar.Tactics.CanonCommSemiring.Cons_monom",
"FStar.Algebra.CommMonoid.__proj__CM__item__mult",
"FStar.Tactics.CanonCommSemiring.__proj__CR__item__cm_mult",
"FStar.Algebra.CommMonoid.__proj__CM__item__unit"
] | [
"mutual recursion"
] | false | false | true | false | false | let rec 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
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);
}
else
if varlist_lt l1 l2
then
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);
}
else
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);
}
| Cons_varlist l2 t2 ->
let c2 = aone in
let s1 = Cons_monom c1 l1 t1 in
if l1 = l2
then
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);
}
else
if varlist_lt l1 l2
then
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);
}
else
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);
} | false |
FStar.UInt16.fsti | FStar.UInt16.eq_mask | 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))) | 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: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 | {
"file_name": "ulib/FStar.UInt16.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 5,
"end_line": 283,
"start_col": 0,
"start_line": 256
} | (*
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 shift value for this type, i.e. its width minus one,
as an UInt32. *)
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.
*) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | Prims.Pure | [] | [] | [
"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"
] | [] | false | false | false | false | false | 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))) =
| 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 | false |
Hacl.Bignum.MontArithmetic.fst | Hacl.Bignum.MontArithmetic.bn_field_get_len | val bn_field_get_len: #t:limb_t -> bn_field_get_len_st t | val bn_field_get_len: #t:limb_t -> bn_field_get_len_st t | let bn_field_get_len #t k =
let open LowStar.BufferOps in
let k1 = !*k in
k1.len | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 8,
"end_line": 36,
"start_col": 0,
"start_line": 33
} | 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 = () | {
"checked_file": "/",
"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"
} | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"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.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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Hacl.Bignum.MontArithmetic.bn_field_get_len_st t | Prims.Tot | [
"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"
] | [] | false | false | false | false | false | let bn_field_get_len #t k =
| let open LowStar.BufferOps in
let k1 = !*k in
k1.len | false |
Hacl.Bignum.MontArithmetic.fst | Hacl.Bignum.MontArithmetic.bn_field_check_modulus | val bn_field_check_modulus: #t:limb_t -> km:BM.mont t -> bn_field_check_modulus_st t km.BM.bn.BN.len | 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 =
let m = km.BM.mont_check n in
BB.unsafe_bool_of_limb m | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 26,
"end_line": 41,
"start_col": 0,
"start_line": 39
} | 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 | {
"checked_file": "/",
"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"
} | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"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.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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | km: Hacl.Bignum.Montgomery.mont t
-> Hacl.Bignum.MontArithmetic.bn_field_check_modulus_st t (Mkbn?.len (Mkmont?.bn km)) | Prims.Tot | [
"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"
] | [] | false | false | false | false | false | let bn_field_check_modulus #t km n =
| let m = km.BM.mont_check n in
BB.unsafe_bool_of_limb m | false |
Hacl.Bignum.MontArithmetic.fst | Hacl.Bignum.MontArithmetic.bn_field_one | val bn_field_one: #t:limb_t -> km:BM.mont t -> bn_field_one_st t km.BM.bn.BN.len | 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 =
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)) | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 58,
"end_line": 139,
"start_col": 0,
"start_line": 133
} | 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)) | {
"checked_file": "/",
"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"
} | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"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.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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | km: Hacl.Bignum.Montgomery.mont t
-> Hacl.Bignum.MontArithmetic.bn_field_one_st t (Mkbn?.len (Mkmont?.bn km)) | Prims.Tot | [
"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"
] | [] | false | false | false | false | 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)) | false |
Hacl.Bignum.MontArithmetic.fst | Hacl.Bignum.MontArithmetic.bn_field_free | val bn_field_free: #t:limb_t -> bn_field_free_st t | val bn_field_free: #t:limb_t -> bn_field_free_st t | 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 | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 10,
"end_line": 76,
"start_col": 0,
"start_line": 68
} | 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 | {
"checked_file": "/",
"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"
} | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"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.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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Hacl.Bignum.MontArithmetic.bn_field_free_st t | Prims.Tot | [
"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"
] | [] | false | false | false | false | 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 | false |
Hacl.Bignum.MontArithmetic.fst | Hacl.Bignum.MontArithmetic.bn_to_field | val bn_to_field: #t:limb_t -> km:BM.mont t -> bn_to_field_st t km.BM.bn.BN.len | 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 =
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)) | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 69,
"end_line": 85,
"start_col": 0,
"start_line": 79
} | 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 | {
"checked_file": "/",
"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"
} | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"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.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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | km: Hacl.Bignum.Montgomery.mont t
-> Hacl.Bignum.MontArithmetic.bn_to_field_st t (Mkbn?.len (Mkmont?.bn km)) | Prims.Tot | [
"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"
] | [] | false | false | false | false | 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)) | false |
Hacl.Bignum.MontArithmetic.fst | Hacl.Bignum.MontArithmetic.bn_field_add | val bn_field_add: #t:limb_t -> km:BM.mont t -> bn_field_add_st t km.BM.bn.BN.len | val bn_field_add: #t:limb_t -> km:BM.mont t -> bn_field_add_st t km.BM.bn.BN.len | 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)) | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 86,
"end_line": 103,
"start_col": 0,
"start_line": 97
} | 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)) | {
"checked_file": "/",
"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"
} | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"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.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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | km: Hacl.Bignum.Montgomery.mont t
-> Hacl.Bignum.MontArithmetic.bn_field_add_st t (Mkbn?.len (Mkmont?.bn km)) | Prims.Tot | [
"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_add",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Hacl.Bignum.__proj__Mkbn__item__add_mod_n",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__n",
"Hacl.Bignum.MontArithmetic.lb",
"Hacl.Bignum.MontArithmetic.ll",
"Hacl.Bignum.MontArithmetic.bn_mont_ctx",
"LowStar.BufferOps.op_Bang_Star",
"LowStar.Buffer.trivial_preorder"
] | [] | false | false | false | false | false | 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)) | false |
Hacl.Bignum.MontArithmetic.fst | Hacl.Bignum.MontArithmetic.bn_field_mul | val bn_field_mul: #t:limb_t -> km:BM.mont t -> bn_field_mul_st t km.BM.bn.BN.len | val bn_field_mul: #t:limb_t -> km:BM.mont t -> bn_field_mul_st t km.BM.bn.BN.len | 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)) | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 86,
"end_line": 121,
"start_col": 0,
"start_line": 115
} | 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)) | {
"checked_file": "/",
"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"
} | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"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.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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | km: Hacl.Bignum.Montgomery.mont t
-> Hacl.Bignum.MontArithmetic.bn_field_mul_st t (Mkbn?.len (Mkmont?.bn km)) | Prims.Tot | [
"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_mul",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Hacl.Bignum.Montgomery.__proj__Mkmont__item__mul",
"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"
] | [] | false | false | false | false | false | 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 |
Hacl.Bignum.MontArithmetic.fst | Hacl.Bignum.MontArithmetic.bn_field_sqr | val bn_field_sqr: #t:limb_t -> km:BM.mont t -> bn_field_sqr_st t km.BM.bn.BN.len | 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 =
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)) | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 71,
"end_line": 130,
"start_col": 0,
"start_line": 124
} | 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)) | {
"checked_file": "/",
"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"
} | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"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.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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | km: Hacl.Bignum.Montgomery.mont t
-> Hacl.Bignum.MontArithmetic.bn_field_sqr_st t (Mkbn?.len (Mkmont?.bn km)) | Prims.Tot | [
"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"
] | [] | false | false | false | false | 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)) | false |
Hacl.Bignum.MontArithmetic.fst | Hacl.Bignum.MontArithmetic.bn_from_field | val bn_from_field: #t:limb_t -> km:BM.mont t -> bn_from_field_st t km.BM.bn.BN.len | 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 =
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)) | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 71,
"end_line": 94,
"start_col": 0,
"start_line": 88
} | 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)) | {
"checked_file": "/",
"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"
} | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"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.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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | km: Hacl.Bignum.Montgomery.mont t
-> Hacl.Bignum.MontArithmetic.bn_from_field_st t (Mkbn?.len (Mkmont?.bn km)) | Prims.Tot | [
"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"
] | [] | false | false | false | false | 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)) | false |
FStar.Stubs.Reflection.V2.Data.fsti | FStar.Stubs.Reflection.V2.Data.ppname_t | val ppname_t : Type0 | let ppname_t = FStar.Sealed.Inhabited.sealed "" | {
"file_name": "ulib/FStar.Stubs.Reflection.V2.Data.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 47,
"end_line": 23,
"start_col": 0,
"start_line": 23
} | (*
Copyright 2008-2018 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES 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.Stubs.Reflection.V2.Data
include FStar.Stubs.Syntax.Syntax
open FStar.Stubs.Reflection.Types
(* The type of a string observable only with a tactic. | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Stubs.Syntax.Syntax.fsti.checked",
"FStar.Stubs.Reflection.Types.fsti.checked",
"FStar.Sealed.Inhabited.fst.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.Stubs.Reflection.V2.Data.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Stubs.Reflection.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Stubs.Syntax.Syntax",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Stubs.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Stubs.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"FStar.Sealed.Inhabited.sealed",
"Prims.string"
] | [] | false | false | false | true | true | let ppname_t =
| FStar.Sealed.Inhabited.sealed "" | false |
|
Hacl.Bignum.MontArithmetic.fst | Hacl.Bignum.MontArithmetic.bn_field_sub | val bn_field_sub: #t:limb_t -> km:BM.mont t -> bn_field_sub_st t km.BM.bn.BN.len | val bn_field_sub: #t:limb_t -> km:BM.mont t -> bn_field_sub_st t km.BM.bn.BN.len | 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)) | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 86,
"end_line": 112,
"start_col": 0,
"start_line": 106
} | 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)) | {
"checked_file": "/",
"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"
} | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"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.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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | km: Hacl.Bignum.Montgomery.mont t
-> Hacl.Bignum.MontArithmetic.bn_field_sub_st t (Mkbn?.len (Mkmont?.bn km)) | Prims.Tot | [
"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_sub",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Hacl.Bignum.__proj__Mkbn__item__sub_mod_n",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__n",
"Hacl.Bignum.MontArithmetic.lb",
"Hacl.Bignum.MontArithmetic.ll",
"Hacl.Bignum.MontArithmetic.bn_mont_ctx",
"LowStar.BufferOps.op_Bang_Star",
"LowStar.Buffer.trivial_preorder"
] | [] | false | false | false | false | false | 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)) | false |
FStar.Stubs.Reflection.V2.Data.fsti | FStar.Stubs.Reflection.V2.Data.notAscription | val notAscription (tv: term_view) : bool | val notAscription (tv: term_view) : bool | let notAscription (tv:term_view) : bool =
not (Tv_AscribedT? tv) && not (Tv_AscribedC? tv) | {
"file_name": "ulib/FStar.Stubs.Reflection.V2.Data.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 50,
"end_line": 158,
"start_col": 0,
"start_line": 157
} | (*
Copyright 2008-2018 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES 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.Stubs.Reflection.V2.Data
include FStar.Stubs.Syntax.Syntax
open FStar.Stubs.Reflection.Types
(* The type of a string observable only with a tactic.
All values of type ppname_t are provably equal *)
let ppname_t = FStar.Sealed.Inhabited.sealed ""
let as_ppname (x:string) : ppname_t = FStar.Sealed.Inhabited.seal x
noeq
type vconst =
| C_Unit : vconst
| C_Int : int -> vconst // Not exposing the full details of our integer repr.
| C_True : vconst
| C_False : vconst
| C_String : string -> vconst
| C_Range : range -> vconst
| C_Reify : vconst
| C_Reflect : name -> vconst
(* TODO: complete *)
type universes = list universe
type ident_view = string & range
noeq
type pattern =
// A built-in constant
| Pat_Constant :
c : vconst ->
pattern
// A fully applied constructor, each boolean marks whether the
// argument was an explicitly-provided implicit argument
| Pat_Cons :
head : fv ->
univs : option universes ->
subpats : list (pattern * bool) ->
pattern
// A pattern-bound variable. It has a sealed sort in it.
// This sort is ignored by the typechecker, but may be useful
// for metaprogram to look at heuristically. There is nothing
// else here but a ppname, the variable is referred to by its DB index.
// This means all Pat_Var are provably equal.
| Pat_Var :
sort : sealed term ->
ppname : ppname_t ->
pattern
// Dot pattern: resolved by other elements in the pattern and type
| Pat_Dot_Term :
t : option term ->
pattern
type branch = pattern * term // | pattern -> term
noeq
type aqualv =
| Q_Implicit
| Q_Explicit
| Q_Meta of term
type argv = term * aqualv
(* A named variable, with a unique identifier *)
noeq
type namedv_view = {
uniq : nat;
sort : sealed typ; // REMOVE?
ppname : ppname_t;
}
(* A bound variable, with a de Bruijn index *)
noeq
type bv_view = {
index : nat;
sort : sealed typ; // REMOVE?
ppname : ppname_t;
}
(* Binders consist of a type, qualifiers, and attributes. There is also
a sealed name. *)
noeq
type binder_view = {
sort : typ;
qual : aqualv;
attrs : list term;
ppname : ppname_t;
}
(* A binding is a variable in the environment. It is like a namedv, but has
an explicit (unsealed) sort *)
noeq
type binding = {
uniq : nat;
sort : typ;
ppname : ppname_t;
}
type bindings = list binding
(** We use the binder type for letbindings and refinements,
but no qualifiers nor attributes can appear there. We call these
binders simple. This module assumes an abstract predicate
for them, which is later assumed to be equivalent to being a binder
without qualifiers nor attributes (once inspect_binder is in scope). *)
val binder_is_simple : binder -> Tot bool
type simple_binder = b:binder{binder_is_simple b}
noeq
type universe_view =
| Uv_Zero : universe_view
| Uv_Succ : universe -> universe_view
| Uv_Max : universes -> universe_view
| Uv_BVar : nat -> universe_view
| Uv_Name : univ_name -> universe_view
| Uv_Unif : universe_uvar -> universe_view
| Uv_Unk : universe_view
noeq
type term_view =
| Tv_Var : v:namedv -> term_view
| Tv_BVar : v:bv -> term_view
| Tv_FVar : v:fv -> term_view
| Tv_UInst : v:fv -> us:universes -> term_view
| Tv_App : hd:term -> a:argv -> term_view
| Tv_Abs : bv:binder -> body:term -> term_view
| Tv_Arrow : bv:binder -> c:comp -> term_view
| Tv_Type : universe -> term_view
| Tv_Refine : b:simple_binder -> ref:term -> term_view
| Tv_Const : vconst -> term_view
| Tv_Uvar : nat -> ctx_uvar_and_subst -> term_view
| Tv_Let : recf:bool -> attrs:(list term) -> b:simple_binder -> def:term -> body:term -> term_view
| Tv_Match : scrutinee:term -> ret:option match_returns_ascription -> brs:(list branch) -> term_view
| Tv_AscribedT : e:term -> t:term -> tac:option term -> use_eq:bool -> term_view
| Tv_AscribedC : e:term -> c:comp -> tac:option term -> use_eq:bool -> term_view
| Tv_Unknown : term_view // An underscore: _
| Tv_Unsupp : term_view // failed to inspect, not supported | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Stubs.Syntax.Syntax.fsti.checked",
"FStar.Stubs.Reflection.Types.fsti.checked",
"FStar.Sealed.Inhabited.fst.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.Stubs.Reflection.V2.Data.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Stubs.Reflection.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Stubs.Syntax.Syntax",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Stubs.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Stubs.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | tv: FStar.Stubs.Reflection.V2.Data.term_view -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"FStar.Stubs.Reflection.V2.Data.term_view",
"Prims.op_AmpAmp",
"Prims.op_Negation",
"FStar.Stubs.Reflection.V2.Data.uu___is_Tv_AscribedT",
"FStar.Stubs.Reflection.V2.Data.uu___is_Tv_AscribedC",
"Prims.bool"
] | [] | false | false | false | true | false | let notAscription (tv: term_view) : bool =
| not (Tv_AscribedT? tv) && not (Tv_AscribedC? tv) | false |
FStar.Stubs.Reflection.V2.Data.fsti | FStar.Stubs.Reflection.V2.Data.as_ppname | val as_ppname (x: string) : ppname_t | val as_ppname (x: string) : ppname_t | let as_ppname (x:string) : ppname_t = FStar.Sealed.Inhabited.seal x | {
"file_name": "ulib/FStar.Stubs.Reflection.V2.Data.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 67,
"end_line": 24,
"start_col": 0,
"start_line": 24
} | (*
Copyright 2008-2018 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES 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.Stubs.Reflection.V2.Data
include FStar.Stubs.Syntax.Syntax
open FStar.Stubs.Reflection.Types
(* The type of a string observable only with a tactic.
All values of type ppname_t are provably equal *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Stubs.Syntax.Syntax.fsti.checked",
"FStar.Stubs.Reflection.Types.fsti.checked",
"FStar.Sealed.Inhabited.fst.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.Stubs.Reflection.V2.Data.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Stubs.Reflection.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Stubs.Syntax.Syntax",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Stubs.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Stubs.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Prims.string -> FStar.Stubs.Reflection.V2.Data.ppname_t | Prims.Tot | [
"total"
] | [] | [
"Prims.string",
"FStar.Sealed.Inhabited.seal",
"FStar.Stubs.Reflection.V2.Data.ppname_t"
] | [] | false | false | false | true | false | let as_ppname (x: string) : ppname_t =
| FStar.Sealed.Inhabited.seal x | false |
FStar.Stubs.Reflection.V2.Data.fsti | FStar.Stubs.Reflection.V2.Data.var | val var:eqtype | val var:eqtype | let var : eqtype = nat | {
"file_name": "ulib/FStar.Stubs.Reflection.V2.Data.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 22,
"end_line": 241,
"start_col": 0,
"start_line": 241
} | (*
Copyright 2008-2018 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES 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.Stubs.Reflection.V2.Data
include FStar.Stubs.Syntax.Syntax
open FStar.Stubs.Reflection.Types
(* The type of a string observable only with a tactic.
All values of type ppname_t are provably equal *)
let ppname_t = FStar.Sealed.Inhabited.sealed ""
let as_ppname (x:string) : ppname_t = FStar.Sealed.Inhabited.seal x
noeq
type vconst =
| C_Unit : vconst
| C_Int : int -> vconst // Not exposing the full details of our integer repr.
| C_True : vconst
| C_False : vconst
| C_String : string -> vconst
| C_Range : range -> vconst
| C_Reify : vconst
| C_Reflect : name -> vconst
(* TODO: complete *)
type universes = list universe
type ident_view = string & range
noeq
type pattern =
// A built-in constant
| Pat_Constant :
c : vconst ->
pattern
// A fully applied constructor, each boolean marks whether the
// argument was an explicitly-provided implicit argument
| Pat_Cons :
head : fv ->
univs : option universes ->
subpats : list (pattern * bool) ->
pattern
// A pattern-bound variable. It has a sealed sort in it.
// This sort is ignored by the typechecker, but may be useful
// for metaprogram to look at heuristically. There is nothing
// else here but a ppname, the variable is referred to by its DB index.
// This means all Pat_Var are provably equal.
| Pat_Var :
sort : sealed term ->
ppname : ppname_t ->
pattern
// Dot pattern: resolved by other elements in the pattern and type
| Pat_Dot_Term :
t : option term ->
pattern
type branch = pattern * term // | pattern -> term
noeq
type aqualv =
| Q_Implicit
| Q_Explicit
| Q_Meta of term
type argv = term * aqualv
(* A named variable, with a unique identifier *)
noeq
type namedv_view = {
uniq : nat;
sort : sealed typ; // REMOVE?
ppname : ppname_t;
}
(* A bound variable, with a de Bruijn index *)
noeq
type bv_view = {
index : nat;
sort : sealed typ; // REMOVE?
ppname : ppname_t;
}
(* Binders consist of a type, qualifiers, and attributes. There is also
a sealed name. *)
noeq
type binder_view = {
sort : typ;
qual : aqualv;
attrs : list term;
ppname : ppname_t;
}
(* A binding is a variable in the environment. It is like a namedv, but has
an explicit (unsealed) sort *)
noeq
type binding = {
uniq : nat;
sort : typ;
ppname : ppname_t;
}
type bindings = list binding
(** We use the binder type for letbindings and refinements,
but no qualifiers nor attributes can appear there. We call these
binders simple. This module assumes an abstract predicate
for them, which is later assumed to be equivalent to being a binder
without qualifiers nor attributes (once inspect_binder is in scope). *)
val binder_is_simple : binder -> Tot bool
type simple_binder = b:binder{binder_is_simple b}
noeq
type universe_view =
| Uv_Zero : universe_view
| Uv_Succ : universe -> universe_view
| Uv_Max : universes -> universe_view
| Uv_BVar : nat -> universe_view
| Uv_Name : univ_name -> universe_view
| Uv_Unif : universe_uvar -> universe_view
| Uv_Unk : universe_view
noeq
type term_view =
| Tv_Var : v:namedv -> term_view
| Tv_BVar : v:bv -> term_view
| Tv_FVar : v:fv -> term_view
| Tv_UInst : v:fv -> us:universes -> term_view
| Tv_App : hd:term -> a:argv -> term_view
| Tv_Abs : bv:binder -> body:term -> term_view
| Tv_Arrow : bv:binder -> c:comp -> term_view
| Tv_Type : universe -> term_view
| Tv_Refine : b:simple_binder -> ref:term -> term_view
| Tv_Const : vconst -> term_view
| Tv_Uvar : nat -> ctx_uvar_and_subst -> term_view
| Tv_Let : recf:bool -> attrs:(list term) -> b:simple_binder -> def:term -> body:term -> term_view
| Tv_Match : scrutinee:term -> ret:option match_returns_ascription -> brs:(list branch) -> term_view
| Tv_AscribedT : e:term -> t:term -> tac:option term -> use_eq:bool -> term_view
| Tv_AscribedC : e:term -> c:comp -> tac:option term -> use_eq:bool -> term_view
| Tv_Unknown : term_view // An underscore: _
| Tv_Unsupp : term_view // failed to inspect, not supported
let notAscription (tv:term_view) : bool =
not (Tv_AscribedT? tv) && not (Tv_AscribedC? tv)
// Very basic for now
noeq
type comp_view =
| C_Total : ret:typ -> comp_view
| C_GTotal : ret:typ -> comp_view
| C_Lemma : term -> term -> term -> comp_view // pre, post, patterns
| C_Eff : us:universes ->
eff_name:name ->
result:term ->
eff_args:(list argv) ->
decrs:list term ->
comp_view
(* Constructor for an inductive type. See explanation in
[Sg_Inductive] below. *)
type ctor = name & typ
noeq
type lb_view = {
lb_fv : fv;
lb_us : list univ_name;
lb_typ : typ;
lb_def : term
}
noeq
type sigelt_view =
| Sg_Let :
(r:bool) ->
(lbs:list letbinding) ->
sigelt_view
// Sg_Inductive basically coalesces the Sig_bundle used internally,
// where the type definition and its constructors are split.
// While that might be better for typechecking, this is probably better for metaprogrammers
// (no mutually defined types for now)
| Sg_Inductive :
(nm:name) -> // name of the inductive type being defined
(univs:list univ_name) -> // universe variables
(params:binders) -> // parameters
(typ:typ) -> // the type annotation for the inductive, i.e., indices -> Type #u
(cts:list ctor) -> // the constructors, opened with univs and applied to params already
sigelt_view
| Sg_Val :
(nm:name) ->
(univs:list univ_name) ->
(typ:typ) ->
sigelt_view
| Unk
(* Qualifiers for sigelts, see src/FStar.Syntax.Syntax for an explanation. *)
noeq
type qualifier =
| Assumption
| InternalAssumption
| New
| Private
| Unfold_for_unification_and_vcgen
| Visible_default
| Irreducible
| Inline_for_extraction
| NoExtract
| Noeq
| Unopteq
| TotalEffect
| Logic
| Reifiable
| Reflectable of name
| Discriminator of name
| Projector of name * ident
| RecordType of list ident * list ident
| RecordConstructor of list ident * list ident
| Action of name
| ExceptionConstructor
| HasMaskedEffect
| Effect
| OnlyName | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Stubs.Syntax.Syntax.fsti.checked",
"FStar.Stubs.Reflection.Types.fsti.checked",
"FStar.Sealed.Inhabited.fst.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.Stubs.Reflection.V2.Data.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Stubs.Reflection.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Stubs.Syntax.Syntax",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Stubs.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Stubs.Reflection.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.eqtype | Prims.Tot | [
"total"
] | [] | [
"Prims.nat"
] | [] | false | false | false | true | false | let var:eqtype =
| nat | false |
Hacl.Bignum.MontArithmetic.fst | Hacl.Bignum.MontArithmetic.bn_field_init | val bn_field_init:
#t:limb_t
-> len:BN.meta_len t
-> precomp_r2:BM.bn_precomp_r2_mod_n_st t len ->
bn_field_init_st t len | val bn_field_init:
#t:limb_t
-> len:BN.meta_len t
-> precomp_r2:BM.bn_precomp_r2_mod_n_st t len ->
bn_field_init_st t len | 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 | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 20,
"end_line": 65,
"start_col": 0,
"start_line": 44
} | 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 | {
"checked_file": "/",
"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"
} | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"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.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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | len: Hacl.Bignum.meta_len t -> precomp_r2: Hacl.Bignum.Montgomery.bn_precomp_r2_mod_n_st t len
-> Hacl.Bignum.MontArithmetic.bn_field_init_st t len | Prims.Tot | [
"total"
] | [] | [
"Hacl.Bignum.Definitions.limb_t",
"Hacl.Bignum.meta_len",
"Hacl.Bignum.Montgomery.bn_precomp_r2_mod_n_st",
"FStar.Monotonic.HyperHeap.rid",
"Hacl.Bignum.Definitions.lbignum",
"LowStar.Buffer.malloc",
"Hacl.Bignum.MontArithmetic.bn_mont_ctx",
"FStar.UInt32.__uint_to_t",
"LowStar.Monotonic.Buffer.mbuffer",
"LowStar.Buffer.trivial_preorder",
"Prims.l_and",
"Prims.eq2",
"Prims.nat",
"LowStar.Monotonic.Buffer.length",
"FStar.UInt32.v",
"Prims.b2t",
"Prims.op_Negation",
"LowStar.Monotonic.Buffer.g_is_null",
"LowStar.Monotonic.Buffer.frameOf",
"LowStar.Monotonic.Buffer.freeable",
"Prims.unit",
"LowStar.Monotonic.Buffer.modifies_only_not_unused_in",
"LowStar.Monotonic.Buffer.loc_none",
"Hacl.Bignum.MontArithmetic.pbn_mont_ctx",
"Prims._assert",
"Hacl.Spec.Bignum.MontArithmetic.bn_mont_ctx_inv",
"Hacl.Bignum.MontArithmetic.as_ctx",
"Hacl.Spec.Bignum.MontArithmetic.bn_mont_ctx",
"Hacl.Spec.Bignum.MontArithmetic.bn_field_init",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.Buffer.as_seq",
"Lib.Buffer.MUT",
"Hacl.Bignum.Definitions.limb",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Hacl.Bignum.MontArithmetic.Mkbn_mont_ctx'",
"Hacl.Bignum.MontArithmetic.lb",
"Hacl.Bignum.MontArithmetic.ll",
"Hacl.Bignum.ModInvLimb.mod_inv_limb",
"Lib.Buffer.op_Array_Access",
"Lib.IntTypes.int_t",
"Lib.IntTypes.op_Star_Bang",
"Lib.IntTypes.size",
"Lib.IntTypes.bits",
"Lib.IntTypes.range",
"Prims.op_Multiply",
"Lib.IntTypes.mk_int",
"Hacl.Spec.Bignum.Base.unsafe_size_from_limb",
"Hacl.Spec.Bignum.Definitions.limb",
"Prims.op_LessThanOrEqual",
"Lib.IntTypes.SEC",
"Prims.op_Subtraction",
"Prims.pow2",
"Hacl.Bignum.Lib.bn_get_top_index",
"Lib.Buffer.copy",
"Prims.int",
"Prims.l_or",
"FStar.UInt.size",
"FStar.UInt32.n",
"Prims.op_GreaterThanOrEqual",
"Lib.Buffer.buffer_t",
"LowStar.Monotonic.Buffer.mmalloc",
"Lib.IntTypes.uint",
"LowStar.Monotonic.Buffer.lmbuffer"
] | [] | false | false | false | false | false | 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
(let open B in 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));
(let open B in modifies_only_not_unused_in loc_none h0 h2);
B.malloc r res 1ul | false |
Hacl.Bignum.MontArithmetic.fst | Hacl.Bignum.MontArithmetic.bn_field_exp_consttime | val bn_field_exp_consttime: #t:limb_t -> km:BM.mont t -> bn_field_exp_consttime_st t km.BM.bn.BN.len | val bn_field_exp_consttime: #t:limb_t -> km:BM.mont t -> bn_field_exp_consttime_st t km.BM.bn.BN.len | let bn_field_exp_consttime #t km k aM bBits b resM =
let open LowStar.BufferOps in
let k1 = !*k in
push_frame ();
let aMc = create k1.len (uint #t #SEC 0) in
copy aMc aM;
ME.bn_exp_mont_consttime #t km k1.n k1.mu k1.r2 aMc bBits b resM;
let h0 = ST.get () in
assert (bn_v h0 aM < bn_v_n h0 k);
BD.bn_eval_inj (v k1.len)
(S.bn_field_exp_consttime (as_pctx h0 k) (as_seq h0 aM) (v bBits) (as_seq h0 b))
(as_seq h0 resM);
pop_frame () | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 14,
"end_line": 154,
"start_col": 0,
"start_line": 142
} | 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))
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)) | {
"checked_file": "/",
"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"
} | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"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.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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | km: Hacl.Bignum.Montgomery.mont t
-> Hacl.Bignum.MontArithmetic.bn_field_exp_consttime_st t (Mkbn?.len (Mkmont?.bn km)) | Prims.Tot | [
"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",
"Lib.IntTypes.size_t",
"Hacl.Bignum.Definitions.blocks0",
"Lib.IntTypes.size",
"Lib.IntTypes.bits",
"FStar.HyperStack.ST.pop_frame",
"Prims.unit",
"Hacl.Spec.Bignum.Definitions.bn_eval_inj",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__len",
"Hacl.Bignum.MontArithmetic.lb",
"Hacl.Bignum.MontArithmetic.ll",
"Hacl.Spec.Bignum.MontArithmetic.bn_field_exp_consttime",
"Hacl.Bignum.MontArithmetic.as_pctx",
"Lib.Buffer.as_seq",
"Lib.Buffer.MUT",
"Hacl.Bignum.Definitions.limb",
"Prims._assert",
"Prims.b2t",
"Prims.op_LessThan",
"Hacl.Bignum.Definitions.bn_v",
"Hacl.Bignum.MontArithmetic.bn_v_n",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Hacl.Bignum.MontExponentiation.bn_exp_mont_consttime",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__n",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__mu",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__r2",
"Lib.Buffer.copy",
"Lib.Buffer.lbuffer_t",
"Lib.Buffer.create",
"Lib.IntTypes.uint",
"Lib.IntTypes.SEC",
"Lib.Buffer.lbuffer",
"FStar.HyperStack.ST.push_frame",
"Hacl.Bignum.MontArithmetic.bn_mont_ctx",
"LowStar.BufferOps.op_Bang_Star",
"LowStar.Buffer.trivial_preorder"
] | [] | false | false | false | false | false | let bn_field_exp_consttime #t km k aM bBits b resM =
| let open LowStar.BufferOps in
let k1 = !*k in
push_frame ();
let aMc = create k1.len (uint #t #SEC 0) in
copy aMc aM;
ME.bn_exp_mont_consttime #t km k1.n k1.mu k1.r2 aMc bBits b resM;
let h0 = ST.get () in
assert (bn_v h0 aM < bn_v_n h0 k);
BD.bn_eval_inj (v k1.len)
(S.bn_field_exp_consttime (as_pctx h0 k) (as_seq h0 aM) (v bBits) (as_seq h0 b))
(as_seq h0 resM);
pop_frame () | false |
Hacl.Bignum.MontArithmetic.fst | Hacl.Bignum.MontArithmetic.bn_field_exp_vartime | val bn_field_exp_vartime: #t:limb_t -> km:BM.mont t -> bn_field_exp_vartime_st t km.BM.bn.BN.len | val bn_field_exp_vartime: #t:limb_t -> km:BM.mont t -> bn_field_exp_vartime_st t km.BM.bn.BN.len | let bn_field_exp_vartime #t km k aM bBits b resM =
let open LowStar.BufferOps in
let k1 = !*k in
push_frame ();
let aMc = create k1.len (uint #t #SEC 0) in
copy aMc aM;
ME.bn_exp_mont_vartime #t km k1.n k1.mu k1.r2 aMc bBits b resM;
let h0 = ST.get () in
assert (bn_v h0 aM < bn_v_n h0 k);
BD.bn_eval_inj (v k1.len)
(S.bn_field_exp_vartime (as_pctx h0 k) (as_seq h0 aM) (v bBits) (as_seq h0 b))
(as_seq h0 resM);
pop_frame () | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 14,
"end_line": 169,
"start_col": 0,
"start_line": 157
} | 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))
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))
let bn_field_exp_consttime #t km k aM bBits b resM =
let open LowStar.BufferOps in
let k1 = !*k in
push_frame ();
let aMc = create k1.len (uint #t #SEC 0) in
copy aMc aM;
ME.bn_exp_mont_consttime #t km k1.n k1.mu k1.r2 aMc bBits b resM;
let h0 = ST.get () in
assert (bn_v h0 aM < bn_v_n h0 k);
BD.bn_eval_inj (v k1.len)
(S.bn_field_exp_consttime (as_pctx h0 k) (as_seq h0 aM) (v bBits) (as_seq h0 b))
(as_seq h0 resM);
pop_frame () | {
"checked_file": "/",
"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"
} | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"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.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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | km: Hacl.Bignum.Montgomery.mont t
-> Hacl.Bignum.MontArithmetic.bn_field_exp_vartime_st t (Mkbn?.len (Mkmont?.bn km)) | Prims.Tot | [
"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",
"Lib.IntTypes.size_t",
"Hacl.Bignum.Definitions.blocks0",
"Lib.IntTypes.size",
"Lib.IntTypes.bits",
"FStar.HyperStack.ST.pop_frame",
"Prims.unit",
"Hacl.Spec.Bignum.Definitions.bn_eval_inj",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__len",
"Hacl.Bignum.MontArithmetic.lb",
"Hacl.Bignum.MontArithmetic.ll",
"Hacl.Spec.Bignum.MontArithmetic.bn_field_exp_vartime",
"Hacl.Bignum.MontArithmetic.as_pctx",
"Lib.Buffer.as_seq",
"Lib.Buffer.MUT",
"Hacl.Bignum.Definitions.limb",
"Prims._assert",
"Prims.b2t",
"Prims.op_LessThan",
"Hacl.Bignum.Definitions.bn_v",
"Hacl.Bignum.MontArithmetic.bn_v_n",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Hacl.Bignum.MontExponentiation.bn_exp_mont_vartime",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__n",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__mu",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__r2",
"Lib.Buffer.copy",
"Lib.Buffer.lbuffer_t",
"Lib.Buffer.create",
"Lib.IntTypes.uint",
"Lib.IntTypes.SEC",
"Lib.Buffer.lbuffer",
"FStar.HyperStack.ST.push_frame",
"Hacl.Bignum.MontArithmetic.bn_mont_ctx",
"LowStar.BufferOps.op_Bang_Star",
"LowStar.Buffer.trivial_preorder"
] | [] | false | false | false | false | false | let bn_field_exp_vartime #t km k aM bBits b resM =
| let open LowStar.BufferOps in
let k1 = !*k in
push_frame ();
let aMc = create k1.len (uint #t #SEC 0) in
copy aMc aM;
ME.bn_exp_mont_vartime #t km k1.n k1.mu k1.r2 aMc bBits b resM;
let h0 = ST.get () in
assert (bn_v h0 aM < bn_v_n h0 k);
BD.bn_eval_inj (v k1.len)
(S.bn_field_exp_vartime (as_pctx h0 k) (as_seq h0 aM) (v bBits) (as_seq h0 b))
(as_seq h0 resM);
pop_frame () | false |
Hacl.Bignum.MontArithmetic.fst | Hacl.Bignum.MontArithmetic.bn_field_inv | val bn_field_inv:
#t:limb_t
-> len:Ghost.erased _
-> bn_field_exp_vartime:bn_field_exp_vartime_st t len ->
bn_field_inv_st t len | val bn_field_inv:
#t:limb_t
-> len:Ghost.erased _
-> bn_field_exp_vartime:bn_field_exp_vartime_st t len ->
bn_field_inv_st t len | let bn_field_inv #t len bn_field_exp_vartime k aM aInvM =
let open LowStar.BufferOps in
let k1 = !*k in
let len = k1.len in
push_frame ();
let n2 = create len (uint #t #SEC 0) in
BI.bn_mod_inv_prime_n2 len k1.n n2;
bn_field_exp_vartime k aM (k1.len *! size (bits t)) n2 aInvM;
pop_frame () | {
"file_name": "code/bignum/Hacl.Bignum.MontArithmetic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 14,
"end_line": 180,
"start_col": 0,
"start_line": 172
} | 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))
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))
let bn_field_exp_consttime #t km k aM bBits b resM =
let open LowStar.BufferOps in
let k1 = !*k in
push_frame ();
let aMc = create k1.len (uint #t #SEC 0) in
copy aMc aM;
ME.bn_exp_mont_consttime #t km k1.n k1.mu k1.r2 aMc bBits b resM;
let h0 = ST.get () in
assert (bn_v h0 aM < bn_v_n h0 k);
BD.bn_eval_inj (v k1.len)
(S.bn_field_exp_consttime (as_pctx h0 k) (as_seq h0 aM) (v bBits) (as_seq h0 b))
(as_seq h0 resM);
pop_frame ()
let bn_field_exp_vartime #t km k aM bBits b resM =
let open LowStar.BufferOps in
let k1 = !*k in
push_frame ();
let aMc = create k1.len (uint #t #SEC 0) in
copy aMc aM;
ME.bn_exp_mont_vartime #t km k1.n k1.mu k1.r2 aMc bBits b resM;
let h0 = ST.get () in
assert (bn_v h0 aM < bn_v_n h0 k);
BD.bn_eval_inj (v k1.len)
(S.bn_field_exp_vartime (as_pctx h0 k) (as_seq h0 aM) (v bBits) (as_seq h0 b))
(as_seq h0 resM);
pop_frame () | {
"checked_file": "/",
"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"
} | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.ModInv",
"short_module": "BI"
},
{
"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.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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
len: FStar.Ghost.erased (Hacl.Bignum.meta_len t) ->
bn_field_exp_vartime:
Hacl.Bignum.MontArithmetic.bn_field_exp_vartime_st t (FStar.Ghost.reveal len)
-> Hacl.Bignum.MontArithmetic.bn_field_inv_st t (FStar.Ghost.reveal len) | Prims.Tot | [
"total"
] | [] | [
"Hacl.Bignum.Definitions.limb_t",
"FStar.Ghost.erased",
"Hacl.Bignum.meta_len",
"Hacl.Bignum.MontArithmetic.bn_field_exp_vartime_st",
"FStar.Ghost.reveal",
"Hacl.Bignum.MontArithmetic.pbn_mont_ctx",
"Hacl.Bignum.Definitions.lbignum",
"FStar.HyperStack.ST.pop_frame",
"Prims.unit",
"Lib.IntTypes.op_Star_Bang",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__len",
"Hacl.Bignum.MontArithmetic.lb",
"Hacl.Bignum.MontArithmetic.ll",
"Lib.IntTypes.size",
"Lib.IntTypes.bits",
"Hacl.Bignum.ModInv.bn_mod_inv_prime_n2",
"Hacl.Bignum.MontArithmetic.__proj__Mkbn_mont_ctx'__item__n",
"Lib.Buffer.lbuffer_t",
"Lib.Buffer.MUT",
"Hacl.Bignum.Definitions.limb",
"Lib.Buffer.create",
"Lib.IntTypes.uint",
"Lib.IntTypes.SEC",
"Lib.Buffer.lbuffer",
"FStar.HyperStack.ST.push_frame",
"Hacl.Bignum.MontArithmetic.bn_mont_ctx",
"LowStar.BufferOps.op_Bang_Star",
"LowStar.Buffer.trivial_preorder"
] | [] | false | false | false | false | false | let bn_field_inv #t len bn_field_exp_vartime k aM aInvM =
| let open LowStar.BufferOps in
let k1 = !*k in
let len = k1.len in
push_frame ();
let n2 = create len (uint #t #SEC 0) in
BI.bn_mod_inv_prime_n2 len k1.n n2;
bn_field_exp_vartime k aM (k1.len *! size (bits t)) n2 aInvM;
pop_frame () | false |
Hacl.SHA2.Scalar32.Lemmas.fst | Hacl.SHA2.Scalar32.Lemmas.lemma_spec_update_384_512 | val lemma_spec_update_384_512 (b st: _)
: Lemma (ensures Hacl.Spec.SHA2.update SHA2_512 b st == Hacl.Spec.SHA2.update SHA2_384 b st) | val lemma_spec_update_384_512 (b st: _)
: Lemma (ensures Hacl.Spec.SHA2.update SHA2_512 b st == Hacl.Spec.SHA2.update SHA2_384 b st) | let lemma_spec_update_384_512 b st: Lemma (ensures
Hacl.Spec.SHA2.update SHA2_512 b st ==
Hacl.Spec.SHA2.update SHA2_384 b st)
=
calc (==) {
Hacl.Spec.SHA2.update SHA2_512 b st;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_512 b st }
Spec.Agile.Hash.update SHA2_512 st b;
(==) { Spec.SHA2.Lemmas.update_384_512 st b }
Spec.Agile.Hash.update SHA2_384 st b;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_384 b st }
Hacl.Spec.SHA2.update SHA2_384 b st;
} | {
"file_name": "code/sha2-mb/Hacl.SHA2.Scalar32.Lemmas.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 3,
"end_line": 158,
"start_col": 0,
"start_line": 146
} | module Hacl.SHA2.Scalar32.Lemmas
open Lib.IntTypes
open Spec.Agile.Hash
open Hacl.Spec.SHA2.Vec
module SpecVec = Hacl.Spec.SHA2.Vec
#push-options "--fuel 0 --ifuel 0 --z3rlimit 50"
let state_spec_v_extensionality (a : hash_alg { is_sha2 a })
(acc1: Hacl.Spec.SHA2.Vec.(state_spec a M32))
(acc2: Hacl.Spec.SHA2.Vec.(state_spec a M32)) :
Lemma
(requires (let open Hacl.Spec.SHA2.Vec in
Lib.Sequence.index (state_spec_v acc1) 0 ==
Lib.Sequence.index (state_spec_v acc2) 0))
(ensures acc1 == acc2) =
let open Lib.Sequence in
let open Lib.IntVector in
let open Hacl.Spec.SHA2.Vec in
allow_inversion hash_alg;
let acc1_s = (state_spec_v acc1).[0] <: lseq (word a) 8 in
let acc2_s = (state_spec_v acc2).[0] <: lseq (word a) 8 in
let aux (i:nat{i < 8}) : Lemma (acc1.[i] == acc2.[i]) =
assert (index (vec_v acc1.[i]) 0 == index #(word a) #8 acc1_s i);
assert (index (vec_v acc2.[i]) 0 == index #(word a) #8 acc2_s i);
assert (index (vec_v acc1.[i]) 0 == index (vec_v acc2.[i]) 0);
eq_intro (vec_v acc1.[i]) (vec_v acc2.[i]);
vecv_extensionality acc1.[i] acc2.[i] in
Classical.forall_intro aux;
eq_intro acc1 acc2
let lemma_spec_update_224_256 b st: Lemma (ensures
Hacl.Spec.SHA2.update SHA2_256 b st ==
Hacl.Spec.SHA2.update SHA2_224 b st)
=
calc (==) {
Hacl.Spec.SHA2.update SHA2_256 b st;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_256 b st }
Spec.Agile.Hash.update SHA2_256 st b;
(==) { Spec.SHA2.Lemmas.update_224_256 st b }
Spec.Agile.Hash.update SHA2_224 st b;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_224 b st }
Hacl.Spec.SHA2.update SHA2_224 b st;
}
let lemma_spec_update_vec_224_256 b0 st0 : Lemma (ensures
SpecVec.update #SHA2_224 #M32 b0 st0 ==
SpecVec.update #SHA2_256 #M32 b0 st0)
=
let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update #SHA2_256 #M32 b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_256 in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_256 in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update #SHA2_256 b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_256 #M32 b0 st0 0 }
Hacl.Spec.SHA2.update SHA2_256 b0.(|0|) st0_m32;
(==) { lemma_spec_update_224_256 b0.(|0|) st0_m32 }
Hacl.Spec.SHA2.update SHA2_224 b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_224 #M32 b0 st0 0 }
(state_spec_v #SHA2_224 #M32 (SpecVec.update #SHA2_224 b0 st0)).[0];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update #SHA2_256 b0 st0)
(SpecVec.update #SHA2_224 #M32 b0 st0)
let lemma_spec_update_nblocks_vec_224_256 (len:size_t) b0 st0 : Lemma (ensures (
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
SpecVec.update_nblocks #SHA2_224 #M32 (v len) b0 st0 ==
SpecVec.update_nblocks #SHA2_256 #M32 (v len) b0 st0))
= let open Lib.IntTypes in
let open Lib.Sequence in
let open Lib.MultiBuffer in
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
let st1 = SpecVec.update_nblocks #SHA2_256 #M32 (v len) b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_256 in
let b0_m32' = Seq.slice b0.(|0|) 0 (Seq.length b0.(|0|) - Seq.length b0.(|0|) % block_length SHA2_256) in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_256 in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update_nblocks #SHA2_256 (v len) b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_256 #M32 (v len) b0 st0 0 }
Hacl.Spec.SHA2.update_nblocks SHA2_256 (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_256 (v len) b0.(|0|) st0_m32 }
Lib.Sequence.repeat_blocks_multi (block_length SHA2_256) b0_m32' (Hacl.Spec.SHA2.update SHA2_256) st0_m32;
(==) {
FStar.Classical.forall_intro_2 lemma_spec_update_224_256;
Lib.Sequence.Lemmas.repeat_blocks_multi_extensionality #uint8 #(words_state SHA2_256) (block_length SHA2_256) b0_m32'
(Hacl.Spec.SHA2.update SHA2_256)
(Hacl.Spec.SHA2.update SHA2_224)
st0_m32
}
Lib.Sequence.repeat_blocks_multi #uint8 #(words_state SHA2_256) (block_length SHA2_256) b0_m32' (Hacl.Spec.SHA2.update SHA2_224) st0_m32;
(==) { }
Lib.Sequence.repeat_blocks_multi #uint8 #(words_state SHA2_224) (block_length SHA2_224) b0_m32' (Hacl.Spec.SHA2.update SHA2_224) (st0_m32 <: words_state SHA2_224);
(==) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_224 (v len) b0.(|0|) st0_m32 }
Hacl.Spec.SHA2.update_nblocks SHA2_224 (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_224 #M32 (v len) b0 st0 0 }
(state_spec_v #SHA2_224 #M32 (SpecVec.update_nblocks #SHA2_224 (v len) b0 st0)).[0];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update_nblocks #SHA2_256 (v len) b0 st0)
(SpecVec.update_nblocks #SHA2_224 #M32 (v len) b0 st0)
let lemma_spec_update_last_vec_224_256 totlen (len:size_t{v len <= block_length SHA2_256}) b0 st0 : Lemma (ensures (
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0 ==
SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0))
= let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_256 in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_256 in
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
let hacl_spec_update_224_256 b st: Lemma (ensures
Hacl.Spec.SHA2.update SHA2_256 b st ==
Hacl.Spec.SHA2.update SHA2_224 b st)
[ SMTPat (Hacl.Spec.SHA2.update SHA2_256 b st) ]
=
lemma_spec_update_224_256 b st
in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update_last #SHA2_256 totlen (v len) b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_last_lemma_l #SHA2_256 #M32 totlen (v len) b0 st0 0 }
Hacl.Spec.SHA2.update_last SHA2_256 totlen (v len) b0.(|0|) st0_m32;
(==) { }
Hacl.Spec.SHA2.update_last SHA2_224 totlen (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_last_lemma_l #SHA2_224 #M32 totlen (v len) b0 st0 0 }
(state_spec_v (SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0)).[0];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0)
(SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0) | {
"checked_file": "/",
"dependencies": [
"Spec.SHA2.Lemmas.fsti.checked",
"Spec.Agile.Hash.fsti.checked",
"prims.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.MultiBuffer.fst.checked",
"Lib.IntVector.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Hacl.Spec.SHA2.Vec.fst.checked",
"Hacl.Spec.SHA2.EquivScalar.fsti.checked",
"Hacl.Spec.SHA2.Equiv.fst.checked",
"Hacl.Spec.SHA2.fst.checked",
"Hacl.Impl.SHA2.Core.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.SHA2.Scalar32.Lemmas.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.SHA2.Vec",
"short_module": "SpecVec"
},
{
"abbrev": false,
"full_module": "Hacl.Spec.SHA2.Vec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile.Hash",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.SHA2.Scalar32",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.SHA2.Scalar32",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
b:
s:
Lib.Sequence.seq Lib.IntTypes.uint8
{ FStar.Seq.Base.length s ==
Spec.Hash.Definitions.block_length Spec.Hash.Definitions.SHA2_512 /\
FStar.Seq.Base.length s ==
Spec.Hash.Definitions.block_length Spec.Hash.Definitions.SHA2_384 } ->
st:
s:
Lib.Sequence.seq (Spec.Hash.Definitions.word Spec.Hash.Definitions.SHA2_384)
{ FStar.Seq.Base.length s ==
Spec.Hash.Definitions.state_word_length Spec.Hash.Definitions.SHA2_384 /\
FStar.Seq.Base.length s ==
Spec.Hash.Definitions.state_word_length Spec.Hash.Definitions.SHA2_512 }
-> FStar.Pervasives.Lemma
(ensures
Hacl.Spec.SHA2.update Spec.Hash.Definitions.SHA2_512 b st ==
Hacl.Spec.SHA2.update Spec.Hash.Definitions.SHA2_384 b st) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Lib.Sequence.seq",
"Lib.IntTypes.uint8",
"Prims.l_and",
"Prims.eq2",
"Prims.nat",
"FStar.Seq.Base.length",
"Spec.Hash.Definitions.block_length",
"Spec.Hash.Definitions.SHA2_512",
"Spec.Hash.Definitions.SHA2_384",
"Spec.Hash.Definitions.word",
"Spec.Hash.Definitions.state_word_length",
"FStar.Calc.calc_finish",
"Spec.Hash.Definitions.words_state",
"Hacl.Spec.SHA2.update",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"Spec.Agile.Hash.update",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Hacl.Spec.SHA2.EquivScalar.update_lemma",
"Prims.squash",
"Spec.SHA2.Lemmas.update_384_512",
"Prims.l_True",
"Prims.l_or",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let lemma_spec_update_384_512 b st
: Lemma (ensures Hacl.Spec.SHA2.update SHA2_512 b st == Hacl.Spec.SHA2.update SHA2_384 b st) =
| calc ( == ) {
Hacl.Spec.SHA2.update SHA2_512 b st;
( == ) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_512 b st }
Spec.Agile.Hash.update SHA2_512 st b;
( == ) { Spec.SHA2.Lemmas.update_384_512 st b }
Spec.Agile.Hash.update SHA2_384 st b;
( == ) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_384 b st }
Hacl.Spec.SHA2.update SHA2_384 b st;
} | false |
Hacl.SHA2.Scalar32.Lemmas.fst | Hacl.SHA2.Scalar32.Lemmas.lemma_spec_update_224_256 | val lemma_spec_update_224_256 (b st: _)
: Lemma (ensures Hacl.Spec.SHA2.update SHA2_256 b st == Hacl.Spec.SHA2.update SHA2_224 b st) | val lemma_spec_update_224_256 (b st: _)
: Lemma (ensures Hacl.Spec.SHA2.update SHA2_256 b st == Hacl.Spec.SHA2.update SHA2_224 b st) | let lemma_spec_update_224_256 b st: Lemma (ensures
Hacl.Spec.SHA2.update SHA2_256 b st ==
Hacl.Spec.SHA2.update SHA2_224 b st)
=
calc (==) {
Hacl.Spec.SHA2.update SHA2_256 b st;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_256 b st }
Spec.Agile.Hash.update SHA2_256 st b;
(==) { Spec.SHA2.Lemmas.update_224_256 st b }
Spec.Agile.Hash.update SHA2_224 st b;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_224 b st }
Hacl.Spec.SHA2.update SHA2_224 b st;
} | {
"file_name": "code/sha2-mb/Hacl.SHA2.Scalar32.Lemmas.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 3,
"end_line": 48,
"start_col": 0,
"start_line": 36
} | module Hacl.SHA2.Scalar32.Lemmas
open Lib.IntTypes
open Spec.Agile.Hash
open Hacl.Spec.SHA2.Vec
module SpecVec = Hacl.Spec.SHA2.Vec
#push-options "--fuel 0 --ifuel 0 --z3rlimit 50"
let state_spec_v_extensionality (a : hash_alg { is_sha2 a })
(acc1: Hacl.Spec.SHA2.Vec.(state_spec a M32))
(acc2: Hacl.Spec.SHA2.Vec.(state_spec a M32)) :
Lemma
(requires (let open Hacl.Spec.SHA2.Vec in
Lib.Sequence.index (state_spec_v acc1) 0 ==
Lib.Sequence.index (state_spec_v acc2) 0))
(ensures acc1 == acc2) =
let open Lib.Sequence in
let open Lib.IntVector in
let open Hacl.Spec.SHA2.Vec in
allow_inversion hash_alg;
let acc1_s = (state_spec_v acc1).[0] <: lseq (word a) 8 in
let acc2_s = (state_spec_v acc2).[0] <: lseq (word a) 8 in
let aux (i:nat{i < 8}) : Lemma (acc1.[i] == acc2.[i]) =
assert (index (vec_v acc1.[i]) 0 == index #(word a) #8 acc1_s i);
assert (index (vec_v acc2.[i]) 0 == index #(word a) #8 acc2_s i);
assert (index (vec_v acc1.[i]) 0 == index (vec_v acc2.[i]) 0);
eq_intro (vec_v acc1.[i]) (vec_v acc2.[i]);
vecv_extensionality acc1.[i] acc2.[i] in
Classical.forall_intro aux;
eq_intro acc1 acc2 | {
"checked_file": "/",
"dependencies": [
"Spec.SHA2.Lemmas.fsti.checked",
"Spec.Agile.Hash.fsti.checked",
"prims.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.MultiBuffer.fst.checked",
"Lib.IntVector.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Hacl.Spec.SHA2.Vec.fst.checked",
"Hacl.Spec.SHA2.EquivScalar.fsti.checked",
"Hacl.Spec.SHA2.Equiv.fst.checked",
"Hacl.Spec.SHA2.fst.checked",
"Hacl.Impl.SHA2.Core.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.SHA2.Scalar32.Lemmas.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.SHA2.Vec",
"short_module": "SpecVec"
},
{
"abbrev": false,
"full_module": "Hacl.Spec.SHA2.Vec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile.Hash",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.SHA2.Scalar32",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.SHA2.Scalar32",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
b:
s:
Lib.Sequence.seq Lib.IntTypes.uint8
{ FStar.Seq.Base.length s ==
Spec.Hash.Definitions.block_length Spec.Hash.Definitions.SHA2_256 /\
FStar.Seq.Base.length s ==
Spec.Hash.Definitions.block_length Spec.Hash.Definitions.SHA2_224 } ->
st:
s:
Lib.Sequence.seq (Spec.Hash.Definitions.word Spec.Hash.Definitions.SHA2_224)
{ FStar.Seq.Base.length s ==
Spec.Hash.Definitions.state_word_length Spec.Hash.Definitions.SHA2_224 /\
FStar.Seq.Base.length s ==
Spec.Hash.Definitions.state_word_length Spec.Hash.Definitions.SHA2_256 }
-> FStar.Pervasives.Lemma
(ensures
Hacl.Spec.SHA2.update Spec.Hash.Definitions.SHA2_256 b st ==
Hacl.Spec.SHA2.update Spec.Hash.Definitions.SHA2_224 b st) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Lib.Sequence.seq",
"Lib.IntTypes.uint8",
"Prims.l_and",
"Prims.eq2",
"Prims.nat",
"FStar.Seq.Base.length",
"Spec.Hash.Definitions.block_length",
"Spec.Hash.Definitions.SHA2_256",
"Spec.Hash.Definitions.SHA2_224",
"Spec.Hash.Definitions.word",
"Spec.Hash.Definitions.state_word_length",
"FStar.Calc.calc_finish",
"Spec.Hash.Definitions.words_state",
"Hacl.Spec.SHA2.update",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"Spec.Agile.Hash.update",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Hacl.Spec.SHA2.EquivScalar.update_lemma",
"Prims.squash",
"Spec.SHA2.Lemmas.update_224_256",
"Prims.l_True",
"Prims.l_or",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let lemma_spec_update_224_256 b st
: Lemma (ensures Hacl.Spec.SHA2.update SHA2_256 b st == Hacl.Spec.SHA2.update SHA2_224 b st) =
| calc ( == ) {
Hacl.Spec.SHA2.update SHA2_256 b st;
( == ) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_256 b st }
Spec.Agile.Hash.update SHA2_256 st b;
( == ) { Spec.SHA2.Lemmas.update_224_256 st b }
Spec.Agile.Hash.update SHA2_224 st b;
( == ) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_224 b st }
Hacl.Spec.SHA2.update SHA2_224 b st;
} | false |
Hacl.SHA2.Scalar32.Lemmas.fst | Hacl.SHA2.Scalar32.Lemmas.lemma_spec_update_vec_224_256 | val lemma_spec_update_vec_224_256 (b0 st0: _)
: Lemma (ensures SpecVec.update #SHA2_224 #M32 b0 st0 == SpecVec.update #SHA2_256 #M32 b0 st0) | val lemma_spec_update_vec_224_256 (b0 st0: _)
: Lemma (ensures SpecVec.update #SHA2_224 #M32 b0 st0 == SpecVec.update #SHA2_256 #M32 b0 st0) | let lemma_spec_update_vec_224_256 b0 st0 : Lemma (ensures
SpecVec.update #SHA2_224 #M32 b0 st0 ==
SpecVec.update #SHA2_256 #M32 b0 st0)
=
let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update #SHA2_256 #M32 b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_256 in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_256 in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update #SHA2_256 b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_256 #M32 b0 st0 0 }
Hacl.Spec.SHA2.update SHA2_256 b0.(|0|) st0_m32;
(==) { lemma_spec_update_224_256 b0.(|0|) st0_m32 }
Hacl.Spec.SHA2.update SHA2_224 b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_224 #M32 b0 st0 0 }
(state_spec_v #SHA2_224 #M32 (SpecVec.update #SHA2_224 b0 st0)).[0];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update #SHA2_256 b0 st0)
(SpecVec.update #SHA2_224 #M32 b0 st0) | {
"file_name": "code/sha2-mb/Hacl.SHA2.Scalar32.Lemmas.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 44,
"end_line": 72,
"start_col": 0,
"start_line": 50
} | module Hacl.SHA2.Scalar32.Lemmas
open Lib.IntTypes
open Spec.Agile.Hash
open Hacl.Spec.SHA2.Vec
module SpecVec = Hacl.Spec.SHA2.Vec
#push-options "--fuel 0 --ifuel 0 --z3rlimit 50"
let state_spec_v_extensionality (a : hash_alg { is_sha2 a })
(acc1: Hacl.Spec.SHA2.Vec.(state_spec a M32))
(acc2: Hacl.Spec.SHA2.Vec.(state_spec a M32)) :
Lemma
(requires (let open Hacl.Spec.SHA2.Vec in
Lib.Sequence.index (state_spec_v acc1) 0 ==
Lib.Sequence.index (state_spec_v acc2) 0))
(ensures acc1 == acc2) =
let open Lib.Sequence in
let open Lib.IntVector in
let open Hacl.Spec.SHA2.Vec in
allow_inversion hash_alg;
let acc1_s = (state_spec_v acc1).[0] <: lseq (word a) 8 in
let acc2_s = (state_spec_v acc2).[0] <: lseq (word a) 8 in
let aux (i:nat{i < 8}) : Lemma (acc1.[i] == acc2.[i]) =
assert (index (vec_v acc1.[i]) 0 == index #(word a) #8 acc1_s i);
assert (index (vec_v acc2.[i]) 0 == index #(word a) #8 acc2_s i);
assert (index (vec_v acc1.[i]) 0 == index (vec_v acc2.[i]) 0);
eq_intro (vec_v acc1.[i]) (vec_v acc2.[i]);
vecv_extensionality acc1.[i] acc2.[i] in
Classical.forall_intro aux;
eq_intro acc1 acc2
let lemma_spec_update_224_256 b st: Lemma (ensures
Hacl.Spec.SHA2.update SHA2_256 b st ==
Hacl.Spec.SHA2.update SHA2_224 b st)
=
calc (==) {
Hacl.Spec.SHA2.update SHA2_256 b st;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_256 b st }
Spec.Agile.Hash.update SHA2_256 st b;
(==) { Spec.SHA2.Lemmas.update_224_256 st b }
Spec.Agile.Hash.update SHA2_224 st b;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_224 b st }
Hacl.Spec.SHA2.update SHA2_224 b st;
} | {
"checked_file": "/",
"dependencies": [
"Spec.SHA2.Lemmas.fsti.checked",
"Spec.Agile.Hash.fsti.checked",
"prims.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.MultiBuffer.fst.checked",
"Lib.IntVector.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Hacl.Spec.SHA2.Vec.fst.checked",
"Hacl.Spec.SHA2.EquivScalar.fsti.checked",
"Hacl.Spec.SHA2.Equiv.fst.checked",
"Hacl.Spec.SHA2.fst.checked",
"Hacl.Impl.SHA2.Core.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.SHA2.Scalar32.Lemmas.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.SHA2.Vec",
"short_module": "SpecVec"
},
{
"abbrev": false,
"full_module": "Hacl.Spec.SHA2.Vec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile.Hash",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.SHA2.Scalar32",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.SHA2.Scalar32",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
b0: Hacl.Spec.SHA2.Vec.multiblock_spec Spec.Hash.Definitions.SHA2_224 Hacl.Spec.SHA2.Vec.M32 ->
st0:
s:
Lib.Sequence.seq (Hacl.Spec.SHA2.Vec.element_t Spec.Hash.Definitions.SHA2_256
Hacl.Spec.SHA2.Vec.M32) {FStar.Seq.Base.length s == 8 /\ FStar.Seq.Base.length s == 8}
-> FStar.Pervasives.Lemma
(ensures Hacl.Spec.SHA2.Vec.update b0 st0 == Hacl.Spec.SHA2.Vec.update b0 st0) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Hacl.Spec.SHA2.Vec.multiblock_spec",
"Spec.Hash.Definitions.SHA2_224",
"Hacl.Spec.SHA2.Vec.M32",
"Lib.Sequence.seq",
"Hacl.Spec.SHA2.Vec.element_t",
"Spec.Hash.Definitions.SHA2_256",
"Prims.l_and",
"Prims.eq2",
"Prims.nat",
"FStar.Seq.Base.length",
"Hacl.SHA2.Scalar32.Lemmas.state_spec_v_extensionality",
"Hacl.Spec.SHA2.Vec.update",
"Prims.unit",
"FStar.Calc.calc_finish",
"Spec.Hash.Definitions.words_state",
"Lib.Sequence.op_String_Access",
"Hacl.Spec.SHA2.Vec.words_state'",
"Hacl.Spec.SHA2.Vec.lanes",
"Hacl.Spec.SHA2.Vec.state_spec_v",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"FStar.Calc.calc_step",
"Hacl.Spec.SHA2.update",
"Lib.MultiBuffer.op_Lens_Access",
"FStar.Seq.Properties.lseq",
"Lib.IntTypes.uint8",
"Spec.Hash.Definitions.block_length",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"Hacl.Spec.SHA2.Equiv.update_lemma_l",
"Hacl.SHA2.Scalar32.Lemmas.lemma_spec_update_224_256",
"Hacl.Spec.SHA2.Vec.state_spec",
"Prims.l_True",
"Prims.l_or",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let lemma_spec_update_vec_224_256 b0 st0
: Lemma (ensures SpecVec.update #SHA2_224 #M32 b0 st0 == SpecVec.update #SHA2_256 #M32 b0 st0) =
| let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update #SHA2_256 #M32 b0 st0 in
let st0_m32 = (state_spec_v st0).[ 0 ] <: words_state SHA2_256 in
let st1_m32 = (state_spec_v st1).[ 0 ] <: words_state SHA2_256 in
calc ( == ) {
st1_m32;
( == ) { () }
(state_spec_v (SpecVec.update #SHA2_256 b0 st0)).[ 0 ];
( == ) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_256 #M32 b0 st0 0 }
Hacl.Spec.SHA2.update SHA2_256 b0.(| 0 |) st0_m32;
( == ) { lemma_spec_update_224_256 b0.(| 0 |) st0_m32 }
Hacl.Spec.SHA2.update SHA2_224 b0.(| 0 |) st0_m32;
( == ) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_224 #M32 b0 st0 0 }
(state_spec_v #SHA2_224 #M32 (SpecVec.update #SHA2_224 b0 st0)).[ 0 ];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update #SHA2_256 b0 st0)
(SpecVec.update #SHA2_224 #M32 b0 st0) | false |
Hacl.SHA2.Scalar32.Lemmas.fst | Hacl.SHA2.Scalar32.Lemmas.lemma_spec_update_vec_384_512 | val lemma_spec_update_vec_384_512 (b0 st0: _)
: Lemma (ensures SpecVec.update #SHA2_384 #M32 b0 st0 == SpecVec.update #SHA2_512 #M32 b0 st0) | val lemma_spec_update_vec_384_512 (b0 st0: _)
: Lemma (ensures SpecVec.update #SHA2_384 #M32 b0 st0 == SpecVec.update #SHA2_512 #M32 b0 st0) | let lemma_spec_update_vec_384_512 b0 st0 : Lemma (ensures
SpecVec.update #SHA2_384 #M32 b0 st0 ==
SpecVec.update #SHA2_512 #M32 b0 st0)
=
let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update #SHA2_512 #M32 b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_512 in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_512 in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update #SHA2_512 b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_512 #M32 b0 st0 0 }
Hacl.Spec.SHA2.update SHA2_512 b0.(|0|) st0_m32;
(==) { lemma_spec_update_384_512 b0.(|0|) st0_m32 }
Hacl.Spec.SHA2.update SHA2_384 b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_384 #M32 b0 st0 0 }
(state_spec_v #SHA2_384 #M32 (SpecVec.update #SHA2_384 b0 st0)).[0];
};
state_spec_v_extensionality SHA2_512
(SpecVec.update #SHA2_512 b0 st0)
(SpecVec.update #SHA2_384 #M32 b0 st0) | {
"file_name": "code/sha2-mb/Hacl.SHA2.Scalar32.Lemmas.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 44,
"end_line": 182,
"start_col": 0,
"start_line": 160
} | module Hacl.SHA2.Scalar32.Lemmas
open Lib.IntTypes
open Spec.Agile.Hash
open Hacl.Spec.SHA2.Vec
module SpecVec = Hacl.Spec.SHA2.Vec
#push-options "--fuel 0 --ifuel 0 --z3rlimit 50"
let state_spec_v_extensionality (a : hash_alg { is_sha2 a })
(acc1: Hacl.Spec.SHA2.Vec.(state_spec a M32))
(acc2: Hacl.Spec.SHA2.Vec.(state_spec a M32)) :
Lemma
(requires (let open Hacl.Spec.SHA2.Vec in
Lib.Sequence.index (state_spec_v acc1) 0 ==
Lib.Sequence.index (state_spec_v acc2) 0))
(ensures acc1 == acc2) =
let open Lib.Sequence in
let open Lib.IntVector in
let open Hacl.Spec.SHA2.Vec in
allow_inversion hash_alg;
let acc1_s = (state_spec_v acc1).[0] <: lseq (word a) 8 in
let acc2_s = (state_spec_v acc2).[0] <: lseq (word a) 8 in
let aux (i:nat{i < 8}) : Lemma (acc1.[i] == acc2.[i]) =
assert (index (vec_v acc1.[i]) 0 == index #(word a) #8 acc1_s i);
assert (index (vec_v acc2.[i]) 0 == index #(word a) #8 acc2_s i);
assert (index (vec_v acc1.[i]) 0 == index (vec_v acc2.[i]) 0);
eq_intro (vec_v acc1.[i]) (vec_v acc2.[i]);
vecv_extensionality acc1.[i] acc2.[i] in
Classical.forall_intro aux;
eq_intro acc1 acc2
let lemma_spec_update_224_256 b st: Lemma (ensures
Hacl.Spec.SHA2.update SHA2_256 b st ==
Hacl.Spec.SHA2.update SHA2_224 b st)
=
calc (==) {
Hacl.Spec.SHA2.update SHA2_256 b st;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_256 b st }
Spec.Agile.Hash.update SHA2_256 st b;
(==) { Spec.SHA2.Lemmas.update_224_256 st b }
Spec.Agile.Hash.update SHA2_224 st b;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_224 b st }
Hacl.Spec.SHA2.update SHA2_224 b st;
}
let lemma_spec_update_vec_224_256 b0 st0 : Lemma (ensures
SpecVec.update #SHA2_224 #M32 b0 st0 ==
SpecVec.update #SHA2_256 #M32 b0 st0)
=
let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update #SHA2_256 #M32 b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_256 in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_256 in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update #SHA2_256 b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_256 #M32 b0 st0 0 }
Hacl.Spec.SHA2.update SHA2_256 b0.(|0|) st0_m32;
(==) { lemma_spec_update_224_256 b0.(|0|) st0_m32 }
Hacl.Spec.SHA2.update SHA2_224 b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_224 #M32 b0 st0 0 }
(state_spec_v #SHA2_224 #M32 (SpecVec.update #SHA2_224 b0 st0)).[0];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update #SHA2_256 b0 st0)
(SpecVec.update #SHA2_224 #M32 b0 st0)
let lemma_spec_update_nblocks_vec_224_256 (len:size_t) b0 st0 : Lemma (ensures (
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
SpecVec.update_nblocks #SHA2_224 #M32 (v len) b0 st0 ==
SpecVec.update_nblocks #SHA2_256 #M32 (v len) b0 st0))
= let open Lib.IntTypes in
let open Lib.Sequence in
let open Lib.MultiBuffer in
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
let st1 = SpecVec.update_nblocks #SHA2_256 #M32 (v len) b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_256 in
let b0_m32' = Seq.slice b0.(|0|) 0 (Seq.length b0.(|0|) - Seq.length b0.(|0|) % block_length SHA2_256) in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_256 in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update_nblocks #SHA2_256 (v len) b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_256 #M32 (v len) b0 st0 0 }
Hacl.Spec.SHA2.update_nblocks SHA2_256 (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_256 (v len) b0.(|0|) st0_m32 }
Lib.Sequence.repeat_blocks_multi (block_length SHA2_256) b0_m32' (Hacl.Spec.SHA2.update SHA2_256) st0_m32;
(==) {
FStar.Classical.forall_intro_2 lemma_spec_update_224_256;
Lib.Sequence.Lemmas.repeat_blocks_multi_extensionality #uint8 #(words_state SHA2_256) (block_length SHA2_256) b0_m32'
(Hacl.Spec.SHA2.update SHA2_256)
(Hacl.Spec.SHA2.update SHA2_224)
st0_m32
}
Lib.Sequence.repeat_blocks_multi #uint8 #(words_state SHA2_256) (block_length SHA2_256) b0_m32' (Hacl.Spec.SHA2.update SHA2_224) st0_m32;
(==) { }
Lib.Sequence.repeat_blocks_multi #uint8 #(words_state SHA2_224) (block_length SHA2_224) b0_m32' (Hacl.Spec.SHA2.update SHA2_224) (st0_m32 <: words_state SHA2_224);
(==) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_224 (v len) b0.(|0|) st0_m32 }
Hacl.Spec.SHA2.update_nblocks SHA2_224 (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_224 #M32 (v len) b0 st0 0 }
(state_spec_v #SHA2_224 #M32 (SpecVec.update_nblocks #SHA2_224 (v len) b0 st0)).[0];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update_nblocks #SHA2_256 (v len) b0 st0)
(SpecVec.update_nblocks #SHA2_224 #M32 (v len) b0 st0)
let lemma_spec_update_last_vec_224_256 totlen (len:size_t{v len <= block_length SHA2_256}) b0 st0 : Lemma (ensures (
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0 ==
SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0))
= let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_256 in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_256 in
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
let hacl_spec_update_224_256 b st: Lemma (ensures
Hacl.Spec.SHA2.update SHA2_256 b st ==
Hacl.Spec.SHA2.update SHA2_224 b st)
[ SMTPat (Hacl.Spec.SHA2.update SHA2_256 b st) ]
=
lemma_spec_update_224_256 b st
in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update_last #SHA2_256 totlen (v len) b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_last_lemma_l #SHA2_256 #M32 totlen (v len) b0 st0 0 }
Hacl.Spec.SHA2.update_last SHA2_256 totlen (v len) b0.(|0|) st0_m32;
(==) { }
Hacl.Spec.SHA2.update_last SHA2_224 totlen (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_last_lemma_l #SHA2_224 #M32 totlen (v len) b0 st0 0 }
(state_spec_v (SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0)).[0];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0)
(SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0)
let lemma_spec_update_384_512 b st: Lemma (ensures
Hacl.Spec.SHA2.update SHA2_512 b st ==
Hacl.Spec.SHA2.update SHA2_384 b st)
=
calc (==) {
Hacl.Spec.SHA2.update SHA2_512 b st;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_512 b st }
Spec.Agile.Hash.update SHA2_512 st b;
(==) { Spec.SHA2.Lemmas.update_384_512 st b }
Spec.Agile.Hash.update SHA2_384 st b;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_384 b st }
Hacl.Spec.SHA2.update SHA2_384 b st;
} | {
"checked_file": "/",
"dependencies": [
"Spec.SHA2.Lemmas.fsti.checked",
"Spec.Agile.Hash.fsti.checked",
"prims.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.MultiBuffer.fst.checked",
"Lib.IntVector.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Hacl.Spec.SHA2.Vec.fst.checked",
"Hacl.Spec.SHA2.EquivScalar.fsti.checked",
"Hacl.Spec.SHA2.Equiv.fst.checked",
"Hacl.Spec.SHA2.fst.checked",
"Hacl.Impl.SHA2.Core.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.SHA2.Scalar32.Lemmas.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.SHA2.Vec",
"short_module": "SpecVec"
},
{
"abbrev": false,
"full_module": "Hacl.Spec.SHA2.Vec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile.Hash",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.SHA2.Scalar32",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.SHA2.Scalar32",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
b0: Hacl.Spec.SHA2.Vec.multiblock_spec Spec.Hash.Definitions.SHA2_384 Hacl.Spec.SHA2.Vec.M32 ->
st0:
s:
Lib.Sequence.seq (Hacl.Spec.SHA2.Vec.element_t Spec.Hash.Definitions.SHA2_512
Hacl.Spec.SHA2.Vec.M32) {FStar.Seq.Base.length s == 8 /\ FStar.Seq.Base.length s == 8}
-> FStar.Pervasives.Lemma
(ensures Hacl.Spec.SHA2.Vec.update b0 st0 == Hacl.Spec.SHA2.Vec.update b0 st0) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Hacl.Spec.SHA2.Vec.multiblock_spec",
"Spec.Hash.Definitions.SHA2_384",
"Hacl.Spec.SHA2.Vec.M32",
"Lib.Sequence.seq",
"Hacl.Spec.SHA2.Vec.element_t",
"Spec.Hash.Definitions.SHA2_512",
"Prims.l_and",
"Prims.eq2",
"Prims.nat",
"FStar.Seq.Base.length",
"Hacl.SHA2.Scalar32.Lemmas.state_spec_v_extensionality",
"Hacl.Spec.SHA2.Vec.update",
"Prims.unit",
"FStar.Calc.calc_finish",
"Spec.Hash.Definitions.words_state",
"Lib.Sequence.op_String_Access",
"Hacl.Spec.SHA2.Vec.words_state'",
"Hacl.Spec.SHA2.Vec.lanes",
"Hacl.Spec.SHA2.Vec.state_spec_v",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"FStar.Calc.calc_step",
"Hacl.Spec.SHA2.update",
"Lib.MultiBuffer.op_Lens_Access",
"FStar.Seq.Properties.lseq",
"Lib.IntTypes.uint8",
"Spec.Hash.Definitions.block_length",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"Hacl.Spec.SHA2.Equiv.update_lemma_l",
"Hacl.SHA2.Scalar32.Lemmas.lemma_spec_update_384_512",
"Hacl.Spec.SHA2.Vec.state_spec",
"Prims.l_True",
"Prims.l_or",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let lemma_spec_update_vec_384_512 b0 st0
: Lemma (ensures SpecVec.update #SHA2_384 #M32 b0 st0 == SpecVec.update #SHA2_512 #M32 b0 st0) =
| let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update #SHA2_512 #M32 b0 st0 in
let st0_m32 = (state_spec_v st0).[ 0 ] <: words_state SHA2_512 in
let st1_m32 = (state_spec_v st1).[ 0 ] <: words_state SHA2_512 in
calc ( == ) {
st1_m32;
( == ) { () }
(state_spec_v (SpecVec.update #SHA2_512 b0 st0)).[ 0 ];
( == ) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_512 #M32 b0 st0 0 }
Hacl.Spec.SHA2.update SHA2_512 b0.(| 0 |) st0_m32;
( == ) { lemma_spec_update_384_512 b0.(| 0 |) st0_m32 }
Hacl.Spec.SHA2.update SHA2_384 b0.(| 0 |) st0_m32;
( == ) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_384 #M32 b0 st0 0 }
(state_spec_v #SHA2_384 #M32 (SpecVec.update #SHA2_384 b0 st0)).[ 0 ];
};
state_spec_v_extensionality SHA2_512
(SpecVec.update #SHA2_512 b0 st0)
(SpecVec.update #SHA2_384 #M32 b0 st0) | false |
Hacl.SHA2.Scalar32.Lemmas.fst | Hacl.SHA2.Scalar32.Lemmas.state_spec_v_extensionality | val state_spec_v_extensionality
(a: hash_alg{is_sha2 a})
(acc1 acc2: Hacl.Spec.SHA2.Vec.(state_spec a M32))
: Lemma
(requires
(let open Hacl.Spec.SHA2.Vec in
Lib.Sequence.index (state_spec_v acc1) 0 == Lib.Sequence.index (state_spec_v acc2) 0))
(ensures acc1 == acc2) | val state_spec_v_extensionality
(a: hash_alg{is_sha2 a})
(acc1 acc2: Hacl.Spec.SHA2.Vec.(state_spec a M32))
: Lemma
(requires
(let open Hacl.Spec.SHA2.Vec in
Lib.Sequence.index (state_spec_v acc1) 0 == Lib.Sequence.index (state_spec_v acc2) 0))
(ensures acc1 == acc2) | let state_spec_v_extensionality (a : hash_alg { is_sha2 a })
(acc1: Hacl.Spec.SHA2.Vec.(state_spec a M32))
(acc2: Hacl.Spec.SHA2.Vec.(state_spec a M32)) :
Lemma
(requires (let open Hacl.Spec.SHA2.Vec in
Lib.Sequence.index (state_spec_v acc1) 0 ==
Lib.Sequence.index (state_spec_v acc2) 0))
(ensures acc1 == acc2) =
let open Lib.Sequence in
let open Lib.IntVector in
let open Hacl.Spec.SHA2.Vec in
allow_inversion hash_alg;
let acc1_s = (state_spec_v acc1).[0] <: lseq (word a) 8 in
let acc2_s = (state_spec_v acc2).[0] <: lseq (word a) 8 in
let aux (i:nat{i < 8}) : Lemma (acc1.[i] == acc2.[i]) =
assert (index (vec_v acc1.[i]) 0 == index #(word a) #8 acc1_s i);
assert (index (vec_v acc2.[i]) 0 == index #(word a) #8 acc2_s i);
assert (index (vec_v acc1.[i]) 0 == index (vec_v acc2.[i]) 0);
eq_intro (vec_v acc1.[i]) (vec_v acc2.[i]);
vecv_extensionality acc1.[i] acc2.[i] in
Classical.forall_intro aux;
eq_intro acc1 acc2 | {
"file_name": "code/sha2-mb/Hacl.SHA2.Scalar32.Lemmas.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 20,
"end_line": 34,
"start_col": 0,
"start_line": 10
} | module Hacl.SHA2.Scalar32.Lemmas
open Lib.IntTypes
open Spec.Agile.Hash
open Hacl.Spec.SHA2.Vec
module SpecVec = Hacl.Spec.SHA2.Vec
#push-options "--fuel 0 --ifuel 0 --z3rlimit 50" | {
"checked_file": "/",
"dependencies": [
"Spec.SHA2.Lemmas.fsti.checked",
"Spec.Agile.Hash.fsti.checked",
"prims.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.MultiBuffer.fst.checked",
"Lib.IntVector.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Hacl.Spec.SHA2.Vec.fst.checked",
"Hacl.Spec.SHA2.EquivScalar.fsti.checked",
"Hacl.Spec.SHA2.Equiv.fst.checked",
"Hacl.Spec.SHA2.fst.checked",
"Hacl.Impl.SHA2.Core.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.SHA2.Scalar32.Lemmas.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.SHA2.Vec",
"short_module": "SpecVec"
},
{
"abbrev": false,
"full_module": "Hacl.Spec.SHA2.Vec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile.Hash",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.SHA2.Scalar32",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.SHA2.Scalar32",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Spec.Hash.Definitions.hash_alg{Spec.Hash.Definitions.is_sha2 a} ->
acc1: Hacl.Spec.SHA2.Vec.state_spec a Hacl.Spec.SHA2.Vec.M32 ->
acc2: Hacl.Spec.SHA2.Vec.state_spec a Hacl.Spec.SHA2.Vec.M32
-> FStar.Pervasives.Lemma
(requires
Lib.Sequence.index (Hacl.Spec.SHA2.Vec.state_spec_v acc1) 0 ==
Lib.Sequence.index (Hacl.Spec.SHA2.Vec.state_spec_v acc2) 0) (ensures acc1 == acc2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Spec.Hash.Definitions.hash_alg",
"Prims.b2t",
"Spec.Hash.Definitions.is_sha2",
"Hacl.Spec.SHA2.Vec.state_spec",
"Hacl.Spec.SHA2.Vec.M32",
"Lib.Sequence.eq_intro",
"Hacl.Spec.SHA2.Vec.element_t",
"Prims.unit",
"FStar.Classical.forall_intro",
"Prims.nat",
"Prims.op_LessThan",
"Prims.eq2",
"Prims.l_or",
"FStar.Seq.Base.index",
"Lib.Sequence.to_seq",
"Lib.Sequence.op_String_Access",
"Prims.l_True",
"Prims.squash",
"Lib.Sequence.index",
"Prims.Nil",
"FStar.Pervasives.pattern",
"Lib.IntVector.vecv_extensionality",
"Spec.Hash.Definitions.word_t",
"Hacl.Spec.SHA2.Vec.lanes",
"Lib.IntTypes.uint_t",
"Lib.IntTypes.SEC",
"Lib.IntVector.vec_v",
"Prims._assert",
"Hacl.Spec.SHA2.Vec.word",
"Lib.Sequence.lseq",
"Hacl.Spec.SHA2.Vec.words_state'",
"Hacl.Spec.SHA2.Vec.state_spec_v",
"FStar.Pervasives.allow_inversion"
] | [] | false | false | true | false | false | let state_spec_v_extensionality
(a: hash_alg{is_sha2 a})
(acc1 acc2: Hacl.Spec.SHA2.Vec.(state_spec a M32))
: Lemma
(requires
(let open Hacl.Spec.SHA2.Vec in
Lib.Sequence.index (state_spec_v acc1) 0 == Lib.Sequence.index (state_spec_v acc2) 0))
(ensures acc1 == acc2) =
| let open Lib.Sequence in
let open Lib.IntVector in
let open Hacl.Spec.SHA2.Vec in
allow_inversion hash_alg;
let acc1_s = (state_spec_v acc1).[ 0 ] <: lseq (word a) 8 in
let acc2_s = (state_spec_v acc2).[ 0 ] <: lseq (word a) 8 in
let aux (i: nat{i < 8}) : Lemma (acc1.[ i ] == acc2.[ i ]) =
assert (index (vec_v acc1.[ i ]) 0 == index #(word a) #8 acc1_s i);
assert (index (vec_v acc2.[ i ]) 0 == index #(word a) #8 acc2_s i);
assert (index (vec_v acc1.[ i ]) 0 == index (vec_v acc2.[ i ]) 0);
eq_intro (vec_v acc1.[ i ]) (vec_v acc2.[ i ]);
vecv_extensionality acc1.[ i ] acc2.[ i ]
in
Classical.forall_intro aux;
eq_intro acc1 acc2 | false |
Hacl.SHA2.Scalar32.Lemmas.fst | Hacl.SHA2.Scalar32.Lemmas.lemma_spec_update_nblocks_vec_384_512 | val lemma_spec_update_nblocks_vec_384_512 (len: size_t) (b0 st0: _)
: Lemma
(ensures
(Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_512 len;
SpecVec.update_nblocks #SHA2_384 #M32 (v len) b0 st0 ==
SpecVec.update_nblocks #SHA2_512 #M32 (v len) b0 st0)) | val lemma_spec_update_nblocks_vec_384_512 (len: size_t) (b0 st0: _)
: Lemma
(ensures
(Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_512 len;
SpecVec.update_nblocks #SHA2_384 #M32 (v len) b0 st0 ==
SpecVec.update_nblocks #SHA2_512 #M32 (v len) b0 st0)) | let lemma_spec_update_nblocks_vec_384_512 (len:size_t) b0 st0 : Lemma (ensures (
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_512 len;
SpecVec.update_nblocks #SHA2_384 #M32 (v len) b0 st0 ==
SpecVec.update_nblocks #SHA2_512 #M32 (v len) b0 st0))
= let open Lib.IntTypes in
let open Lib.Sequence in
let open Lib.MultiBuffer in
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_512 len;
let st1 = SpecVec.update_nblocks #SHA2_512 #M32 (v len) b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_512 in
let b0_m32' = Seq.slice b0.(|0|) 0 (Seq.length b0.(|0|) - Seq.length b0.(|0|) % block_length SHA2_512) in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_512 in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update_nblocks #SHA2_512 (v len) b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_512 #M32 (v len) b0 st0 0 }
Hacl.Spec.SHA2.update_nblocks SHA2_512 (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_512 (v len) b0.(|0|) st0_m32 }
Lib.Sequence.repeat_blocks_multi (block_length SHA2_512) b0_m32' (Hacl.Spec.SHA2.update SHA2_512) st0_m32;
(==) {
FStar.Classical.forall_intro_2 lemma_spec_update_384_512;
Lib.Sequence.Lemmas.repeat_blocks_multi_extensionality #uint8 #(words_state SHA2_512) (block_length SHA2_512) b0_m32'
(Hacl.Spec.SHA2.update SHA2_512)
(Hacl.Spec.SHA2.update SHA2_384)
st0_m32
}
Lib.Sequence.repeat_blocks_multi #uint8 #(words_state SHA2_512) (block_length SHA2_512) b0_m32' (Hacl.Spec.SHA2.update SHA2_384) st0_m32;
(==) { }
Lib.Sequence.repeat_blocks_multi #uint8 #(words_state SHA2_384) (block_length SHA2_384) b0_m32' (Hacl.Spec.SHA2.update SHA2_384) (st0_m32 <: words_state SHA2_384);
(==) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_384 (v len) b0.(|0|) st0_m32 }
Hacl.Spec.SHA2.update_nblocks SHA2_384 (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_384 #M32 (v len) b0 st0 0 }
(state_spec_v #SHA2_384 #M32 (SpecVec.update_nblocks #SHA2_384 (v len) b0 st0)).[0];
};
state_spec_v_extensionality SHA2_512
(SpecVec.update_nblocks #SHA2_512 (v len) b0 st0)
(SpecVec.update_nblocks #SHA2_384 #M32 (v len) b0 st0) | {
"file_name": "code/sha2-mb/Hacl.SHA2.Scalar32.Lemmas.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 60,
"end_line": 221,
"start_col": 0,
"start_line": 184
} | module Hacl.SHA2.Scalar32.Lemmas
open Lib.IntTypes
open Spec.Agile.Hash
open Hacl.Spec.SHA2.Vec
module SpecVec = Hacl.Spec.SHA2.Vec
#push-options "--fuel 0 --ifuel 0 --z3rlimit 50"
let state_spec_v_extensionality (a : hash_alg { is_sha2 a })
(acc1: Hacl.Spec.SHA2.Vec.(state_spec a M32))
(acc2: Hacl.Spec.SHA2.Vec.(state_spec a M32)) :
Lemma
(requires (let open Hacl.Spec.SHA2.Vec in
Lib.Sequence.index (state_spec_v acc1) 0 ==
Lib.Sequence.index (state_spec_v acc2) 0))
(ensures acc1 == acc2) =
let open Lib.Sequence in
let open Lib.IntVector in
let open Hacl.Spec.SHA2.Vec in
allow_inversion hash_alg;
let acc1_s = (state_spec_v acc1).[0] <: lseq (word a) 8 in
let acc2_s = (state_spec_v acc2).[0] <: lseq (word a) 8 in
let aux (i:nat{i < 8}) : Lemma (acc1.[i] == acc2.[i]) =
assert (index (vec_v acc1.[i]) 0 == index #(word a) #8 acc1_s i);
assert (index (vec_v acc2.[i]) 0 == index #(word a) #8 acc2_s i);
assert (index (vec_v acc1.[i]) 0 == index (vec_v acc2.[i]) 0);
eq_intro (vec_v acc1.[i]) (vec_v acc2.[i]);
vecv_extensionality acc1.[i] acc2.[i] in
Classical.forall_intro aux;
eq_intro acc1 acc2
let lemma_spec_update_224_256 b st: Lemma (ensures
Hacl.Spec.SHA2.update SHA2_256 b st ==
Hacl.Spec.SHA2.update SHA2_224 b st)
=
calc (==) {
Hacl.Spec.SHA2.update SHA2_256 b st;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_256 b st }
Spec.Agile.Hash.update SHA2_256 st b;
(==) { Spec.SHA2.Lemmas.update_224_256 st b }
Spec.Agile.Hash.update SHA2_224 st b;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_224 b st }
Hacl.Spec.SHA2.update SHA2_224 b st;
}
let lemma_spec_update_vec_224_256 b0 st0 : Lemma (ensures
SpecVec.update #SHA2_224 #M32 b0 st0 ==
SpecVec.update #SHA2_256 #M32 b0 st0)
=
let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update #SHA2_256 #M32 b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_256 in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_256 in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update #SHA2_256 b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_256 #M32 b0 st0 0 }
Hacl.Spec.SHA2.update SHA2_256 b0.(|0|) st0_m32;
(==) { lemma_spec_update_224_256 b0.(|0|) st0_m32 }
Hacl.Spec.SHA2.update SHA2_224 b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_224 #M32 b0 st0 0 }
(state_spec_v #SHA2_224 #M32 (SpecVec.update #SHA2_224 b0 st0)).[0];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update #SHA2_256 b0 st0)
(SpecVec.update #SHA2_224 #M32 b0 st0)
let lemma_spec_update_nblocks_vec_224_256 (len:size_t) b0 st0 : Lemma (ensures (
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
SpecVec.update_nblocks #SHA2_224 #M32 (v len) b0 st0 ==
SpecVec.update_nblocks #SHA2_256 #M32 (v len) b0 st0))
= let open Lib.IntTypes in
let open Lib.Sequence in
let open Lib.MultiBuffer in
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
let st1 = SpecVec.update_nblocks #SHA2_256 #M32 (v len) b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_256 in
let b0_m32' = Seq.slice b0.(|0|) 0 (Seq.length b0.(|0|) - Seq.length b0.(|0|) % block_length SHA2_256) in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_256 in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update_nblocks #SHA2_256 (v len) b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_256 #M32 (v len) b0 st0 0 }
Hacl.Spec.SHA2.update_nblocks SHA2_256 (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_256 (v len) b0.(|0|) st0_m32 }
Lib.Sequence.repeat_blocks_multi (block_length SHA2_256) b0_m32' (Hacl.Spec.SHA2.update SHA2_256) st0_m32;
(==) {
FStar.Classical.forall_intro_2 lemma_spec_update_224_256;
Lib.Sequence.Lemmas.repeat_blocks_multi_extensionality #uint8 #(words_state SHA2_256) (block_length SHA2_256) b0_m32'
(Hacl.Spec.SHA2.update SHA2_256)
(Hacl.Spec.SHA2.update SHA2_224)
st0_m32
}
Lib.Sequence.repeat_blocks_multi #uint8 #(words_state SHA2_256) (block_length SHA2_256) b0_m32' (Hacl.Spec.SHA2.update SHA2_224) st0_m32;
(==) { }
Lib.Sequence.repeat_blocks_multi #uint8 #(words_state SHA2_224) (block_length SHA2_224) b0_m32' (Hacl.Spec.SHA2.update SHA2_224) (st0_m32 <: words_state SHA2_224);
(==) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_224 (v len) b0.(|0|) st0_m32 }
Hacl.Spec.SHA2.update_nblocks SHA2_224 (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_224 #M32 (v len) b0 st0 0 }
(state_spec_v #SHA2_224 #M32 (SpecVec.update_nblocks #SHA2_224 (v len) b0 st0)).[0];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update_nblocks #SHA2_256 (v len) b0 st0)
(SpecVec.update_nblocks #SHA2_224 #M32 (v len) b0 st0)
let lemma_spec_update_last_vec_224_256 totlen (len:size_t{v len <= block_length SHA2_256}) b0 st0 : Lemma (ensures (
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0 ==
SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0))
= let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_256 in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_256 in
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
let hacl_spec_update_224_256 b st: Lemma (ensures
Hacl.Spec.SHA2.update SHA2_256 b st ==
Hacl.Spec.SHA2.update SHA2_224 b st)
[ SMTPat (Hacl.Spec.SHA2.update SHA2_256 b st) ]
=
lemma_spec_update_224_256 b st
in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update_last #SHA2_256 totlen (v len) b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_last_lemma_l #SHA2_256 #M32 totlen (v len) b0 st0 0 }
Hacl.Spec.SHA2.update_last SHA2_256 totlen (v len) b0.(|0|) st0_m32;
(==) { }
Hacl.Spec.SHA2.update_last SHA2_224 totlen (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_last_lemma_l #SHA2_224 #M32 totlen (v len) b0 st0 0 }
(state_spec_v (SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0)).[0];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0)
(SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0)
let lemma_spec_update_384_512 b st: Lemma (ensures
Hacl.Spec.SHA2.update SHA2_512 b st ==
Hacl.Spec.SHA2.update SHA2_384 b st)
=
calc (==) {
Hacl.Spec.SHA2.update SHA2_512 b st;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_512 b st }
Spec.Agile.Hash.update SHA2_512 st b;
(==) { Spec.SHA2.Lemmas.update_384_512 st b }
Spec.Agile.Hash.update SHA2_384 st b;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_384 b st }
Hacl.Spec.SHA2.update SHA2_384 b st;
}
let lemma_spec_update_vec_384_512 b0 st0 : Lemma (ensures
SpecVec.update #SHA2_384 #M32 b0 st0 ==
SpecVec.update #SHA2_512 #M32 b0 st0)
=
let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update #SHA2_512 #M32 b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_512 in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_512 in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update #SHA2_512 b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_512 #M32 b0 st0 0 }
Hacl.Spec.SHA2.update SHA2_512 b0.(|0|) st0_m32;
(==) { lemma_spec_update_384_512 b0.(|0|) st0_m32 }
Hacl.Spec.SHA2.update SHA2_384 b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_384 #M32 b0 st0 0 }
(state_spec_v #SHA2_384 #M32 (SpecVec.update #SHA2_384 b0 st0)).[0];
};
state_spec_v_extensionality SHA2_512
(SpecVec.update #SHA2_512 b0 st0)
(SpecVec.update #SHA2_384 #M32 b0 st0) | {
"checked_file": "/",
"dependencies": [
"Spec.SHA2.Lemmas.fsti.checked",
"Spec.Agile.Hash.fsti.checked",
"prims.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.MultiBuffer.fst.checked",
"Lib.IntVector.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Hacl.Spec.SHA2.Vec.fst.checked",
"Hacl.Spec.SHA2.EquivScalar.fsti.checked",
"Hacl.Spec.SHA2.Equiv.fst.checked",
"Hacl.Spec.SHA2.fst.checked",
"Hacl.Impl.SHA2.Core.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.SHA2.Scalar32.Lemmas.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.SHA2.Vec",
"short_module": "SpecVec"
},
{
"abbrev": false,
"full_module": "Hacl.Spec.SHA2.Vec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile.Hash",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.SHA2.Scalar32",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.SHA2.Scalar32",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
len: Lib.IntTypes.size_t ->
b0:
Hacl.Spec.SHA2.Vec.multiseq (Hacl.Spec.SHA2.Vec.lanes Spec.Hash.Definitions.SHA2_384
Hacl.Spec.SHA2.Vec.M32)
(Lib.IntTypes.v len) ->
st0:
s:
Lib.Sequence.seq (Hacl.Spec.SHA2.Vec.element_t Spec.Hash.Definitions.SHA2_512
Hacl.Spec.SHA2.Vec.M32) {FStar.Seq.Base.length s == 8 /\ FStar.Seq.Base.length s == 8}
-> FStar.Pervasives.Lemma
(ensures
(Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t Spec.Hash.Definitions.SHA2_512 len;
Hacl.Spec.SHA2.Vec.update_nblocks (Lib.IntTypes.v len) b0 st0 ==
Hacl.Spec.SHA2.Vec.update_nblocks (Lib.IntTypes.v len) b0 st0)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Lib.IntTypes.size_t",
"Hacl.Spec.SHA2.Vec.multiseq",
"Hacl.Spec.SHA2.Vec.lanes",
"Spec.Hash.Definitions.SHA2_384",
"Hacl.Spec.SHA2.Vec.M32",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.Sequence.seq",
"Hacl.Spec.SHA2.Vec.element_t",
"Spec.Hash.Definitions.SHA2_512",
"Prims.l_and",
"Prims.eq2",
"Prims.nat",
"FStar.Seq.Base.length",
"Hacl.SHA2.Scalar32.Lemmas.state_spec_v_extensionality",
"Hacl.Spec.SHA2.Vec.update_nblocks",
"Prims.unit",
"FStar.Calc.calc_finish",
"Spec.Hash.Definitions.words_state",
"Lib.Sequence.op_String_Access",
"Hacl.Spec.SHA2.Vec.words_state'",
"Hacl.Spec.SHA2.Vec.state_spec_v",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"FStar.Calc.calc_step",
"Hacl.Spec.SHA2.update_nblocks",
"Lib.MultiBuffer.op_Lens_Access",
"FStar.Seq.Properties.lseq",
"Lib.IntTypes.uint8",
"Lib.Sequence.repeat_blocks_multi",
"Spec.Hash.Definitions.block_length",
"Hacl.Spec.SHA2.update",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l",
"Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi",
"Lib.Sequence.Lemmas.repeat_blocks_multi_extensionality",
"FStar.Classical.forall_intro_2",
"Spec.Hash.Definitions.word",
"Spec.Hash.Definitions.state_word_length",
"Prims.l_or",
"Hacl.SHA2.Scalar32.Lemmas.lemma_spec_update_384_512",
"FStar.Seq.Base.seq",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U8",
"Lib.IntTypes.SEC",
"FStar.Seq.Base.slice",
"Prims.op_Subtraction",
"Prims.op_Modulus",
"Hacl.Spec.SHA2.Vec.state_spec",
"Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t",
"Prims.l_True",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let lemma_spec_update_nblocks_vec_384_512 (len: size_t) b0 st0
: Lemma
(ensures
(Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_512 len;
SpecVec.update_nblocks #SHA2_384 #M32 (v len) b0 st0 ==
SpecVec.update_nblocks #SHA2_512 #M32 (v len) b0 st0)) =
| let open Lib.IntTypes in
let open Lib.Sequence in
let open Lib.MultiBuffer in
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_512 len;
let st1 = SpecVec.update_nblocks #SHA2_512 #M32 (v len) b0 st0 in
let st0_m32 = (state_spec_v st0).[ 0 ] <: words_state SHA2_512 in
let b0_m32' =
Seq.slice b0.(| 0 |) 0 (Seq.length b0.(| 0 |) - Seq.length b0.(| 0 |) % block_length SHA2_512)
in
let st1_m32 = (state_spec_v st1).[ 0 ] <: words_state SHA2_512 in
calc ( == ) {
st1_m32;
( == ) { () }
(state_spec_v (SpecVec.update_nblocks #SHA2_512 (v len) b0 st0)).[ 0 ];
( == ) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_512 #M32 (v len) b0 st0 0 }
Hacl.Spec.SHA2.update_nblocks SHA2_512 (v len) b0.(| 0 |) st0_m32;
( == ) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_512
(v len)
b0.(| 0 |)
st0_m32 }
Lib.Sequence.repeat_blocks_multi (block_length SHA2_512)
b0_m32'
(Hacl.Spec.SHA2.update SHA2_512)
st0_m32;
( == ) { (FStar.Classical.forall_intro_2 lemma_spec_update_384_512;
Lib.Sequence.Lemmas.repeat_blocks_multi_extensionality #uint8
#(words_state SHA2_512)
(block_length SHA2_512)
b0_m32'
(Hacl.Spec.SHA2.update SHA2_512)
(Hacl.Spec.SHA2.update SHA2_384)
st0_m32) }
Lib.Sequence.repeat_blocks_multi #uint8
#(words_state SHA2_512)
(block_length SHA2_512)
b0_m32'
(Hacl.Spec.SHA2.update SHA2_384)
st0_m32;
( == ) { () }
Lib.Sequence.repeat_blocks_multi #uint8
#(words_state SHA2_384)
(block_length SHA2_384)
b0_m32'
(Hacl.Spec.SHA2.update SHA2_384)
(st0_m32 <: words_state SHA2_384);
( == ) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_384
(v len)
b0.(| 0 |)
st0_m32 }
Hacl.Spec.SHA2.update_nblocks SHA2_384 (v len) b0.(| 0 |) st0_m32;
( == ) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_384 #M32 (v len) b0 st0 0 }
(state_spec_v #SHA2_384 #M32 (SpecVec.update_nblocks #SHA2_384 (v len) b0 st0)).[ 0 ];
};
state_spec_v_extensionality SHA2_512
(SpecVec.update_nblocks #SHA2_512 (v len) b0 st0)
(SpecVec.update_nblocks #SHA2_384 #M32 (v len) b0 st0) | false |
Hacl.SHA2.Scalar32.Lemmas.fst | Hacl.SHA2.Scalar32.Lemmas.lemma_spec_update_nblocks_vec_224_256 | val lemma_spec_update_nblocks_vec_224_256 (len: size_t) (b0 st0: _)
: Lemma
(ensures
(Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
SpecVec.update_nblocks #SHA2_224 #M32 (v len) b0 st0 ==
SpecVec.update_nblocks #SHA2_256 #M32 (v len) b0 st0)) | val lemma_spec_update_nblocks_vec_224_256 (len: size_t) (b0 st0: _)
: Lemma
(ensures
(Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
SpecVec.update_nblocks #SHA2_224 #M32 (v len) b0 st0 ==
SpecVec.update_nblocks #SHA2_256 #M32 (v len) b0 st0)) | let lemma_spec_update_nblocks_vec_224_256 (len:size_t) b0 st0 : Lemma (ensures (
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
SpecVec.update_nblocks #SHA2_224 #M32 (v len) b0 st0 ==
SpecVec.update_nblocks #SHA2_256 #M32 (v len) b0 st0))
= let open Lib.IntTypes in
let open Lib.Sequence in
let open Lib.MultiBuffer in
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
let st1 = SpecVec.update_nblocks #SHA2_256 #M32 (v len) b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_256 in
let b0_m32' = Seq.slice b0.(|0|) 0 (Seq.length b0.(|0|) - Seq.length b0.(|0|) % block_length SHA2_256) in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_256 in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update_nblocks #SHA2_256 (v len) b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_256 #M32 (v len) b0 st0 0 }
Hacl.Spec.SHA2.update_nblocks SHA2_256 (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_256 (v len) b0.(|0|) st0_m32 }
Lib.Sequence.repeat_blocks_multi (block_length SHA2_256) b0_m32' (Hacl.Spec.SHA2.update SHA2_256) st0_m32;
(==) {
FStar.Classical.forall_intro_2 lemma_spec_update_224_256;
Lib.Sequence.Lemmas.repeat_blocks_multi_extensionality #uint8 #(words_state SHA2_256) (block_length SHA2_256) b0_m32'
(Hacl.Spec.SHA2.update SHA2_256)
(Hacl.Spec.SHA2.update SHA2_224)
st0_m32
}
Lib.Sequence.repeat_blocks_multi #uint8 #(words_state SHA2_256) (block_length SHA2_256) b0_m32' (Hacl.Spec.SHA2.update SHA2_224) st0_m32;
(==) { }
Lib.Sequence.repeat_blocks_multi #uint8 #(words_state SHA2_224) (block_length SHA2_224) b0_m32' (Hacl.Spec.SHA2.update SHA2_224) (st0_m32 <: words_state SHA2_224);
(==) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_224 (v len) b0.(|0|) st0_m32 }
Hacl.Spec.SHA2.update_nblocks SHA2_224 (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_224 #M32 (v len) b0 st0 0 }
(state_spec_v #SHA2_224 #M32 (SpecVec.update_nblocks #SHA2_224 (v len) b0 st0)).[0];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update_nblocks #SHA2_256 (v len) b0 st0)
(SpecVec.update_nblocks #SHA2_224 #M32 (v len) b0 st0) | {
"file_name": "code/sha2-mb/Hacl.SHA2.Scalar32.Lemmas.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 60,
"end_line": 111,
"start_col": 0,
"start_line": 74
} | module Hacl.SHA2.Scalar32.Lemmas
open Lib.IntTypes
open Spec.Agile.Hash
open Hacl.Spec.SHA2.Vec
module SpecVec = Hacl.Spec.SHA2.Vec
#push-options "--fuel 0 --ifuel 0 --z3rlimit 50"
let state_spec_v_extensionality (a : hash_alg { is_sha2 a })
(acc1: Hacl.Spec.SHA2.Vec.(state_spec a M32))
(acc2: Hacl.Spec.SHA2.Vec.(state_spec a M32)) :
Lemma
(requires (let open Hacl.Spec.SHA2.Vec in
Lib.Sequence.index (state_spec_v acc1) 0 ==
Lib.Sequence.index (state_spec_v acc2) 0))
(ensures acc1 == acc2) =
let open Lib.Sequence in
let open Lib.IntVector in
let open Hacl.Spec.SHA2.Vec in
allow_inversion hash_alg;
let acc1_s = (state_spec_v acc1).[0] <: lseq (word a) 8 in
let acc2_s = (state_spec_v acc2).[0] <: lseq (word a) 8 in
let aux (i:nat{i < 8}) : Lemma (acc1.[i] == acc2.[i]) =
assert (index (vec_v acc1.[i]) 0 == index #(word a) #8 acc1_s i);
assert (index (vec_v acc2.[i]) 0 == index #(word a) #8 acc2_s i);
assert (index (vec_v acc1.[i]) 0 == index (vec_v acc2.[i]) 0);
eq_intro (vec_v acc1.[i]) (vec_v acc2.[i]);
vecv_extensionality acc1.[i] acc2.[i] in
Classical.forall_intro aux;
eq_intro acc1 acc2
let lemma_spec_update_224_256 b st: Lemma (ensures
Hacl.Spec.SHA2.update SHA2_256 b st ==
Hacl.Spec.SHA2.update SHA2_224 b st)
=
calc (==) {
Hacl.Spec.SHA2.update SHA2_256 b st;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_256 b st }
Spec.Agile.Hash.update SHA2_256 st b;
(==) { Spec.SHA2.Lemmas.update_224_256 st b }
Spec.Agile.Hash.update SHA2_224 st b;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_224 b st }
Hacl.Spec.SHA2.update SHA2_224 b st;
}
let lemma_spec_update_vec_224_256 b0 st0 : Lemma (ensures
SpecVec.update #SHA2_224 #M32 b0 st0 ==
SpecVec.update #SHA2_256 #M32 b0 st0)
=
let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update #SHA2_256 #M32 b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_256 in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_256 in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update #SHA2_256 b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_256 #M32 b0 st0 0 }
Hacl.Spec.SHA2.update SHA2_256 b0.(|0|) st0_m32;
(==) { lemma_spec_update_224_256 b0.(|0|) st0_m32 }
Hacl.Spec.SHA2.update SHA2_224 b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_224 #M32 b0 st0 0 }
(state_spec_v #SHA2_224 #M32 (SpecVec.update #SHA2_224 b0 st0)).[0];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update #SHA2_256 b0 st0)
(SpecVec.update #SHA2_224 #M32 b0 st0) | {
"checked_file": "/",
"dependencies": [
"Spec.SHA2.Lemmas.fsti.checked",
"Spec.Agile.Hash.fsti.checked",
"prims.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.MultiBuffer.fst.checked",
"Lib.IntVector.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Hacl.Spec.SHA2.Vec.fst.checked",
"Hacl.Spec.SHA2.EquivScalar.fsti.checked",
"Hacl.Spec.SHA2.Equiv.fst.checked",
"Hacl.Spec.SHA2.fst.checked",
"Hacl.Impl.SHA2.Core.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.SHA2.Scalar32.Lemmas.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.SHA2.Vec",
"short_module": "SpecVec"
},
{
"abbrev": false,
"full_module": "Hacl.Spec.SHA2.Vec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile.Hash",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.SHA2.Scalar32",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.SHA2.Scalar32",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
len: Lib.IntTypes.size_t ->
b0:
Hacl.Spec.SHA2.Vec.multiseq (Hacl.Spec.SHA2.Vec.lanes Spec.Hash.Definitions.SHA2_224
Hacl.Spec.SHA2.Vec.M32)
(Lib.IntTypes.v len) ->
st0:
s:
Lib.Sequence.seq (Hacl.Spec.SHA2.Vec.element_t Spec.Hash.Definitions.SHA2_256
Hacl.Spec.SHA2.Vec.M32) {FStar.Seq.Base.length s == 8 /\ FStar.Seq.Base.length s == 8}
-> FStar.Pervasives.Lemma
(ensures
(Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t Spec.Hash.Definitions.SHA2_256 len;
Hacl.Spec.SHA2.Vec.update_nblocks (Lib.IntTypes.v len) b0 st0 ==
Hacl.Spec.SHA2.Vec.update_nblocks (Lib.IntTypes.v len) b0 st0)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Lib.IntTypes.size_t",
"Hacl.Spec.SHA2.Vec.multiseq",
"Hacl.Spec.SHA2.Vec.lanes",
"Spec.Hash.Definitions.SHA2_224",
"Hacl.Spec.SHA2.Vec.M32",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.Sequence.seq",
"Hacl.Spec.SHA2.Vec.element_t",
"Spec.Hash.Definitions.SHA2_256",
"Prims.l_and",
"Prims.eq2",
"Prims.nat",
"FStar.Seq.Base.length",
"Hacl.SHA2.Scalar32.Lemmas.state_spec_v_extensionality",
"Hacl.Spec.SHA2.Vec.update_nblocks",
"Prims.unit",
"FStar.Calc.calc_finish",
"Spec.Hash.Definitions.words_state",
"Lib.Sequence.op_String_Access",
"Hacl.Spec.SHA2.Vec.words_state'",
"Hacl.Spec.SHA2.Vec.state_spec_v",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"FStar.Calc.calc_step",
"Hacl.Spec.SHA2.update_nblocks",
"Lib.MultiBuffer.op_Lens_Access",
"FStar.Seq.Properties.lseq",
"Lib.IntTypes.uint8",
"Lib.Sequence.repeat_blocks_multi",
"Spec.Hash.Definitions.block_length",
"Hacl.Spec.SHA2.update",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l",
"Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi",
"Lib.Sequence.Lemmas.repeat_blocks_multi_extensionality",
"FStar.Classical.forall_intro_2",
"Spec.Hash.Definitions.word",
"Spec.Hash.Definitions.state_word_length",
"Prims.l_or",
"Hacl.SHA2.Scalar32.Lemmas.lemma_spec_update_224_256",
"FStar.Seq.Base.seq",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U8",
"Lib.IntTypes.SEC",
"FStar.Seq.Base.slice",
"Prims.op_Subtraction",
"Prims.op_Modulus",
"Hacl.Spec.SHA2.Vec.state_spec",
"Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t",
"Prims.l_True",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let lemma_spec_update_nblocks_vec_224_256 (len: size_t) b0 st0
: Lemma
(ensures
(Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
SpecVec.update_nblocks #SHA2_224 #M32 (v len) b0 st0 ==
SpecVec.update_nblocks #SHA2_256 #M32 (v len) b0 st0)) =
| let open Lib.IntTypes in
let open Lib.Sequence in
let open Lib.MultiBuffer in
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
let st1 = SpecVec.update_nblocks #SHA2_256 #M32 (v len) b0 st0 in
let st0_m32 = (state_spec_v st0).[ 0 ] <: words_state SHA2_256 in
let b0_m32' =
Seq.slice b0.(| 0 |) 0 (Seq.length b0.(| 0 |) - Seq.length b0.(| 0 |) % block_length SHA2_256)
in
let st1_m32 = (state_spec_v st1).[ 0 ] <: words_state SHA2_256 in
calc ( == ) {
st1_m32;
( == ) { () }
(state_spec_v (SpecVec.update_nblocks #SHA2_256 (v len) b0 st0)).[ 0 ];
( == ) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_256 #M32 (v len) b0 st0 0 }
Hacl.Spec.SHA2.update_nblocks SHA2_256 (v len) b0.(| 0 |) st0_m32;
( == ) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_256
(v len)
b0.(| 0 |)
st0_m32 }
Lib.Sequence.repeat_blocks_multi (block_length SHA2_256)
b0_m32'
(Hacl.Spec.SHA2.update SHA2_256)
st0_m32;
( == ) { (FStar.Classical.forall_intro_2 lemma_spec_update_224_256;
Lib.Sequence.Lemmas.repeat_blocks_multi_extensionality #uint8
#(words_state SHA2_256)
(block_length SHA2_256)
b0_m32'
(Hacl.Spec.SHA2.update SHA2_256)
(Hacl.Spec.SHA2.update SHA2_224)
st0_m32) }
Lib.Sequence.repeat_blocks_multi #uint8
#(words_state SHA2_256)
(block_length SHA2_256)
b0_m32'
(Hacl.Spec.SHA2.update SHA2_224)
st0_m32;
( == ) { () }
Lib.Sequence.repeat_blocks_multi #uint8
#(words_state SHA2_224)
(block_length SHA2_224)
b0_m32'
(Hacl.Spec.SHA2.update SHA2_224)
(st0_m32 <: words_state SHA2_224);
( == ) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_224
(v len)
b0.(| 0 |)
st0_m32 }
Hacl.Spec.SHA2.update_nblocks SHA2_224 (v len) b0.(| 0 |) st0_m32;
( == ) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_224 #M32 (v len) b0 st0 0 }
(state_spec_v #SHA2_224 #M32 (SpecVec.update_nblocks #SHA2_224 (v len) b0 st0)).[ 0 ];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update_nblocks #SHA2_256 (v len) b0 st0)
(SpecVec.update_nblocks #SHA2_224 #M32 (v len) b0 st0) | false |
Hacl.SHA2.Scalar32.Lemmas.fst | Hacl.SHA2.Scalar32.Lemmas.lemma_spec_update_last_vec_224_256 | val lemma_spec_update_last_vec_224_256
(totlen: _)
(len: size_t{v len <= block_length SHA2_256})
(b0 st0: _)
: Lemma
(ensures
(Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0 ==
SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0)) | val lemma_spec_update_last_vec_224_256
(totlen: _)
(len: size_t{v len <= block_length SHA2_256})
(b0 st0: _)
: Lemma
(ensures
(Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0 ==
SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0)) | let lemma_spec_update_last_vec_224_256 totlen (len:size_t{v len <= block_length SHA2_256}) b0 st0 : Lemma (ensures (
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0 ==
SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0))
= let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_256 in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_256 in
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
let hacl_spec_update_224_256 b st: Lemma (ensures
Hacl.Spec.SHA2.update SHA2_256 b st ==
Hacl.Spec.SHA2.update SHA2_224 b st)
[ SMTPat (Hacl.Spec.SHA2.update SHA2_256 b st) ]
=
lemma_spec_update_224_256 b st
in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update_last #SHA2_256 totlen (v len) b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_last_lemma_l #SHA2_256 #M32 totlen (v len) b0 st0 0 }
Hacl.Spec.SHA2.update_last SHA2_256 totlen (v len) b0.(|0|) st0_m32;
(==) { }
Hacl.Spec.SHA2.update_last SHA2_224 totlen (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_last_lemma_l #SHA2_224 #M32 totlen (v len) b0 st0 0 }
(state_spec_v (SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0)).[0];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0)
(SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0) | {
"file_name": "code/sha2-mb/Hacl.SHA2.Scalar32.Lemmas.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 64,
"end_line": 143,
"start_col": 0,
"start_line": 113
} | module Hacl.SHA2.Scalar32.Lemmas
open Lib.IntTypes
open Spec.Agile.Hash
open Hacl.Spec.SHA2.Vec
module SpecVec = Hacl.Spec.SHA2.Vec
#push-options "--fuel 0 --ifuel 0 --z3rlimit 50"
let state_spec_v_extensionality (a : hash_alg { is_sha2 a })
(acc1: Hacl.Spec.SHA2.Vec.(state_spec a M32))
(acc2: Hacl.Spec.SHA2.Vec.(state_spec a M32)) :
Lemma
(requires (let open Hacl.Spec.SHA2.Vec in
Lib.Sequence.index (state_spec_v acc1) 0 ==
Lib.Sequence.index (state_spec_v acc2) 0))
(ensures acc1 == acc2) =
let open Lib.Sequence in
let open Lib.IntVector in
let open Hacl.Spec.SHA2.Vec in
allow_inversion hash_alg;
let acc1_s = (state_spec_v acc1).[0] <: lseq (word a) 8 in
let acc2_s = (state_spec_v acc2).[0] <: lseq (word a) 8 in
let aux (i:nat{i < 8}) : Lemma (acc1.[i] == acc2.[i]) =
assert (index (vec_v acc1.[i]) 0 == index #(word a) #8 acc1_s i);
assert (index (vec_v acc2.[i]) 0 == index #(word a) #8 acc2_s i);
assert (index (vec_v acc1.[i]) 0 == index (vec_v acc2.[i]) 0);
eq_intro (vec_v acc1.[i]) (vec_v acc2.[i]);
vecv_extensionality acc1.[i] acc2.[i] in
Classical.forall_intro aux;
eq_intro acc1 acc2
let lemma_spec_update_224_256 b st: Lemma (ensures
Hacl.Spec.SHA2.update SHA2_256 b st ==
Hacl.Spec.SHA2.update SHA2_224 b st)
=
calc (==) {
Hacl.Spec.SHA2.update SHA2_256 b st;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_256 b st }
Spec.Agile.Hash.update SHA2_256 st b;
(==) { Spec.SHA2.Lemmas.update_224_256 st b }
Spec.Agile.Hash.update SHA2_224 st b;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_224 b st }
Hacl.Spec.SHA2.update SHA2_224 b st;
}
let lemma_spec_update_vec_224_256 b0 st0 : Lemma (ensures
SpecVec.update #SHA2_224 #M32 b0 st0 ==
SpecVec.update #SHA2_256 #M32 b0 st0)
=
let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update #SHA2_256 #M32 b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_256 in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_256 in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update #SHA2_256 b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_256 #M32 b0 st0 0 }
Hacl.Spec.SHA2.update SHA2_256 b0.(|0|) st0_m32;
(==) { lemma_spec_update_224_256 b0.(|0|) st0_m32 }
Hacl.Spec.SHA2.update SHA2_224 b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_224 #M32 b0 st0 0 }
(state_spec_v #SHA2_224 #M32 (SpecVec.update #SHA2_224 b0 st0)).[0];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update #SHA2_256 b0 st0)
(SpecVec.update #SHA2_224 #M32 b0 st0)
let lemma_spec_update_nblocks_vec_224_256 (len:size_t) b0 st0 : Lemma (ensures (
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
SpecVec.update_nblocks #SHA2_224 #M32 (v len) b0 st0 ==
SpecVec.update_nblocks #SHA2_256 #M32 (v len) b0 st0))
= let open Lib.IntTypes in
let open Lib.Sequence in
let open Lib.MultiBuffer in
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
let st1 = SpecVec.update_nblocks #SHA2_256 #M32 (v len) b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_256 in
let b0_m32' = Seq.slice b0.(|0|) 0 (Seq.length b0.(|0|) - Seq.length b0.(|0|) % block_length SHA2_256) in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_256 in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update_nblocks #SHA2_256 (v len) b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_256 #M32 (v len) b0 st0 0 }
Hacl.Spec.SHA2.update_nblocks SHA2_256 (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_256 (v len) b0.(|0|) st0_m32 }
Lib.Sequence.repeat_blocks_multi (block_length SHA2_256) b0_m32' (Hacl.Spec.SHA2.update SHA2_256) st0_m32;
(==) {
FStar.Classical.forall_intro_2 lemma_spec_update_224_256;
Lib.Sequence.Lemmas.repeat_blocks_multi_extensionality #uint8 #(words_state SHA2_256) (block_length SHA2_256) b0_m32'
(Hacl.Spec.SHA2.update SHA2_256)
(Hacl.Spec.SHA2.update SHA2_224)
st0_m32
}
Lib.Sequence.repeat_blocks_multi #uint8 #(words_state SHA2_256) (block_length SHA2_256) b0_m32' (Hacl.Spec.SHA2.update SHA2_224) st0_m32;
(==) { }
Lib.Sequence.repeat_blocks_multi #uint8 #(words_state SHA2_224) (block_length SHA2_224) b0_m32' (Hacl.Spec.SHA2.update SHA2_224) (st0_m32 <: words_state SHA2_224);
(==) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_224 (v len) b0.(|0|) st0_m32 }
Hacl.Spec.SHA2.update_nblocks SHA2_224 (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_224 #M32 (v len) b0 st0 0 }
(state_spec_v #SHA2_224 #M32 (SpecVec.update_nblocks #SHA2_224 (v len) b0 st0)).[0];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update_nblocks #SHA2_256 (v len) b0 st0)
(SpecVec.update_nblocks #SHA2_224 #M32 (v len) b0 st0) | {
"checked_file": "/",
"dependencies": [
"Spec.SHA2.Lemmas.fsti.checked",
"Spec.Agile.Hash.fsti.checked",
"prims.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.MultiBuffer.fst.checked",
"Lib.IntVector.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Hacl.Spec.SHA2.Vec.fst.checked",
"Hacl.Spec.SHA2.EquivScalar.fsti.checked",
"Hacl.Spec.SHA2.Equiv.fst.checked",
"Hacl.Spec.SHA2.fst.checked",
"Hacl.Impl.SHA2.Core.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.SHA2.Scalar32.Lemmas.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.SHA2.Vec",
"short_module": "SpecVec"
},
{
"abbrev": false,
"full_module": "Hacl.Spec.SHA2.Vec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile.Hash",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.SHA2.Scalar32",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.SHA2.Scalar32",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
totlen: Spec.Hash.Definitions.len_t Spec.Hash.Definitions.SHA2_224 ->
len:
Lib.IntTypes.size_t
{Lib.IntTypes.v len <= Spec.Hash.Definitions.block_length Spec.Hash.Definitions.SHA2_256} ->
b0:
Hacl.Spec.SHA2.Vec.multiseq (Hacl.Spec.SHA2.Vec.lanes Spec.Hash.Definitions.SHA2_224
Hacl.Spec.SHA2.Vec.M32)
(Lib.IntTypes.v len) ->
st0:
s:
Lib.Sequence.seq (Hacl.Spec.SHA2.Vec.element_t Spec.Hash.Definitions.SHA2_256
Hacl.Spec.SHA2.Vec.M32) {FStar.Seq.Base.length s == 8 /\ FStar.Seq.Base.length s == 8}
-> FStar.Pervasives.Lemma
(ensures
(Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t Spec.Hash.Definitions.SHA2_256 len;
Hacl.Spec.SHA2.Vec.update_last totlen (Lib.IntTypes.v len) b0 st0 ==
Hacl.Spec.SHA2.Vec.update_last totlen (Lib.IntTypes.v len) b0 st0)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Spec.Hash.Definitions.len_t",
"Spec.Hash.Definitions.SHA2_224",
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Spec.Hash.Definitions.block_length",
"Spec.Hash.Definitions.SHA2_256",
"Hacl.Spec.SHA2.Vec.multiseq",
"Hacl.Spec.SHA2.Vec.lanes",
"Hacl.Spec.SHA2.Vec.M32",
"Lib.Sequence.seq",
"Hacl.Spec.SHA2.Vec.element_t",
"Prims.l_and",
"Prims.eq2",
"Prims.nat",
"FStar.Seq.Base.length",
"Hacl.SHA2.Scalar32.Lemmas.state_spec_v_extensionality",
"Hacl.Spec.SHA2.Vec.update_last",
"Prims.unit",
"FStar.Calc.calc_finish",
"Spec.Hash.Definitions.words_state",
"Lib.Sequence.op_String_Access",
"Hacl.Spec.SHA2.Vec.words_state'",
"Hacl.Spec.SHA2.Vec.state_spec_v",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"FStar.Calc.calc_step",
"Hacl.Spec.SHA2.update_last",
"Lib.MultiBuffer.op_Lens_Access",
"FStar.Seq.Properties.lseq",
"Lib.IntTypes.uint8",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"Hacl.Spec.SHA2.Equiv.update_last_lemma_l",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U8",
"Lib.IntTypes.SEC",
"Spec.Hash.Definitions.word",
"Spec.Hash.Definitions.state_word_length",
"Prims.l_True",
"Prims.l_or",
"Hacl.Spec.SHA2.update",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Hacl.SHA2.Scalar32.Lemmas.lemma_spec_update_224_256",
"Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t",
"Hacl.Spec.SHA2.Vec.state_spec"
] | [] | false | false | true | false | false | let lemma_spec_update_last_vec_224_256 totlen (len: size_t{v len <= block_length SHA2_256}) b0 st0
: Lemma
(ensures
(Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0 ==
SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0)) =
| let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0 in
let st0_m32 = (state_spec_v st0).[ 0 ] <: words_state SHA2_256 in
let st1_m32 = (state_spec_v st1).[ 0 ] <: words_state SHA2_256 in
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
let hacl_spec_update_224_256 b st
: Lemma (ensures Hacl.Spec.SHA2.update SHA2_256 b st == Hacl.Spec.SHA2.update SHA2_224 b st)
[SMTPat (Hacl.Spec.SHA2.update SHA2_256 b st)] =
lemma_spec_update_224_256 b st
in
calc ( == ) {
st1_m32;
( == ) { () }
(state_spec_v (SpecVec.update_last #SHA2_256 totlen (v len) b0 st0)).[ 0 ];
( == ) { Hacl.Spec.SHA2.Equiv.update_last_lemma_l #SHA2_256 #M32 totlen (v len) b0 st0 0 }
Hacl.Spec.SHA2.update_last SHA2_256 totlen (v len) b0.(| 0 |) st0_m32;
( == ) { () }
Hacl.Spec.SHA2.update_last SHA2_224 totlen (v len) b0.(| 0 |) st0_m32;
( == ) { Hacl.Spec.SHA2.Equiv.update_last_lemma_l #SHA2_224 #M32 totlen (v len) b0 st0 0 }
(state_spec_v (SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0)).[ 0 ];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0)
(SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0) | false |
Spec.Chacha20.fst | Spec.Chacha20.size_key | val size_key : Prims.int | let size_key = 32 | {
"file_name": "specs/Spec.Chacha20.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 17,
"end_line": 14,
"start_col": 0,
"start_line": 14
} | module Spec.Chacha20
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Lib.LoopCombinators
#set-options "--max_fuel 0 --z3rlimit 100"
/// Constants and Types | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Chacha20.fst"
} | [
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.int | Prims.Tot | [
"total"
] | [] | [] | [] | false | false | false | true | false | let size_key =
| 32 | false |
|
Spec.Chacha20.fst | Spec.Chacha20.size_nonce | val size_nonce : Prims.int | let size_nonce = 12 | {
"file_name": "specs/Spec.Chacha20.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 19,
"end_line": 16,
"start_col": 0,
"start_line": 16
} | module Spec.Chacha20
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Lib.LoopCombinators
#set-options "--max_fuel 0 --z3rlimit 100"
/// Constants and Types
let size_key = 32 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Chacha20.fst"
} | [
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.int | Prims.Tot | [
"total"
] | [] | [] | [] | false | false | false | true | false | let size_nonce =
| 12 | false |
|
Spec.Chacha20.fst | Spec.Chacha20.size_block | val size_block : Prims.int | let size_block = 64 | {
"file_name": "specs/Spec.Chacha20.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 19,
"end_line": 15,
"start_col": 0,
"start_line": 15
} | module Spec.Chacha20
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Lib.LoopCombinators
#set-options "--max_fuel 0 --z3rlimit 100"
/// Constants and Types | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Chacha20.fst"
} | [
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.int | Prims.Tot | [
"total"
] | [] | [] | [] | false | false | false | true | false | let size_block =
| 64 | false |
|
Spec.Chacha20.fst | Spec.Chacha20.keylen | val keylen : Prims.int | let keylen = 32 | {
"file_name": "specs/Spec.Chacha20.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 15,
"end_line": 19,
"start_col": 0,
"start_line": 19
} | module Spec.Chacha20
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Lib.LoopCombinators
#set-options "--max_fuel 0 --z3rlimit 100"
/// Constants and Types
let size_key = 32
let size_block = 64
let size_nonce = 12 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Chacha20.fst"
} | [
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.int | Prims.Tot | [
"total"
] | [] | [] | [] | false | false | false | true | false | let keylen =
| 32 | false |
|
Hacl.SHA2.Scalar32.Lemmas.fst | Hacl.SHA2.Scalar32.Lemmas.lemma_spec_update_last_vec_384_512 | val lemma_spec_update_last_vec_384_512
(totlen: _)
(len: size_t{v len <= block_length SHA2_512})
(b0 st0: _)
: Lemma
(ensures
(Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_512 len;
SpecVec.update_last #SHA2_384 #M32 totlen (v len) b0 st0 ==
SpecVec.update_last #SHA2_512 #M32 totlen (v len) b0 st0)) | val lemma_spec_update_last_vec_384_512
(totlen: _)
(len: size_t{v len <= block_length SHA2_512})
(b0 st0: _)
: Lemma
(ensures
(Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_512 len;
SpecVec.update_last #SHA2_384 #M32 totlen (v len) b0 st0 ==
SpecVec.update_last #SHA2_512 #M32 totlen (v len) b0 st0)) | let lemma_spec_update_last_vec_384_512 totlen (len:size_t{v len <= block_length SHA2_512}) b0 st0 : Lemma (ensures (
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_512 len;
SpecVec.update_last #SHA2_384 #M32 totlen (v len) b0 st0 ==
SpecVec.update_last #SHA2_512 #M32 totlen (v len) b0 st0))
= let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update_last #SHA2_512 #M32 totlen (v len) b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_512 in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_512 in
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_512 len;
let hacl_spec_update_384_512 b st: Lemma (ensures
Hacl.Spec.SHA2.update SHA2_512 b st ==
Hacl.Spec.SHA2.update SHA2_384 b st)
[ SMTPat (Hacl.Spec.SHA2.update SHA2_512 b st) ]
=
lemma_spec_update_384_512 b st
in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update_last #SHA2_512 totlen (v len) b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_last_lemma_l #SHA2_512 #M32 totlen (v len) b0 st0 0 }
Hacl.Spec.SHA2.update_last SHA2_512 totlen (v len) b0.(|0|) st0_m32;
(==) { }
Hacl.Spec.SHA2.update_last SHA2_384 totlen (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_last_lemma_l #SHA2_384 #M32 totlen (v len) b0 st0 0 }
(state_spec_v (SpecVec.update_last #SHA2_384 #M32 totlen (v len) b0 st0)).[0];
};
state_spec_v_extensionality SHA2_512
(SpecVec.update_last #SHA2_384 #M32 totlen (v len) b0 st0)
(SpecVec.update_last #SHA2_512 #M32 totlen (v len) b0 st0) | {
"file_name": "code/sha2-mb/Hacl.SHA2.Scalar32.Lemmas.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 64,
"end_line": 253,
"start_col": 0,
"start_line": 223
} | module Hacl.SHA2.Scalar32.Lemmas
open Lib.IntTypes
open Spec.Agile.Hash
open Hacl.Spec.SHA2.Vec
module SpecVec = Hacl.Spec.SHA2.Vec
#push-options "--fuel 0 --ifuel 0 --z3rlimit 50"
let state_spec_v_extensionality (a : hash_alg { is_sha2 a })
(acc1: Hacl.Spec.SHA2.Vec.(state_spec a M32))
(acc2: Hacl.Spec.SHA2.Vec.(state_spec a M32)) :
Lemma
(requires (let open Hacl.Spec.SHA2.Vec in
Lib.Sequence.index (state_spec_v acc1) 0 ==
Lib.Sequence.index (state_spec_v acc2) 0))
(ensures acc1 == acc2) =
let open Lib.Sequence in
let open Lib.IntVector in
let open Hacl.Spec.SHA2.Vec in
allow_inversion hash_alg;
let acc1_s = (state_spec_v acc1).[0] <: lseq (word a) 8 in
let acc2_s = (state_spec_v acc2).[0] <: lseq (word a) 8 in
let aux (i:nat{i < 8}) : Lemma (acc1.[i] == acc2.[i]) =
assert (index (vec_v acc1.[i]) 0 == index #(word a) #8 acc1_s i);
assert (index (vec_v acc2.[i]) 0 == index #(word a) #8 acc2_s i);
assert (index (vec_v acc1.[i]) 0 == index (vec_v acc2.[i]) 0);
eq_intro (vec_v acc1.[i]) (vec_v acc2.[i]);
vecv_extensionality acc1.[i] acc2.[i] in
Classical.forall_intro aux;
eq_intro acc1 acc2
let lemma_spec_update_224_256 b st: Lemma (ensures
Hacl.Spec.SHA2.update SHA2_256 b st ==
Hacl.Spec.SHA2.update SHA2_224 b st)
=
calc (==) {
Hacl.Spec.SHA2.update SHA2_256 b st;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_256 b st }
Spec.Agile.Hash.update SHA2_256 st b;
(==) { Spec.SHA2.Lemmas.update_224_256 st b }
Spec.Agile.Hash.update SHA2_224 st b;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_224 b st }
Hacl.Spec.SHA2.update SHA2_224 b st;
}
let lemma_spec_update_vec_224_256 b0 st0 : Lemma (ensures
SpecVec.update #SHA2_224 #M32 b0 st0 ==
SpecVec.update #SHA2_256 #M32 b0 st0)
=
let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update #SHA2_256 #M32 b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_256 in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_256 in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update #SHA2_256 b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_256 #M32 b0 st0 0 }
Hacl.Spec.SHA2.update SHA2_256 b0.(|0|) st0_m32;
(==) { lemma_spec_update_224_256 b0.(|0|) st0_m32 }
Hacl.Spec.SHA2.update SHA2_224 b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_224 #M32 b0 st0 0 }
(state_spec_v #SHA2_224 #M32 (SpecVec.update #SHA2_224 b0 st0)).[0];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update #SHA2_256 b0 st0)
(SpecVec.update #SHA2_224 #M32 b0 st0)
let lemma_spec_update_nblocks_vec_224_256 (len:size_t) b0 st0 : Lemma (ensures (
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
SpecVec.update_nblocks #SHA2_224 #M32 (v len) b0 st0 ==
SpecVec.update_nblocks #SHA2_256 #M32 (v len) b0 st0))
= let open Lib.IntTypes in
let open Lib.Sequence in
let open Lib.MultiBuffer in
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
let st1 = SpecVec.update_nblocks #SHA2_256 #M32 (v len) b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_256 in
let b0_m32' = Seq.slice b0.(|0|) 0 (Seq.length b0.(|0|) - Seq.length b0.(|0|) % block_length SHA2_256) in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_256 in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update_nblocks #SHA2_256 (v len) b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_256 #M32 (v len) b0 st0 0 }
Hacl.Spec.SHA2.update_nblocks SHA2_256 (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_256 (v len) b0.(|0|) st0_m32 }
Lib.Sequence.repeat_blocks_multi (block_length SHA2_256) b0_m32' (Hacl.Spec.SHA2.update SHA2_256) st0_m32;
(==) {
FStar.Classical.forall_intro_2 lemma_spec_update_224_256;
Lib.Sequence.Lemmas.repeat_blocks_multi_extensionality #uint8 #(words_state SHA2_256) (block_length SHA2_256) b0_m32'
(Hacl.Spec.SHA2.update SHA2_256)
(Hacl.Spec.SHA2.update SHA2_224)
st0_m32
}
Lib.Sequence.repeat_blocks_multi #uint8 #(words_state SHA2_256) (block_length SHA2_256) b0_m32' (Hacl.Spec.SHA2.update SHA2_224) st0_m32;
(==) { }
Lib.Sequence.repeat_blocks_multi #uint8 #(words_state SHA2_224) (block_length SHA2_224) b0_m32' (Hacl.Spec.SHA2.update SHA2_224) (st0_m32 <: words_state SHA2_224);
(==) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_224 (v len) b0.(|0|) st0_m32 }
Hacl.Spec.SHA2.update_nblocks SHA2_224 (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_224 #M32 (v len) b0 st0 0 }
(state_spec_v #SHA2_224 #M32 (SpecVec.update_nblocks #SHA2_224 (v len) b0 st0)).[0];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update_nblocks #SHA2_256 (v len) b0 st0)
(SpecVec.update_nblocks #SHA2_224 #M32 (v len) b0 st0)
let lemma_spec_update_last_vec_224_256 totlen (len:size_t{v len <= block_length SHA2_256}) b0 st0 : Lemma (ensures (
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0 ==
SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0))
= let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_256 in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_256 in
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_256 len;
let hacl_spec_update_224_256 b st: Lemma (ensures
Hacl.Spec.SHA2.update SHA2_256 b st ==
Hacl.Spec.SHA2.update SHA2_224 b st)
[ SMTPat (Hacl.Spec.SHA2.update SHA2_256 b st) ]
=
lemma_spec_update_224_256 b st
in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update_last #SHA2_256 totlen (v len) b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_last_lemma_l #SHA2_256 #M32 totlen (v len) b0 st0 0 }
Hacl.Spec.SHA2.update_last SHA2_256 totlen (v len) b0.(|0|) st0_m32;
(==) { }
Hacl.Spec.SHA2.update_last SHA2_224 totlen (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_last_lemma_l #SHA2_224 #M32 totlen (v len) b0 st0 0 }
(state_spec_v (SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0)).[0];
};
state_spec_v_extensionality SHA2_256
(SpecVec.update_last #SHA2_224 #M32 totlen (v len) b0 st0)
(SpecVec.update_last #SHA2_256 #M32 totlen (v len) b0 st0)
let lemma_spec_update_384_512 b st: Lemma (ensures
Hacl.Spec.SHA2.update SHA2_512 b st ==
Hacl.Spec.SHA2.update SHA2_384 b st)
=
calc (==) {
Hacl.Spec.SHA2.update SHA2_512 b st;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_512 b st }
Spec.Agile.Hash.update SHA2_512 st b;
(==) { Spec.SHA2.Lemmas.update_384_512 st b }
Spec.Agile.Hash.update SHA2_384 st b;
(==) { Hacl.Spec.SHA2.EquivScalar.update_lemma SHA2_384 b st }
Hacl.Spec.SHA2.update SHA2_384 b st;
}
let lemma_spec_update_vec_384_512 b0 st0 : Lemma (ensures
SpecVec.update #SHA2_384 #M32 b0 st0 ==
SpecVec.update #SHA2_512 #M32 b0 st0)
=
let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update #SHA2_512 #M32 b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_512 in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_512 in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update #SHA2_512 b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_512 #M32 b0 st0 0 }
Hacl.Spec.SHA2.update SHA2_512 b0.(|0|) st0_m32;
(==) { lemma_spec_update_384_512 b0.(|0|) st0_m32 }
Hacl.Spec.SHA2.update SHA2_384 b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_lemma_l #SHA2_384 #M32 b0 st0 0 }
(state_spec_v #SHA2_384 #M32 (SpecVec.update #SHA2_384 b0 st0)).[0];
};
state_spec_v_extensionality SHA2_512
(SpecVec.update #SHA2_512 b0 st0)
(SpecVec.update #SHA2_384 #M32 b0 st0)
let lemma_spec_update_nblocks_vec_384_512 (len:size_t) b0 st0 : Lemma (ensures (
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_512 len;
SpecVec.update_nblocks #SHA2_384 #M32 (v len) b0 st0 ==
SpecVec.update_nblocks #SHA2_512 #M32 (v len) b0 st0))
= let open Lib.IntTypes in
let open Lib.Sequence in
let open Lib.MultiBuffer in
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_512 len;
let st1 = SpecVec.update_nblocks #SHA2_512 #M32 (v len) b0 st0 in
let st0_m32 = (state_spec_v st0).[0] <: words_state SHA2_512 in
let b0_m32' = Seq.slice b0.(|0|) 0 (Seq.length b0.(|0|) - Seq.length b0.(|0|) % block_length SHA2_512) in
let st1_m32 = (state_spec_v st1).[0] <: words_state SHA2_512 in
calc (==) {
st1_m32;
(==) {}
(state_spec_v (SpecVec.update_nblocks #SHA2_512 (v len) b0 st0)).[0];
(==) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_512 #M32 (v len) b0 st0 0 }
Hacl.Spec.SHA2.update_nblocks SHA2_512 (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_512 (v len) b0.(|0|) st0_m32 }
Lib.Sequence.repeat_blocks_multi (block_length SHA2_512) b0_m32' (Hacl.Spec.SHA2.update SHA2_512) st0_m32;
(==) {
FStar.Classical.forall_intro_2 lemma_spec_update_384_512;
Lib.Sequence.Lemmas.repeat_blocks_multi_extensionality #uint8 #(words_state SHA2_512) (block_length SHA2_512) b0_m32'
(Hacl.Spec.SHA2.update SHA2_512)
(Hacl.Spec.SHA2.update SHA2_384)
st0_m32
}
Lib.Sequence.repeat_blocks_multi #uint8 #(words_state SHA2_512) (block_length SHA2_512) b0_m32' (Hacl.Spec.SHA2.update SHA2_384) st0_m32;
(==) { }
Lib.Sequence.repeat_blocks_multi #uint8 #(words_state SHA2_384) (block_length SHA2_384) b0_m32' (Hacl.Spec.SHA2.update SHA2_384) (st0_m32 <: words_state SHA2_384);
(==) { Hacl.Spec.SHA2.EquivScalar.update_nblocks_is_repeat_blocks_multi SHA2_384 (v len) b0.(|0|) st0_m32 }
Hacl.Spec.SHA2.update_nblocks SHA2_384 (v len) b0.(|0|) st0_m32;
(==) { Hacl.Spec.SHA2.Equiv.update_nblocks_lemma_l #SHA2_384 #M32 (v len) b0 st0 0 }
(state_spec_v #SHA2_384 #M32 (SpecVec.update_nblocks #SHA2_384 (v len) b0 st0)).[0];
};
state_spec_v_extensionality SHA2_512
(SpecVec.update_nblocks #SHA2_512 (v len) b0 st0)
(SpecVec.update_nblocks #SHA2_384 #M32 (v len) b0 st0) | {
"checked_file": "/",
"dependencies": [
"Spec.SHA2.Lemmas.fsti.checked",
"Spec.Agile.Hash.fsti.checked",
"prims.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.MultiBuffer.fst.checked",
"Lib.IntVector.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Hacl.Spec.SHA2.Vec.fst.checked",
"Hacl.Spec.SHA2.EquivScalar.fsti.checked",
"Hacl.Spec.SHA2.Equiv.fst.checked",
"Hacl.Spec.SHA2.fst.checked",
"Hacl.Impl.SHA2.Core.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.SHA2.Scalar32.Lemmas.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.SHA2.Vec",
"short_module": "SpecVec"
},
{
"abbrev": false,
"full_module": "Hacl.Spec.SHA2.Vec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile.Hash",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.SHA2.Scalar32",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.SHA2.Scalar32",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
totlen: Spec.Hash.Definitions.len_t Spec.Hash.Definitions.SHA2_384 ->
len:
Lib.IntTypes.size_t
{Lib.IntTypes.v len <= Spec.Hash.Definitions.block_length Spec.Hash.Definitions.SHA2_512} ->
b0:
Hacl.Spec.SHA2.Vec.multiseq (Hacl.Spec.SHA2.Vec.lanes Spec.Hash.Definitions.SHA2_384
Hacl.Spec.SHA2.Vec.M32)
(Lib.IntTypes.v len) ->
st0:
s:
Lib.Sequence.seq (Hacl.Spec.SHA2.Vec.element_t Spec.Hash.Definitions.SHA2_512
Hacl.Spec.SHA2.Vec.M32) {FStar.Seq.Base.length s == 8 /\ FStar.Seq.Base.length s == 8}
-> FStar.Pervasives.Lemma
(ensures
(Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t Spec.Hash.Definitions.SHA2_512 len;
Hacl.Spec.SHA2.Vec.update_last totlen (Lib.IntTypes.v len) b0 st0 ==
Hacl.Spec.SHA2.Vec.update_last totlen (Lib.IntTypes.v len) b0 st0)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Spec.Hash.Definitions.len_t",
"Spec.Hash.Definitions.SHA2_384",
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Spec.Hash.Definitions.block_length",
"Spec.Hash.Definitions.SHA2_512",
"Hacl.Spec.SHA2.Vec.multiseq",
"Hacl.Spec.SHA2.Vec.lanes",
"Hacl.Spec.SHA2.Vec.M32",
"Lib.Sequence.seq",
"Hacl.Spec.SHA2.Vec.element_t",
"Prims.l_and",
"Prims.eq2",
"Prims.nat",
"FStar.Seq.Base.length",
"Hacl.SHA2.Scalar32.Lemmas.state_spec_v_extensionality",
"Hacl.Spec.SHA2.Vec.update_last",
"Prims.unit",
"FStar.Calc.calc_finish",
"Spec.Hash.Definitions.words_state",
"Lib.Sequence.op_String_Access",
"Hacl.Spec.SHA2.Vec.words_state'",
"Hacl.Spec.SHA2.Vec.state_spec_v",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"FStar.Calc.calc_step",
"Hacl.Spec.SHA2.update_last",
"Lib.MultiBuffer.op_Lens_Access",
"FStar.Seq.Properties.lseq",
"Lib.IntTypes.uint8",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"Hacl.Spec.SHA2.Equiv.update_last_lemma_l",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U8",
"Lib.IntTypes.SEC",
"Spec.Hash.Definitions.word",
"Spec.Hash.Definitions.state_word_length",
"Prims.l_True",
"Prims.l_or",
"Hacl.Spec.SHA2.update",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Hacl.SHA2.Scalar32.Lemmas.lemma_spec_update_384_512",
"Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t",
"Hacl.Spec.SHA2.Vec.state_spec"
] | [] | false | false | true | false | false | let lemma_spec_update_last_vec_384_512 totlen (len: size_t{v len <= block_length SHA2_512}) b0 st0
: Lemma
(ensures
(Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_512 len;
SpecVec.update_last #SHA2_384 #M32 totlen (v len) b0 st0 ==
SpecVec.update_last #SHA2_512 #M32 totlen (v len) b0 st0)) =
| let open Lib.Sequence in
let open Lib.MultiBuffer in
let st1 = SpecVec.update_last #SHA2_512 #M32 totlen (v len) b0 st0 in
let st0_m32 = (state_spec_v st0).[ 0 ] <: words_state SHA2_512 in
let st1_m32 = (state_spec_v st1).[ 0 ] <: words_state SHA2_512 in
Hacl.Impl.SHA2.Core.lemma_len_lt_max_a_fits_size_t SHA2_512 len;
let hacl_spec_update_384_512 b st
: Lemma (ensures Hacl.Spec.SHA2.update SHA2_512 b st == Hacl.Spec.SHA2.update SHA2_384 b st)
[SMTPat (Hacl.Spec.SHA2.update SHA2_512 b st)] =
lemma_spec_update_384_512 b st
in
calc ( == ) {
st1_m32;
( == ) { () }
(state_spec_v (SpecVec.update_last #SHA2_512 totlen (v len) b0 st0)).[ 0 ];
( == ) { Hacl.Spec.SHA2.Equiv.update_last_lemma_l #SHA2_512 #M32 totlen (v len) b0 st0 0 }
Hacl.Spec.SHA2.update_last SHA2_512 totlen (v len) b0.(| 0 |) st0_m32;
( == ) { () }
Hacl.Spec.SHA2.update_last SHA2_384 totlen (v len) b0.(| 0 |) st0_m32;
( == ) { Hacl.Spec.SHA2.Equiv.update_last_lemma_l #SHA2_384 #M32 totlen (v len) b0 st0 0 }
(state_spec_v (SpecVec.update_last #SHA2_384 #M32 totlen (v len) b0 st0)).[ 0 ];
};
state_spec_v_extensionality SHA2_512
(SpecVec.update_last #SHA2_384 #M32 totlen (v len) b0 st0)
(SpecVec.update_last #SHA2_512 #M32 totlen (v len) b0 st0) | false |
Spec.Chacha20.fst | Spec.Chacha20.op_At | val op_At : f: (_: _ -> _) -> g: (_: _ -> _) -> x: _ -> _ | let op_At f g = fun x -> g (f x) | {
"file_name": "specs/Spec.Chacha20.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 32,
"end_line": 35,
"start_col": 0,
"start_line": 35
} | module Spec.Chacha20
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Lib.LoopCombinators
#set-options "--max_fuel 0 --z3rlimit 100"
/// Constants and Types
let size_key = 32
let size_block = 64
let size_nonce = 12
(* TODO: Remove, left here to avoid breaking implementation *)
let keylen = 32 (* in bytes *)
let blocklen = 64 (* in bytes *)
let noncelen = 12 (* in bytes *)
type key = lbytes size_key
type block = lbytes size_block
type nonce = lbytes size_nonce
type counter = size_nat
type subblock = b:bytes{length b <= size_block}
// Internally, blocks are represented as 16 x 4-byte integers
type state = lseq uint32 16
type idx = n:size_nat{n < 16}
type shuffle = state -> Tot state | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Chacha20.fst"
} | [
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: _ -> _) -> g: (_: _ -> _) -> x: _ -> _ | Prims.Tot | [
"total"
] | [] | [] | [] | false | false | false | true | false | let ( @ ) f g =
| fun x -> g (f x) | false |
|
Spec.Chacha20.fst | Spec.Chacha20.blocklen | val blocklen : Prims.int | let blocklen = 64 | {
"file_name": "specs/Spec.Chacha20.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 17,
"end_line": 20,
"start_col": 0,
"start_line": 20
} | module Spec.Chacha20
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Lib.LoopCombinators
#set-options "--max_fuel 0 --z3rlimit 100"
/// Constants and Types
let size_key = 32
let size_block = 64
let size_nonce = 12
(* TODO: Remove, left here to avoid breaking implementation *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Chacha20.fst"
} | [
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.int | Prims.Tot | [
"total"
] | [] | [] | [] | false | false | false | true | false | let blocklen =
| 64 | false |
|
Spec.Chacha20.fst | Spec.Chacha20.noncelen | val noncelen : Prims.int | let noncelen = 12 | {
"file_name": "specs/Spec.Chacha20.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 17,
"end_line": 21,
"start_col": 0,
"start_line": 21
} | module Spec.Chacha20
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Lib.LoopCombinators
#set-options "--max_fuel 0 --z3rlimit 100"
/// Constants and Types
let size_key = 32
let size_block = 64
let size_nonce = 12
(* TODO: Remove, left here to avoid breaking implementation *)
let keylen = 32 (* in bytes *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Chacha20.fst"
} | [
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.int | Prims.Tot | [
"total"
] | [] | [] | [] | false | false | false | true | false | let noncelen =
| 12 | false |
|
Spec.Chacha20.fst | Spec.Chacha20.double_round | val double_round:shuffle | val double_round:shuffle | let double_round : shuffle =
column_round @ diagonal_round | {
"file_name": "specs/Spec.Chacha20.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 31,
"end_line": 63,
"start_col": 0,
"start_line": 62
} | module Spec.Chacha20
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Lib.LoopCombinators
#set-options "--max_fuel 0 --z3rlimit 100"
/// Constants and Types
let size_key = 32
let size_block = 64
let size_nonce = 12
(* TODO: Remove, left here to avoid breaking implementation *)
let keylen = 32 (* in bytes *)
let blocklen = 64 (* in bytes *)
let noncelen = 12 (* in bytes *)
type key = lbytes size_key
type block = lbytes size_block
type nonce = lbytes size_nonce
type counter = size_nat
type subblock = b:bytes{length b <= size_block}
// Internally, blocks are represented as 16 x 4-byte integers
type state = lseq uint32 16
type idx = n:size_nat{n < 16}
type shuffle = state -> Tot state
// Using @ as a functional substitute for ;
let op_At f g = fun x -> g (f x)
/// Specification
let line (a:idx) (b:idx) (d:idx) (s:rotval U32) (m:state) : Tot state =
let m = m.[a] <- (m.[a] +. m.[b]) in
let m = m.[d] <- ((m.[d] ^. m.[a]) <<<. s) in m
let quarter_round a b c d : Tot shuffle =
line a b d (size 16) @
line c d b (size 12) @
line a b d (size 8) @
line c d b (size 7)
let column_round : shuffle =
quarter_round 0 4 8 12 @
quarter_round 1 5 9 13 @
quarter_round 2 6 10 14 @
quarter_round 3 7 11 15
let diagonal_round : shuffle =
quarter_round 0 5 10 15 @
quarter_round 1 6 11 12 @
quarter_round 2 7 8 13 @
quarter_round 3 4 9 14 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Chacha20.fst"
} | [
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Spec.Chacha20.shuffle | Prims.Tot | [
"total"
] | [] | [
"Spec.Chacha20.op_At",
"Spec.Chacha20.state",
"Spec.Chacha20.column_round",
"Spec.Chacha20.diagonal_round"
] | [] | false | false | false | true | false | let double_round:shuffle =
| column_round @ diagonal_round | false |
Spec.Chacha20.fst | Spec.Chacha20.rounds | val rounds:shuffle | val rounds:shuffle | let rounds : shuffle =
repeat 10 double_round | {
"file_name": "specs/Spec.Chacha20.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 24,
"end_line": 66,
"start_col": 0,
"start_line": 65
} | module Spec.Chacha20
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Lib.LoopCombinators
#set-options "--max_fuel 0 --z3rlimit 100"
/// Constants and Types
let size_key = 32
let size_block = 64
let size_nonce = 12
(* TODO: Remove, left here to avoid breaking implementation *)
let keylen = 32 (* in bytes *)
let blocklen = 64 (* in bytes *)
let noncelen = 12 (* in bytes *)
type key = lbytes size_key
type block = lbytes size_block
type nonce = lbytes size_nonce
type counter = size_nat
type subblock = b:bytes{length b <= size_block}
// Internally, blocks are represented as 16 x 4-byte integers
type state = lseq uint32 16
type idx = n:size_nat{n < 16}
type shuffle = state -> Tot state
// Using @ as a functional substitute for ;
let op_At f g = fun x -> g (f x)
/// Specification
let line (a:idx) (b:idx) (d:idx) (s:rotval U32) (m:state) : Tot state =
let m = m.[a] <- (m.[a] +. m.[b]) in
let m = m.[d] <- ((m.[d] ^. m.[a]) <<<. s) in m
let quarter_round a b c d : Tot shuffle =
line a b d (size 16) @
line c d b (size 12) @
line a b d (size 8) @
line c d b (size 7)
let column_round : shuffle =
quarter_round 0 4 8 12 @
quarter_round 1 5 9 13 @
quarter_round 2 6 10 14 @
quarter_round 3 7 11 15
let diagonal_round : shuffle =
quarter_round 0 5 10 15 @
quarter_round 1 6 11 12 @
quarter_round 2 7 8 13 @
quarter_round 3 4 9 14
let double_round : shuffle =
column_round @ diagonal_round (* 2 rounds *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Chacha20.fst"
} | [
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Spec.Chacha20.shuffle | Prims.Tot | [
"total"
] | [] | [
"Lib.LoopCombinators.repeat",
"Spec.Chacha20.state",
"Spec.Chacha20.double_round"
] | [] | false | false | false | true | false | let rounds:shuffle =
| repeat 10 double_round | false |
Spec.Frodo.Pack.fst | Spec.Frodo.Pack.frodo_unpack_state | val frodo_unpack_state:
#n1:size_nat
-> #n2:size_nat{n1 * n2 <= max_size_t /\ (n1 * n2) % 8 = 0}
-> i:size_nat{i <= (n1 * n2) / 8}
-> Type0 | val frodo_unpack_state:
#n1:size_nat
-> #n2:size_nat{n1 * n2 <= max_size_t /\ (n1 * n2) % 8 = 0}
-> i:size_nat{i <= (n1 * n2) / 8}
-> Type0 | let frodo_unpack_state #n1 #n2 i = lseq uint16 (8 * i) | {
"file_name": "specs/frodo/Spec.Frodo.Pack.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 54,
"end_line": 102,
"start_col": 0,
"start_line": 102
} | module Spec.Frodo.Pack
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Spec.Matrix
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
#reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0 --using_facts_from '* -FStar +FStar.Pervasives +FStar.UInt -Spec +Spec.Frodo +Spec.Frodo.Params +Spec.Matrix'"
/// Pack
val frodo_pack8:
d:size_nat{d <= 16}
-> a:lseq uint16 8
-> lbytes d
let frodo_pack8 d a =
let maskd = to_u16 (u32 1 <<. size d) -. u16 1 in
let a0 = Seq.index a 0 &. maskd in
let a1 = Seq.index a 1 &. maskd in
let a2 = Seq.index a 2 &. maskd in
let a3 = Seq.index a 3 &. maskd in
let a4 = Seq.index a 4 &. maskd in
let a5 = Seq.index a 5 &. maskd in
let a6 = Seq.index a 6 &. maskd in
let a7 = Seq.index a 7 &. maskd in
let templong =
to_u128 a0 <<. size (7 * d)
|. to_u128 a1 <<. size (6 * d)
|. to_u128 a2 <<. size (5 * d)
|. to_u128 a3 <<. size (4 * d)
|. to_u128 a4 <<. size (3 * d)
|. to_u128 a5 <<. size (2 * d)
|. to_u128 a6 <<. size (1 * d)
|. to_u128 a7 <<. size (0 * d)
in
let v16 = uint_to_bytes_be templong in
Seq.sub v16 (16 - d) d
val frodo_pack_state:
#n1:size_nat
-> #n2:size_nat{n1 * n2 <= max_size_t /\ (n1 * n2) % 8 = 0}
-> d:size_nat{d * ((n1 * n2) / 8) <= max_size_t /\ d <= 16}
-> i:size_nat{i <= (n1 * n2) / 8}
-> Type0
let frodo_pack_state #n1 #n2 d i = lseq uint8 (d * i)
val frodo_pack_inner:
#n1:size_nat
-> #n2:size_nat{n1 * n2 <= max_size_t /\ (n1 * n2) % 8 = 0}
-> d:size_nat{d * ((n1 * n2) / 8) <= max_size_t /\ d <= 16}
-> a:matrix n1 n2
-> i:size_nat{i < (n1 * n2) / 8}
-> frodo_pack_state #n1 #n2 d i
-> frodo_pack_state #n1 #n2 d (i + 1)
let frodo_pack_inner #n1 #n2 d a i s =
s @| frodo_pack8 d (Seq.sub a (8 * i) 8)
val frodo_pack:
#n1:size_nat
-> #n2:size_nat{n1 * n2 <= max_size_t /\ (n1 * n2) % 8 = 0}
-> d:size_nat{d * ((n1 * n2) / 8) <= max_size_t /\ d <= 16}
-> a:matrix n1 n2
-> lbytes (d * ((n1 * n2) / 8))
let frodo_pack #n1 #n2 d a =
Loops.repeat_gen ((n1 * n2) / 8)
(frodo_pack_state #n1 #n2 d)
(frodo_pack_inner #n1 #n2 d a)
(Seq.create 0 (u8 0))
/// Unpack
val frodo_unpack8:
d:size_nat{d <= 16}
-> b:lbytes d
-> lseq uint16 8
let frodo_unpack8 d b =
let maskd = to_u16 (u32 1 <<. size d) -. u16 1 in
let v16 = Seq.create 16 (u8 0) in
let src = update_sub v16 (16 - d) d b in
let templong: uint_t U128 SEC = uint_from_bytes_be src in
let res = Seq.create 8 (u16 0) in
let res = res.[0] <- to_u16 (templong >>. size (7 * d)) &. maskd in
let res = res.[1] <- to_u16 (templong >>. size (6 * d)) &. maskd in
let res = res.[2] <- to_u16 (templong >>. size (5 * d)) &. maskd in
let res = res.[3] <- to_u16 (templong >>. size (4 * d)) &. maskd in
let res = res.[4] <- to_u16 (templong >>. size (3 * d)) &. maskd in
let res = res.[5] <- to_u16 (templong >>. size (2 * d)) &. maskd in
let res = res.[6] <- to_u16 (templong >>. size (1 * d)) &. maskd in
let res = res.[7] <- to_u16 (templong >>. size (0 * d)) &. maskd in
res
val frodo_unpack_state:
#n1:size_nat
-> #n2:size_nat{n1 * n2 <= max_size_t /\ (n1 * n2) % 8 = 0}
-> i:size_nat{i <= (n1 * n2) / 8} | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Frodo.Pack.fst"
} | [
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "Spec.Matrix",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec.Frodo",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Frodo",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | i: Lib.IntTypes.size_nat{i <= n1 * n2 / 8} -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Lib.IntTypes.size_nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.max_size_t",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Modulus",
"Prims.op_Division",
"Lib.Sequence.lseq",
"Lib.IntTypes.uint16"
] | [] | false | false | false | false | true | let frodo_unpack_state #n1 #n2 i =
| lseq uint16 (8 * i) | false |
Spec.Chacha20.fst | Spec.Chacha20.chacha20_core | val chacha20_core (ctr: counter) (s0: state) : Tot state | val chacha20_core (ctr: counter) (s0: state) : Tot state | let chacha20_core (ctr:counter) (s0:state) : Tot state =
let k = chacha20_add_counter s0 ctr in
let k = rounds k in
let k = sum_state k s0 in
chacha20_add_counter k ctr | {
"file_name": "specs/Spec.Chacha20.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 29,
"end_line": 98,
"start_col": 0,
"start_line": 94
} | module Spec.Chacha20
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Lib.LoopCombinators
#set-options "--max_fuel 0 --z3rlimit 100"
/// Constants and Types
let size_key = 32
let size_block = 64
let size_nonce = 12
(* TODO: Remove, left here to avoid breaking implementation *)
let keylen = 32 (* in bytes *)
let blocklen = 64 (* in bytes *)
let noncelen = 12 (* in bytes *)
type key = lbytes size_key
type block = lbytes size_block
type nonce = lbytes size_nonce
type counter = size_nat
type subblock = b:bytes{length b <= size_block}
// Internally, blocks are represented as 16 x 4-byte integers
type state = lseq uint32 16
type idx = n:size_nat{n < 16}
type shuffle = state -> Tot state
// Using @ as a functional substitute for ;
let op_At f g = fun x -> g (f x)
/// Specification
let line (a:idx) (b:idx) (d:idx) (s:rotval U32) (m:state) : Tot state =
let m = m.[a] <- (m.[a] +. m.[b]) in
let m = m.[d] <- ((m.[d] ^. m.[a]) <<<. s) in m
let quarter_round a b c d : Tot shuffle =
line a b d (size 16) @
line c d b (size 12) @
line a b d (size 8) @
line c d b (size 7)
let column_round : shuffle =
quarter_round 0 4 8 12 @
quarter_round 1 5 9 13 @
quarter_round 2 6 10 14 @
quarter_round 3 7 11 15
let diagonal_round : shuffle =
quarter_round 0 5 10 15 @
quarter_round 1 6 11 12 @
quarter_round 2 7 8 13 @
quarter_round 3 4 9 14
let double_round : shuffle =
column_round @ diagonal_round (* 2 rounds *)
let rounds : shuffle =
repeat 10 double_round (* 20 rounds *)
let sum_state (s0:state) (s1:state) : Tot state =
map2 (+.) s0 s1
let chacha20_add_counter (s0:state) (ctr:counter) : Tot state =
s0.[12] <- s0.[12] +. u32 ctr
// protz 10:37 AM
// question about chacha20 spec: why the double counter increment in chacha20_core?
// https://github.com/project-everest/hacl-star/blob/_dev/specs/Spec.Chacha20.fst#L75
// is this in the spec?
// karthik 11:28 AM
// This is doing the same as:
//
// let chacha20_core (ctr:counter) (s0:state) : Tot state =
// let s0 = chacha20_add_counter s0 ctr in
// let k = rounds s0 in
// sum_state k s0
//
// but we rewrite in this way so that s0 remains constant
// (in the code)
// protz 11:32 AM
// do sum_state and add_counter commute?
// I feel like I'm missing some equational property of these sub-combinators
// to understand why this is true
// karthik 11:33 AM | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Chacha20.fst"
} | [
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | ctr: Spec.Chacha20.counter -> s0: Spec.Chacha20.state -> Spec.Chacha20.state | Prims.Tot | [
"total"
] | [] | [
"Spec.Chacha20.counter",
"Spec.Chacha20.state",
"Spec.Chacha20.chacha20_add_counter",
"Spec.Chacha20.sum_state",
"Spec.Chacha20.rounds"
] | [] | false | false | false | true | false | let chacha20_core (ctr: counter) (s0: state) : Tot state =
| let k = chacha20_add_counter s0 ctr in
let k = rounds k in
let k = sum_state k s0 in
chacha20_add_counter k ctr | false |
Spec.Chacha20.fst | Spec.Chacha20.c1 | val c1 : FStar.UInt32.t | let c1 = 0x3320646eul | {
"file_name": "specs/Spec.Chacha20.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 21,
"end_line": 103,
"start_col": 0,
"start_line": 103
} | module Spec.Chacha20
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Lib.LoopCombinators
#set-options "--max_fuel 0 --z3rlimit 100"
/// Constants and Types
let size_key = 32
let size_block = 64
let size_nonce = 12
(* TODO: Remove, left here to avoid breaking implementation *)
let keylen = 32 (* in bytes *)
let blocklen = 64 (* in bytes *)
let noncelen = 12 (* in bytes *)
type key = lbytes size_key
type block = lbytes size_block
type nonce = lbytes size_nonce
type counter = size_nat
type subblock = b:bytes{length b <= size_block}
// Internally, blocks are represented as 16 x 4-byte integers
type state = lseq uint32 16
type idx = n:size_nat{n < 16}
type shuffle = state -> Tot state
// Using @ as a functional substitute for ;
let op_At f g = fun x -> g (f x)
/// Specification
let line (a:idx) (b:idx) (d:idx) (s:rotval U32) (m:state) : Tot state =
let m = m.[a] <- (m.[a] +. m.[b]) in
let m = m.[d] <- ((m.[d] ^. m.[a]) <<<. s) in m
let quarter_round a b c d : Tot shuffle =
line a b d (size 16) @
line c d b (size 12) @
line a b d (size 8) @
line c d b (size 7)
let column_round : shuffle =
quarter_round 0 4 8 12 @
quarter_round 1 5 9 13 @
quarter_round 2 6 10 14 @
quarter_round 3 7 11 15
let diagonal_round : shuffle =
quarter_round 0 5 10 15 @
quarter_round 1 6 11 12 @
quarter_round 2 7 8 13 @
quarter_round 3 4 9 14
let double_round : shuffle =
column_round @ diagonal_round (* 2 rounds *)
let rounds : shuffle =
repeat 10 double_round (* 20 rounds *)
let sum_state (s0:state) (s1:state) : Tot state =
map2 (+.) s0 s1
let chacha20_add_counter (s0:state) (ctr:counter) : Tot state =
s0.[12] <- s0.[12] +. u32 ctr
// protz 10:37 AM
// question about chacha20 spec: why the double counter increment in chacha20_core?
// https://github.com/project-everest/hacl-star/blob/_dev/specs/Spec.Chacha20.fst#L75
// is this in the spec?
// karthik 11:28 AM
// This is doing the same as:
//
// let chacha20_core (ctr:counter) (s0:state) : Tot state =
// let s0 = chacha20_add_counter s0 ctr in
// let k = rounds s0 in
// sum_state k s0
//
// but we rewrite in this way so that s0 remains constant
// (in the code)
// protz 11:32 AM
// do sum_state and add_counter commute?
// I feel like I'm missing some equational property of these sub-combinators
// to understand why this is true
// karthik 11:33 AM
// yes, they do.
let chacha20_core (ctr:counter) (s0:state) : Tot state =
let k = chacha20_add_counter s0 ctr in
let k = rounds k in
let k = sum_state k s0 in
chacha20_add_counter k ctr
inline_for_extraction
let c0 = 0x61707865ul | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Chacha20.fst"
} | [
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | FStar.UInt32.t | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt32.__uint_to_t"
] | [] | false | false | false | true | false | let c1 =
| 0x3320646eul | false |
|
Spec.Chacha20.fst | Spec.Chacha20.c2 | val c2 : FStar.UInt32.t | let c2 = 0x79622d32ul | {
"file_name": "specs/Spec.Chacha20.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 21,
"end_line": 105,
"start_col": 0,
"start_line": 105
} | module Spec.Chacha20
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Lib.LoopCombinators
#set-options "--max_fuel 0 --z3rlimit 100"
/// Constants and Types
let size_key = 32
let size_block = 64
let size_nonce = 12
(* TODO: Remove, left here to avoid breaking implementation *)
let keylen = 32 (* in bytes *)
let blocklen = 64 (* in bytes *)
let noncelen = 12 (* in bytes *)
type key = lbytes size_key
type block = lbytes size_block
type nonce = lbytes size_nonce
type counter = size_nat
type subblock = b:bytes{length b <= size_block}
// Internally, blocks are represented as 16 x 4-byte integers
type state = lseq uint32 16
type idx = n:size_nat{n < 16}
type shuffle = state -> Tot state
// Using @ as a functional substitute for ;
let op_At f g = fun x -> g (f x)
/// Specification
let line (a:idx) (b:idx) (d:idx) (s:rotval U32) (m:state) : Tot state =
let m = m.[a] <- (m.[a] +. m.[b]) in
let m = m.[d] <- ((m.[d] ^. m.[a]) <<<. s) in m
let quarter_round a b c d : Tot shuffle =
line a b d (size 16) @
line c d b (size 12) @
line a b d (size 8) @
line c d b (size 7)
let column_round : shuffle =
quarter_round 0 4 8 12 @
quarter_round 1 5 9 13 @
quarter_round 2 6 10 14 @
quarter_round 3 7 11 15
let diagonal_round : shuffle =
quarter_round 0 5 10 15 @
quarter_round 1 6 11 12 @
quarter_round 2 7 8 13 @
quarter_round 3 4 9 14
let double_round : shuffle =
column_round @ diagonal_round (* 2 rounds *)
let rounds : shuffle =
repeat 10 double_round (* 20 rounds *)
let sum_state (s0:state) (s1:state) : Tot state =
map2 (+.) s0 s1
let chacha20_add_counter (s0:state) (ctr:counter) : Tot state =
s0.[12] <- s0.[12] +. u32 ctr
// protz 10:37 AM
// question about chacha20 spec: why the double counter increment in chacha20_core?
// https://github.com/project-everest/hacl-star/blob/_dev/specs/Spec.Chacha20.fst#L75
// is this in the spec?
// karthik 11:28 AM
// This is doing the same as:
//
// let chacha20_core (ctr:counter) (s0:state) : Tot state =
// let s0 = chacha20_add_counter s0 ctr in
// let k = rounds s0 in
// sum_state k s0
//
// but we rewrite in this way so that s0 remains constant
// (in the code)
// protz 11:32 AM
// do sum_state and add_counter commute?
// I feel like I'm missing some equational property of these sub-combinators
// to understand why this is true
// karthik 11:33 AM
// yes, they do.
let chacha20_core (ctr:counter) (s0:state) : Tot state =
let k = chacha20_add_counter s0 ctr in
let k = rounds k in
let k = sum_state k s0 in
chacha20_add_counter k ctr
inline_for_extraction
let c0 = 0x61707865ul
inline_for_extraction
let c1 = 0x3320646eul | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Chacha20.fst"
} | [
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | FStar.UInt32.t | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt32.__uint_to_t"
] | [] | false | false | false | true | false | let c2 =
| 0x79622d32ul | false |
|
Spec.Chacha20.fst | Spec.Chacha20.c0 | val c0 : FStar.UInt32.t | let c0 = 0x61707865ul | {
"file_name": "specs/Spec.Chacha20.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 21,
"end_line": 101,
"start_col": 0,
"start_line": 101
} | module Spec.Chacha20
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Lib.LoopCombinators
#set-options "--max_fuel 0 --z3rlimit 100"
/// Constants and Types
let size_key = 32
let size_block = 64
let size_nonce = 12
(* TODO: Remove, left here to avoid breaking implementation *)
let keylen = 32 (* in bytes *)
let blocklen = 64 (* in bytes *)
let noncelen = 12 (* in bytes *)
type key = lbytes size_key
type block = lbytes size_block
type nonce = lbytes size_nonce
type counter = size_nat
type subblock = b:bytes{length b <= size_block}
// Internally, blocks are represented as 16 x 4-byte integers
type state = lseq uint32 16
type idx = n:size_nat{n < 16}
type shuffle = state -> Tot state
// Using @ as a functional substitute for ;
let op_At f g = fun x -> g (f x)
/// Specification
let line (a:idx) (b:idx) (d:idx) (s:rotval U32) (m:state) : Tot state =
let m = m.[a] <- (m.[a] +. m.[b]) in
let m = m.[d] <- ((m.[d] ^. m.[a]) <<<. s) in m
let quarter_round a b c d : Tot shuffle =
line a b d (size 16) @
line c d b (size 12) @
line a b d (size 8) @
line c d b (size 7)
let column_round : shuffle =
quarter_round 0 4 8 12 @
quarter_round 1 5 9 13 @
quarter_round 2 6 10 14 @
quarter_round 3 7 11 15
let diagonal_round : shuffle =
quarter_round 0 5 10 15 @
quarter_round 1 6 11 12 @
quarter_round 2 7 8 13 @
quarter_round 3 4 9 14
let double_round : shuffle =
column_round @ diagonal_round (* 2 rounds *)
let rounds : shuffle =
repeat 10 double_round (* 20 rounds *)
let sum_state (s0:state) (s1:state) : Tot state =
map2 (+.) s0 s1
let chacha20_add_counter (s0:state) (ctr:counter) : Tot state =
s0.[12] <- s0.[12] +. u32 ctr
// protz 10:37 AM
// question about chacha20 spec: why the double counter increment in chacha20_core?
// https://github.com/project-everest/hacl-star/blob/_dev/specs/Spec.Chacha20.fst#L75
// is this in the spec?
// karthik 11:28 AM
// This is doing the same as:
//
// let chacha20_core (ctr:counter) (s0:state) : Tot state =
// let s0 = chacha20_add_counter s0 ctr in
// let k = rounds s0 in
// sum_state k s0
//
// but we rewrite in this way so that s0 remains constant
// (in the code)
// protz 11:32 AM
// do sum_state and add_counter commute?
// I feel like I'm missing some equational property of these sub-combinators
// to understand why this is true
// karthik 11:33 AM
// yes, they do.
let chacha20_core (ctr:counter) (s0:state) : Tot state =
let k = chacha20_add_counter s0 ctr in
let k = rounds k in
let k = sum_state k s0 in
chacha20_add_counter k ctr | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Chacha20.fst"
} | [
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | FStar.UInt32.t | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt32.__uint_to_t"
] | [] | false | false | false | true | false | let c0 =
| 0x61707865ul | false |
|
Spec.Frodo.Pack.fst | Spec.Frodo.Pack.frodo_pack | val frodo_pack:
#n1:size_nat
-> #n2:size_nat{n1 * n2 <= max_size_t /\ (n1 * n2) % 8 = 0}
-> d:size_nat{d * ((n1 * n2) / 8) <= max_size_t /\ d <= 16}
-> a:matrix n1 n2
-> lbytes (d * ((n1 * n2) / 8)) | val frodo_pack:
#n1:size_nat
-> #n2:size_nat{n1 * n2 <= max_size_t /\ (n1 * n2) % 8 = 0}
-> d:size_nat{d * ((n1 * n2) / 8) <= max_size_t /\ d <= 16}
-> a:matrix n1 n2
-> lbytes (d * ((n1 * n2) / 8)) | let frodo_pack #n1 #n2 d a =
Loops.repeat_gen ((n1 * n2) / 8)
(frodo_pack_state #n1 #n2 d)
(frodo_pack_inner #n1 #n2 d a)
(Seq.create 0 (u8 0)) | {
"file_name": "specs/frodo/Spec.Frodo.Pack.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 25,
"end_line": 73,
"start_col": 0,
"start_line": 69
} | module Spec.Frodo.Pack
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Spec.Matrix
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
#reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0 --using_facts_from '* -FStar +FStar.Pervasives +FStar.UInt -Spec +Spec.Frodo +Spec.Frodo.Params +Spec.Matrix'"
/// Pack
val frodo_pack8:
d:size_nat{d <= 16}
-> a:lseq uint16 8
-> lbytes d
let frodo_pack8 d a =
let maskd = to_u16 (u32 1 <<. size d) -. u16 1 in
let a0 = Seq.index a 0 &. maskd in
let a1 = Seq.index a 1 &. maskd in
let a2 = Seq.index a 2 &. maskd in
let a3 = Seq.index a 3 &. maskd in
let a4 = Seq.index a 4 &. maskd in
let a5 = Seq.index a 5 &. maskd in
let a6 = Seq.index a 6 &. maskd in
let a7 = Seq.index a 7 &. maskd in
let templong =
to_u128 a0 <<. size (7 * d)
|. to_u128 a1 <<. size (6 * d)
|. to_u128 a2 <<. size (5 * d)
|. to_u128 a3 <<. size (4 * d)
|. to_u128 a4 <<. size (3 * d)
|. to_u128 a5 <<. size (2 * d)
|. to_u128 a6 <<. size (1 * d)
|. to_u128 a7 <<. size (0 * d)
in
let v16 = uint_to_bytes_be templong in
Seq.sub v16 (16 - d) d
val frodo_pack_state:
#n1:size_nat
-> #n2:size_nat{n1 * n2 <= max_size_t /\ (n1 * n2) % 8 = 0}
-> d:size_nat{d * ((n1 * n2) / 8) <= max_size_t /\ d <= 16}
-> i:size_nat{i <= (n1 * n2) / 8}
-> Type0
let frodo_pack_state #n1 #n2 d i = lseq uint8 (d * i)
val frodo_pack_inner:
#n1:size_nat
-> #n2:size_nat{n1 * n2 <= max_size_t /\ (n1 * n2) % 8 = 0}
-> d:size_nat{d * ((n1 * n2) / 8) <= max_size_t /\ d <= 16}
-> a:matrix n1 n2
-> i:size_nat{i < (n1 * n2) / 8}
-> frodo_pack_state #n1 #n2 d i
-> frodo_pack_state #n1 #n2 d (i + 1)
let frodo_pack_inner #n1 #n2 d a i s =
s @| frodo_pack8 d (Seq.sub a (8 * i) 8)
val frodo_pack:
#n1:size_nat
-> #n2:size_nat{n1 * n2 <= max_size_t /\ (n1 * n2) % 8 = 0}
-> d:size_nat{d * ((n1 * n2) / 8) <= max_size_t /\ d <= 16}
-> a:matrix n1 n2 | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Frodo.Pack.fst"
} | [
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "Spec.Matrix",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec.Frodo",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Frodo",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
d: Lib.IntTypes.size_nat{d * (n1 * n2 / 8) <= Lib.IntTypes.max_size_t /\ d <= 16} ->
a: Spec.Matrix.matrix n1 n2
-> Lib.ByteSequence.lbytes (d * (n1 * n2 / 8)) | Prims.Tot | [
"total"
] | [] | [
"Lib.IntTypes.size_nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.max_size_t",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Modulus",
"Prims.op_Division",
"Spec.Matrix.matrix",
"Lib.LoopCombinators.repeat_gen",
"Spec.Frodo.Pack.frodo_pack_state",
"Spec.Frodo.Pack.frodo_pack_inner",
"Lib.Sequence.create",
"Lib.IntTypes.uint8",
"Lib.IntTypes.u8",
"Lib.ByteSequence.lbytes"
] | [] | false | false | false | false | false | let frodo_pack #n1 #n2 d a =
| Loops.repeat_gen ((n1 * n2) / 8)
(frodo_pack_state #n1 #n2 d)
(frodo_pack_inner #n1 #n2 d a)
(Seq.create 0 (u8 0)) | false |
Spec.Frodo.Pack.fst | Spec.Frodo.Pack.frodo_pack_state | val frodo_pack_state:
#n1:size_nat
-> #n2:size_nat{n1 * n2 <= max_size_t /\ (n1 * n2) % 8 = 0}
-> d:size_nat{d * ((n1 * n2) / 8) <= max_size_t /\ d <= 16}
-> i:size_nat{i <= (n1 * n2) / 8}
-> Type0 | val frodo_pack_state:
#n1:size_nat
-> #n2:size_nat{n1 * n2 <= max_size_t /\ (n1 * n2) % 8 = 0}
-> d:size_nat{d * ((n1 * n2) / 8) <= max_size_t /\ d <= 16}
-> i:size_nat{i <= (n1 * n2) / 8}
-> Type0 | let frodo_pack_state #n1 #n2 d i = lseq uint8 (d * i) | {
"file_name": "specs/frodo/Spec.Frodo.Pack.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 53,
"end_line": 50,
"start_col": 0,
"start_line": 50
} | module Spec.Frodo.Pack
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Spec.Matrix
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
#reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0 --using_facts_from '* -FStar +FStar.Pervasives +FStar.UInt -Spec +Spec.Frodo +Spec.Frodo.Params +Spec.Matrix'"
/// Pack
val frodo_pack8:
d:size_nat{d <= 16}
-> a:lseq uint16 8
-> lbytes d
let frodo_pack8 d a =
let maskd = to_u16 (u32 1 <<. size d) -. u16 1 in
let a0 = Seq.index a 0 &. maskd in
let a1 = Seq.index a 1 &. maskd in
let a2 = Seq.index a 2 &. maskd in
let a3 = Seq.index a 3 &. maskd in
let a4 = Seq.index a 4 &. maskd in
let a5 = Seq.index a 5 &. maskd in
let a6 = Seq.index a 6 &. maskd in
let a7 = Seq.index a 7 &. maskd in
let templong =
to_u128 a0 <<. size (7 * d)
|. to_u128 a1 <<. size (6 * d)
|. to_u128 a2 <<. size (5 * d)
|. to_u128 a3 <<. size (4 * d)
|. to_u128 a4 <<. size (3 * d)
|. to_u128 a5 <<. size (2 * d)
|. to_u128 a6 <<. size (1 * d)
|. to_u128 a7 <<. size (0 * d)
in
let v16 = uint_to_bytes_be templong in
Seq.sub v16 (16 - d) d
val frodo_pack_state:
#n1:size_nat
-> #n2:size_nat{n1 * n2 <= max_size_t /\ (n1 * n2) % 8 = 0}
-> d:size_nat{d * ((n1 * n2) / 8) <= max_size_t /\ d <= 16}
-> i:size_nat{i <= (n1 * n2) / 8} | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Frodo.Pack.fst"
} | [
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "Spec.Matrix",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec.Frodo",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Frodo",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
d: Lib.IntTypes.size_nat{d * (n1 * n2 / 8) <= Lib.IntTypes.max_size_t /\ d <= 16} ->
i: Lib.IntTypes.size_nat{i <= n1 * n2 / 8}
-> Type0 | Prims.Tot | [
"total"
] | [] | [
"Lib.IntTypes.size_nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.max_size_t",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Modulus",
"Prims.op_Division",
"Lib.Sequence.lseq",
"Lib.IntTypes.uint8"
] | [] | false | false | false | false | true | let frodo_pack_state #n1 #n2 d i =
| lseq uint8 (d * i) | false |
Spec.Chacha20.fst | Spec.Chacha20.c3 | val c3 : FStar.UInt32.t | let c3 = 0x6b206574ul | {
"file_name": "specs/Spec.Chacha20.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 21,
"end_line": 107,
"start_col": 0,
"start_line": 107
} | module Spec.Chacha20
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Lib.LoopCombinators
#set-options "--max_fuel 0 --z3rlimit 100"
/// Constants and Types
let size_key = 32
let size_block = 64
let size_nonce = 12
(* TODO: Remove, left here to avoid breaking implementation *)
let keylen = 32 (* in bytes *)
let blocklen = 64 (* in bytes *)
let noncelen = 12 (* in bytes *)
type key = lbytes size_key
type block = lbytes size_block
type nonce = lbytes size_nonce
type counter = size_nat
type subblock = b:bytes{length b <= size_block}
// Internally, blocks are represented as 16 x 4-byte integers
type state = lseq uint32 16
type idx = n:size_nat{n < 16}
type shuffle = state -> Tot state
// Using @ as a functional substitute for ;
let op_At f g = fun x -> g (f x)
/// Specification
let line (a:idx) (b:idx) (d:idx) (s:rotval U32) (m:state) : Tot state =
let m = m.[a] <- (m.[a] +. m.[b]) in
let m = m.[d] <- ((m.[d] ^. m.[a]) <<<. s) in m
let quarter_round a b c d : Tot shuffle =
line a b d (size 16) @
line c d b (size 12) @
line a b d (size 8) @
line c d b (size 7)
let column_round : shuffle =
quarter_round 0 4 8 12 @
quarter_round 1 5 9 13 @
quarter_round 2 6 10 14 @
quarter_round 3 7 11 15
let diagonal_round : shuffle =
quarter_round 0 5 10 15 @
quarter_round 1 6 11 12 @
quarter_round 2 7 8 13 @
quarter_round 3 4 9 14
let double_round : shuffle =
column_round @ diagonal_round (* 2 rounds *)
let rounds : shuffle =
repeat 10 double_round (* 20 rounds *)
let sum_state (s0:state) (s1:state) : Tot state =
map2 (+.) s0 s1
let chacha20_add_counter (s0:state) (ctr:counter) : Tot state =
s0.[12] <- s0.[12] +. u32 ctr
// protz 10:37 AM
// question about chacha20 spec: why the double counter increment in chacha20_core?
// https://github.com/project-everest/hacl-star/blob/_dev/specs/Spec.Chacha20.fst#L75
// is this in the spec?
// karthik 11:28 AM
// This is doing the same as:
//
// let chacha20_core (ctr:counter) (s0:state) : Tot state =
// let s0 = chacha20_add_counter s0 ctr in
// let k = rounds s0 in
// sum_state k s0
//
// but we rewrite in this way so that s0 remains constant
// (in the code)
// protz 11:32 AM
// do sum_state and add_counter commute?
// I feel like I'm missing some equational property of these sub-combinators
// to understand why this is true
// karthik 11:33 AM
// yes, they do.
let chacha20_core (ctr:counter) (s0:state) : Tot state =
let k = chacha20_add_counter s0 ctr in
let k = rounds k in
let k = sum_state k s0 in
chacha20_add_counter k ctr
inline_for_extraction
let c0 = 0x61707865ul
inline_for_extraction
let c1 = 0x3320646eul
inline_for_extraction
let c2 = 0x79622d32ul | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Chacha20.fst"
} | [
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | FStar.UInt32.t | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt32.__uint_to_t"
] | [] | false | false | false | true | false | let c3 =
| 0x6b206574ul | false |
|
Spec.Chacha20.fst | Spec.Chacha20.chacha20_encrypt_block | val chacha20_encrypt_block (st0: state) (incr: counter) (b: block) : Tot block | val chacha20_encrypt_block (st0: state) (incr: counter) (b: block) : Tot block | let chacha20_encrypt_block (st0:state) (incr:counter) (b:block) : Tot block =
let k = chacha20_core incr st0 in
xor_block k b | {
"file_name": "specs/Spec.Chacha20.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 15,
"end_line": 143,
"start_col": 0,
"start_line": 141
} | module Spec.Chacha20
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Lib.LoopCombinators
#set-options "--max_fuel 0 --z3rlimit 100"
/// Constants and Types
let size_key = 32
let size_block = 64
let size_nonce = 12
(* TODO: Remove, left here to avoid breaking implementation *)
let keylen = 32 (* in bytes *)
let blocklen = 64 (* in bytes *)
let noncelen = 12 (* in bytes *)
type key = lbytes size_key
type block = lbytes size_block
type nonce = lbytes size_nonce
type counter = size_nat
type subblock = b:bytes{length b <= size_block}
// Internally, blocks are represented as 16 x 4-byte integers
type state = lseq uint32 16
type idx = n:size_nat{n < 16}
type shuffle = state -> Tot state
// Using @ as a functional substitute for ;
let op_At f g = fun x -> g (f x)
/// Specification
let line (a:idx) (b:idx) (d:idx) (s:rotval U32) (m:state) : Tot state =
let m = m.[a] <- (m.[a] +. m.[b]) in
let m = m.[d] <- ((m.[d] ^. m.[a]) <<<. s) in m
let quarter_round a b c d : Tot shuffle =
line a b d (size 16) @
line c d b (size 12) @
line a b d (size 8) @
line c d b (size 7)
let column_round : shuffle =
quarter_round 0 4 8 12 @
quarter_round 1 5 9 13 @
quarter_round 2 6 10 14 @
quarter_round 3 7 11 15
let diagonal_round : shuffle =
quarter_round 0 5 10 15 @
quarter_round 1 6 11 12 @
quarter_round 2 7 8 13 @
quarter_round 3 4 9 14
let double_round : shuffle =
column_round @ diagonal_round (* 2 rounds *)
let rounds : shuffle =
repeat 10 double_round (* 20 rounds *)
let sum_state (s0:state) (s1:state) : Tot state =
map2 (+.) s0 s1
let chacha20_add_counter (s0:state) (ctr:counter) : Tot state =
s0.[12] <- s0.[12] +. u32 ctr
// protz 10:37 AM
// question about chacha20 spec: why the double counter increment in chacha20_core?
// https://github.com/project-everest/hacl-star/blob/_dev/specs/Spec.Chacha20.fst#L75
// is this in the spec?
// karthik 11:28 AM
// This is doing the same as:
//
// let chacha20_core (ctr:counter) (s0:state) : Tot state =
// let s0 = chacha20_add_counter s0 ctr in
// let k = rounds s0 in
// sum_state k s0
//
// but we rewrite in this way so that s0 remains constant
// (in the code)
// protz 11:32 AM
// do sum_state and add_counter commute?
// I feel like I'm missing some equational property of these sub-combinators
// to understand why this is true
// karthik 11:33 AM
// yes, they do.
let chacha20_core (ctr:counter) (s0:state) : Tot state =
let k = chacha20_add_counter s0 ctr in
let k = rounds k in
let k = sum_state k s0 in
chacha20_add_counter k ctr
inline_for_extraction
let c0 = 0x61707865ul
inline_for_extraction
let c1 = 0x3320646eul
inline_for_extraction
let c2 = 0x79622d32ul
inline_for_extraction
let c3 = 0x6b206574ul
let chacha20_constants : lseq size_t 4 =
[@ inline_let]
let l = [c0;c1;c2;c3] in
assert_norm(List.Tot.length l == 4);
createL l
let setup (k:key) (n:nonce) (ctr0:counter) (st:state) : Tot state =
let st = update_sub st 0 4 (map secret chacha20_constants) in
let st = update_sub st 4 8 (uints_from_bytes_le #U32 #SEC #8 k) in
let st = st.[12] <- u32 ctr0 in
let st = update_sub st 13 3 (uints_from_bytes_le #U32 #SEC #3 n) in
st
let chacha20_init (k:key) (n:nonce) (ctr0:counter) : Tot state =
let st = create 16 (u32 0) in
let st = setup k n ctr0 st in
st
let chacha20_key_block0 (k:key) (n:nonce) : Tot block =
let st = chacha20_init k n 0 in
let st = chacha20_core 0 st in
uints_to_bytes_le st
let chacha20_key_block (st:state) : Tot block =
let st = chacha20_core 0 st in
uints_to_bytes_le st
let xor_block (k:state) (b:block) : block =
let ib = uints_from_bytes_le b in
let ob = map2 (^.) ib k in
uints_to_bytes_le ob | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Chacha20.fst"
} | [
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | st0: Spec.Chacha20.state -> incr: Spec.Chacha20.counter -> b: Spec.Chacha20.block
-> Spec.Chacha20.block | Prims.Tot | [
"total"
] | [] | [
"Spec.Chacha20.state",
"Spec.Chacha20.counter",
"Spec.Chacha20.block",
"Spec.Chacha20.xor_block",
"Spec.Chacha20.chacha20_core"
] | [] | false | false | false | true | false | let chacha20_encrypt_block (st0: state) (incr: counter) (b: block) : Tot block =
| let k = chacha20_core incr st0 in
xor_block k b | false |
Spec.Chacha20.fst | Spec.Chacha20.diagonal_round | val diagonal_round:shuffle | val diagonal_round:shuffle | let diagonal_round : shuffle =
quarter_round 0 5 10 15 @
quarter_round 1 6 11 12 @
quarter_round 2 7 8 13 @
quarter_round 3 4 9 14 | {
"file_name": "specs/Spec.Chacha20.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 25,
"end_line": 60,
"start_col": 0,
"start_line": 56
} | module Spec.Chacha20
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Lib.ByteSequence
open Lib.LoopCombinators
#set-options "--max_fuel 0 --z3rlimit 100"
/// Constants and Types
let size_key = 32
let size_block = 64
let size_nonce = 12
(* TODO: Remove, left here to avoid breaking implementation *)
let keylen = 32 (* in bytes *)
let blocklen = 64 (* in bytes *)
let noncelen = 12 (* in bytes *)
type key = lbytes size_key
type block = lbytes size_block
type nonce = lbytes size_nonce
type counter = size_nat
type subblock = b:bytes{length b <= size_block}
// Internally, blocks are represented as 16 x 4-byte integers
type state = lseq uint32 16
type idx = n:size_nat{n < 16}
type shuffle = state -> Tot state
// Using @ as a functional substitute for ;
let op_At f g = fun x -> g (f x)
/// Specification
let line (a:idx) (b:idx) (d:idx) (s:rotval U32) (m:state) : Tot state =
let m = m.[a] <- (m.[a] +. m.[b]) in
let m = m.[d] <- ((m.[d] ^. m.[a]) <<<. s) in m
let quarter_round a b c d : Tot shuffle =
line a b d (size 16) @
line c d b (size 12) @
line a b d (size 8) @
line c d b (size 7)
let column_round : shuffle =
quarter_round 0 4 8 12 @
quarter_round 1 5 9 13 @
quarter_round 2 6 10 14 @
quarter_round 3 7 11 15 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Spec.Chacha20.fst"
} | [
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"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": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Spec.Chacha20.shuffle | Prims.Tot | [
"total"
] | [] | [
"Spec.Chacha20.op_At",
"Spec.Chacha20.state",
"Spec.Chacha20.quarter_round"
] | [] | false | false | false | true | false | let diagonal_round:shuffle =
| quarter_round 0 5 10 15 @ quarter_round 1 6 11 12 @ quarter_round 2 7 8 13 @ quarter_round 3 4 9 14 | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.