42 |
42 |
43 (*Utilities*) |
43 (*Utilities*) |
44 |
44 |
45 fun mk_numeral i = \<^const>\<open>integ_of\<close> $ mk_bin i; |
45 fun mk_numeral i = \<^const>\<open>integ_of\<close> $ mk_bin i; |
46 |
46 |
47 fun dest_numeral (Const(\<^const_name>\<open>integ_of\<close>, _) $ w) = dest_bin w |
47 fun dest_numeral \<^Const_>\<open>integ_of for w\<close> = dest_bin w |
48 | dest_numeral t = raise TERM ("dest_numeral", [t]); |
48 | dest_numeral t = raise TERM ("dest_numeral", [t]); |
49 |
49 |
50 fun find_first_numeral past (t::terms) = |
50 fun find_first_numeral past (t::terms) = |
51 ((dest_numeral t, rev past @ terms) |
51 ((dest_numeral t, rev past @ terms) |
52 handle TERM _ => find_first_numeral (t::past) terms) |
52 handle TERM _ => find_first_numeral (t::past) terms) |
63 (*this version ALWAYS includes a trailing zero*) |
63 (*this version ALWAYS includes a trailing zero*) |
64 fun long_mk_sum [] = zero |
64 fun long_mk_sum [] = zero |
65 | long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts); |
65 | long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts); |
66 |
66 |
67 (*decompose additions AND subtractions as a sum*) |
67 (*decompose additions AND subtractions as a sum*) |
68 fun dest_summing (pos, Const (\<^const_name>\<open>zadd\<close>, _) $ t $ u, ts) = |
68 fun dest_summing (pos, \<^Const_>\<open>zadd for t u\<close>, ts) = |
69 dest_summing (pos, t, dest_summing (pos, u, ts)) |
69 dest_summing (pos, t, dest_summing (pos, u, ts)) |
70 | dest_summing (pos, Const (\<^const_name>\<open>zdiff\<close>, _) $ t $ u, ts) = |
70 | dest_summing (pos, \<^Const_>\<open>zdiff for t u\<close>, ts) = |
71 dest_summing (pos, t, dest_summing (not pos, u, ts)) |
71 dest_summing (pos, t, dest_summing (not pos, u, ts)) |
72 | dest_summing (pos, t, ts) = |
72 | dest_summing (pos, t, ts) = |
73 if pos then t::ts else \<^const>\<open>zminus\<close> $ t :: ts; |
73 if pos then t::ts else \<^const>\<open>zminus\<close> $ t :: ts; |
74 |
74 |
75 fun dest_sum t = dest_summing (true, t, []); |
75 fun dest_sum t = dest_summing (true, t, []); |
91 |
91 |
92 (*DON'T do the obvious simplifications; that would create special cases*) |
92 (*DON'T do the obvious simplifications; that would create special cases*) |
93 fun mk_coeff (k, t) = mk_times (mk_numeral k, t); |
93 fun mk_coeff (k, t) = mk_times (mk_numeral k, t); |
94 |
94 |
95 (*Express t as a product of (possibly) a numeral with other sorted terms*) |
95 (*Express t as a product of (possibly) a numeral with other sorted terms*) |
96 fun dest_coeff sign (Const (\<^const_name>\<open>zminus\<close>, _) $ t) = dest_coeff (~sign) t |
96 fun dest_coeff sign \<^Const_>\<open>zminus for t\<close> = dest_coeff (~sign) t |
97 | dest_coeff sign t = |
97 | dest_coeff sign t = |
98 let val ts = sort Term_Ord.term_ord (dest_prod t) |
98 let val ts = sort Term_Ord.term_ord (dest_prod t) |
99 val (n, ts') = find_first_numeral [] ts |
99 val (n, ts') = find_first_numeral [] ts |
100 handle TERM _ => (1, ts) |
100 handle TERM _ => (1, ts) |
101 in (sign*n, mk_prod ts') end; |
101 in (sign*n, mk_prod ts') end; |