author | skalberg |
Fri, 05 Dec 2003 19:39:39 +0100 | |
changeset 14278 | ae499452700a |
parent 13902 | b41fc9a31975 |
child 14331 | 8dbbb7cf3637 |
permissions | -rw-r--r-- |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
1 |
(* Title: HOL/arith_data.ML |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
2 |
ID: $Id$ |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
3 |
Author: Markus Wenzel, Stefan Berghofer and Tobias Nipkow |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
4 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
5 |
Various arithmetic proof procedures. |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
6 |
*) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
7 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
8 |
(*---------------------------------------------------------------------------*) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
9 |
(* 1. Cancellation of common terms *) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
10 |
(*---------------------------------------------------------------------------*) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
11 |
|
13517 | 12 |
structure NatArithUtils = |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
13 |
struct |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
14 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
15 |
(** abstract syntax of structure nat: 0, Suc, + **) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
16 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
17 |
(* mk_sum, mk_norm_sum *) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
18 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
19 |
val one = HOLogic.mk_nat 1; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
20 |
val mk_plus = HOLogic.mk_binop "op +"; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
21 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
22 |
fun mk_sum [] = HOLogic.zero |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
23 |
| mk_sum [t] = t |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
24 |
| mk_sum (t :: ts) = mk_plus (t, mk_sum ts); |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
25 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
26 |
(*normal form of sums: Suc (... (Suc (a + (b + ...))))*) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
27 |
fun mk_norm_sum ts = |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
28 |
let val (ones, sums) = partition (equal one) ts in |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
29 |
funpow (length ones) HOLogic.mk_Suc (mk_sum sums) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
30 |
end; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
31 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
32 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
33 |
(* dest_sum *) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
34 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
35 |
val dest_plus = HOLogic.dest_bin "op +" HOLogic.natT; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
36 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
37 |
fun dest_sum tm = |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
38 |
if HOLogic.is_zero tm then [] |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
39 |
else |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
40 |
(case try HOLogic.dest_Suc tm of |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
41 |
Some t => one :: dest_sum t |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
42 |
| None => |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
43 |
(case try dest_plus tm of |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
44 |
Some (t, u) => dest_sum t @ dest_sum u |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
45 |
| None => [tm])); |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
46 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
47 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
48 |
(** generic proof tools **) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
49 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
50 |
(* prove conversions *) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
51 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
52 |
val mk_eqv = HOLogic.mk_Trueprop o HOLogic.mk_eq; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
53 |
|
13517 | 54 |
fun prove_conv expand_tac norm_tac sg tu = |
55 |
mk_meta_eq (prove_goalw_cterm_nocheck [] (cterm_of sg (mk_eqv tu)) |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
56 |
(K [expand_tac, norm_tac])) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
57 |
handle ERROR => error ("The error(s) above occurred while trying to prove " ^ |
13517 | 58 |
(string_of_cterm (cterm_of sg (mk_eqv tu)))); |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
59 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
60 |
val subst_equals = prove_goal HOL.thy "[| t = s; u = t |] ==> u = s" |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
61 |
(fn prems => [cut_facts_tac prems 1, SIMPSET' asm_simp_tac 1]); |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
62 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
63 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
64 |
(* rewriting *) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
65 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
66 |
fun simp_all rules = ALLGOALS (simp_tac (HOL_ss addsimps rules)); |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
67 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
68 |
val add_rules = [add_Suc, add_Suc_right, add_0, add_0_right]; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
69 |
val mult_rules = [mult_Suc, mult_Suc_right, mult_0, mult_0_right]; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
70 |
|
13517 | 71 |
fun prep_simproc (name, pats, proc) = |
72 |
Simplifier.simproc (Theory.sign_of (the_context ())) name pats proc; |
|
73 |
||
74 |
end; |
|
75 |
||
76 |
signature ARITH_DATA = |
|
77 |
sig |
|
78 |
val nat_cancel_sums_add: simproc list |
|
79 |
val nat_cancel_sums: simproc list |
|
80 |
end; |
|
81 |
||
82 |
structure ArithData: ARITH_DATA = |
|
83 |
struct |
|
84 |
||
85 |
open NatArithUtils; |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
86 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
87 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
88 |
(** cancel common summands **) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
89 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
90 |
structure Sum = |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
91 |
struct |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
92 |
val mk_sum = mk_norm_sum; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
93 |
val dest_sum = dest_sum; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
94 |
val prove_conv = prove_conv; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
95 |
val norm_tac = simp_all add_rules THEN simp_all add_ac; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
96 |
end; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
97 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
98 |
fun gen_uncancel_tac rule ct = |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
99 |
rtac (instantiate' [] [None, Some ct] (rule RS subst_equals)) 1; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
100 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
101 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
102 |
(* nat eq *) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
103 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
104 |
structure EqCancelSums = CancelSumsFun |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
105 |
(struct |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
106 |
open Sum; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
107 |
val mk_bal = HOLogic.mk_eq; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
108 |
val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
109 |
val uncancel_tac = gen_uncancel_tac add_left_cancel; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
110 |
end); |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
111 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
112 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
113 |
(* nat less *) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
114 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
115 |
structure LessCancelSums = CancelSumsFun |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
116 |
(struct |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
117 |
open Sum; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
118 |
val mk_bal = HOLogic.mk_binrel "op <"; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
119 |
val dest_bal = HOLogic.dest_bin "op <" HOLogic.natT; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
120 |
val uncancel_tac = gen_uncancel_tac add_left_cancel_less; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
121 |
end); |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
122 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
123 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
124 |
(* nat le *) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
125 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
126 |
structure LeCancelSums = CancelSumsFun |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
127 |
(struct |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
128 |
open Sum; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
129 |
val mk_bal = HOLogic.mk_binrel "op <="; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
130 |
val dest_bal = HOLogic.dest_bin "op <=" HOLogic.natT; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
131 |
val uncancel_tac = gen_uncancel_tac add_left_cancel_le; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
132 |
end); |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
133 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
134 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
135 |
(* nat diff *) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
136 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
137 |
structure DiffCancelSums = CancelSumsFun |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
138 |
(struct |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
139 |
open Sum; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
140 |
val mk_bal = HOLogic.mk_binop "op -"; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
141 |
val dest_bal = HOLogic.dest_bin "op -" HOLogic.natT; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
142 |
val uncancel_tac = gen_uncancel_tac diff_cancel; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
143 |
end); |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
144 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
145 |
|
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 |
(** prepare nat_cancel simprocs **) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
148 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
149 |
val nat_cancel_sums_add = map prep_simproc |
13462 | 150 |
[("nateq_cancel_sums", |
151 |
["(l::nat) + m = n", "(l::nat) = m + n", "Suc m = n", "m = Suc n"], EqCancelSums.proc), |
|
152 |
("natless_cancel_sums", |
|
153 |
["(l::nat) + m < n", "(l::nat) < m + n", "Suc m < n", "m < Suc n"], LessCancelSums.proc), |
|
154 |
("natle_cancel_sums", |
|
155 |
["(l::nat) + m <= n", "(l::nat) <= m + n", "Suc m <= n", "m <= Suc n"], LeCancelSums.proc)]; |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
156 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
157 |
val nat_cancel_sums = nat_cancel_sums_add @ |
13462 | 158 |
[prep_simproc ("natdiff_cancel_sums", |
159 |
["((l::nat) + m) - n", "(l::nat) - (m + n)", "Suc m - n", "m - Suc n"], DiffCancelSums.proc)]; |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
160 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
161 |
end; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
162 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
163 |
open ArithData; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
164 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
165 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
166 |
(*---------------------------------------------------------------------------*) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
167 |
(* 2. Linear arithmetic *) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
168 |
(*---------------------------------------------------------------------------*) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
169 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
170 |
(* Parameters data for general linear arithmetic functor *) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
171 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
172 |
structure LA_Logic: LIN_ARITH_LOGIC = |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
173 |
struct |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
174 |
val ccontr = ccontr; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
175 |
val conjI = conjI; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
176 |
val neqE = linorder_neqE; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
177 |
val notI = notI; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
178 |
val sym = sym; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
179 |
val not_lessD = linorder_not_less RS iffD1; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
180 |
val not_leD = linorder_not_le RS iffD1; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
181 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
182 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
183 |
fun mk_Eq thm = (thm RS Eq_FalseI) handle THM _ => (thm RS Eq_TrueI); |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
184 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
185 |
val mk_Trueprop = HOLogic.mk_Trueprop; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
186 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
187 |
fun neg_prop(TP$(Const("Not",_)$t)) = TP$t |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
188 |
| neg_prop(TP$t) = TP $ (Const("Not",HOLogic.boolT-->HOLogic.boolT)$t); |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
189 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
190 |
fun is_False thm = |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
191 |
let val _ $ t = #prop(rep_thm thm) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
192 |
in t = Const("False",HOLogic.boolT) end; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
193 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
194 |
fun is_nat(t) = fastype_of1 t = HOLogic.natT; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
195 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
196 |
fun mk_nat_thm sg t = |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
197 |
let val ct = cterm_of sg t and cn = cterm_of sg (Var(("n",0),HOLogic.natT)) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
198 |
in instantiate ([],[(cn,ct)]) le0 end; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
199 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
200 |
end; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
201 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
202 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
203 |
(* arith theory data *) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
204 |
|
9593 | 205 |
structure ArithTheoryDataArgs = |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
206 |
struct |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
207 |
val name = "HOL/arith"; |
13877
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
208 |
type T = {splits: thm list, inj_consts: (string * typ)list, discrete: (string * bool) list, presburger: (int -> tactic) option}; |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
209 |
|
13877
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
210 |
val empty = {splits = [], inj_consts = [], discrete = [], presburger = None}; |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
211 |
val copy = I; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
212 |
val prep_ext = I; |
13877
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
213 |
fun merge ({splits= splits1, inj_consts= inj_consts1, discrete= discrete1, presburger= presburger1}, |
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
214 |
{splits= splits2, inj_consts= inj_consts2, discrete= discrete2, presburger= presburger2}) = |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
215 |
{splits = Drule.merge_rules (splits1, splits2), |
10574
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents:
10516
diff
changeset
|
216 |
inj_consts = merge_lists inj_consts1 inj_consts2, |
13877
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
217 |
discrete = merge_alists discrete1 discrete2, |
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
218 |
presburger = (case presburger1 of None => presburger2 | p => p)}; |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
219 |
fun print _ _ = (); |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
220 |
end; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
221 |
|
9593 | 222 |
structure ArithTheoryData = TheoryDataFun(ArithTheoryDataArgs); |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
223 |
|
13877
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
224 |
fun arith_split_add (thy, thm) = (ArithTheoryData.map (fn {splits,inj_consts,discrete,presburger} => |
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
225 |
{splits= thm::splits, inj_consts= inj_consts, discrete= discrete, presburger= presburger}) thy, thm); |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
226 |
|
13877
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
227 |
fun arith_discrete d = ArithTheoryData.map (fn {splits,inj_consts,discrete,presburger} => |
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
228 |
{splits = splits, inj_consts = inj_consts, discrete = d :: discrete, presburger= presburger}); |
10574
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents:
10516
diff
changeset
|
229 |
|
13877
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
230 |
fun arith_inj_const c = ArithTheoryData.map (fn {splits,inj_consts,discrete,presburger} => |
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
231 |
{splits = splits, inj_consts = c :: inj_consts, discrete = discrete, presburger = presburger}); |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
232 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
233 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
234 |
structure LA_Data_Ref: LIN_ARITH_DATA = |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
235 |
struct |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
236 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
237 |
(* Decomposition of terms *) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
238 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
239 |
fun nT (Type("fun",[N,_])) = N = HOLogic.natT |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
240 |
| nT _ = false; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
241 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
242 |
fun add_atom(t,m,(p,i)) = (case assoc(p,t) of None => ((t,m)::p,i) |
10693 | 243 |
| Some n => (overwrite(p,(t,ratadd(n,m))), i)); |
244 |
||
245 |
exception Zero; |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
246 |
|
10693 | 247 |
fun rat_of_term(numt,dent) = |
248 |
let val num = HOLogic.dest_binum numt and den = HOLogic.dest_binum dent |
|
249 |
in if den = 0 then raise Zero else int_ratdiv(num,den) end; |
|
10718 | 250 |
|
251 |
(* Warning: in rare cases number_of encloses a non-numeral, |
|
252 |
in which case dest_binum raises TERM; hence all the handles below. |
|
11334
a16eaf2a1edd
Allow Suc-numerals as coefficients in lin-arith formulae
nipkow
parents:
10906
diff
changeset
|
253 |
Same for Suc-terms that turn out not to be numerals - |
a16eaf2a1edd
Allow Suc-numerals as coefficients in lin-arith formulae
nipkow
parents:
10906
diff
changeset
|
254 |
although the simplifier should eliminate those anyway... |
10718 | 255 |
*) |
256 |
||
11334
a16eaf2a1edd
Allow Suc-numerals as coefficients in lin-arith formulae
nipkow
parents:
10906
diff
changeset
|
257 |
fun number_of_Sucs (Const("Suc",_) $ n) = number_of_Sucs n + 1 |
a16eaf2a1edd
Allow Suc-numerals as coefficients in lin-arith formulae
nipkow
parents:
10906
diff
changeset
|
258 |
| number_of_Sucs t = if HOLogic.is_zero t then 0 |
a16eaf2a1edd
Allow Suc-numerals as coefficients in lin-arith formulae
nipkow
parents:
10906
diff
changeset
|
259 |
else raise TERM("number_of_Sucs",[]) |
a16eaf2a1edd
Allow Suc-numerals as coefficients in lin-arith formulae
nipkow
parents:
10906
diff
changeset
|
260 |
|
10718 | 261 |
(* decompose nested multiplications, bracketing them to the right and combining all |
262 |
their coefficients |
|
263 |
*) |
|
264 |
||
13499 | 265 |
fun demult inj_consts = |
266 |
let |
|
10718 | 267 |
fun demult((mC as Const("op *",_)) $ s $ t,m) = ((case s of |
268 |
Const("Numeral.number_of",_)$n |
|
269 |
=> demult(t,ratmul(m,rat_of_int(HOLogic.dest_binum n))) |
|
12480
32e67277a4b9
tuned conversion from terms to "polynomials" for arith_tac: takes care
nipkow
parents:
12338
diff
changeset
|
270 |
| Const("uminus",_)$(Const("Numeral.number_of",_)$n) |
32e67277a4b9
tuned conversion from terms to "polynomials" for arith_tac: takes care
nipkow
parents:
12338
diff
changeset
|
271 |
=> demult(t,ratmul(m,rat_of_int(~(HOLogic.dest_binum n)))) |
11334
a16eaf2a1edd
Allow Suc-numerals as coefficients in lin-arith formulae
nipkow
parents:
10906
diff
changeset
|
272 |
| Const("Suc",_) $ _ |
a16eaf2a1edd
Allow Suc-numerals as coefficients in lin-arith formulae
nipkow
parents:
10906
diff
changeset
|
273 |
=> demult(t,ratmul(m,rat_of_int(number_of_Sucs s))) |
10718 | 274 |
| Const("op *",_) $ s1 $ s2 => demult(mC $ s1 $ (mC $ s2 $ t),m) |
275 |
| Const("HOL.divide",_) $ numt $ (Const("Numeral.number_of",_)$dent) => |
|
276 |
let val den = HOLogic.dest_binum dent |
|
277 |
in if den = 0 then raise Zero |
|
278 |
else demult(mC $ numt $ t,ratmul(m, ratinv(rat_of_int den))) |
|
279 |
end |
|
280 |
| _ => atomult(mC,s,t,m) |
|
281 |
) handle TERM _ => atomult(mC,s,t,m)) |
|
282 |
| demult(atom as Const("HOL.divide",_) $ t $ (Const("Numeral.number_of",_)$dent), m) = |
|
283 |
(let val den = HOLogic.dest_binum dent |
|
284 |
in if den = 0 then raise Zero else demult(t,ratmul(m, ratinv(rat_of_int den))) end |
|
285 |
handle TERM _ => (Some atom,m)) |
|
286 |
| demult(t as Const("Numeral.number_of",_)$n,m) = |
|
287 |
((None,ratmul(m,rat_of_int(HOLogic.dest_binum n))) |
|
288 |
handle TERM _ => (Some t,m)) |
|
12480
32e67277a4b9
tuned conversion from terms to "polynomials" for arith_tac: takes care
nipkow
parents:
12338
diff
changeset
|
289 |
| demult(Const("uminus",_)$t, m) = demult(t,ratmul(m,rat_of_int(~1))) |
13499 | 290 |
| demult(t as Const f $ x, m) = |
291 |
(if f mem inj_consts then Some x else Some t,m) |
|
10718 | 292 |
| demult(atom,m) = (Some atom,m) |
293 |
||
294 |
and atomult(mC,atom,t,m) = (case demult(t,m) of (None,m') => (Some atom,m') |
|
295 |
| (Some t',m') => (Some(mC $ atom $ t'),m')) |
|
13499 | 296 |
in demult end; |
10718 | 297 |
|
10574
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents:
10516
diff
changeset
|
298 |
fun decomp2 inj_consts (rel,lhs,rhs) = |
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents:
10516
diff
changeset
|
299 |
let |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
300 |
(* Turn term into list of summand * multiplicity plus a constant *) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
301 |
fun poly(Const("op +",_) $ s $ t, m, pi) = poly(s,m,poly(t,m,pi)) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
302 |
| poly(all as Const("op -",T) $ s $ t, m, pi) = |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
303 |
if nT T then add_atom(all,m,pi) |
10693 | 304 |
else poly(s,m,poly(t,ratneg m,pi)) |
305 |
| poly(Const("uminus",_) $ t, m, pi) = poly(t,ratneg m,pi) |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
306 |
| poly(Const("0",_), _, pi) = pi |
11464 | 307 |
| poly(Const("1",_), m, (p,i)) = (p,ratadd(i,m)) |
10693 | 308 |
| poly(Const("Suc",_)$t, m, (p,i)) = poly(t, m, (p,ratadd(i,m))) |
10718 | 309 |
| poly(t as Const("op *",_) $ _ $ _, m, pi as (p,i)) = |
13499 | 310 |
(case demult inj_consts (t,m) of |
10718 | 311 |
(None,m') => (p,ratadd(i,m)) |
312 |
| (Some u,m') => add_atom(u,m',pi)) |
|
313 |
| poly(t as Const("HOL.divide",_) $ _ $ _, m, pi as (p,i)) = |
|
13499 | 314 |
(case demult inj_consts (t,m) of |
10718 | 315 |
(None,m') => (p,ratadd(i,m)) |
316 |
| (Some u,m') => add_atom(u,m',pi)) |
|
317 |
| poly(all as (Const("Numeral.number_of",_)$t,m,(p,i))) = |
|
318 |
((p,ratadd(i,ratmul(m,rat_of_int(HOLogic.dest_binum t)))) |
|
319 |
handle TERM _ => add_atom all) |
|
10574
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents:
10516
diff
changeset
|
320 |
| poly(all as Const f $ x, m, pi) = |
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents:
10516
diff
changeset
|
321 |
if f mem inj_consts then poly(x,m,pi) else add_atom(all,m,pi) |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
322 |
| poly x = add_atom x; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
323 |
|
10718 | 324 |
val (p,i) = poly(lhs,rat_of_int 1,([],rat_of_int 0)) |
325 |
and (q,j) = poly(rhs,rat_of_int 1,([],rat_of_int 0)) |
|
10693 | 326 |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
327 |
in case rel of |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
328 |
"op <" => Some(p,i,"<",q,j) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
329 |
| "op <=" => Some(p,i,"<=",q,j) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
330 |
| "op =" => Some(p,i,"=",q,j) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
331 |
| _ => None |
10693 | 332 |
end handle Zero => None; |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
333 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
334 |
fun negate(Some(x,i,rel,y,j,d)) = Some(x,i,"~"^rel,y,j,d) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
335 |
| negate None = None; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
336 |
|
10574
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents:
10516
diff
changeset
|
337 |
fun decomp1 (discrete,inj_consts) (T,xxx) = |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
338 |
(case T of |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
339 |
Type("fun",[Type(D,[]),_]) => |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
340 |
(case assoc(discrete,D) of |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
341 |
None => None |
10574
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents:
10516
diff
changeset
|
342 |
| Some d => (case decomp2 inj_consts xxx of |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
343 |
None => None |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
344 |
| Some(p,i,rel,q,j) => Some(p,i,rel,q,j,d))) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
345 |
| _ => None); |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
346 |
|
10574
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents:
10516
diff
changeset
|
347 |
fun decomp2 data (_$(Const(rel,T)$lhs$rhs)) = decomp1 data (T,(rel,lhs,rhs)) |
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents:
10516
diff
changeset
|
348 |
| decomp2 data (_$(Const("Not",_)$(Const(rel,T)$lhs$rhs))) = |
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents:
10516
diff
changeset
|
349 |
negate(decomp1 data (T,(rel,lhs,rhs))) |
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents:
10516
diff
changeset
|
350 |
| decomp2 data _ = None |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
351 |
|
10574
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents:
10516
diff
changeset
|
352 |
fun decomp sg = |
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents:
10516
diff
changeset
|
353 |
let val {discrete, inj_consts, ...} = ArithTheoryData.get_sg sg |
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents:
10516
diff
changeset
|
354 |
in decomp2 (discrete,inj_consts) end |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
355 |
|
10693 | 356 |
fun number_of(n,T) = HOLogic.number_of_const T $ (HOLogic.mk_bin n) |
357 |
||
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
358 |
end; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
359 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
360 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
361 |
structure Fast_Arith = |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
362 |
Fast_Lin_Arith(structure LA_Logic=LA_Logic and LA_Data=LA_Data_Ref); |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
363 |
|
13499 | 364 |
val fast_arith_tac = Fast_Arith.lin_arith_tac false |
365 |
and fast_ex_arith_tac = Fast_Arith.lin_arith_tac |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
366 |
and trace_arith = Fast_Arith.trace; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
367 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
368 |
local |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
369 |
|
13902
b41fc9a31975
added isolate_Suc to counteract deficiencies in one of Larry's simprocs.
nipkow
parents:
13877
diff
changeset
|
370 |
val isolateSuc = |
b41fc9a31975
added isolate_Suc to counteract deficiencies in one of Larry's simprocs.
nipkow
parents:
13877
diff
changeset
|
371 |
let val thy = theory "Nat" |
b41fc9a31975
added isolate_Suc to counteract deficiencies in one of Larry's simprocs.
nipkow
parents:
13877
diff
changeset
|
372 |
in prove_goal thy "Suc(i+j) = i+j + Suc 0" |
b41fc9a31975
added isolate_Suc to counteract deficiencies in one of Larry's simprocs.
nipkow
parents:
13877
diff
changeset
|
373 |
(fn _ => [simp_tac (simpset_of thy) 1]) |
b41fc9a31975
added isolate_Suc to counteract deficiencies in one of Larry's simprocs.
nipkow
parents:
13877
diff
changeset
|
374 |
end; |
b41fc9a31975
added isolate_Suc to counteract deficiencies in one of Larry's simprocs.
nipkow
parents:
13877
diff
changeset
|
375 |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
376 |
(* reduce contradictory <= to False. |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
377 |
Most of the work is done by the cancel tactics. |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
378 |
*) |
12931
2c0251fada94
solved the problem that Larry's simproce cancle_numerals(?) returns
nipkow
parents:
12480
diff
changeset
|
379 |
val add_rules = |
2c0251fada94
solved the problem that Larry's simproce cancle_numerals(?) returns
nipkow
parents:
12480
diff
changeset
|
380 |
[add_0,add_0_right,Zero_not_Suc,Suc_not_Zero,le_0_eq, |
13902
b41fc9a31975
added isolate_Suc to counteract deficiencies in one of Larry's simprocs.
nipkow
parents:
13877
diff
changeset
|
381 |
One_nat_def,isolateSuc]; |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
382 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
383 |
val add_mono_thms_nat = map (fn s => prove_goal (the_context ()) s |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
384 |
(fn prems => [cut_facts_tac prems 1, |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
385 |
blast_tac (claset() addIs [add_le_mono]) 1])) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
386 |
["(i <= j) & (k <= l) ==> i + k <= j + (l::nat)", |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
387 |
"(i = j) & (k <= l) ==> i + k <= j + (l::nat)", |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
388 |
"(i <= j) & (k = l) ==> i + k <= j + (l::nat)", |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
389 |
"(i = j) & (k = l) ==> i + k = j + (l::nat)" |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
390 |
]; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
391 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
392 |
in |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
393 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
394 |
val init_lin_arith_data = |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
395 |
Fast_Arith.setup @ |
10693 | 396 |
[Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, simpset = _} => |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
397 |
{add_mono_thms = add_mono_thms @ add_mono_thms_nat, |
10693 | 398 |
mult_mono_thms = mult_mono_thms, |
10574
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents:
10516
diff
changeset
|
399 |
inj_thms = inj_thms, |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
400 |
lessD = lessD @ [Suc_leI], |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
401 |
simpset = HOL_basic_ss addsimps add_rules addsimprocs nat_cancel_sums_add}), |
9593 | 402 |
ArithTheoryData.init, arith_discrete ("nat", true)]; |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
403 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
404 |
end; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
405 |
|
13462 | 406 |
val fast_nat_arith_simproc = |
407 |
Simplifier.simproc (Theory.sign_of (the_context ())) "fast_nat_arith" |
|
408 |
["(m::nat) < n","(m::nat) <= n", "(m::nat) = n"] Fast_Arith.lin_arith_prover; |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
409 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
410 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
411 |
(* Because of fast_nat_arith_simproc, the arithmetic solver is really only |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
412 |
useful to detect inconsistencies among the premises for subgoals which are |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
413 |
*not* themselves (in)equalities, because the latter activate |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
414 |
fast_nat_arith_simproc anyway. However, it seems cheaper to activate the |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
415 |
solver all the time rather than add the additional check. *) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
416 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
417 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
418 |
(* arith proof method *) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
419 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
420 |
(* FIXME: K true should be replaced by a sensible test to speed things up |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
421 |
in case there are lots of irrelevant terms involved; |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
422 |
elimination of min/max can be optimized: |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
423 |
(max m n + k <= r) = (m+k <= r & n+k <= r) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
424 |
(l <= min m n + k) = (l <= m+k & l <= n+k) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
425 |
*) |
10516 | 426 |
local |
427 |
||
13499 | 428 |
fun raw_arith_tac ex i st = |
429 |
refute_tac (K true) |
|
430 |
(REPEAT o split_tac (#splits (ArithTheoryData.get_sg (Thm.sign_of_thm st)))) |
|
431 |
((REPEAT_DETERM o etac linorder_neqE) THEN' fast_ex_arith_tac ex) |
|
432 |
i st; |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
433 |
|
13877
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
434 |
fun presburger_tac i st = |
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
435 |
(case ArithTheoryData.get_sg (sign_of_thm st) of |
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
436 |
{presburger = Some tac, ...} => |
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
437 |
(tracing "Simple arithmetic decision procedure failed.\nNow trying full Presburger arithmetic..."; tac i st) |
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
438 |
| _ => no_tac st); |
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
439 |
|
10516 | 440 |
in |
441 |
||
13877
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
442 |
val simple_arith_tac = FIRST' [fast_arith_tac, |
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
443 |
ObjectLogic.atomize_tac THEN' raw_arith_tac true]; |
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
444 |
|
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
445 |
val arith_tac = FIRST' [fast_arith_tac, |
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
446 |
ObjectLogic.atomize_tac THEN' raw_arith_tac true, |
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
447 |
presburger_tac]; |
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
448 |
|
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
449 |
val silent_arith_tac = FIRST' [fast_arith_tac, |
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
450 |
ObjectLogic.atomize_tac THEN' raw_arith_tac false, |
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
berghofe
parents:
13517
diff
changeset
|
451 |
presburger_tac]; |
10516 | 452 |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
453 |
fun arith_method prems = |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
454 |
Method.METHOD (fn facts => HEADGOAL (Method.insert_tac (prems @ facts) THEN' arith_tac)); |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
455 |
|
10516 | 456 |
end; |
457 |
||
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
458 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
459 |
(* theory setup *) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
460 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
461 |
val arith_setup = |
10766 | 462 |
[Simplifier.change_simpset_of (op addsimprocs) nat_cancel_sums] @ |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
463 |
init_lin_arith_data @ |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
464 |
[Simplifier.change_simpset_of (op addSolver) |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
465 |
(mk_solver "lin. arith." Fast_Arith.cut_lin_arith_tac), |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
466 |
Simplifier.change_simpset_of (op addsimprocs) [fast_nat_arith_simproc], |
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
467 |
Method.add_methods [("arith", (arith_method o #2) oo Method.syntax Args.bang_facts, |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
468 |
"decide linear arithmethic")], |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
469 |
Attrib.add_attributes [("arith_split", |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff
changeset
|
470 |
(Attrib.no_args arith_split_add, Attrib.no_args Attrib.undef_local_attribute), |
9893 | 471 |
"declaration of split rules for arithmetic procedure")]]; |