20 signature CANCEL_NUMERAL_FACTOR_DATA = |
20 signature CANCEL_NUMERAL_FACTOR_DATA = |
21 sig |
21 sig |
22 (*abstract syntax*) |
22 (*abstract syntax*) |
23 val mk_bal: term * term -> term |
23 val mk_bal: term * term -> term |
24 val dest_bal: term -> term * term |
24 val dest_bal: term -> term * term |
25 val mk_coeff: int * term -> term |
25 val mk_coeff: IntInf.int * term -> term |
26 val dest_coeff: term -> int * term |
26 val dest_coeff: term -> IntInf.int * term |
27 (*rules*) |
27 (*rules*) |
28 val cancel: thm |
28 val cancel: thm |
29 val neg_exchanges: bool (*true if a negative coeff swaps the two operands, |
29 val neg_exchanges: bool (*true if a negative coeff swaps the two operands, |
30 as with < and <= but not = and div*) |
30 as with < and <= but not = and div*) |
31 (*proof tools*) |
31 (*proof tools*) |
43 val proc: Sign.sg -> simpset -> term -> thm option |
43 val proc: Sign.sg -> simpset -> term -> thm option |
44 end |
44 end |
45 = |
45 = |
46 struct |
46 struct |
47 |
47 |
48 |
|
49 (* greatest common divisor *) |
|
50 fun gcd (0, n) = abs n |
|
51 | gcd (m, n) = gcd (n mod m, m); |
|
52 |
|
53 (*the simplification procedure*) |
48 (*the simplification procedure*) |
54 fun proc sg ss t = |
49 fun proc sg ss t = |
55 let |
50 let |
56 val hyps = prems_of_ss ss; |
51 val hyps = prems_of_ss ss; |
57 (*first freeze any Vars in the term to prevent flex-flex problems*) |
52 (*first freeze any Vars in the term to prevent flex-flex problems*) |
59 val (t1,t2) = Data.dest_bal t' |
54 val (t1,t2) = Data.dest_bal t' |
60 val (m1, t1') = Data.dest_coeff t1 |
55 val (m1, t1') = Data.dest_coeff t1 |
61 and (m2, t2') = Data.dest_coeff t2 |
56 and (m2, t2') = Data.dest_coeff t2 |
62 val d = (*if both are negative, also divide through by ~1*) |
57 val d = (*if both are negative, also divide through by ~1*) |
63 if (m1<0 andalso m2<=0) orelse |
58 if (m1<0 andalso m2<=0) orelse |
64 (m1<=0 andalso m2<0) then ~ (gcd(m1,m2)) else gcd(m1,m2) |
59 (m1<=0 andalso m2<0) then ~ (abs(gcd(m1,m2))) else abs (gcd(m1,m2)) |
65 val _ = if d=1 then (*trivial, so do nothing*) |
60 val _ = if d=1 then (*trivial, so do nothing*) |
66 raise TERM("cancel_numeral_factor", []) |
61 raise TERM("cancel_numeral_factor", []) |
67 else () |
62 else () |
68 fun newshape (i,t) = Data.mk_coeff(d, Data.mk_coeff(i,t)) |
63 fun newshape (i,t) = Data.mk_coeff(d, Data.mk_coeff(i,t)) |
69 val n1 = m1 div d and n2 = m2 div d |
64 val n1 = m1 div d and n2 = m2 div d |