author  wenzelm 
Sun, 01 Mar 2009 23:36:12 +0100  
changeset 30190  479806475f3c 
parent 29302  eb782d1dc07c 
child 30496  7cdcc9dd95cb 
permissions  rwrr 
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

1 
(* Title: HOL/arith_data.ML 
24095  2 
Author: Markus Wenzel, Stefan Berghofer, and Tobias Nipkow 
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

3 

24095  4 
Basic arithmetic proof tools. 
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

5 
*) 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

6 

26101  7 
signature ARITH_DATA = 
8 
sig 

29302  9 
val prove_conv: tactic > (simpset > tactic) > simpset > term * term > thm 
10 
val simp_all_tac: thm list > simpset > tactic 

26101  11 

12 
val mk_sum: term list > term 

13 
val mk_norm_sum: term list > term 

14 
val dest_sum: term > term list 

15 

16 
val nat_cancel_sums_add: simproc list 

17 
val nat_cancel_sums: simproc list 

18 
val setup: Context.generic > Context.generic 

19 
end; 

9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

20 

26101  21 
structure ArithData: ARITH_DATA = 
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

22 
struct 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

23 

26101  24 
(** generic proof tools **) 
25 

26 
(* prove conversions *) 

27 

28 
fun prove_conv expand_tac norm_tac ss tu = (* FIXME avoid standard *) 

29 
mk_meta_eq (standard (Goal.prove (Simplifier.the_context ss) [] [] 

30 
(HOLogic.mk_Trueprop (HOLogic.mk_eq tu)) 

31 
(K (EVERY [expand_tac, norm_tac ss])))); 

32 

33 
(* rewriting *) 

34 

35 
fun simp_all_tac rules = 

36 
let val ss0 = HOL_ss addsimps rules 

37 
in fn ss => ALLGOALS (simp_tac (Simplifier.inherit_context ss ss0)) end; 

38 

39 

9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

40 
(** abstract syntax of structure nat: 0, Suc, + **) 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

41 

26101  42 
local 
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

43 

22997  44 
val mk_plus = HOLogic.mk_binop @{const_name HOL.plus}; 
26101  45 
val dest_plus = HOLogic.dest_bin @{const_name HOL.plus} HOLogic.natT; 
46 

47 
in 

9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

48 

62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

49 
fun mk_sum [] = HOLogic.zero 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

50 
 mk_sum [t] = t 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

51 
 mk_sum (t :: ts) = mk_plus (t, mk_sum ts); 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

52 

62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

53 
(*normal form of sums: Suc (... (Suc (a + (b + ...))))*) 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

54 
fun mk_norm_sum ts = 
21621  55 
let val (ones, sums) = List.partition (equal HOLogic.Suc_zero) ts in 
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

56 
funpow (length ones) HOLogic.mk_Suc (mk_sum sums) 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

57 
end; 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

58 

24095  59 

9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

60 
fun dest_sum tm = 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

61 
if HOLogic.is_zero tm then [] 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

62 
else 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

63 
(case try HOLogic.dest_Suc tm of 
21621  64 
SOME t => HOLogic.Suc_zero :: dest_sum t 
15531  65 
 NONE => 
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

66 
(case try dest_plus tm of 
15531  67 
SOME (t, u) => dest_sum t @ dest_sum u 
68 
 NONE => [tm])); 

9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

69 

24095  70 
end; 
71 

20217
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
webertj
parents:
20044
diff
changeset

72 

9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

73 
(** cancel common summands **) 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

74 

62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

75 
structure Sum = 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

76 
struct 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

77 
val mk_sum = mk_norm_sum; 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

78 
val dest_sum = dest_sum; 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

79 
val prove_conv = prove_conv; 
22838  80 
val norm_tac1 = simp_all_tac [@{thm "add_Suc"}, @{thm "add_Suc_right"}, 
81 
@{thm "add_0"}, @{thm "add_0_right"}]; 

22548  82 
val norm_tac2 = simp_all_tac @{thms add_ac}; 
18328  83 
fun norm_tac ss = norm_tac1 ss THEN norm_tac2 ss; 
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

84 
end; 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

85 

62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

86 
fun gen_uncancel_tac rule ct = 
22838  87 
rtac (instantiate' [] [NONE, SOME ct] (rule RS @{thm subst_equals})) 1; 
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

88 

24095  89 

9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

90 
(* nat eq *) 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

91 

62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

92 
structure EqCancelSums = CancelSumsFun 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

93 
(struct 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

94 
open Sum; 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

95 
val mk_bal = HOLogic.mk_eq; 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

96 
val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT; 
22838  97 
val uncancel_tac = gen_uncancel_tac @{thm "nat_add_left_cancel"}; 
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

98 
end); 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

99 

24095  100 

9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

101 
(* nat less *) 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

102 

62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

103 
structure LessCancelSums = CancelSumsFun 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

104 
(struct 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

105 
open Sum; 
23881  106 
val mk_bal = HOLogic.mk_binrel @{const_name HOL.less}; 
107 
val dest_bal = HOLogic.dest_bin @{const_name HOL.less} HOLogic.natT; 

22838  108 
val uncancel_tac = gen_uncancel_tac @{thm "nat_add_left_cancel_less"}; 
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

109 
end); 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

110 

24095  111 

9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

112 
(* nat le *) 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

113 

62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

114 
structure LeCancelSums = CancelSumsFun 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

115 
(struct 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

116 
open Sum; 
23881  117 
val mk_bal = HOLogic.mk_binrel @{const_name HOL.less_eq}; 
118 
val dest_bal = HOLogic.dest_bin @{const_name HOL.less_eq} HOLogic.natT; 

22838  119 
val uncancel_tac = gen_uncancel_tac @{thm "nat_add_left_cancel_le"}; 
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

120 
end); 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

121 

24095  122 

9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

123 
(* nat diff *) 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

124 

62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

125 
structure DiffCancelSums = CancelSumsFun 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

126 
(struct 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

127 
open Sum; 
22997  128 
val mk_bal = HOLogic.mk_binop @{const_name HOL.minus}; 
129 
val dest_bal = HOLogic.dest_bin @{const_name HOL.minus} HOLogic.natT; 

22838  130 
val uncancel_tac = gen_uncancel_tac @{thm "diff_cancel"}; 
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

131 
end); 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

132 

24095  133 

134 
(* prepare nat_cancel simprocs *) 

9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

135 

26101  136 
val nat_cancel_sums_add = 
28262
aa7ca36d67fd
back to dynamic the_context(), because static @{theory} is invalidated if ML environment changes within the same code block;
wenzelm
parents:
26101
diff
changeset

137 
[Simplifier.simproc (the_context ()) "nateq_cancel_sums" 
26101  138 
["(l::nat) + m = n", "(l::nat) = m + n", "Suc m = n", "m = Suc n"] 
139 
(K EqCancelSums.proc), 

28262
aa7ca36d67fd
back to dynamic the_context(), because static @{theory} is invalidated if ML environment changes within the same code block;
wenzelm
parents:
26101
diff
changeset

140 
Simplifier.simproc (the_context ()) "natless_cancel_sums" 
26101  141 
["(l::nat) + m < n", "(l::nat) < m + n", "Suc m < n", "m < Suc n"] 
142 
(K LessCancelSums.proc), 

28262
aa7ca36d67fd
back to dynamic the_context(), because static @{theory} is invalidated if ML environment changes within the same code block;
wenzelm
parents:
26101
diff
changeset

143 
Simplifier.simproc (the_context ()) "natle_cancel_sums" 
26101  144 
["(l::nat) + m <= n", "(l::nat) <= m + n", "Suc m <= n", "m <= Suc n"] 
145 
(K LeCancelSums.proc)]; 

9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

146 

62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

147 
val nat_cancel_sums = nat_cancel_sums_add @ 
28262
aa7ca36d67fd
back to dynamic the_context(), because static @{theory} is invalidated if ML environment changes within the same code block;
wenzelm
parents:
26101
diff
changeset

148 
[Simplifier.simproc (the_context ()) "natdiff_cancel_sums" 
26101  149 
["((l::nat) + m)  n", "(l::nat)  (m + n)", "Suc m  n", "m  Suc n"] 
150 
(K DiffCancelSums.proc)]; 

9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset

151 

26101  152 
val setup = 
24095  153 
Simplifier.map_ss (fn ss => ss addsimprocs nat_cancel_sums); 
24076  154 

24095  155 
end; 