Theory SubPhase
subsection ‹SubNode Phase›
theory SubPhase
imports
Common
Proofs.StampEvalThms
begin
phase SubNode
terminating size
begin
lemma bin_sub_after_right_add:
shows "((x::('a::len) word) + (y::('a::len) word)) - y = x"
by simp
lemma sub_self_is_zero:
shows "(x::('a::len) word) - x = 0"
by simp
lemma bin_sub_then_left_add:
shows "(x::('a::len) word) - (x + (y::('a::len) word)) = -y"
by simp
lemma bin_sub_then_left_sub:
shows "(x::('a::len) word) - (x - (y::('a::len) word)) = y"
by simp
lemma bin_subtract_zero:
shows "(x :: 'a::len word) - (0 :: 'a::len word) = x"
by simp
lemma bin_sub_negative_value:
"(x :: ('a::len) word) - (-(y :: ('a::len) word)) = x + y"
by simp
lemma bin_sub_self_is_zero:
"(x :: ('a::len) word) - x = 0"
by simp
lemma bin_sub_negative_const:
"(x :: 'a::len word) - (-(y :: 'a::len word)) = x + y"
by simp
lemma val_sub_after_right_add_2:
assumes "x = new_int b v"
assumes "val[(x + y) - y] ≠ UndefVal"
shows "val[(x + y) - y] = x"
using assms apply (cases x; cases y; auto)
by (metis (full_types) intval_sub.simps(2))
lemma val_sub_after_left_sub:
assumes "val[(x - y) - x] ≠ UndefVal"
shows "val[(x - y) - x] = val[-y]"
using assms intval_sub.elims apply (cases x; cases y; auto)
by fastforce
lemma val_sub_then_left_sub:
assumes "y = new_int b v"
assumes "val[x - (x - y)] ≠ UndefVal"
shows "val[x - (x - y)] = y"
using assms apply (cases x; auto)
by (metis (mono_tags) intval_sub.simps(6))
lemma val_subtract_zero:
assumes "x = new_int b v"
assumes "val[x - (IntVal b 0)] ≠ UndefVal"
shows "val[x - (IntVal b 0)] = x"
by (cases x; simp add: assms)
lemma val_zero_subtract_value:
assumes "x = new_int b v"
assumes "val[(IntVal b 0) - x] ≠ UndefVal"
shows "val[(IntVal b 0) - x] = val[-x]"
by (cases x; simp add: assms)
lemma val_sub_then_left_add:
assumes "val[x - (x + y)] ≠ UndefVal"
shows "val[x - (x + y)] = val[-y]"
using assms apply (cases x; cases y; auto)
by (metis (mono_tags, lifting) intval_sub.simps(6))
lemma val_sub_negative_value:
assumes "val[x - (-y)] ≠ UndefVal"
shows "val[x - (-y)] = val[x + y]"
by (cases x; cases y; simp add: assms)
lemma val_sub_self_is_zero:
assumes "x = new_int b v ∧ val[x - x] ≠ UndefVal"
shows "val[x - x] = new_int b 0"
by (cases x; simp add: assms)
lemma val_sub_negative_const:
assumes "y = new_int b v ∧ val[x - (-y)] ≠ UndefVal"
shows "val[x - (-y)] = val[x + y]"
by (cases x; simp add: assms)
lemma exp_sub_after_right_add:
shows "exp[(x + y) - y] ≥ x"
apply auto
subgoal premises p for m p ya xa yaa
proof-
obtain xv where xv: "[m,p] ⊢ x ↦ xv"
using p(3) by auto
obtain yv where yv: "[m,p] ⊢ y ↦ yv"
using p(1) by auto
obtain xb xvv where xvv: "xv = IntVal xb xvv"
by (metis Value.exhaust evalDet evaltree_not_undef intval_add.simps(3,4,5) intval_sub.simps(2)
p(2,3) xv)
obtain yb yvv where yvv: "yv = IntVal yb yvv"
by (metis evalDet evaltree_not_undef intval_add.simps(7,8,9) intval_logic_negation.cases yv
intval_sub.simps(2) p(2,4))
then have lhsDefined: "val[(xv + yv) - yv] ≠ UndefVal"
using xvv yvv apply (cases xv; cases yv; auto)
by (metis evalDet intval_add.simps(1) p(3,4,5) xv yv)
then show ?thesis
by (metis ‹⋀thesis. (⋀(xb) xvv. (xv) = IntVal xb xvv ⟹ thesis) ⟹ thesis› evalDet xv yv
eval_unused_bits_zero lhsDefined new_int.simps p(1,3,4) val_sub_after_right_add_2)
qed
done
lemma exp_sub_after_right_add2:
shows "exp[(x + y) - x] ≥ y"
using exp_sub_after_right_add apply auto
by (metis bin_eval.simps(1,2) intval_add_sym unfold_binary)
lemma exp_sub_negative_value:
"exp[x - (-y)] ≥ exp[x + y]"
apply auto
subgoal premises p for m p xa ya
proof -
obtain xv where xv: "[m,p] ⊢ x ↦ xv"
using p(1) by auto
obtain yv where yv: "[m,p] ⊢ y ↦ yv"
using p(3) by auto
then have rhsEval: "[m,p] ⊢ exp[x + y] ↦ val[xv + yv]"
by (metis bin_eval.simps(1) evalDet p(1,2,3) unfold_binary val_sub_negative_value xv)
then show ?thesis
by (metis evalDet p(1,2,3) val_sub_negative_value xv yv)
qed
done
lemma exp_sub_then_left_sub:
"exp[x - (x - y)] ≥ y"
using val_sub_then_left_sub apply auto
subgoal premises p for m p xa xaa ya
proof-
obtain xa where xa: "[m, p] ⊢ x ↦ xa"
using p(2) by blast
obtain ya where ya: "[m, p] ⊢ y ↦ ya"
using p(5) by auto
obtain xaa where xaa: "[m, p] ⊢ x ↦ xaa"
using p(2) by blast
have 1: "val[xa - (xaa - ya)] ≠ UndefVal"
by (metis evalDet p(2,3,4,5) xa xaa ya)
then have "val[xaa - ya] ≠ UndefVal"
by auto
then have "[m, p] ⊢ y ↦ val[xa - (xaa - ya)]"
by (metis "1" Value.exhaust eval_unused_bits_zero evaltree_not_undef xa xaa ya new_int.simps
intval_sub.simps(6,7,8,9) evalDet val_sub_then_left_sub)
then show ?thesis
by (metis evalDet p(2,4,5) xa xaa ya)
qed
done
thm_oracles exp_sub_then_left_sub
lemma SubtractZero_Exp:
"exp[(x - (const IntVal b 0))] ≥ x"
apply auto
subgoal premises p for m p xa
proof-
obtain xv where xv: "[m,p] ⊢ x ↦ xv"
using p(1) by auto
obtain xb xvv where xvv: "xv = IntVal xb xvv"
by (metis array_length.cases evalDet evaltree_not_undef intval_sub.simps(3,4,5) p(1,2) xv)
then have widthSame: "xb=b"
by (metis evalDet intval_sub.simps(1) new_int_bin.simps p(1) p(2) xv)
then have unfoldSub: "val[xv - (IntVal b 0)] = (new_int xb (xvv-0))"
by (simp add: xvv)
then have rhsSame: "val[xv] = (new_int xb (xvv))"
using eval_unused_bits_zero xv xvv by auto
then show ?thesis
by (metis diff_zero evalDet p(1) unfoldSub xv)
qed
done
lemma ZeroSubtractValue_Exp:
assumes "wf_stamp x"
assumes "stamp_expr x = IntegerStamp b lo hi"
assumes "¬(is_ConstantExpr x)"
shows "exp[(const IntVal b 0) - x] ≥ exp[-x]"
using assms apply auto
subgoal premises p for m p xa
proof-
obtain xv where xv: "[m,p] ⊢ x ↦ xv"
using p(4) by auto
obtain xb xvv where xvv: "xv = IntVal xb xvv"
by (metis constantAsStamp.cases evalDet evaltree_not_undef intval_sub.simps(7,8,9) p(4,5) xv)
then have unfoldSub: "val[(IntVal b 0) - xv] = (new_int xb (0-xvv))"
by (metis intval_sub.simps(1) new_int_bin.simps p(1,2) valid_int_same_bits wf_stamp_def xv)
then show ?thesis
by (metis UnaryExpr intval_negate.simps(1) p(4,5) unary_eval.simps(2) verit_minus_simplify(3)
evalDet xv xvv)
qed
done
text ‹Optimisations›
optimization SubAfterAddRight: "((x + y) - y) ⟼ x"
using exp_sub_after_right_add by blast
optimization SubAfterAddLeft: "((x + y) - x) ⟼ y"
using exp_sub_after_right_add2 by blast
optimization SubAfterSubLeft: "((x - y) - x) ⟼ -y"
by (smt (verit) Suc_lessI add_2_eq_Suc' add_less_cancel_right less_trans_Suc not_add_less1 evalDet
size_binary_const size_binary_lhs size_binary_rhs size_non_add BinaryExprE bin_eval.simps(2)
le_expr_def unary_eval.simps(2) unfold_unary val_sub_after_left_sub)+
optimization SubThenAddLeft: "(x - (x + y)) ⟼ -y"
apply auto
by (metis evalDet unary_eval.simps(2) unfold_unary val_sub_then_left_add)
optimization SubThenAddRight: "(y - (x + y)) ⟼ -x"
apply auto
by (metis evalDet intval_add_sym unary_eval.simps(2) unfold_unary val_sub_then_left_add)
optimization SubThenSubLeft: "(x - (x - y)) ⟼ y"
using size_simps exp_sub_then_left_sub by auto
optimization SubtractZero: "(x - (const IntVal b 0)) ⟼ x"
using SubtractZero_Exp by fast
thm_oracles SubtractZero
optimization SubNegativeValue: "(x - (-y)) ⟼ x + y"
apply (metis add_2_eq_Suc' less_SucI less_add_Suc1 not_less_eq size_binary_const size_non_add)
using exp_sub_negative_value by blast
thm_oracles SubNegativeValue
lemma negate_idempotent:
assumes "x = IntVal b v ∧ take_bit b v = v"
shows "x = val[-(-x)]"
by (auto simp: assms is_IntVal_def)
optimization ZeroSubtractValue: "((const IntVal b 0) - x) ⟼ (-x)
when (wf_stamp x ∧ stamp_expr x = IntegerStamp b lo hi ∧ ¬(is_ConstantExpr x))"
using size_flip_binary ZeroSubtractValue_Exp by simp+
optimization SubSelfIsZero: "(x - x) ⟼ const IntVal b 0 when
(wf_stamp x ∧ stamp_expr x = IntegerStamp b lo hi)"
using size_non_const apply auto
by (smt (verit) wf_value_def ConstantExpr eval_bits_1_64 eval_unused_bits_zero new_int.simps
take_bit_of_0 val_sub_self_is_zero validDefIntConst valid_int wf_stamp_def One_nat_def
evalDet)
end
end