251 let val num = HOLogic.dest_binum numt and den = HOLogic.dest_binum dent |
251 let val num = HOLogic.dest_binum numt and den = HOLogic.dest_binum dent |
252 in if den = 0 then raise Zero else int_ratdiv(num,den) end; |
252 in if den = 0 then raise Zero else int_ratdiv(num,den) end; |
253 |
253 |
254 (* Warning: in rare cases number_of encloses a non-numeral, |
254 (* Warning: in rare cases number_of encloses a non-numeral, |
255 in which case dest_binum raises TERM; hence all the handles below. |
255 in which case dest_binum raises TERM; hence all the handles below. |
|
256 Same for Suc-terms that turn out not to be numerals - |
|
257 although the simplifier should eliminate those anyway... |
256 *) |
258 *) |
|
259 |
|
260 fun number_of_Sucs (Const("Suc",_) $ n) = number_of_Sucs n + 1 |
|
261 | number_of_Sucs t = if HOLogic.is_zero t then 0 |
|
262 else raise TERM("number_of_Sucs",[]) |
257 |
263 |
258 (* decompose nested multiplications, bracketing them to the right and combining all |
264 (* decompose nested multiplications, bracketing them to the right and combining all |
259 their coefficients |
265 their coefficients |
260 *) |
266 *) |
261 |
267 |
262 fun demult((mC as Const("op *",_)) $ s $ t,m) = ((case s of |
268 fun demult((mC as Const("op *",_)) $ s $ t,m) = ((case s of |
263 Const("Numeral.number_of",_)$n |
269 Const("Numeral.number_of",_)$n |
264 => demult(t,ratmul(m,rat_of_int(HOLogic.dest_binum n))) |
270 => demult(t,ratmul(m,rat_of_int(HOLogic.dest_binum n))) |
|
271 | Const("Suc",_) $ _ |
|
272 => demult(t,ratmul(m,rat_of_int(number_of_Sucs s))) |
265 | Const("op *",_) $ s1 $ s2 => demult(mC $ s1 $ (mC $ s2 $ t),m) |
273 | Const("op *",_) $ s1 $ s2 => demult(mC $ s1 $ (mC $ s2 $ t),m) |
266 | Const("HOL.divide",_) $ numt $ (Const("Numeral.number_of",_)$dent) => |
274 | Const("HOL.divide",_) $ numt $ (Const("Numeral.number_of",_)$dent) => |
267 let val den = HOLogic.dest_binum dent |
275 let val den = HOLogic.dest_binum dent |
268 in if den = 0 then raise Zero |
276 in if den = 0 then raise Zero |
269 else demult(mC $ numt $ t,ratmul(m, ratinv(rat_of_int den))) |
277 else demult(mC $ numt $ t,ratmul(m, ratinv(rat_of_int den))) |