src/HOL/Integ/NatBin.thy
changeset 19601 299d4cd2ef51
parent 19380 b808efaa5828
child 19656 09be06943252
equal deleted inserted replaced
19600:2d969d9a233b 19601:299d4cd2ef51
   778 by auto
   778 by auto
   779 
   779 
   780 lemma nat_mult_div_cancel_disj:
   780 lemma nat_mult_div_cancel_disj:
   781      "(k*m) div (k*n) = (if k = (0::nat) then 0 else m div n)"
   781      "(k*m) div (k*n) = (if k = (0::nat) then 0 else m div n)"
   782 by (simp add: nat_mult_div_cancel1)
   782 by (simp add: nat_mult_div_cancel1)
       
   783 
       
   784 subsection {* code generator setup *}
       
   785 
       
   786 lemma number_of_eval [code fun]:
       
   787   "number_of Numeral.Pls = (0::int)"
       
   788   and "number_of Numeral.Min = uminus (1::int)"
       
   789   and "number_of (n BIT bit.B0) = (2::int) * number_of n"
       
   790   and "number_of (n BIT bit.B1) = (2::int) * number_of n + 1"
       
   791   by simp_all
       
   792 
       
   793 lemma elim_nat [code unfolt]:
       
   794   "(number_of n :: nat) = nat (number_of n)"
       
   795   by simp
       
   796 
       
   797 lemma elim_zero [code unfolt]:
       
   798   "(0::int) = number_of (Numeral.Pls)" 
       
   799   by simp
       
   800 
       
   801 lemma elim_one [code unfolt]:
       
   802   "(1::int) = number_of (Numeral.Pls BIT bit.B1)" 
       
   803   by simp
       
   804 
       
   805 lemmas [code unfolt] =
       
   806   bin_minus_Pls bin_minus_Min bin_minus_1 bin_minus_0
       
   807   bin_pred_Pls  bin_pred_Min  bin_pred_1  bin_pred_0
       
   808 
       
   809 lemma elim_negate:
       
   810   "(number_of n :: int) == - number_of (bin_minus n)"
       
   811   unfolding number_of_minus IntDef.zminus_zminus by (rule reflexive)
       
   812 
       
   813 ML {*
       
   814 local
       
   815   val elim_negate_thm = thm "elim_negate";
       
   816 in fun elim_negate thy thms =
       
   817   let
       
   818     fun bins_of (Const _) =
       
   819           I
       
   820       | bins_of (Var _) =
       
   821           I
       
   822       | bins_of (Free _) =
       
   823           I
       
   824       | bins_of (Bound _) =
       
   825           I
       
   826       | bins_of (Abs (_, _, t)) =
       
   827           bins_of t
       
   828       | bins_of (t as _ $ _) =
       
   829           case strip_comb t
       
   830            of (Const ("Numeral.number_of", _), [bin]) => cons bin
       
   831             | (t', ts) => bins_of t' #> fold bins_of ts;
       
   832     fun is_negative bin = case try HOLogic.dest_binum bin
       
   833      of SOME i => i < 0
       
   834       | _ => false;
       
   835     fun instantiate_with bin =
       
   836       Drule.instantiate' [] [(SOME o cterm_of thy) bin] elim_negate_thm;
       
   837     val rewrites  =
       
   838       []
       
   839       |> fold (bins_of o prop_of) thms
       
   840       |> filter is_negative
       
   841       |> map instantiate_with
       
   842   in
       
   843     thms
       
   844     |> map (rewrite_rule rewrites)
       
   845   end;
       
   846 end; (*local*)
       
   847 *}
       
   848 
       
   849 setup {*
       
   850   CodegenTheorems.add_preproc elim_negate
       
   851 *}
       
   852 
       
   853 subsection {* legacy ML bindings *}
   783 
   854 
   784 ML
   855 ML
   785 {*
   856 {*
   786 val eq_nat_nat_iff = thm"eq_nat_nat_iff";
   857 val eq_nat_nat_iff = thm"eq_nat_nat_iff";
   787 val eq_nat_number_of = thm"eq_nat_number_of";
   858 val eq_nat_number_of = thm"eq_nat_number_of";