src/HOL/Tools/numeral_simprocs.ML
changeset 51717 9e7d1c139569
parent 49323 6dff6b1f5417
child 54230 b1d955791529
equal deleted inserted replaced
51709:19b47bfac6ef 51717:9e7d1c139569
    14 and d = gcd(m,m') and n=m/d and n'=m'/d.
    14 and d = gcd(m,m') and n=m/d and n'=m'/d.
    15 *)
    15 *)
    16 
    16 
    17 signature NUMERAL_SIMPROCS =
    17 signature NUMERAL_SIMPROCS =
    18 sig
    18 sig
    19   val prep_simproc: theory -> string * string list * (theory -> simpset -> term -> thm option)
    19   val prep_simproc: theory -> string * string list * (Proof.context -> term -> thm option)
    20     -> simproc
    20     -> simproc
    21   val trans_tac: thm option -> tactic
    21   val trans_tac: thm option -> tactic
    22   val assoc_fold: simpset -> cterm -> thm option
    22   val assoc_fold: Proof.context -> cterm -> thm option
    23   val combine_numerals: simpset -> cterm -> thm option
    23   val combine_numerals: Proof.context -> cterm -> thm option
    24   val eq_cancel_numerals: simpset -> cterm -> thm option
    24   val eq_cancel_numerals: Proof.context -> cterm -> thm option
    25   val less_cancel_numerals: simpset -> cterm -> thm option
    25   val less_cancel_numerals: Proof.context -> cterm -> thm option
    26   val le_cancel_numerals: simpset -> cterm -> thm option
    26   val le_cancel_numerals: Proof.context -> cterm -> thm option
    27   val eq_cancel_factor: simpset -> cterm -> thm option
    27   val eq_cancel_factor: Proof.context -> cterm -> thm option
    28   val le_cancel_factor: simpset -> cterm -> thm option
    28   val le_cancel_factor: Proof.context -> cterm -> thm option
    29   val less_cancel_factor: simpset -> cterm -> thm option
    29   val less_cancel_factor: Proof.context -> cterm -> thm option
    30   val div_cancel_factor: simpset -> cterm -> thm option
    30   val div_cancel_factor: Proof.context -> cterm -> thm option
    31   val mod_cancel_factor: simpset -> cterm -> thm option
    31   val mod_cancel_factor: Proof.context -> cterm -> thm option
    32   val dvd_cancel_factor: simpset -> cterm -> thm option
    32   val dvd_cancel_factor: Proof.context -> cterm -> thm option
    33   val divide_cancel_factor: simpset -> cterm -> thm option
    33   val divide_cancel_factor: Proof.context -> cterm -> thm option
    34   val eq_cancel_numeral_factor: simpset -> cterm -> thm option
    34   val eq_cancel_numeral_factor: Proof.context -> cterm -> thm option
    35   val less_cancel_numeral_factor: simpset -> cterm -> thm option
    35   val less_cancel_numeral_factor: Proof.context -> cterm -> thm option
    36   val le_cancel_numeral_factor: simpset -> cterm -> thm option
    36   val le_cancel_numeral_factor: Proof.context -> cterm -> thm option
    37   val div_cancel_numeral_factor: simpset -> cterm -> thm option
    37   val div_cancel_numeral_factor: Proof.context -> cterm -> thm option
    38   val divide_cancel_numeral_factor: simpset -> cterm -> thm option
    38   val divide_cancel_numeral_factor: Proof.context -> cterm -> thm option
    39   val field_combine_numerals: simpset -> cterm -> thm option
    39   val field_combine_numerals: Proof.context -> cterm -> thm option
    40   val field_cancel_numeral_factors: simproc list
    40   val field_cancel_numeral_factors: simproc list
    41   val num_ss: simpset
    41   val num_ss: simpset
    42   val field_comp_conv: conv
    42   val field_comp_conv: Proof.context -> conv
    43 end;
    43 end;
    44 
    44 
    45 structure Numeral_Simprocs : NUMERAL_SIMPROCS =
    45 structure Numeral_Simprocs : NUMERAL_SIMPROCS =
    46 struct
    46 struct
    47 
    47 
   170 and numterms_ord (ts, us) = list_ord numterm_ord (ts, us)
   170 and numterms_ord (ts, us) = list_ord numterm_ord (ts, us)
   171 end;
   171 end;
   172 
   172 
   173 fun numtermless tu = (numterm_ord tu = LESS);
   173 fun numtermless tu = (numterm_ord tu = LESS);
   174 
   174 
   175 val num_ss = HOL_basic_ss |> Simplifier.set_termless numtermless;
   175 val num_ss =
       
   176   simpset_of (put_simpset HOL_basic_ss @{context}
       
   177     |> Simplifier.set_termless numtermless);
   176 
   178 
   177 (*Maps 1 to Numeral1 so that arithmetic isn't complicated by the abstract 1.*)
   179 (*Maps 1 to Numeral1 so that arithmetic isn't complicated by the abstract 1.*)
   178 val numeral_syms = [@{thm numeral_1_eq_1} RS sym];
   180 val numeral_syms = [@{thm numeral_1_eq_1} RS sym];
   179 
   181 
   180 (*Simplify 0+n, n+0, Numeral1*n, n*Numeral1, 1*x, x*1, x/1 *)
   182 (*Simplify 0+n, n+0, Numeral1*n, n*Numeral1, 1*x, x*1, x/1 *)
   232 
   234 
   233 (*combine unary minus with numeric literals, however nested within a product*)
   235 (*combine unary minus with numeric literals, however nested within a product*)
   234 val mult_minus_simps =
   236 val mult_minus_simps =
   235     [@{thm mult_assoc}, @{thm minus_mult_left}, minus_mult_eq_1_to_2];
   237     [@{thm mult_assoc}, @{thm minus_mult_left}, minus_mult_eq_1_to_2];
   236 
   238 
   237 val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @
   239 val norm_ss1 =
   238   diff_simps @ minus_simps @ @{thms add_ac}
   240   simpset_of (put_simpset num_ss @{context}
   239 val norm_ss2 = num_ss addsimps non_add_simps @ mult_minus_simps
   241     addsimps numeral_syms @ add_0s @ mult_1s @
   240 val norm_ss3 = num_ss addsimps minus_from_mult_simps @ @{thms add_ac} @ @{thms mult_ac}
   242     diff_simps @ minus_simps @ @{thms add_ac})
       
   243 
       
   244 val norm_ss2 =
       
   245   simpset_of (put_simpset num_ss @{context}
       
   246     addsimps non_add_simps @ mult_minus_simps)
       
   247 
       
   248 val norm_ss3 =
       
   249   simpset_of (put_simpset num_ss @{context}
       
   250     addsimps minus_from_mult_simps @ @{thms add_ac} @ @{thms mult_ac})
   241 
   251 
   242 structure CancelNumeralsCommon =
   252 structure CancelNumeralsCommon =
   243 struct
   253 struct
   244   val mk_sum = mk_sum
   254   val mk_sum = mk_sum
   245   val dest_sum = dest_sum
   255   val dest_sum = dest_sum
   246   val mk_coeff = mk_coeff
   256   val mk_coeff = mk_coeff
   247   val dest_coeff = dest_coeff 1
   257   val dest_coeff = dest_coeff 1
   248   val find_first_coeff = find_first_coeff []
   258   val find_first_coeff = find_first_coeff []
   249   val trans_tac = trans_tac
   259   val trans_tac = trans_tac
   250 
   260 
   251   fun norm_tac ss =
   261   fun norm_tac ctxt =
   252     ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
   262     ALLGOALS (simp_tac (put_simpset norm_ss1 ctxt))
   253     THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
   263     THEN ALLGOALS (simp_tac (put_simpset norm_ss2 ctxt))
   254     THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
   264     THEN ALLGOALS (simp_tac (put_simpset norm_ss3 ctxt))
   255 
   265 
   256   val numeral_simp_ss = HOL_basic_ss addsimps add_0s @ simps
   266   val numeral_simp_ss =
   257   fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
   267     simpset_of (put_simpset HOL_basic_ss @{context} addsimps add_0s @ simps)
       
   268   fun numeral_simp_tac ctxt =
       
   269     ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt))
   258   val simplify_meta_eq = Arith_Data.simplify_meta_eq post_simps
   270   val simplify_meta_eq = Arith_Data.simplify_meta_eq post_simps
   259   val prove_conv = Arith_Data.prove_conv
   271   val prove_conv = Arith_Data.prove_conv
   260 end;
   272 end;
   261 
   273 
   262 structure EqCancelNumerals = CancelNumeralsFun
   274 structure EqCancelNumerals = CancelNumeralsFun
   281   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} dummyT
   293   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} dummyT
   282   val bal_add1 = @{thm le_add_iff1} RS trans
   294   val bal_add1 = @{thm le_add_iff1} RS trans
   283   val bal_add2 = @{thm le_add_iff2} RS trans
   295   val bal_add2 = @{thm le_add_iff2} RS trans
   284 );
   296 );
   285 
   297 
   286 fun eq_cancel_numerals ss ct = EqCancelNumerals.proc ss (term_of ct)
   298 fun eq_cancel_numerals ctxt ct = EqCancelNumerals.proc ctxt (term_of ct)
   287 fun less_cancel_numerals ss ct = LessCancelNumerals.proc ss (term_of ct)
   299 fun less_cancel_numerals ctxt ct = LessCancelNumerals.proc ctxt (term_of ct)
   288 fun le_cancel_numerals ss ct = LeCancelNumerals.proc ss (term_of ct)
   300 fun le_cancel_numerals ctxt ct = LeCancelNumerals.proc ctxt (term_of ct)
   289 
   301 
   290 structure CombineNumeralsData =
   302 structure CombineNumeralsData =
   291 struct
   303 struct
   292   type coeff = int
   304   type coeff = int
   293   val iszero = (fn x => x = 0)
   305   val iszero = (fn x => x = 0)
   298   val dest_coeff = dest_coeff 1
   310   val dest_coeff = dest_coeff 1
   299   val left_distrib = @{thm combine_common_factor} RS trans
   311   val left_distrib = @{thm combine_common_factor} RS trans
   300   val prove_conv = Arith_Data.prove_conv_nohyps
   312   val prove_conv = Arith_Data.prove_conv_nohyps
   301   val trans_tac = trans_tac
   313   val trans_tac = trans_tac
   302 
   314 
   303   fun norm_tac ss =
   315   fun norm_tac ctxt =
   304     ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
   316     ALLGOALS (simp_tac (put_simpset norm_ss1 ctxt))
   305     THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
   317     THEN ALLGOALS (simp_tac (put_simpset norm_ss2 ctxt))
   306     THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
   318     THEN ALLGOALS (simp_tac (put_simpset norm_ss3 ctxt))
   307 
   319 
   308   val numeral_simp_ss = HOL_basic_ss addsimps add_0s @ simps
   320   val numeral_simp_ss =
   309   fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
   321     simpset_of (put_simpset HOL_basic_ss @{context} addsimps add_0s @ simps)
       
   322   fun numeral_simp_tac ctxt =
       
   323     ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt))
   310   val simplify_meta_eq = Arith_Data.simplify_meta_eq post_simps
   324   val simplify_meta_eq = Arith_Data.simplify_meta_eq post_simps
   311 end;
   325 end;
   312 
   326 
   313 structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
   327 structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
   314 
   328 
   324   val dest_coeff = dest_fcoeff 1
   338   val dest_coeff = dest_fcoeff 1
   325   val left_distrib = @{thm combine_common_factor} RS trans
   339   val left_distrib = @{thm combine_common_factor} RS trans
   326   val prove_conv = Arith_Data.prove_conv_nohyps
   340   val prove_conv = Arith_Data.prove_conv_nohyps
   327   val trans_tac = trans_tac
   341   val trans_tac = trans_tac
   328 
   342 
   329   val norm_ss1a = norm_ss1 addsimps inverse_1s @ divide_simps
   343   val norm_ss1a =
   330   fun norm_tac ss =
   344     simpset_of (put_simpset norm_ss1 @{context} addsimps inverse_1s @ divide_simps)
   331     ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1a))
   345   fun norm_tac ctxt =
   332     THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
   346     ALLGOALS (simp_tac (put_simpset norm_ss1a ctxt))
   333     THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
   347     THEN ALLGOALS (simp_tac (put_simpset norm_ss2 ctxt))
   334 
   348     THEN ALLGOALS (simp_tac (put_simpset norm_ss3 ctxt))
   335   val numeral_simp_ss = HOL_basic_ss addsimps add_0s @ simps @ [@{thm add_frac_eq}, @{thm not_False_eq_True}]
   349 
   336   fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
   350   val numeral_simp_ss =
       
   351     simpset_of (put_simpset HOL_basic_ss @{context}
       
   352       addsimps add_0s @ simps @ [@{thm add_frac_eq}, @{thm not_False_eq_True}])
       
   353   fun numeral_simp_tac ctxt =
       
   354     ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt))
   337   val simplify_meta_eq = Arith_Data.simplify_meta_eq field_post_simps
   355   val simplify_meta_eq = Arith_Data.simplify_meta_eq field_post_simps
   338 end;
   356 end;
   339 
   357 
   340 structure FieldCombineNumerals = CombineNumeralsFun(FieldCombineNumeralsData);
   358 structure FieldCombineNumerals = CombineNumeralsFun(FieldCombineNumeralsData);
   341 
   359 
   342 fun combine_numerals ss ct = CombineNumerals.proc ss (term_of ct)
   360 fun combine_numerals ctxt ct = CombineNumerals.proc ctxt (term_of ct)
   343 
   361 
   344 fun field_combine_numerals ss ct = FieldCombineNumerals.proc ss (term_of ct)
   362 fun field_combine_numerals ctxt ct = FieldCombineNumerals.proc ctxt (term_of ct)
       
   363 
   345 
   364 
   346 (** Constant folding for multiplication in semirings **)
   365 (** Constant folding for multiplication in semirings **)
   347 
   366 
   348 (*We do not need folding for addition: combine_numerals does the same thing*)
   367 (*We do not need folding for addition: combine_numerals does the same thing*)
   349 
   368 
   350 structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA =
   369 structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA =
   351 struct
   370 struct
   352   val assoc_ss = HOL_basic_ss addsimps @{thms mult_ac}
   371   val assoc_ss = simpset_of (put_simpset HOL_basic_ss @{context} addsimps @{thms mult_ac})
   353   val eq_reflection = eq_reflection
   372   val eq_reflection = eq_reflection
   354   val is_numeral = can HOLogic.dest_number
   373   val is_numeral = can HOLogic.dest_number
   355 end;
   374 end;
   356 
   375 
   357 structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);
   376 structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);
   358 
   377 
   359 fun assoc_fold ss ct = Semiring_Times_Assoc.proc ss (term_of ct)
   378 fun assoc_fold ctxt ct = Semiring_Times_Assoc.proc ctxt (term_of ct)
   360 
   379 
   361 structure CancelNumeralFactorCommon =
   380 structure CancelNumeralFactorCommon =
   362 struct
   381 struct
   363   val mk_coeff = mk_coeff
   382   val mk_coeff = mk_coeff
   364   val dest_coeff = dest_coeff 1
   383   val dest_coeff = dest_coeff 1
   365   val trans_tac = trans_tac
   384   val trans_tac = trans_tac
   366 
   385 
   367   val norm_ss1 = HOL_basic_ss addsimps minus_from_mult_simps @ mult_1s
   386   val norm_ss1 =
   368   val norm_ss2 = HOL_basic_ss addsimps simps @ mult_minus_simps
   387     simpset_of (put_simpset HOL_basic_ss @{context} addsimps minus_from_mult_simps @ mult_1s)
   369   val norm_ss3 = HOL_basic_ss addsimps @{thms mult_ac}
   388   val norm_ss2 =
   370   fun norm_tac ss =
   389     simpset_of (put_simpset HOL_basic_ss @{context} addsimps simps @ mult_minus_simps)
   371     ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
   390   val norm_ss3 =
   372     THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
   391     simpset_of (put_simpset HOL_basic_ss @{context} addsimps @{thms mult_ac})
   373     THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
   392   fun norm_tac ctxt =
       
   393     ALLGOALS (simp_tac (put_simpset norm_ss1 ctxt))
       
   394     THEN ALLGOALS (simp_tac (put_simpset norm_ss2 ctxt))
       
   395     THEN ALLGOALS (simp_tac (put_simpset norm_ss3 ctxt))
   374 
   396 
   375   (* simp_thms are necessary because some of the cancellation rules below
   397   (* simp_thms are necessary because some of the cancellation rules below
   376   (e.g. mult_less_cancel_left) introduce various logical connectives *)
   398   (e.g. mult_less_cancel_left) introduce various logical connectives *)
   377   val numeral_simp_ss = HOL_basic_ss addsimps simps @ @{thms simp_thms}
   399   val numeral_simp_ss =
   378   fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
   400     simpset_of (put_simpset HOL_basic_ss @{context} addsimps simps @ @{thms simp_thms})
       
   401   fun numeral_simp_tac ctxt =
       
   402     ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt))
   379   val simplify_meta_eq = Arith_Data.simplify_meta_eq
   403   val simplify_meta_eq = Arith_Data.simplify_meta_eq
   380     ([@{thm Nat.add_0}, @{thm Nat.add_0_right}] @ post_simps)
   404     ([@{thm Nat.add_0}, @{thm Nat.add_0_right}] @ post_simps)
   381   val prove_conv = Arith_Data.prove_conv
   405   val prove_conv = Arith_Data.prove_conv
   382 end
   406 end
   383 
   407 
   421   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} dummyT
   445   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} dummyT
   422   val cancel = @{thm mult_le_cancel_left} RS trans
   446   val cancel = @{thm mult_le_cancel_left} RS trans
   423   val neg_exchanges = true
   447   val neg_exchanges = true
   424 )
   448 )
   425 
   449 
   426 fun eq_cancel_numeral_factor ss ct = EqCancelNumeralFactor.proc ss (term_of ct)
   450 fun eq_cancel_numeral_factor ctxt ct = EqCancelNumeralFactor.proc ctxt (term_of ct)
   427 fun less_cancel_numeral_factor ss ct = LessCancelNumeralFactor.proc ss (term_of ct)
   451 fun less_cancel_numeral_factor ctxt ct = LessCancelNumeralFactor.proc ctxt (term_of ct)
   428 fun le_cancel_numeral_factor ss ct = LeCancelNumeralFactor.proc ss (term_of ct)
   452 fun le_cancel_numeral_factor ctxt ct = LeCancelNumeralFactor.proc ctxt (term_of ct)
   429 fun div_cancel_numeral_factor ss ct = DivCancelNumeralFactor.proc ss (term_of ct)
   453 fun div_cancel_numeral_factor ctxt ct = DivCancelNumeralFactor.proc ctxt (term_of ct)
   430 fun divide_cancel_numeral_factor ss ct = DivideCancelNumeralFactor.proc ss (term_of ct)
   454 fun divide_cancel_numeral_factor ctxt ct = DivideCancelNumeralFactor.proc ctxt (term_of ct)
   431 
   455 
   432 val field_cancel_numeral_factors =
   456 val field_cancel_numeral_factors =
   433   map (prep_simproc @{theory})
   457   map (prep_simproc @{theory})
   434    [("field_eq_cancel_numeral_factor",
   458    [("field_eq_cancel_numeral_factor",
   435      ["(l::'a::field) * m = n",
   459      ["(l::'a::field) * m = n",
   436       "(l::'a::field) = m * n"],
   460       "(l::'a::field) = m * n"],
   437      K EqCancelNumeralFactor.proc),
   461      EqCancelNumeralFactor.proc),
   438     ("field_cancel_numeral_factor",
   462     ("field_cancel_numeral_factor",
   439      ["((l::'a::field_inverse_zero) * m) / n",
   463      ["((l::'a::field_inverse_zero) * m) / n",
   440       "(l::'a::field_inverse_zero) / (m * n)",
   464       "(l::'a::field_inverse_zero) / (m * n)",
   441       "((numeral v)::'a::field_inverse_zero) / (numeral w)",
   465       "((numeral v)::'a::field_inverse_zero) / (numeral w)",
   442       "((numeral v)::'a::field_inverse_zero) / (neg_numeral w)",
   466       "((numeral v)::'a::field_inverse_zero) / (neg_numeral w)",
   443       "((neg_numeral v)::'a::field_inverse_zero) / (numeral w)",
   467       "((neg_numeral v)::'a::field_inverse_zero) / (numeral w)",
   444       "((neg_numeral v)::'a::field_inverse_zero) / (neg_numeral w)"],
   468       "((neg_numeral v)::'a::field_inverse_zero) / (neg_numeral w)"],
   445      K DivideCancelNumeralFactor.proc)]
   469      DivideCancelNumeralFactor.proc)]
   446 
   470 
   447 
   471 
   448 (** Declarations for ExtractCommonTerm **)
   472 (** Declarations for ExtractCommonTerm **)
   449 
   473 
   450 (*Find first term that matches u*)
   474 (*Find first term that matches u*)
   456 
   480 
   457 (** Final simplification for the CancelFactor simprocs **)
   481 (** Final simplification for the CancelFactor simprocs **)
   458 val simplify_one = Arith_Data.simplify_meta_eq  
   482 val simplify_one = Arith_Data.simplify_meta_eq  
   459   [@{thm mult_1_left}, @{thm mult_1_right}, @{thm div_by_1}, @{thm numeral_1_eq_1}];
   483   [@{thm mult_1_left}, @{thm mult_1_right}, @{thm div_by_1}, @{thm numeral_1_eq_1}];
   460 
   484 
   461 fun cancel_simplify_meta_eq ss cancel_th th =
   485 fun cancel_simplify_meta_eq ctxt cancel_th th =
   462     simplify_one ss (([th, cancel_th]) MRS trans);
   486     simplify_one ctxt (([th, cancel_th]) MRS trans);
   463 
   487 
   464 local
   488 local
   465   val Tp_Eq = Thm.reflexive (Thm.cterm_of @{theory HOL} HOLogic.Trueprop)
   489   val Tp_Eq = Thm.reflexive (Thm.cterm_of @{theory HOL} HOLogic.Trueprop)
   466   fun Eq_True_elim Eq = 
   490   fun Eq_True_elim Eq = 
   467     Thm.equal_elim (Thm.combination Tp_Eq (Thm.symmetric Eq)) @{thm TrueI}
   491     Thm.equal_elim (Thm.combination Tp_Eq (Thm.symmetric Eq)) @{thm TrueI}
   468 in
   492 in
   469 fun sign_conv pos_th neg_th ss t =
   493 fun sign_conv pos_th neg_th ctxt t =
   470   let val T = fastype_of t;
   494   let val T = fastype_of t;
   471       val zero = Const(@{const_name Groups.zero}, T);
   495       val zero = Const(@{const_name Groups.zero}, T);
   472       val less = Const(@{const_name Orderings.less}, [T,T] ---> HOLogic.boolT);
   496       val less = Const(@{const_name Orderings.less}, [T,T] ---> HOLogic.boolT);
   473       val pos = less $ zero $ t and neg = less $ t $ zero
   497       val pos = less $ zero $ t and neg = less $ t $ zero
   474       val thy = Proof_Context.theory_of (Simplifier.the_context ss)
   498       val thy = Proof_Context.theory_of ctxt
   475       fun prove p =
   499       fun prove p =
   476         SOME (Eq_True_elim (Simplifier.asm_rewrite ss (Thm.cterm_of thy p)))
   500         SOME (Eq_True_elim (Simplifier.asm_rewrite ctxt (Thm.cterm_of thy p)))
   477         handle THM _ => NONE
   501         handle THM _ => NONE
   478     in case prove pos of
   502     in case prove pos of
   479          SOME th => SOME(th RS pos_th)
   503          SOME th => SOME(th RS pos_th)
   480        | NONE => (case prove neg of
   504        | NONE => (case prove neg of
   481                     SOME th => SOME(th RS neg_th)
   505                     SOME th => SOME(th RS neg_th)
   489   val dest_sum = dest_prod
   513   val dest_sum = dest_prod
   490   val mk_coeff = mk_coeff
   514   val mk_coeff = mk_coeff
   491   val dest_coeff = dest_coeff
   515   val dest_coeff = dest_coeff
   492   val find_first = find_first_t []
   516   val find_first = find_first_t []
   493   val trans_tac = trans_tac
   517   val trans_tac = trans_tac
   494   val norm_ss = HOL_basic_ss addsimps mult_1s @ @{thms mult_ac}
   518   val norm_ss =
   495   fun norm_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss))
   519     simpset_of (put_simpset HOL_basic_ss @{context} addsimps mult_1s @ @{thms mult_ac})
       
   520   fun norm_tac ctxt =
       
   521     ALLGOALS (simp_tac (put_simpset norm_ss ctxt))
   496   val simplify_meta_eq  = cancel_simplify_meta_eq 
   522   val simplify_meta_eq  = cancel_simplify_meta_eq 
   497   fun mk_eq (a, b) = HOLogic.mk_Trueprop (HOLogic.mk_eq (a, b))
   523   fun mk_eq (a, b) = HOLogic.mk_Trueprop (HOLogic.mk_eq (a, b))
   498 end;
   524 end;
   499 
   525 
   500 (*mult_cancel_left requires a ring with no zero divisors.*)
   526 (*mult_cancel_left requires a ring with no zero divisors.*)
   552   val mk_bal   = HOLogic.mk_binop @{const_name Fields.divide}
   578   val mk_bal   = HOLogic.mk_binop @{const_name Fields.divide}
   553   val dest_bal = HOLogic.dest_bin @{const_name Fields.divide} dummyT
   579   val dest_bal = HOLogic.dest_bin @{const_name Fields.divide} dummyT
   554   fun simp_conv _ _ = SOME @{thm mult_divide_mult_cancel_left_if}
   580   fun simp_conv _ _ = SOME @{thm mult_divide_mult_cancel_left_if}
   555 );
   581 );
   556 
   582 
   557 fun eq_cancel_factor ss ct = EqCancelFactor.proc ss (term_of ct)
   583 fun eq_cancel_factor ctxt ct = EqCancelFactor.proc ctxt (term_of ct)
   558 fun le_cancel_factor ss ct = LeCancelFactor.proc ss (term_of ct)
   584 fun le_cancel_factor ctxt ct = LeCancelFactor.proc ctxt (term_of ct)
   559 fun less_cancel_factor ss ct = LessCancelFactor.proc ss (term_of ct)
   585 fun less_cancel_factor ctxt ct = LessCancelFactor.proc ctxt (term_of ct)
   560 fun div_cancel_factor ss ct = DivCancelFactor.proc ss (term_of ct)
   586 fun div_cancel_factor ctxt ct = DivCancelFactor.proc ctxt (term_of ct)
   561 fun mod_cancel_factor ss ct = ModCancelFactor.proc ss (term_of ct)
   587 fun mod_cancel_factor ctxt ct = ModCancelFactor.proc ctxt (term_of ct)
   562 fun dvd_cancel_factor ss ct = DvdCancelFactor.proc ss (term_of ct)
   588 fun dvd_cancel_factor ctxt ct = DvdCancelFactor.proc ctxt (term_of ct)
   563 fun divide_cancel_factor ss ct = DivideCancelFactor.proc ss (term_of ct)
   589 fun divide_cancel_factor ctxt ct = DivideCancelFactor.proc ctxt (term_of ct)
   564 
   590 
   565 local
   591 local
   566  val zr = @{cpat "0"}
   592  val zr = @{cpat "0"}
   567  val zT = ctyp_of_term zr
   593  val zT = ctyp_of_term zr
   568  val geq = @{cpat HOL.eq}
   594  val geq = @{cpat HOL.eq}
   569  val eqT = Thm.dest_ctyp (ctyp_of_term geq) |> hd
   595  val eqT = Thm.dest_ctyp (ctyp_of_term geq) |> hd
   570  val add_frac_eq = mk_meta_eq @{thm "add_frac_eq"}
   596  val add_frac_eq = mk_meta_eq @{thm "add_frac_eq"}
   571  val add_frac_num = mk_meta_eq @{thm "add_frac_num"}
   597  val add_frac_num = mk_meta_eq @{thm "add_frac_num"}
   572  val add_num_frac = mk_meta_eq @{thm "add_num_frac"}
   598  val add_num_frac = mk_meta_eq @{thm "add_num_frac"}
   573 
   599 
   574  fun prove_nz ss T t =
   600  fun prove_nz ctxt T t =
   575     let
   601     let
   576       val z = Thm.instantiate_cterm ([(zT,T)],[]) zr
   602       val z = Thm.instantiate_cterm ([(zT,T)],[]) zr
   577       val eq = Thm.instantiate_cterm ([(eqT,T)],[]) geq
   603       val eq = Thm.instantiate_cterm ([(eqT,T)],[]) geq
   578       val th = Simplifier.rewrite (ss addsimps @{thms simp_thms})
   604       val th = Simplifier.rewrite (ctxt addsimps @{thms simp_thms})
   579            (Thm.apply @{cterm "Trueprop"} (Thm.apply @{cterm "Not"}
   605            (Thm.apply @{cterm "Trueprop"} (Thm.apply @{cterm "Not"}
   580                   (Thm.apply (Thm.apply eq t) z)))
   606                   (Thm.apply (Thm.apply eq t) z)))
   581     in Thm.equal_elim (Thm.symmetric th) TrueI
   607     in Thm.equal_elim (Thm.symmetric th) TrueI
   582     end
   608     end
   583 
   609 
   584  fun proc phi ss ct =
   610  fun proc phi ctxt ct =
   585   let
   611   let
   586     val ((x,y),(w,z)) =
   612     val ((x,y),(w,z)) =
   587          (Thm.dest_binop #> (fn (a,b) => (Thm.dest_binop a, Thm.dest_binop b))) ct
   613          (Thm.dest_binop #> (fn (a,b) => (Thm.dest_binop a, Thm.dest_binop b))) ct
   588     val _ = map (HOLogic.dest_number o term_of) [x,y,z,w]
   614     val _ = map (HOLogic.dest_number o term_of) [x,y,z,w]
   589     val T = ctyp_of_term x
   615     val T = ctyp_of_term x
   590     val [y_nz, z_nz] = map (prove_nz ss T) [y, z]
   616     val [y_nz, z_nz] = map (prove_nz ctxt T) [y, z]
   591     val th = instantiate' [SOME T] (map SOME [y,z,x,w]) add_frac_eq
   617     val th = instantiate' [SOME T] (map SOME [y,z,x,w]) add_frac_eq
   592   in SOME (Thm.implies_elim (Thm.implies_elim th y_nz) z_nz)
   618   in SOME (Thm.implies_elim (Thm.implies_elim th y_nz) z_nz)
   593   end
   619   end
   594   handle CTERM _ => NONE | TERM _ => NONE | THM _ => NONE
   620   handle CTERM _ => NONE | TERM _ => NONE | THM _ => NONE
   595 
   621 
   596  fun proc2 phi ss ct =
   622  fun proc2 phi ctxt ct =
   597   let
   623   let
   598     val (l,r) = Thm.dest_binop ct
   624     val (l,r) = Thm.dest_binop ct
   599     val T = ctyp_of_term l
   625     val T = ctyp_of_term l
   600   in (case (term_of l, term_of r) of
   626   in (case (term_of l, term_of r) of
   601       (Const(@{const_name Fields.divide},_)$_$_, _) =>
   627       (Const(@{const_name Fields.divide},_)$_$_, _) =>
   602         let val (x,y) = Thm.dest_binop l val z = r
   628         let val (x,y) = Thm.dest_binop l val z = r
   603             val _ = map (HOLogic.dest_number o term_of) [x,y,z]
   629             val _ = map (HOLogic.dest_number o term_of) [x,y,z]
   604             val ynz = prove_nz ss T y
   630             val ynz = prove_nz ctxt T y
   605         in SOME (Thm.implies_elim (instantiate' [SOME T] (map SOME [y,x,z]) add_frac_num) ynz)
   631         in SOME (Thm.implies_elim (instantiate' [SOME T] (map SOME [y,x,z]) add_frac_num) ynz)
   606         end
   632         end
   607      | (_, Const (@{const_name Fields.divide},_)$_$_) =>
   633      | (_, Const (@{const_name Fields.divide},_)$_$_) =>
   608         let val (x,y) = Thm.dest_binop r val z = l
   634         let val (x,y) = Thm.dest_binop r val z = l
   609             val _ = map (HOLogic.dest_number o term_of) [x,y,z]
   635             val _ = map (HOLogic.dest_number o term_of) [x,y,z]
   610             val ynz = prove_nz ss T y
   636             val ynz = prove_nz ctxt T y
   611         in SOME (Thm.implies_elim (instantiate' [SOME T] (map SOME [y,z,x]) add_num_frac) ynz)
   637         in SOME (Thm.implies_elim (instantiate' [SOME T] (map SOME [y,z,x]) add_num_frac) ynz)
   612         end
   638         end
   613      | _ => NONE)
   639      | _ => NONE)
   614   end
   640   end
   615   handle CTERM _ => NONE | TERM _ => NONE | THM _ => NONE
   641   handle CTERM _ => NONE | TERM _ => NONE | THM _ => NONE
   617  fun is_number (Const(@{const_name Fields.divide},_)$a$b) = is_number a andalso is_number b
   643  fun is_number (Const(@{const_name Fields.divide},_)$a$b) = is_number a andalso is_number b
   618    | is_number t = can HOLogic.dest_number t
   644    | is_number t = can HOLogic.dest_number t
   619 
   645 
   620  val is_number = is_number o term_of
   646  val is_number = is_number o term_of
   621 
   647 
   622  fun proc3 phi ss ct =
   648  fun proc3 phi ctxt ct =
   623   (case term_of ct of
   649   (case term_of ct of
   624     Const(@{const_name Orderings.less},_)$(Const(@{const_name Fields.divide},_)$_$_)$_ =>
   650     Const(@{const_name Orderings.less},_)$(Const(@{const_name Fields.divide},_)$_$_)$_ =>
   625       let
   651       let
   626         val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop
   652         val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop
   627         val _ = map is_number [a,b,c]
   653         val _ = map is_number [a,b,c]
   697            @{thm "add_divide_distrib"} RS sym,
   723            @{thm "add_divide_distrib"} RS sym,
   698            @{thm field_divide_inverse} RS sym, @{thm inverse_divide}, 
   724            @{thm field_divide_inverse} RS sym, @{thm inverse_divide}, 
   699            Conv.fconv_rule (Conv.arg_conv (Conv.arg1_conv (Conv.rewr_conv (mk_meta_eq @{thm mult_commute}))))   
   725            Conv.fconv_rule (Conv.arg_conv (Conv.arg1_conv (Conv.rewr_conv (mk_meta_eq @{thm mult_commute}))))   
   700            (@{thm field_divide_inverse} RS sym)]
   726            (@{thm field_divide_inverse} RS sym)]
   701 
   727 
   702 in
   728 val field_comp_ss =
   703 
   729   simpset_of
   704 val field_comp_conv =
   730     (put_simpset HOL_basic_ss @{context}
   705   Simplifier.rewrite
   731       addsimps @{thms "semiring_norm"}
   706     (HOL_basic_ss addsimps @{thms "semiring_norm"}
       
   707       addsimps ths addsimps @{thms simp_thms}
   732       addsimps ths addsimps @{thms simp_thms}
   708       addsimprocs field_cancel_numeral_factors
   733       addsimprocs field_cancel_numeral_factors
   709       addsimprocs [add_frac_frac_simproc, add_frac_num_simproc, ord_frac_simproc]
   734       addsimprocs [add_frac_frac_simproc, add_frac_num_simproc, ord_frac_simproc]
   710       |> Simplifier.add_cong @{thm "if_weak_cong"})
   735       |> Simplifier.add_cong @{thm "if_weak_cong"})
       
   736 
       
   737 in
       
   738 
       
   739 fun field_comp_conv ctxt =
       
   740   Simplifier.rewrite (put_simpset field_comp_ss ctxt)
   711   then_conv
   741   then_conv
   712   Simplifier.rewrite (HOL_basic_ss addsimps [@{thm numeral_1_eq_1}])
   742   Simplifier.rewrite (put_simpset HOL_basic_ss ctxt addsimps [@{thm numeral_1_eq_1}])
   713 
   743 
   714 end
   744 end
   715 
   745 
   716 end;
   746 end;
   717 
   747