src/HOL/Library/Efficient_Nat.thy
changeset 28522 eacb54d9e78d
parent 28423 9fc3befd8191
child 28562 4e74209f113e
equal deleted inserted replaced
28521:5b426c051e36 28522:eacb54d9e78d
    25   using their counterparts on the integers:
    25   using their counterparts on the integers:
    26 *}
    26 *}
    27 
    27 
    28 code_datatype number_nat_inst.number_of_nat
    28 code_datatype number_nat_inst.number_of_nat
    29 
    29 
    30 lemma zero_nat_code [code, code unfold]:
    30 lemma zero_nat_code [code, code inline]:
    31   "0 = (Numeral0 :: nat)"
    31   "0 = (Numeral0 :: nat)"
    32   by simp
    32   by simp
    33 lemmas [code post] = zero_nat_code [symmetric]
    33 lemmas [code post] = zero_nat_code [symmetric]
    34 
    34 
    35 lemma one_nat_code [code, code unfold]:
    35 lemma one_nat_code [code, code inline]:
    36   "1 = (Numeral1 :: nat)"
    36   "1 = (Numeral1 :: nat)"
    37   by simp
    37   by simp
    38 lemmas [code post] = one_nat_code [symmetric]
    38 lemmas [code post] = one_nat_code [symmetric]
    39 
    39 
    40 lemma Suc_code [code]:
    40 lemma Suc_code [code]:
    57   and @{term "op mod \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"} operations. *}
    57   and @{term "op mod \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"} operations. *}
    58 
    58 
    59 definition
    59 definition
    60   divmod_aux ::  "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat"
    60   divmod_aux ::  "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat"
    61 where
    61 where
    62   [code func del]: "divmod_aux = divmod"
    62   [code del]: "divmod_aux = divmod"
    63 
    63 
    64 lemma [code func]:
    64 lemma [code]:
    65   "divmod n m = (if m = 0 then (0, n) else divmod_aux n m)"
    65   "divmod n m = (if m = 0 then (0, n) else divmod_aux n m)"
    66   unfolding divmod_aux_def divmod_div_mod by simp
    66   unfolding divmod_aux_def divmod_div_mod by simp
    67 
    67 
    68 lemma divmod_aux_code [code]:
    68 lemma divmod_aux_code [code]:
    69   "divmod_aux n m = (nat (of_nat n div of_nat m), nat (of_nat n mod of_nat m))"
    69   "divmod_aux n m = (nat (of_nat n div of_nat m), nat (of_nat n mod of_nat m))"
    90 text {*
    90 text {*
    91   Case analysis on natural numbers is rephrased using a conditional
    91   Case analysis on natural numbers is rephrased using a conditional
    92   expression:
    92   expression:
    93 *}
    93 *}
    94 
    94 
    95 lemma [code func, code unfold]:
    95 lemma [code, code unfold]:
    96   "nat_case = (\<lambda>f g n. if n = 0 then f else g (n - 1))"
    96   "nat_case = (\<lambda>f g n. if n = 0 then f else g (n - 1))"
    97   by (auto simp add: expand_fun_eq dest!: gr0_implies_Suc)
    97   by (auto simp add: expand_fun_eq dest!: gr0_implies_Suc)
    98 
    98 
    99 
    99 
   100 subsection {* Preprocessors *}
   100 subsection {* Preprocessors *}
   219     then remove_suc_clause thy ths else ths
   219     then remove_suc_clause thy ths else ths
   220   end;
   220   end;
   221 
   221 
   222 fun lift f thy eqns1 =
   222 fun lift f thy eqns1 =
   223   let
   223   let
   224     val eqns2 = ((map o apfst) (AxClass.overload thy)
   224     val eqns2 = burrow_fst Drule.zero_var_indexes_list eqns1;
   225       o burrow_fst Drule.zero_var_indexes_list) eqns1;
       
   226     val thms3 = try (map fst
   225     val thms3 = try (map fst
   227       #> map (fn thm => thm RS @{thm meta_eq_to_obj_eq})
   226       #> map (fn thm => thm RS @{thm meta_eq_to_obj_eq})
   228       #> f thy
   227       #> f thy
   229       #> map (fn thm => thm RS @{thm eq_reflection})
   228       #> map (fn thm => thm RS @{thm eq_reflection})
   230       #> map (Conv.fconv_rule Drule.beta_eta_conversion)) eqns2;
   229       #> map (Conv.fconv_rule Drule.beta_eta_conversion)) eqns2;
   231     val thms4 = Option.map Drule.zero_var_indexes_list thms3;
   230     val thms4 = Option.map Drule.zero_var_indexes_list thms3;
   232   in case thms4
   231   in case thms4
   233    of NONE => NONE
   232    of NONE => NONE
   234     | SOME thms4 => if Thm.eq_thms (map fst eqns2, thms4)
   233     | SOME thms4 => if Thm.eq_thms (map fst eqns2, thms4)
   235         then NONE else SOME (map (Code_Unit.mk_eqn thy) thms4)
   234         then NONE else SOME (map (apfst (AxClass.overload thy) o Code_Unit.mk_eqn thy) thms4)
       
   235           (*FIXME*)
   236   end
   236   end
   237 
   237 
   238 in
   238 in
   239 
   239 
   240   Codegen.add_preprocessor eqn_suc_preproc
   240   Codegen.add_preprocessor eqn_suc_preproc
   421   (SML "IntInf.< ((_), (_))")
   421   (SML "IntInf.< ((_), (_))")
   422   (OCaml "Big'_int.lt'_big'_int")
   422   (OCaml "Big'_int.lt'_big'_int")
   423   (Haskell infix 4 "<")
   423   (Haskell infix 4 "<")
   424 
   424 
   425 consts_code
   425 consts_code
   426   0                            ("0")
   426   "0::nat"                     ("0")
       
   427   "1::nat"                     ("1")
   427   Suc                          ("(_ +/ 1)")
   428   Suc                          ("(_ +/ 1)")
   428   "op + \<Colon>  nat \<Rightarrow> nat \<Rightarrow> nat"   ("(_ +/ _)")
   429   "op + \<Colon>  nat \<Rightarrow> nat \<Rightarrow> nat"   ("(_ +/ _)")
   429   "op * \<Colon>  nat \<Rightarrow> nat \<Rightarrow> nat"   ("(_ */ _)")
   430   "op * \<Colon>  nat \<Rightarrow> nat \<Rightarrow> nat"   ("(_ */ _)")
   430   "op \<le> \<Colon>  nat \<Rightarrow> nat \<Rightarrow> bool"  ("(_ <=/ _)")
   431   "op \<le> \<Colon>  nat \<Rightarrow> nat \<Rightarrow> bool"  ("(_ <=/ _)")
   431   "op < \<Colon>  nat \<Rightarrow> nat \<Rightarrow> bool"  ("(_ </ _)")
   432   "op < \<Colon>  nat \<Rightarrow> nat \<Rightarrow> bool"  ("(_ </ _)")