| author | wenzelm | 
| Fri, 11 May 2018 20:22:20 +0200 | |
| changeset 68151 | 3c321783bae3 | 
| parent 67561 | f0b11413f1c9 | 
| child 69593 | 3dda49e08b9d | 
| permissions | -rw-r--r-- | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 1 | (* Author: Lawrence C Paulson, Cambridge University Computer Laboratory | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 2 | Copyright 2000 University of Cambridge | 
| 23164 | 3 | |
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 4 | Simprocs for the (integer) numerals. | 
| 23164 | 5 | *) | 
| 6 | ||
| 7 | (*To quote from Provers/Arith/cancel_numeral_factor.ML: | |
| 8 | ||
| 9 | Cancels common coefficients in balanced expressions: | |
| 10 | ||
| 11 | u*#m ~~ u'*#m' == #n*u ~~ #n'*u' | |
| 12 | ||
| 13 | where ~~ is an appropriate balancing operation (e.g. =, <=, <, div, /) | |
| 14 | and d = gcd(m,m') and n=m/d and n'=m'/d. | |
| 15 | *) | |
| 16 | ||
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 17 | signature NUMERAL_SIMPROCS = | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 18 | sig | 
| 59530 | 19 | val trans_tac: Proof.context -> thm option -> tactic | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 20 | val assoc_fold: Proof.context -> cterm -> thm option | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 21 | val combine_numerals: Proof.context -> cterm -> thm option | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 22 | val eq_cancel_numerals: Proof.context -> cterm -> thm option | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 23 | val less_cancel_numerals: Proof.context -> cterm -> thm option | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 24 | val le_cancel_numerals: Proof.context -> cterm -> thm option | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 25 | val eq_cancel_factor: Proof.context -> cterm -> thm option | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 26 | val le_cancel_factor: Proof.context -> cterm -> thm option | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 27 | val less_cancel_factor: Proof.context -> cterm -> thm option | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 28 | val div_cancel_factor: Proof.context -> cterm -> thm option | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 29 | val mod_cancel_factor: Proof.context -> cterm -> thm option | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 30 | val dvd_cancel_factor: Proof.context -> cterm -> thm option | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 31 | val divide_cancel_factor: Proof.context -> cterm -> thm option | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 32 | val eq_cancel_numeral_factor: Proof.context -> cterm -> thm option | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 33 | val less_cancel_numeral_factor: Proof.context -> cterm -> thm option | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 34 | val le_cancel_numeral_factor: Proof.context -> cterm -> thm option | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 35 | val div_cancel_numeral_factor: Proof.context -> cterm -> thm option | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 36 | val divide_cancel_numeral_factor: Proof.context -> cterm -> thm option | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 37 | val field_combine_numerals: Proof.context -> cterm -> thm option | 
| 61144 | 38 | val field_divide_cancel_numeral_factor: simproc | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 39 | val num_ss: simpset | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 40 | val field_comp_conv: Proof.context -> conv | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 41 | end; | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 42 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 43 | structure Numeral_Simprocs : NUMERAL_SIMPROCS = | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 44 | struct | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 45 | |
| 59530 | 46 | fun trans_tac _ NONE = all_tac | 
| 47 | | trans_tac ctxt (SOME th) = ALLGOALS (resolve_tac ctxt [th RS trans]); | |
| 44945 | 48 | |
| 33359 | 49 | val mk_number = Arith_Data.mk_number; | 
| 50 | val mk_sum = Arith_Data.mk_sum; | |
| 51 | val long_mk_sum = Arith_Data.long_mk_sum; | |
| 52 | val dest_sum = Arith_Data.dest_sum; | |
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 53 | |
| 35267 
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
 haftmann parents: 
35092diff
changeset | 54 | val mk_times = HOLogic.mk_binop @{const_name Groups.times};
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 55 | |
| 35267 
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
 haftmann parents: 
35092diff
changeset | 56 | fun one_of T = Const(@{const_name Groups.one}, T);
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 57 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 58 | (* build product with trailing 1 rather than Numeral 1 in order to avoid the | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 59 | unnecessary restriction to type class number_ring | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 60 | which is not required for cancellation of common factors in divisions. | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 61 | UPDATE: this reasoning no longer applies (number_ring is gone) | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 62 | *) | 
| 61144 | 63 | fun mk_prod T = | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 64 | let val one = one_of T | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 65 | fun mk [] = one | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 66 | | mk [t] = t | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 67 | | mk (t :: ts) = if t = one then mk ts else mk_times (t, mk ts) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 68 | in mk end; | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 69 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 70 | (*This version ALWAYS includes a trailing one*) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 71 | fun long_mk_prod T [] = one_of T | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 72 | | long_mk_prod T (t :: ts) = mk_times (t, mk_prod T ts); | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 73 | |
| 49323 | 74 | val dest_times = HOLogic.dest_bin @{const_name Groups.times} dummyT;
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 75 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 76 | fun dest_prod t = | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 77 | let val (t,u) = dest_times t | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 78 | in dest_prod t @ dest_prod u end | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 79 | handle TERM _ => [t]; | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 80 | |
| 33359 | 81 | fun find_first_numeral past (t::terms) = | 
| 82 | ((snd (HOLogic.dest_number t), rev past @ terms) | |
| 83 | handle TERM _ => find_first_numeral (t::past) terms) | |
| 84 |   | find_first_numeral past [] = raise TERM("find_first_numeral", []);
 | |
| 85 | ||
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 86 | (*DON'T do the obvious simplifications; that would create special cases*) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 87 | fun mk_coeff (k, t) = mk_times (mk_number (Term.fastype_of t) k, t); | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 88 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 89 | (*Express t as a product of (possibly) a numeral with other sorted terms*) | 
| 35267 
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
 haftmann parents: 
35092diff
changeset | 90 | fun dest_coeff sign (Const (@{const_name Groups.uminus}, _) $ t) = dest_coeff (~sign) t
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 91 | | dest_coeff sign t = | 
| 35408 | 92 | let val ts = sort Term_Ord.term_ord (dest_prod t) | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 93 | val (n, ts') = find_first_numeral [] ts | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 94 | handle TERM _ => (1, ts) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 95 | in (sign*n, mk_prod (Term.fastype_of t) ts') end; | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 96 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 97 | (*Find first coefficient-term THAT MATCHES u*) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 98 | fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
 | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 99 | | find_first_coeff past u (t::terms) = | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 100 | let val (n,u') = dest_coeff 1 t | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 101 | in if u aconv u' then (n, rev past @ terms) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 102 | else find_first_coeff (t::past) u terms | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 103 | end | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 104 | handle TERM _ => find_first_coeff (t::past) u terms; | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 105 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 106 | (*Fractions as pairs of ints. Can't use Rat.rat because the representation | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 107 | needs to preserve negative values in the denominator.*) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 108 | fun mk_frac (p, q) = if q = 0 then raise Div else (p, q); | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 109 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 110 | (*Don't reduce fractions; sums must be proved by rule add_frac_eq. | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 111 | Fractions are reduced later by the cancel_numeral_factor simproc.*) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 112 | fun add_frac ((p1, q1), (p2, q2)) = (p1 * q2 + p2 * q1, q1 * q2); | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 113 | |
| 60352 
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
 haftmann parents: 
59867diff
changeset | 114 | val mk_divide = HOLogic.mk_binop @{const_name Rings.divide};
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 115 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 116 | (*Build term (p / q) * t*) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 117 | fun mk_fcoeff ((p, q), t) = | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 118 | let val T = Term.fastype_of t | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 119 | in mk_times (mk_divide (mk_number T p, mk_number T q), t) end; | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 120 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 121 | (*Express t as a product of a fraction with other sorted terms*) | 
| 35267 
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
 haftmann parents: 
35092diff
changeset | 122 | fun dest_fcoeff sign (Const (@{const_name Groups.uminus}, _) $ t) = dest_fcoeff (~sign) t
 | 
| 60352 
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
 haftmann parents: 
59867diff
changeset | 123 |   | dest_fcoeff sign (Const (@{const_name Rings.divide}, _) $ t $ u) =
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 124 | let val (p, t') = dest_coeff sign t | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 125 | val (q, u') = dest_coeff 1 u | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 126 | in (mk_frac (p, q), mk_divide (t', u')) end | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 127 | | dest_fcoeff sign t = | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 128 | let val (p, t') = dest_coeff sign t | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 129 | val T = Term.fastype_of t | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 130 | in (mk_frac (p, 1), mk_divide (t', one_of T)) end; | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 131 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 132 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 133 | (** New term ordering so that AC-rewriting brings numerals to the front **) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 134 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 135 | (*Order integers by absolute value and then by sign. The standard integer | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 136 | ordering is not well-founded.*) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 137 | fun num_ord (i,j) = | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 138 | (case int_ord (abs i, abs j) of | 
| 61144 | 139 | EQUAL => int_ord (Int.sign i, Int.sign j) | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 140 | | ord => ord); | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 141 | |
| 35408 | 142 | (*This resembles Term_Ord.term_ord, but it puts binary numerals before other | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 143 | non-atomic terms.*) | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 144 | local open Term | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 145 | in | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 146 | fun numterm_ord (t, u) = | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 147 | case (try HOLogic.dest_number t, try HOLogic.dest_number u) of | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 148 | (SOME (_, i), SOME (_, j)) => num_ord (i, j) | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 149 | | (SOME _, NONE) => LESS | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 150 | | (NONE, SOME _) => GREATER | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 151 | | _ => ( | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 152 | case (t, u) of | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 153 | (Abs (_, T, t), Abs(_, U, u)) => | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 154 | (prod_ord numterm_ord Term_Ord.typ_ord ((t, T), (u, U))) | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 155 | | _ => ( | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 156 | case int_ord (size_of_term t, size_of_term u) of | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 157 | EQUAL => | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 158 | let val (f, ts) = strip_comb t and (g, us) = strip_comb u in | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 159 | (prod_ord Term_Ord.hd_ord numterms_ord ((f, ts), (g, us))) | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 160 | end | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 161 | | ord => ord)) | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 162 | and numterms_ord (ts, us) = list_ord numterm_ord (ts, us) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 163 | end; | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 164 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 165 | val num_ss = | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
67560diff
changeset | 166 |   simpset_of (put_simpset HOL_basic_ss @{context} |> Simplifier.set_term_ord numterm_ord);
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 167 | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 168 | (*Maps 1 to Numeral1 so that arithmetic isn't complicated by the abstract 1.*) | 
| 61694 
6571c78c9667
Removed some legacy theorems; minor adjustments to simplification rules; new material on homotopic paths
 paulson <lp15@cam.ac.uk> parents: 
61150diff
changeset | 169 | val numeral_syms = [@{thm numeral_One} RS sym];
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 170 | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 171 | (*Simplify 0+n, n+0, Numeral1*n, n*Numeral1, 1*x, x*1, x/1 *) | 
| 59556 | 172 | val add_0s =  @{thms add_0_left add_0_right};
 | 
| 64240 | 173 | val mult_1s = @{thms mult_1s divide_numeral_1 mult_1_left mult_1_right mult_minus1 mult_minus1_right div_by_1};
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 174 | |
| 45437 
958d19d3405b
tune post-processing of simproc-generated rules so they won't produce Numeral0 or Numeral1
 huffman parents: 
45306diff
changeset | 175 | (* For post-simplification of the rhs of simproc-generated rules *) | 
| 
958d19d3405b
tune post-processing of simproc-generated rules so they won't produce Numeral0 or Numeral1
 huffman parents: 
45306diff
changeset | 176 | val post_simps = | 
| 61694 
6571c78c9667
Removed some legacy theorems; minor adjustments to simplification rules; new material on homotopic paths
 paulson <lp15@cam.ac.uk> parents: 
61150diff
changeset | 177 |     [@{thm numeral_One},
 | 
| 45437 
958d19d3405b
tune post-processing of simproc-generated rules so they won't produce Numeral0 or Numeral1
 huffman parents: 
45306diff
changeset | 178 |      @{thm add_0_left}, @{thm add_0_right},
 | 
| 
958d19d3405b
tune post-processing of simproc-generated rules so they won't produce Numeral0 or Numeral1
 huffman parents: 
45306diff
changeset | 179 |      @{thm mult_zero_left}, @{thm mult_zero_right},
 | 
| 
958d19d3405b
tune post-processing of simproc-generated rules so they won't produce Numeral0 or Numeral1
 huffman parents: 
45306diff
changeset | 180 |      @{thm mult_1_left}, @{thm mult_1_right},
 | 
| 
958d19d3405b
tune post-processing of simproc-generated rules so they won't produce Numeral0 or Numeral1
 huffman parents: 
45306diff
changeset | 181 |      @{thm mult_minus1}, @{thm mult_minus1_right}]
 | 
| 
958d19d3405b
tune post-processing of simproc-generated rules so they won't produce Numeral0 or Numeral1
 huffman parents: 
45306diff
changeset | 182 | |
| 
958d19d3405b
tune post-processing of simproc-generated rules so they won't produce Numeral0 or Numeral1
 huffman parents: 
45306diff
changeset | 183 | val field_post_simps = | 
| 64240 | 184 |     post_simps @ [@{thm div_0}, @{thm div_by_1}]
 | 
| 61144 | 185 | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54230diff
changeset | 186 | (*Simplify inverse Numeral1*) | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 187 | val inverse_1s = [@{thm inverse_numeral_1}];
 | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 188 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 189 | (*To perform binary arithmetic. The "left" rewriting handles patterns | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 190 | created by the Numeral_Simprocs, such as 3 * (5 * x). *) | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 191 | val simps = | 
| 61694 
6571c78c9667
Removed some legacy theorems; minor adjustments to simplification rules; new material on homotopic paths
 paulson <lp15@cam.ac.uk> parents: 
61150diff
changeset | 192 |     [@{thm numeral_One} RS sym] @
 | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 193 |     @{thms add_numeral_left} @
 | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 194 |     @{thms add_neg_numeral_left} @
 | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 195 |     @{thms mult_numeral_left} @
 | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 196 |     @{thms arith_simps} @ @{thms rel_simps};
 | 
| 61144 | 197 | |
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 198 | (*Binary arithmetic BUT NOT ADDITION since it may collapse adjacent terms | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 199 | during re-arrangement*) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 200 | val non_add_simps = | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 201 | subtract Thm.eq_thm | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 202 |     (@{thms add_numeral_left} @
 | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 203 |      @{thms add_neg_numeral_left} @
 | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 204 |      @{thms numeral_plus_numeral} @
 | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 205 |      @{thms add_neg_numeral_simps}) simps;
 | 
| 23164 | 206 | |
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 207 | (*To evaluate binary negations of coefficients*) | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54230diff
changeset | 208 | val minus_simps = [@{thm minus_zero}, @{thm minus_minus}];
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 209 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 210 | (*To let us treat subtraction as addition*) | 
| 54230 
b1d955791529
more simplification rules on unary and binary minus
 haftmann parents: 
51717diff
changeset | 211 | val diff_simps = [@{thm diff_conv_add_uminus}, @{thm minus_add_distrib}, @{thm minus_minus}];
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 212 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 213 | (*To let us treat division as multiplication*) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 214 | val divide_simps = [@{thm divide_inverse}, @{thm inverse_mult_distrib}, @{thm inverse_inverse_eq}];
 | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 215 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 216 | (*to extract again any uncancelled minuses*) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 217 | val minus_from_mult_simps = | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 218 |     [@{thm minus_minus}, @{thm mult_minus_left}, @{thm mult_minus_right}];
 | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 219 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 220 | (*combine unary minus with numeric literals, however nested within a product*) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 221 | val mult_minus_simps = | 
| 59757 
93d4169e07dc
fixed crash in cancel_numeral_simprocs. NB they still don't work except for type nat!
 paulson <lp15@cam.ac.uk> parents: 
59642diff
changeset | 222 |     [@{thm mult.assoc}, @{thm minus_mult_right}, @{thm minus_mult_commute}, @{thm numeral_times_minus_swap}];
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 223 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 224 | val norm_ss1 = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 225 |   simpset_of (put_simpset num_ss @{context}
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 226 | addsimps numeral_syms @ add_0s @ mult_1s @ | 
| 57514 
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
 haftmann parents: 
57512diff
changeset | 227 |     diff_simps @ minus_simps @ @{thms ac_simps})
 | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 228 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 229 | val norm_ss2 = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 230 |   simpset_of (put_simpset num_ss @{context}
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 231 | addsimps non_add_simps @ mult_minus_simps) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 232 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 233 | val norm_ss3 = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 234 |   simpset_of (put_simpset num_ss @{context}
 | 
| 57514 
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
 haftmann parents: 
57512diff
changeset | 235 |     addsimps minus_from_mult_simps @ @{thms ac_simps} @ @{thms ac_simps minus_mult_commute})
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 236 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 237 | structure CancelNumeralsCommon = | 
| 44945 | 238 | struct | 
| 239 | val mk_sum = mk_sum | |
| 240 | val dest_sum = dest_sum | |
| 241 | val mk_coeff = mk_coeff | |
| 242 | val dest_coeff = dest_coeff 1 | |
| 243 | val find_first_coeff = find_first_coeff [] | |
| 44947 
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
 haftmann parents: 
44945diff
changeset | 244 | val trans_tac = trans_tac | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 245 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 246 | fun norm_tac ctxt = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 247 | ALLGOALS (simp_tac (put_simpset norm_ss1 ctxt)) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 248 | THEN ALLGOALS (simp_tac (put_simpset norm_ss2 ctxt)) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 249 | THEN ALLGOALS (simp_tac (put_simpset norm_ss3 ctxt)) | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 250 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 251 | val numeral_simp_ss = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 252 |     simpset_of (put_simpset HOL_basic_ss @{context} addsimps add_0s @ simps)
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 253 | fun numeral_simp_tac ctxt = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 254 | ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt)) | 
| 45437 
958d19d3405b
tune post-processing of simproc-generated rules so they won't produce Numeral0 or Numeral1
 huffman parents: 
45306diff
changeset | 255 | val simplify_meta_eq = Arith_Data.simplify_meta_eq post_simps | 
| 44945 | 256 | val prove_conv = Arith_Data.prove_conv | 
| 257 | end; | |
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 258 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 259 | structure EqCancelNumerals = CancelNumeralsFun | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 260 | (open CancelNumeralsCommon | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 261 | val mk_bal = HOLogic.mk_eq | 
| 49323 | 262 |   val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} dummyT
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 263 |   val bal_add1 = @{thm eq_add_iff1} RS trans
 | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 264 |   val bal_add2 = @{thm eq_add_iff2} RS trans
 | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 265 | ); | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 266 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 267 | structure LessCancelNumerals = CancelNumeralsFun | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 268 | (open CancelNumeralsCommon | 
| 35092 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35084diff
changeset | 269 |   val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less}
 | 
| 49323 | 270 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} dummyT
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 271 |   val bal_add1 = @{thm less_add_iff1} RS trans
 | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 272 |   val bal_add2 = @{thm less_add_iff2} RS trans
 | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 273 | ); | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 274 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 275 | structure LeCancelNumerals = CancelNumeralsFun | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 276 | (open CancelNumeralsCommon | 
| 35092 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35084diff
changeset | 277 |   val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less_eq}
 | 
| 49323 | 278 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} dummyT
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 279 |   val bal_add1 = @{thm le_add_iff1} RS trans
 | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 280 |   val bal_add2 = @{thm le_add_iff2} RS trans
 | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 281 | ); | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 282 | |
| 61144 | 283 | val eq_cancel_numerals = EqCancelNumerals.proc | 
| 284 | val less_cancel_numerals = LessCancelNumerals.proc | |
| 285 | val le_cancel_numerals = LeCancelNumerals.proc | |
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 286 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 287 | structure CombineNumeralsData = | 
| 44945 | 288 | struct | 
| 289 | type coeff = int | |
| 290 | val iszero = (fn x => x = 0) | |
| 291 | val add = op + | |
| 292 | val mk_sum = long_mk_sum (*to work for e.g. 2*x + 3*x *) | |
| 293 | val dest_sum = dest_sum | |
| 294 | val mk_coeff = mk_coeff | |
| 295 | val dest_coeff = dest_coeff 1 | |
| 296 |   val left_distrib = @{thm combine_common_factor} RS trans
 | |
| 297 | val prove_conv = Arith_Data.prove_conv_nohyps | |
| 44947 
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
 haftmann parents: 
44945diff
changeset | 298 | val trans_tac = trans_tac | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 299 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 300 | fun norm_tac ctxt = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 301 | ALLGOALS (simp_tac (put_simpset norm_ss1 ctxt)) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 302 | THEN ALLGOALS (simp_tac (put_simpset norm_ss2 ctxt)) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 303 | THEN ALLGOALS (simp_tac (put_simpset norm_ss3 ctxt)) | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 304 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 305 | val numeral_simp_ss = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 306 |     simpset_of (put_simpset HOL_basic_ss @{context} addsimps add_0s @ simps)
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 307 | fun numeral_simp_tac ctxt = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 308 | ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt)) | 
| 45437 
958d19d3405b
tune post-processing of simproc-generated rules so they won't produce Numeral0 or Numeral1
 huffman parents: 
45306diff
changeset | 309 | val simplify_meta_eq = Arith_Data.simplify_meta_eq post_simps | 
| 44945 | 310 | end; | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 311 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 312 | structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData); | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 313 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 314 | (*Version for fields, where coefficients can be fractions*) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 315 | structure FieldCombineNumeralsData = | 
| 44945 | 316 | struct | 
| 317 | type coeff = int * int | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54230diff
changeset | 318 | val iszero = (fn (p, _) => p = 0) | 
| 44945 | 319 | val add = add_frac | 
| 320 | val mk_sum = long_mk_sum | |
| 321 | val dest_sum = dest_sum | |
| 322 | val mk_coeff = mk_fcoeff | |
| 323 | val dest_coeff = dest_fcoeff 1 | |
| 324 |   val left_distrib = @{thm combine_common_factor} RS trans
 | |
| 325 | val prove_conv = Arith_Data.prove_conv_nohyps | |
| 44947 
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
 haftmann parents: 
44945diff
changeset | 326 | val trans_tac = trans_tac | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 327 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 328 | val norm_ss1a = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 329 |     simpset_of (put_simpset norm_ss1 @{context} addsimps inverse_1s @ divide_simps)
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 330 | fun norm_tac ctxt = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 331 | ALLGOALS (simp_tac (put_simpset norm_ss1a ctxt)) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 332 | THEN ALLGOALS (simp_tac (put_simpset norm_ss2 ctxt)) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 333 | THEN ALLGOALS (simp_tac (put_simpset norm_ss3 ctxt)) | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 334 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 335 | val numeral_simp_ss = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 336 |     simpset_of (put_simpset HOL_basic_ss @{context}
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 337 |       addsimps add_0s @ simps @ [@{thm add_frac_eq}, @{thm not_False_eq_True}])
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 338 | fun numeral_simp_tac ctxt = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 339 | ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt)) | 
| 45437 
958d19d3405b
tune post-processing of simproc-generated rules so they won't produce Numeral0 or Numeral1
 huffman parents: 
45306diff
changeset | 340 | val simplify_meta_eq = Arith_Data.simplify_meta_eq field_post_simps | 
| 44945 | 341 | end; | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 342 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 343 | structure FieldCombineNumerals = CombineNumeralsFun(FieldCombineNumeralsData); | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 344 | |
| 61144 | 345 | val combine_numerals = CombineNumerals.proc | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 346 | |
| 61144 | 347 | val field_combine_numerals = FieldCombineNumerals.proc | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 348 | |
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 349 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 350 | (** Constant folding for multiplication in semirings **) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 351 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 352 | (*We do not need folding for addition: combine_numerals does the same thing*) | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 353 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 354 | structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA = | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 355 | struct | 
| 57514 
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
 haftmann parents: 
57512diff
changeset | 356 |   val assoc_ss = simpset_of (put_simpset HOL_basic_ss @{context} addsimps @{thms ac_simps minus_mult_commute})
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 357 | val eq_reflection = eq_reflection | 
| 35983 
27e2fa7d4ce7
slightly more general simproc (avoids errors of linarith)
 boehmes parents: 
35408diff
changeset | 358 | val is_numeral = can HOLogic.dest_number | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 359 | end; | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 360 | |
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 361 | structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data); | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
31067diff
changeset | 362 | |
| 59582 | 363 | fun assoc_fold ctxt ct = Semiring_Times_Assoc.proc ctxt (Thm.term_of ct) | 
| 23164 | 364 | |
| 365 | structure CancelNumeralFactorCommon = | |
| 44945 | 366 | struct | 
| 367 | val mk_coeff = mk_coeff | |
| 368 | val dest_coeff = dest_coeff 1 | |
| 44947 
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
 haftmann parents: 
44945diff
changeset | 369 | val trans_tac = trans_tac | 
| 23164 | 370 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 371 | val norm_ss1 = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 372 |     simpset_of (put_simpset HOL_basic_ss @{context} addsimps minus_from_mult_simps @ mult_1s)
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 373 | val norm_ss2 = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 374 |     simpset_of (put_simpset HOL_basic_ss @{context} addsimps simps @ mult_minus_simps)
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 375 | val norm_ss3 = | 
| 59757 
93d4169e07dc
fixed crash in cancel_numeral_simprocs. NB they still don't work except for type nat!
 paulson <lp15@cam.ac.uk> parents: 
59642diff
changeset | 376 |     simpset_of (put_simpset HOL_basic_ss @{context} addsimps @{thms ac_simps minus_mult_commute numeral_times_minus_swap})
 | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 377 | fun norm_tac ctxt = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 378 | ALLGOALS (simp_tac (put_simpset norm_ss1 ctxt)) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 379 | THEN ALLGOALS (simp_tac (put_simpset norm_ss2 ctxt)) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 380 | THEN ALLGOALS (simp_tac (put_simpset norm_ss3 ctxt)) | 
| 23164 | 381 | |
| 45668 
0ea1c705eebb
use HOL_basic_ss instead of HOL_ss for internal simproc proofs (cf. b36eedcd1633)
 huffman parents: 
45625diff
changeset | 382 | (* simp_thms are necessary because some of the cancellation rules below | 
| 
0ea1c705eebb
use HOL_basic_ss instead of HOL_ss for internal simproc proofs (cf. b36eedcd1633)
 huffman parents: 
45625diff
changeset | 383 | (e.g. mult_less_cancel_left) introduce various logical connectives *) | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 384 | val numeral_simp_ss = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 385 |     simpset_of (put_simpset HOL_basic_ss @{context} addsimps simps @ @{thms simp_thms})
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 386 | fun numeral_simp_tac ctxt = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 387 | ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt)) | 
| 30518 | 388 | val simplify_meta_eq = Arith_Data.simplify_meta_eq | 
| 45437 
958d19d3405b
tune post-processing of simproc-generated rules so they won't produce Numeral0 or Numeral1
 huffman parents: 
45306diff
changeset | 389 |     ([@{thm Nat.add_0}, @{thm Nat.add_0_right}] @ post_simps)
 | 
| 44945 | 390 | val prove_conv = Arith_Data.prove_conv | 
| 391 | end | |
| 23164 | 392 | |
| 30931 
86ca651da03e
generalized some simprocs from int to semiring_div
 haftmann parents: 
30685diff
changeset | 393 | (*Version for semiring_div*) | 
| 
86ca651da03e
generalized some simprocs from int to semiring_div
 haftmann parents: 
30685diff
changeset | 394 | structure DivCancelNumeralFactor = CancelNumeralFactorFun | 
| 23164 | 395 | (open CancelNumeralFactorCommon | 
| 60352 
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
 haftmann parents: 
59867diff
changeset | 396 |   val mk_bal   = HOLogic.mk_binop @{const_name Rings.divide}
 | 
| 
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
 haftmann parents: 
59867diff
changeset | 397 |   val dest_bal = HOLogic.dest_bin @{const_name Rings.divide} dummyT
 | 
| 30931 
86ca651da03e
generalized some simprocs from int to semiring_div
 haftmann parents: 
30685diff
changeset | 398 |   val cancel = @{thm div_mult_mult1} RS trans
 | 
| 23164 | 399 | val neg_exchanges = false | 
| 400 | ) | |
| 401 | ||
| 402 | (*Version for fields*) | |
| 403 | structure DivideCancelNumeralFactor = CancelNumeralFactorFun | |
| 404 | (open CancelNumeralFactorCommon | |
| 60352 
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
 haftmann parents: 
59867diff
changeset | 405 |   val mk_bal   = HOLogic.mk_binop @{const_name Rings.divide}
 | 
| 
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
 haftmann parents: 
59867diff
changeset | 406 |   val dest_bal = HOLogic.dest_bin @{const_name Rings.divide} dummyT
 | 
| 23413 
5caa2710dd5b
tuned laws for cancellation in divisions for fields.
 nipkow parents: 
23401diff
changeset | 407 |   val cancel = @{thm mult_divide_mult_cancel_left} RS trans
 | 
| 23164 | 408 | val neg_exchanges = false | 
| 409 | ) | |
| 410 | ||
| 411 | structure EqCancelNumeralFactor = CancelNumeralFactorFun | |
| 412 | (open CancelNumeralFactorCommon | |
| 413 | val mk_bal = HOLogic.mk_eq | |
| 49323 | 414 |   val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} dummyT
 | 
| 23164 | 415 |   val cancel = @{thm mult_cancel_left} RS trans
 | 
| 416 | val neg_exchanges = false | |
| 417 | ) | |
| 418 | ||
| 419 | structure LessCancelNumeralFactor = CancelNumeralFactorFun | |
| 420 | (open CancelNumeralFactorCommon | |
| 35092 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35084diff
changeset | 421 |   val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less}
 | 
| 49323 | 422 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} dummyT
 | 
| 23164 | 423 |   val cancel = @{thm mult_less_cancel_left} RS trans
 | 
| 424 | val neg_exchanges = true | |
| 425 | ) | |
| 426 | ||
| 427 | structure LeCancelNumeralFactor = CancelNumeralFactorFun | |
| 61144 | 428 | ( | 
| 429 | open CancelNumeralFactorCommon | |
| 430 |   val mk_bal = HOLogic.mk_binrel @{const_name Orderings.less_eq}
 | |
| 49323 | 431 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} dummyT
 | 
| 23164 | 432 |   val cancel = @{thm mult_le_cancel_left} RS trans
 | 
| 433 | val neg_exchanges = true | |
| 434 | ) | |
| 435 | ||
| 61144 | 436 | val eq_cancel_numeral_factor = EqCancelNumeralFactor.proc | 
| 437 | val less_cancel_numeral_factor = LessCancelNumeralFactor.proc | |
| 438 | val le_cancel_numeral_factor = LeCancelNumeralFactor.proc | |
| 439 | val div_cancel_numeral_factor = DivCancelNumeralFactor.proc | |
| 440 | val divide_cancel_numeral_factor = DivideCancelNumeralFactor.proc | |
| 23164 | 441 | |
| 57136 
653e56c6c963
must not cancel common factors on both sides of (in)equations in linear arithmetic decicision procedure
 nipkow parents: 
56282diff
changeset | 442 | val field_divide_cancel_numeral_factor = | 
| 61144 | 443 |   Simplifier.make_simproc @{context} "field_divide_cancel_numeral_factor"
 | 
| 444 |    {lhss =
 | |
| 445 |      [@{term "((l::'a::field) * m) / n"},
 | |
| 446 |       @{term "(l::'a::field) / (m * n)"},
 | |
| 447 |       @{term "((numeral v)::'a::field) / (numeral w)"},
 | |
| 448 |       @{term "((numeral v)::'a::field) / (- numeral w)"},
 | |
| 449 |       @{term "((- numeral v)::'a::field) / (numeral w)"},
 | |
| 450 |       @{term "((- numeral v)::'a::field) / (- numeral w)"}],
 | |
| 62913 | 451 | proc = K DivideCancelNumeralFactor.proc} | 
| 57136 
653e56c6c963
must not cancel common factors on both sides of (in)equations in linear arithmetic decicision procedure
 nipkow parents: 
56282diff
changeset | 452 | |
| 
653e56c6c963
must not cancel common factors on both sides of (in)equations in linear arithmetic decicision procedure
 nipkow parents: 
56282diff
changeset | 453 | val field_cancel_numeral_factors = | 
| 61144 | 454 |   [Simplifier.make_simproc @{context} "field_eq_cancel_numeral_factor"
 | 
| 455 |     {lhss =
 | |
| 456 |        [@{term "(l::'a::field) * m = n"},
 | |
| 457 |         @{term "(l::'a::field) = m * n"}],
 | |
| 62913 | 458 | proc = K EqCancelNumeralFactor.proc}, | 
| 61144 | 459 | field_divide_cancel_numeral_factor] | 
| 23164 | 460 | |
| 461 | ||
| 462 | (** Declarations for ExtractCommonTerm **) | |
| 463 | ||
| 464 | (*Find first term that matches u*) | |
| 465 | fun find_first_t past u []         = raise TERM ("find_first_t", [])
 | |
| 466 | | find_first_t past u (t::terms) = | |
| 467 | if u aconv t then (rev past @ terms) | |
| 468 | else find_first_t (t::past) u terms | |
| 469 | handle TERM _ => find_first_t (t::past) u terms; | |
| 470 | ||
| 471 | (** Final simplification for the CancelFactor simprocs **) | |
| 61144 | 472 | val simplify_one = Arith_Data.simplify_meta_eq | 
| 61694 
6571c78c9667
Removed some legacy theorems; minor adjustments to simplification rules; new material on homotopic paths
 paulson <lp15@cam.ac.uk> parents: 
61150diff
changeset | 473 |   [@{thm mult_1_left}, @{thm mult_1_right}, @{thm div_by_1}, @{thm numeral_One}];
 | 
| 23164 | 474 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 475 | fun cancel_simplify_meta_eq ctxt cancel_th th = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 476 | simplify_one ctxt (([th, cancel_th]) MRS trans); | 
| 23164 | 477 | |
| 30649 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 478 | local | 
| 59642 | 479 |   val Tp_Eq = Thm.reflexive (Thm.cterm_of @{theory_context HOL} HOLogic.Trueprop)
 | 
| 61144 | 480 | fun Eq_True_elim Eq = | 
| 30649 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 481 |     Thm.equal_elim (Thm.combination Tp_Eq (Thm.symmetric Eq)) @{thm TrueI}
 | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 482 | in | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 483 | fun sign_conv pos_th neg_th ctxt t = | 
| 30649 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 484 | let val T = fastype_of t; | 
| 35267 
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
 haftmann parents: 
35092diff
changeset | 485 |       val zero = Const(@{const_name Groups.zero}, T);
 | 
| 35092 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35084diff
changeset | 486 |       val less = Const(@{const_name Orderings.less}, [T,T] ---> HOLogic.boolT);
 | 
| 30649 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 487 | val pos = less $ zero $ t and neg = less $ t $ zero | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 488 | fun prove p = | 
| 59642 | 489 | SOME (Eq_True_elim (Simplifier.asm_rewrite ctxt (Thm.cterm_of ctxt p))) | 
| 30649 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 490 | handle THM _ => NONE | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 491 | in case prove pos of | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 492 | SOME th => SOME(th RS pos_th) | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 493 | | NONE => (case prove neg of | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 494 | SOME th => SOME(th RS neg_th) | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 495 | | NONE => NONE) | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 496 | end; | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 497 | end | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 498 | |
| 23164 | 499 | structure CancelFactorCommon = | 
| 44945 | 500 | struct | 
| 501 | val mk_sum = long_mk_prod | |
| 502 | val dest_sum = dest_prod | |
| 503 | val mk_coeff = mk_coeff | |
| 504 | val dest_coeff = dest_coeff | |
| 505 | val find_first = find_first_t [] | |
| 44947 
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
 haftmann parents: 
44945diff
changeset | 506 | val trans_tac = trans_tac | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 507 | val norm_ss = | 
| 57514 
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
 haftmann parents: 
57512diff
changeset | 508 |     simpset_of (put_simpset HOL_basic_ss @{context} addsimps mult_1s @ @{thms ac_simps minus_mult_commute})
 | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 509 | fun norm_tac ctxt = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 510 | ALLGOALS (simp_tac (put_simpset norm_ss ctxt)) | 
| 61144 | 511 | val simplify_meta_eq = cancel_simplify_meta_eq | 
| 45270 
d5b5c9259afd
fix bug in cancel_factor simprocs so they will work on goals like 'x * y < x * z' where the common term is already on the left
 huffman parents: 
44984diff
changeset | 512 | fun mk_eq (a, b) = HOLogic.mk_Trueprop (HOLogic.mk_eq (a, b)) | 
| 44945 | 513 | end; | 
| 23164 | 514 | |
| 515 | (*mult_cancel_left requires a ring with no zero divisors.*) | |
| 516 | structure EqCancelFactor = ExtractCommonTermFun | |
| 517 | (open CancelFactorCommon | |
| 518 | val mk_bal = HOLogic.mk_eq | |
| 49323 | 519 |   val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} dummyT
 | 
| 31368 | 520 |   fun simp_conv _ _ = SOME @{thm mult_cancel_left}
 | 
| 30649 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 521 | ); | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 522 | |
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 523 | (*for ordered rings*) | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 524 | structure LeCancelFactor = ExtractCommonTermFun | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 525 | (open CancelFactorCommon | 
| 35092 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35084diff
changeset | 526 |   val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less_eq}
 | 
| 49323 | 527 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} dummyT
 | 
| 30649 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 528 | val simp_conv = sign_conv | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 529 |     @{thm mult_le_cancel_left_pos} @{thm mult_le_cancel_left_neg}
 | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 530 | ); | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 531 | |
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 532 | (*for ordered rings*) | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 533 | structure LessCancelFactor = ExtractCommonTermFun | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 534 | (open CancelFactorCommon | 
| 35092 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35084diff
changeset | 535 |   val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less}
 | 
| 49323 | 536 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} dummyT
 | 
| 30649 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 537 | val simp_conv = sign_conv | 
| 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 538 |     @{thm mult_less_cancel_left_pos} @{thm mult_less_cancel_left_neg}
 | 
| 23164 | 539 | ); | 
| 540 | ||
| 30931 
86ca651da03e
generalized some simprocs from int to semiring_div
 haftmann parents: 
30685diff
changeset | 541 | (*for semirings with division*) | 
| 
86ca651da03e
generalized some simprocs from int to semiring_div
 haftmann parents: 
30685diff
changeset | 542 | structure DivCancelFactor = ExtractCommonTermFun | 
| 23164 | 543 | (open CancelFactorCommon | 
| 60352 
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
 haftmann parents: 
59867diff
changeset | 544 |   val mk_bal   = HOLogic.mk_binop @{const_name Rings.divide}
 | 
| 
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
 haftmann parents: 
59867diff
changeset | 545 |   val dest_bal = HOLogic.dest_bin @{const_name Rings.divide} dummyT
 | 
| 31368 | 546 |   fun simp_conv _ _ = SOME @{thm div_mult_mult1_if}
 | 
| 23164 | 547 | ); | 
| 548 | ||
| 30931 
86ca651da03e
generalized some simprocs from int to semiring_div
 haftmann parents: 
30685diff
changeset | 549 | structure ModCancelFactor = ExtractCommonTermFun | 
| 24395 | 550 | (open CancelFactorCommon | 
| 63950 
cdc1e59aa513
syntactic type class for operation mod named after mod;
 haftmann parents: 
62913diff
changeset | 551 |   val mk_bal   = HOLogic.mk_binop @{const_name modulo}
 | 
| 
cdc1e59aa513
syntactic type class for operation mod named after mod;
 haftmann parents: 
62913diff
changeset | 552 |   val dest_bal = HOLogic.dest_bin @{const_name modulo} dummyT
 | 
| 31368 | 553 |   fun simp_conv _ _ = SOME @{thm mod_mult_mult1}
 | 
| 24395 | 554 | ); | 
| 555 | ||
| 30931 
86ca651da03e
generalized some simprocs from int to semiring_div
 haftmann parents: 
30685diff
changeset | 556 | (*for idoms*) | 
| 
86ca651da03e
generalized some simprocs from int to semiring_div
 haftmann parents: 
30685diff
changeset | 557 | structure DvdCancelFactor = ExtractCommonTermFun | 
| 23969 | 558 | (open CancelFactorCommon | 
| 35050 
9f841f20dca6
renamed OrderedGroup to Groups; split theory Ring_and_Field into Rings Fields
 haftmann parents: 
35043diff
changeset | 559 |   val mk_bal   = HOLogic.mk_binrel @{const_name Rings.dvd}
 | 
| 49323 | 560 |   val dest_bal = HOLogic.dest_bin @{const_name Rings.dvd} dummyT
 | 
| 31368 | 561 |   fun simp_conv _ _ = SOME @{thm dvd_mult_cancel_left}
 | 
| 23969 | 562 | ); | 
| 563 | ||
| 23164 | 564 | (*Version for all fields, including unordered ones (type complex).*) | 
| 565 | structure DivideCancelFactor = ExtractCommonTermFun | |
| 566 | (open CancelFactorCommon | |
| 60352 
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
 haftmann parents: 
59867diff
changeset | 567 |   val mk_bal   = HOLogic.mk_binop @{const_name Rings.divide}
 | 
| 
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
 haftmann parents: 
59867diff
changeset | 568 |   val dest_bal = HOLogic.dest_bin @{const_name Rings.divide} dummyT
 | 
| 31368 | 569 |   fun simp_conv _ _ = SOME @{thm mult_divide_mult_cancel_left_if}
 | 
| 23164 | 570 | ); | 
| 571 | ||
| 59582 | 572 | fun eq_cancel_factor ctxt ct = EqCancelFactor.proc ctxt (Thm.term_of ct) | 
| 573 | fun le_cancel_factor ctxt ct = LeCancelFactor.proc ctxt (Thm.term_of ct) | |
| 574 | fun less_cancel_factor ctxt ct = LessCancelFactor.proc ctxt (Thm.term_of ct) | |
| 575 | fun div_cancel_factor ctxt ct = DivCancelFactor.proc ctxt (Thm.term_of ct) | |
| 576 | fun mod_cancel_factor ctxt ct = ModCancelFactor.proc ctxt (Thm.term_of ct) | |
| 577 | fun dvd_cancel_factor ctxt ct = DvdCancelFactor.proc ctxt (Thm.term_of ct) | |
| 578 | fun divide_cancel_factor ctxt ct = DivideCancelFactor.proc ctxt (Thm.term_of ct) | |
| 23164 | 579 | |
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 580 | local | 
| 61144 | 581 | |
| 61150 | 582 | val cterm_of = Thm.cterm_of @{context};
 | 
| 583 | fun tvar S = (("'a", 0), S);
 | |
| 584 | ||
| 585 | val zero_tvar = tvar @{sort zero};
 | |
| 586 | val zero = cterm_of (Const (@{const_name zero_class.zero}, TVar zero_tvar));
 | |
| 587 | ||
| 588 | val type_tvar = tvar @{sort type};
 | |
| 589 | val geq = cterm_of (Const (@{const_name HOL.eq}, TVar type_tvar --> TVar type_tvar --> @{typ bool}));
 | |
| 590 | ||
| 61144 | 591 | val add_frac_eq = mk_meta_eq @{thm "add_frac_eq"}
 | 
| 592 | val add_frac_num = mk_meta_eq @{thm "add_frac_num"}
 | |
| 593 | val add_num_frac = mk_meta_eq @{thm "add_num_frac"}
 | |
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 594 | |
| 61144 | 595 | fun prove_nz ctxt T t = | 
| 596 | let | |
| 61150 | 597 | val z = Thm.instantiate_cterm ([(zero_tvar, T)], []) zero | 
| 598 | val eq = Thm.instantiate_cterm ([(type_tvar, T)], []) geq | |
| 599 | val th = | |
| 600 |       Simplifier.rewrite (ctxt addsimps @{thms simp_thms})
 | |
| 601 |         (Thm.apply @{cterm "Trueprop"} (Thm.apply @{cterm "Not"}
 | |
| 602 | (Thm.apply (Thm.apply eq t) z))) | |
| 603 | in Thm.equal_elim (Thm.symmetric th) TrueI end | |
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 604 | |
| 61144 | 605 | fun proc ctxt ct = | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 606 | let | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 607 | val ((x,y),(w,z)) = | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 608 | (Thm.dest_binop #> (fn (a,b) => (Thm.dest_binop a, Thm.dest_binop b))) ct | 
| 59582 | 609 | val _ = map (HOLogic.dest_number o Thm.term_of) [x,y,z,w] | 
| 59586 | 610 | val T = Thm.ctyp_of_cterm x | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 611 | val [y_nz, z_nz] = map (prove_nz ctxt T) [y, z] | 
| 60801 | 612 | val th = Thm.instantiate' [SOME T] (map SOME [y,z,x,w]) add_frac_eq | 
| 61144 | 613 | in SOME (Thm.implies_elim (Thm.implies_elim th y_nz) z_nz) end | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 614 | handle CTERM _ => NONE | TERM _ => NONE | THM _ => NONE | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 615 | |
| 61144 | 616 | fun proc2 ctxt ct = | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 617 | let | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 618 | val (l,r) = Thm.dest_binop ct | 
| 59586 | 619 | val T = Thm.ctyp_of_cterm l | 
| 59582 | 620 | in (case (Thm.term_of l, Thm.term_of r) of | 
| 60352 
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
 haftmann parents: 
59867diff
changeset | 621 |       (Const(@{const_name Rings.divide},_)$_$_, _) =>
 | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 622 | let val (x,y) = Thm.dest_binop l val z = r | 
| 59582 | 623 | val _ = map (HOLogic.dest_number o Thm.term_of) [x,y,z] | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 624 | val ynz = prove_nz ctxt T y | 
| 60801 | 625 | in SOME (Thm.implies_elim (Thm.instantiate' [SOME T] (map SOME [y,x,z]) add_frac_num) ynz) | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 626 | end | 
| 60352 
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
 haftmann parents: 
59867diff
changeset | 627 |      | (_, Const (@{const_name Rings.divide},_)$_$_) =>
 | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 628 | let val (x,y) = Thm.dest_binop r val z = l | 
| 59582 | 629 | val _ = map (HOLogic.dest_number o Thm.term_of) [x,y,z] | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 630 | val ynz = prove_nz ctxt T y | 
| 60801 | 631 | in SOME (Thm.implies_elim (Thm.instantiate' [SOME T] (map SOME [y,z,x]) add_num_frac) ynz) | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 632 | end | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 633 | | _ => NONE) | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 634 | end | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 635 | handle CTERM _ => NONE | TERM _ => NONE | THM _ => NONE | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 636 | |
| 61144 | 637 | fun is_number (Const(@{const_name Rings.divide},_)$a$b) = is_number a andalso is_number b
 | 
| 638 | | is_number t = can HOLogic.dest_number t | |
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 639 | |
| 61144 | 640 | val is_number = is_number o Thm.term_of | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 641 | |
| 61144 | 642 | fun proc3 ctxt ct = | 
| 59582 | 643 | (case Thm.term_of ct of | 
| 60352 
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
 haftmann parents: 
59867diff
changeset | 644 |     Const(@{const_name Orderings.less},_)$(Const(@{const_name Rings.divide},_)$_$_)$_ =>
 | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 645 | let | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 646 | val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 647 | val _ = map is_number [a,b,c] | 
| 59586 | 648 | val T = Thm.ctyp_of_cterm c | 
| 60801 | 649 |         val th = Thm.instantiate' [SOME T] (map SOME [a,b,c]) @{thm "divide_less_eq"}
 | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 650 | in SOME (mk_meta_eq th) end | 
| 60352 
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
 haftmann parents: 
59867diff
changeset | 651 |   | Const(@{const_name Orderings.less_eq},_)$(Const(@{const_name Rings.divide},_)$_$_)$_ =>
 | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 652 | let | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 653 | val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 654 | val _ = map is_number [a,b,c] | 
| 59586 | 655 | val T = Thm.ctyp_of_cterm c | 
| 60801 | 656 |         val th = Thm.instantiate' [SOME T] (map SOME [a,b,c]) @{thm "divide_le_eq"}
 | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 657 | in SOME (mk_meta_eq th) end | 
| 60352 
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
 haftmann parents: 
59867diff
changeset | 658 |   | Const(@{const_name HOL.eq},_)$(Const(@{const_name Rings.divide},_)$_$_)$_ =>
 | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 659 | let | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 660 | val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 661 | val _ = map is_number [a,b,c] | 
| 59586 | 662 | val T = Thm.ctyp_of_cterm c | 
| 60801 | 663 |         val th = Thm.instantiate' [SOME T] (map SOME [a,b,c]) @{thm "divide_eq_eq"}
 | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 664 | in SOME (mk_meta_eq th) end | 
| 60352 
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
 haftmann parents: 
59867diff
changeset | 665 |   | Const(@{const_name Orderings.less},_)$_$(Const(@{const_name Rings.divide},_)$_$_) =>
 | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 666 | let | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 667 | val (a,(b,c)) = Thm.dest_binop ct ||> Thm.dest_binop | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 668 | val _ = map is_number [a,b,c] | 
| 59586 | 669 | val T = Thm.ctyp_of_cterm c | 
| 60801 | 670 |         val th = Thm.instantiate' [SOME T] (map SOME [a,b,c]) @{thm "less_divide_eq"}
 | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 671 | in SOME (mk_meta_eq th) end | 
| 60352 
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
 haftmann parents: 
59867diff
changeset | 672 |   | Const(@{const_name Orderings.less_eq},_)$_$(Const(@{const_name Rings.divide},_)$_$_) =>
 | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 673 | let | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 674 | val (a,(b,c)) = Thm.dest_binop ct ||> Thm.dest_binop | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 675 | val _ = map is_number [a,b,c] | 
| 59586 | 676 | val T = Thm.ctyp_of_cterm c | 
| 60801 | 677 |         val th = Thm.instantiate' [SOME T] (map SOME [a,b,c]) @{thm "le_divide_eq"}
 | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 678 | in SOME (mk_meta_eq th) end | 
| 60352 
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
 haftmann parents: 
59867diff
changeset | 679 |   | Const(@{const_name HOL.eq},_)$_$(Const(@{const_name Rings.divide},_)$_$_) =>
 | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 680 | let | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 681 | val (a,(b,c)) = Thm.dest_binop ct ||> Thm.dest_binop | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 682 | val _ = map is_number [a,b,c] | 
| 59586 | 683 | val T = Thm.ctyp_of_cterm c | 
| 60801 | 684 |         val th = Thm.instantiate' [SOME T] (map SOME [a,b,c]) @{thm "eq_divide_eq"}
 | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 685 | in SOME (mk_meta_eq th) end | 
| 61144 | 686 | | _ => NONE) handle TERM _ => NONE | CTERM _ => NONE | THM _ => NONE | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 687 | |
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 688 | val add_frac_frac_simproc = | 
| 61144 | 689 |   Simplifier.make_simproc @{context} "add_frac_frac_simproc"
 | 
| 690 |    {lhss = [@{term "(x::'a::field) / y + (w::'a::field) / z"}],
 | |
| 62913 | 691 | proc = K proc} | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 692 | |
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 693 | val add_frac_num_simproc = | 
| 61144 | 694 |   Simplifier.make_simproc @{context} "add_frac_num_simproc"
 | 
| 695 |    {lhss = [@{term "(x::'a::field) / y + z"}, @{term "z + (x::'a::field) / y"}],
 | |
| 62913 | 696 | proc = K proc2} | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 697 | |
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 698 | val ord_frac_simproc = | 
| 61144 | 699 |   Simplifier.make_simproc @{context} "ord_frac_simproc"
 | 
| 700 |    {lhss =
 | |
| 701 |      [@{term "(a::'a::{field,ord}) / b < c"},
 | |
| 702 |       @{term "(a::'a::{field,ord}) / b \<le> c"},
 | |
| 703 |       @{term "c < (a::'a::{field,ord}) / b"},
 | |
| 704 |       @{term "c \<le> (a::'a::{field,ord}) / b"},
 | |
| 705 |       @{term "c = (a::'a::{field,ord}) / b"},
 | |
| 706 |       @{term "(a::'a::{field, ord}) / b = c"}],
 | |
| 62913 | 707 | proc = K proc3} | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 708 | |
| 61144 | 709 | val ths = | 
| 710 |  [@{thm "mult_numeral_1"}, @{thm "mult_numeral_1_right"},
 | |
| 711 |   @{thm "divide_numeral_1"},
 | |
| 64240 | 712 |   @{thm "div_by_0"}, @{thm div_0},
 | 
| 61144 | 713 |   @{thm "divide_divide_eq_left"},
 | 
| 714 |   @{thm "times_divide_eq_left"}, @{thm "times_divide_eq_right"},
 | |
| 715 |   @{thm "times_divide_times_eq"},
 | |
| 716 |   @{thm "divide_divide_eq_right"},
 | |
| 717 |   @{thm diff_conv_add_uminus}, @{thm "minus_divide_left"},
 | |
| 718 |   @{thm "add_divide_distrib"} RS sym,
 | |
| 719 |   @{thm Fields.field_divide_inverse} RS sym, @{thm inverse_divide},
 | |
| 720 |   Conv.fconv_rule (Conv.arg_conv (Conv.arg1_conv (Conv.rewr_conv (mk_meta_eq @{thm mult.commute}))))
 | |
| 721 |   (@{thm Fields.field_divide_inverse} RS sym)]
 | |
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 722 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 723 | val field_comp_ss = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 724 | simpset_of | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 725 |     (put_simpset HOL_basic_ss @{context}
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 726 |       addsimps @{thms "semiring_norm"}
 | 
| 45620 
f2a587696afb
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
 wenzelm parents: 
45437diff
changeset | 727 |       addsimps ths addsimps @{thms simp_thms}
 | 
| 
f2a587696afb
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
 wenzelm parents: 
45437diff
changeset | 728 | addsimprocs field_cancel_numeral_factors | 
| 
f2a587696afb
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
 wenzelm parents: 
45437diff
changeset | 729 | addsimprocs [add_frac_frac_simproc, add_frac_num_simproc, ord_frac_simproc] | 
| 
f2a587696afb
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
 wenzelm parents: 
45437diff
changeset | 730 |       |> Simplifier.add_cong @{thm "if_weak_cong"})
 | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 731 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 732 | in | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 733 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 734 | fun field_comp_conv ctxt = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
49323diff
changeset | 735 | Simplifier.rewrite (put_simpset field_comp_ss ctxt) | 
| 45620 
f2a587696afb
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
 wenzelm parents: 
45437diff
changeset | 736 | then_conv | 
| 61694 
6571c78c9667
Removed some legacy theorems; minor adjustments to simplification rules; new material on homotopic paths
 paulson <lp15@cam.ac.uk> parents: 
61150diff
changeset | 737 |   Simplifier.rewrite (put_simpset HOL_basic_ss ctxt addsimps [@{thm numeral_One}])
 | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 738 | |
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 739 | end | 
| 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36409diff
changeset | 740 | |
| 23164 | 741 | end; |