src/HOL/Library/Efficient_Nat.thy
author haftmann
Mon, 26 Jul 2010 11:10:36 +0200
changeset 37969 3bf1fffcdd48
parent 37958 9728342bcd56
child 38771 f9cd27cbe8a4
permissions -rw-r--r--
corrected range check once more
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Library/Efficient_Nat.thy
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
     2
    Author:     Stefan Berghofer, Florian Haftmann, TU Muenchen
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     3
*)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     4
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
     5
header {* Implementation of natural numbers by target-language integers *}
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     6
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     7
theory Efficient_Nat
31203
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31128
diff changeset
     8
imports Code_Integer Main
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     9
begin
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    10
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    11
text {*
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    12
  When generating code for functions on natural numbers, the
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    13
  canonical representation using @{term "0::nat"} and
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37223
diff changeset
    14
  @{term Suc} is unsuitable for computations involving large
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    15
  numbers.  The efficiency of the generated code can be improved
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    16
  drastically by implementing natural numbers by target-language
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    17
  integers.  To do this, just include this theory.
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    18
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    19
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    20
subsection {* Basic arithmetic *}
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    21
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    22
text {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    23
  Most standard arithmetic functions on natural numbers are implemented
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    24
  using their counterparts on the integers:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    25
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    26
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    27
code_datatype number_nat_inst.number_of_nat
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    28
32069
6d28bbd33e2c prefer code_inline over code_unfold; use code_unfold_post where appropriate
haftmann
parents: 31998
diff changeset
    29
lemma zero_nat_code [code, code_unfold_post]:
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    30
  "0 = (Numeral0 :: nat)"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    31
  by simp
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    32
32069
6d28bbd33e2c prefer code_inline over code_unfold; use code_unfold_post where appropriate
haftmann
parents: 31998
diff changeset
    33
lemma one_nat_code [code, code_unfold_post]:
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    34
  "1 = (Numeral1 :: nat)"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    35
  by simp
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    36
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    37
lemma Suc_code [code]:
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    38
  "Suc n = n + 1"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    39
  by simp
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    40
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    41
lemma plus_nat_code [code]:
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    42
  "n + m = nat (of_nat n + of_nat m)"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    43
  by simp
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    44
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    45
lemma minus_nat_code [code]:
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    46
  "n - m = nat (of_nat n - of_nat m)"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    47
  by simp
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    48
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    49
lemma times_nat_code [code]:
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    50
  "n * m = nat (of_nat n * of_nat m)"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    51
  unfolding of_nat_mult [symmetric] by simp
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    52
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
    53
lemma divmod_nat_code [code]:
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
    54
  "divmod_nat n m = prod_fun nat nat (pdivmod (of_nat n) (of_nat m))"
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
    55
  by (simp add: prod_fun_def split_def pdivmod_def nat_div_distrib nat_mod_distrib divmod_nat_div_mod)
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    56
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    57
lemma eq_nat_code [code]:
28351
abfc66969d1f non left-linear equations for nbe
haftmann
parents: 28346
diff changeset
    58
  "eq_class.eq n m \<longleftrightarrow> eq_class.eq (of_nat n \<Colon> int) (of_nat m)"
abfc66969d1f non left-linear equations for nbe
haftmann
parents: 28346
diff changeset
    59
  by (simp add: eq)
abfc66969d1f non left-linear equations for nbe
haftmann
parents: 28346
diff changeset
    60
abfc66969d1f non left-linear equations for nbe
haftmann
parents: 28346
diff changeset
    61
lemma eq_nat_refl [code nbe]:
abfc66969d1f non left-linear equations for nbe
haftmann
parents: 28346
diff changeset
    62
  "eq_class.eq (n::nat) n \<longleftrightarrow> True"
abfc66969d1f non left-linear equations for nbe
haftmann
parents: 28346
diff changeset
    63
  by (rule HOL.eq_refl)
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    64
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    65
lemma less_eq_nat_code [code]:
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    66
  "n \<le> m \<longleftrightarrow> (of_nat n \<Colon> int) \<le> of_nat m"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    67
  by simp
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    68
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    69
lemma less_nat_code [code]:
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    70
  "n < m \<longleftrightarrow> (of_nat n \<Colon> int) < of_nat m"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    71
  by simp
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    72
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    73
subsection {* Case analysis *}
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    74
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    75
text {*
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    76
  Case analysis on natural numbers is rephrased using a conditional
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    77
  expression:
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    78
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    79
31998
2c7a24f74db9 code attributes use common underscore convention
haftmann
parents: 31954
diff changeset
    80
lemma [code, code_unfold]:
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    81
  "nat_case = (\<lambda>f g n. if n = 0 then f else g (n - 1))"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    82
  by (auto simp add: expand_fun_eq dest!: gr0_implies_Suc)
25615
b337edd55a07 target language div and mod
haftmann
parents: 25488
diff changeset
    83
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    84
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    85
subsection {* Preprocessors *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    86
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    87
text {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    88
  In contrast to @{term "Suc n"}, the term @{term "n + (1::nat)"} is no longer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    89
  a constructor term. Therefore, all occurrences of this term in a position
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    90
  where a pattern is expected (i.e.\ on the left-hand side of a recursion
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    91
  equation or in the arguments of an inductive relation in an introduction
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    92
  rule) must be eliminated.
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    93
  This can be accomplished by applying the following transformation rules:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    94
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    95
29937
ffed4bd4bfad faster preprocessor
haftmann
parents: 29932
diff changeset
    96
lemma Suc_if_eq: "(\<And>n. f (Suc n) \<equiv> h n) \<Longrightarrow> f 0 \<equiv> g \<Longrightarrow>
ffed4bd4bfad faster preprocessor
haftmann
parents: 29932
diff changeset
    97
  f n \<equiv> if n = 0 then g else h (n - 1)"
31954
8db19c99b00a Stefan Berghofer's code generator uses Pure equality instead of HOL equality now
haftmann
parents: 31377
diff changeset
    98
  by (rule eq_reflection) (cases n, simp_all)
29937
ffed4bd4bfad faster preprocessor
haftmann
parents: 29932
diff changeset
    99
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   100
lemma Suc_clause: "(\<And>n. P n (Suc n)) \<Longrightarrow> n \<noteq> 0 \<Longrightarrow> P (n - 1) n"
29932
a2594b5c945a dropped clause_suc_preproc for generic code generator
haftmann
parents: 29815
diff changeset
   101
  by (cases n) simp_all
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   102
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   103
text {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   104
  The rules above are built into a preprocessor that is plugged into
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   105
  the code generator. Since the preprocessor for introduction rules
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   106
  does not know anything about modes, some of the modes that worked
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   107
  for the canonical representation of natural numbers may no longer work.
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   108
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   109
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   110
(*<*)
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   111
setup {*
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   112
let
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   113
31954
8db19c99b00a Stefan Berghofer's code generator uses Pure equality instead of HOL equality now
haftmann
parents: 31377
diff changeset
   114
fun remove_suc thy thms =
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   115
  let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   116
    val vname = Name.variant (map fst
29937
ffed4bd4bfad faster preprocessor
haftmann
parents: 29932
diff changeset
   117
      (fold (Term.add_var_names o Thm.full_prop_of) thms [])) "n";
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   118
    val cv = cterm_of thy (Var ((vname, 0), HOLogic.natT));
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   119
    fun lhs_of th = snd (Thm.dest_comb
31954
8db19c99b00a Stefan Berghofer's code generator uses Pure equality instead of HOL equality now
haftmann
parents: 31377
diff changeset
   120
      (fst (Thm.dest_comb (cprop_of th))));
8db19c99b00a Stefan Berghofer's code generator uses Pure equality instead of HOL equality now
haftmann
parents: 31377
diff changeset
   121
    fun rhs_of th = snd (Thm.dest_comb (cprop_of th));
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   122
    fun find_vars ct = (case term_of ct of
29932
a2594b5c945a dropped clause_suc_preproc for generic code generator
haftmann
parents: 29815
diff changeset
   123
        (Const (@{const_name Suc}, _) $ Var _) => [(cv, snd (Thm.dest_comb ct))]
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   124
      | _ $ _ =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   125
        let val (ct1, ct2) = Thm.dest_comb ct
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   126
        in 
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   127
          map (apfst (fn ct => Thm.capply ct ct2)) (find_vars ct1) @
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   128
          map (apfst (Thm.capply ct1)) (find_vars ct2)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   129
        end
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   130
      | _ => []);
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   131
    val eqs = maps
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   132
      (fn th => map (pair th) (find_vars (lhs_of th))) thms;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   133
    fun mk_thms (th, (ct, cv')) =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   134
      let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   135
        val th' =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   136
          Thm.implies_elim
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   137
           (Conv.fconv_rule (Thm.beta_conversion true)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   138
             (Drule.instantiate'
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   139
               [SOME (ctyp_of_term ct)] [SOME (Thm.cabs cv ct),
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   140
                 SOME (Thm.cabs cv' (rhs_of th)), NONE, SOME cv']
31954
8db19c99b00a Stefan Berghofer's code generator uses Pure equality instead of HOL equality now
haftmann
parents: 31377
diff changeset
   141
               @{thm Suc_if_eq})) (Thm.forall_intr cv' th)
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   142
      in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   143
        case map_filter (fn th'' =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   144
            SOME (th'', singleton
36603
d5d6111761a6 renamed Variable.thm_context to Variable.global_thm_context to emphasize that this is not the real thing;
wenzelm
parents: 36176
diff changeset
   145
              (Variable.trade (K (fn [th'''] => [th''' RS th']))
d5d6111761a6 renamed Variable.thm_context to Variable.global_thm_context to emphasize that this is not the real thing;
wenzelm
parents: 36176
diff changeset
   146
                (Variable.global_thm_context th'')) th'')
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   147
          handle THM _ => NONE) thms of
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   148
            [] => NONE
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   149
          | thps =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   150
              let val (ths1, ths2) = split_list thps
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   151
              in SOME (subtract Thm.eq_thm (th :: ths1) thms @ ths2) end
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   152
      end
29937
ffed4bd4bfad faster preprocessor
haftmann
parents: 29932
diff changeset
   153
  in get_first mk_thms eqs end;
ffed4bd4bfad faster preprocessor
haftmann
parents: 29932
diff changeset
   154
34893
ecdc526af73a function transformer preprocessor applies to both code generators
haftmann
parents: 33364
diff changeset
   155
fun eqn_suc_base_preproc thy thms =
29937
ffed4bd4bfad faster preprocessor
haftmann
parents: 29932
diff changeset
   156
  let
31954
8db19c99b00a Stefan Berghofer's code generator uses Pure equality instead of HOL equality now
haftmann
parents: 31377
diff changeset
   157
    val dest = fst o Logic.dest_equals o prop_of;
29937
ffed4bd4bfad faster preprocessor
haftmann
parents: 29932
diff changeset
   158
    val contains_suc = exists_Const (fn (c, _) => c = @{const_name Suc});
ffed4bd4bfad faster preprocessor
haftmann
parents: 29932
diff changeset
   159
  in
ffed4bd4bfad faster preprocessor
haftmann
parents: 29932
diff changeset
   160
    if forall (can dest) thms andalso exists (contains_suc o dest) thms
32348
36dbff4841ab repair mess produced by resolution afterwards
haftmann
parents: 32073
diff changeset
   161
      then thms |> perhaps_loop (remove_suc thy) |> (Option.map o map) Drule.zero_var_indexes
29937
ffed4bd4bfad faster preprocessor
haftmann
parents: 29932
diff changeset
   162
       else NONE
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   163
  end;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   164
34893
ecdc526af73a function transformer preprocessor applies to both code generators
haftmann
parents: 33364
diff changeset
   165
val eqn_suc_preproc = Code_Preproc.simple_functrans eqn_suc_base_preproc;
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   166
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   167
fun remove_suc_clause thy thms =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   168
  let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   169
    val vname = Name.variant (map fst
29258
bce03c644efb canonical Term.add_var_names;
wenzelm
parents: 28969
diff changeset
   170
      (fold (Term.add_var_names o Thm.full_prop_of) thms [])) "x";
24222
a8a28c15c5cc new structure for code generator modules
haftmann
parents: 24195
diff changeset
   171
    fun find_var (t as Const (@{const_name Suc}, _) $ (v as Var _)) = SOME (t, v)
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   172
      | find_var (t $ u) = (case find_var t of NONE => find_var u | x => x)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   173
      | find_var _ = NONE;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   174
    fun find_thm th =
35625
9c818cab0dd0 modernized structure Object_Logic;
wenzelm
parents: 34944
diff changeset
   175
      let val th' = Conv.fconv_rule Object_Logic.atomize th
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   176
      in Option.map (pair (th, th')) (find_var (prop_of th')) end
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   177
  in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   178
    case get_first find_thm thms of
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   179
      NONE => thms
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   180
    | SOME ((th, th'), (Sucv, v)) =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   181
        let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   182
          val cert = cterm_of (Thm.theory_of_thm th);
35625
9c818cab0dd0 modernized structure Object_Logic;
wenzelm
parents: 34944
diff changeset
   183
          val th'' = Object_Logic.rulify (Thm.implies_elim
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   184
            (Conv.fconv_rule (Thm.beta_conversion true)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   185
              (Drule.instantiate' []
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   186
                [SOME (cert (lambda v (Abs ("x", HOLogic.natT,
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   187
                   abstract_over (Sucv,
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   188
                     HOLogic.dest_Trueprop (prop_of th')))))),
24222
a8a28c15c5cc new structure for code generator modules
haftmann
parents: 24195
diff changeset
   189
                 SOME (cert v)] @{thm Suc_clause}))
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   190
            (Thm.forall_intr (cert v) th'))
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   191
        in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   192
          remove_suc_clause thy (map (fn th''' =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   193
            if (op = o pairself prop_of) (th''', th) then th'' else th''') thms)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   194
        end
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   195
  end;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   196
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   197
fun clause_suc_preproc thy ths =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   198
  let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   199
    val dest = fst o HOLogic.dest_mem o HOLogic.dest_Trueprop
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   200
  in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   201
    if forall (can (dest o concl_of)) ths andalso
29287
5b0bfd63b5da eliminated OldTerm.(add_)term_consts;
wenzelm
parents: 29270
diff changeset
   202
      exists (fn th => exists (exists_Const (fn (c, _) => c = @{const_name Suc}))
5b0bfd63b5da eliminated OldTerm.(add_)term_consts;
wenzelm
parents: 29270
diff changeset
   203
        (map_filter (try dest) (concl_of th :: prems_of th))) ths
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   204
    then remove_suc_clause thy ths else ths
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   205
  end;
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   206
in
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   207
34893
ecdc526af73a function transformer preprocessor applies to both code generators
haftmann
parents: 33364
diff changeset
   208
  Code_Preproc.add_functrans ("eqn_Suc", eqn_suc_preproc)
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   209
  #> Codegen.add_preprocessor clause_suc_preproc
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   210
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   211
end;
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   212
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   213
(*>*)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   214
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   215
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   216
subsection {* Target language setup *}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   217
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   218
text {*
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   219
  For ML, we map @{typ nat} to target language integers, where we
34899
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   220
  ensure that values are always non-negative.
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   221
*}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   222
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   223
code_type nat
27496
f56684dd75a5 fix: using IntInf.int for SML
haftmann
parents: 27368
diff changeset
   224
  (SML "IntInf.int")
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   225
  (OCaml "Big'_int.big'_int")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   226
  (Eval "int")
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   227
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   228
types_code
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   229
  nat ("int")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   230
attach (term_of) {*
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   231
val term_of_nat = HOLogic.mk_number HOLogic.natT;
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   232
*}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   233
attach (test) {*
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   234
fun gen_nat i =
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   235
  let val n = random_range 0 i
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   236
  in (n, fn () => term_of_nat n) end;
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   237
*}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   238
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   239
text {*
34899
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   240
  For Haskell ans Scala we define our own @{typ nat} type.  The reason
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   241
  is that we have to distinguish type class instances for @{typ nat}
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   242
  and @{typ int}.
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   243
*}
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   244
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   245
code_include Haskell "Nat" {*
37050
4a021f6be77c nats in Haskell are readable
haftmann
parents: 36603
diff changeset
   246
newtype Nat = Nat Integer deriving (Eq, Show, Read);
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   247
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   248
instance Num Nat where {
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   249
  fromInteger k = Nat (if k >= 0 then k else 0);
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   250
  Nat n + Nat m = Nat (n + m);
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   251
  Nat n - Nat m = fromInteger (n - m);
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   252
  Nat n * Nat m = Nat (n * m);
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   253
  abs n = n;
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   254
  signum _ = 1;
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   255
  negate n = error "negate Nat";
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   256
};
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   257
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   258
instance Ord Nat where {
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   259
  Nat n <= Nat m = n <= m;
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   260
  Nat n < Nat m = n < m;
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   261
};
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   262
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   263
instance Real Nat where {
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   264
  toRational (Nat n) = toRational n;
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   265
};
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   266
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   267
instance Enum Nat where {
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   268
  toEnum k = fromInteger (toEnum k);
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   269
  fromEnum (Nat n) = fromEnum n;
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   270
};
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   271
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   272
instance Integral Nat where {
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   273
  toInteger (Nat n) = n;
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   274
  divMod n m = quotRem n m;
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   275
  quotRem (Nat n) (Nat m)
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   276
    | (m == 0) = (0, Nat n)
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   277
    | otherwise = (Nat k, Nat l) where (k, l) = quotRem n m;
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   278
};
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   279
*}
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   280
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   281
code_reserved Haskell Nat
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   282
34899
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   283
code_include Scala "Nat" {*
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34902
diff changeset
   284
import scala.Math
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34902
diff changeset
   285
34899
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   286
object Nat {
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   287
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   288
  def apply(numeral: BigInt): Nat = new Nat(numeral max 0)
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   289
  def apply(numeral: Int): Nat = Nat(BigInt(numeral))
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   290
  def apply(numeral: String): Nat = Nat(BigInt(numeral))
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   291
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   292
}
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   293
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   294
class Nat private(private val value: BigInt) {
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   295
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   296
  override def hashCode(): Int = this.value.hashCode()
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   297
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   298
  override def equals(that: Any): Boolean = that match {
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   299
    case that: Nat => this equals that
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   300
    case _ => false
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   301
  }
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   302
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   303
  override def toString(): String = this.value.toString
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   304
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   305
  def equals(that: Nat): Boolean = this.value == that.value
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   306
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   307
  def as_BigInt: BigInt = this.value
37892
3d8857f42a64 avoid deprecation
haftmann
parents: 37878
diff changeset
   308
  def as_Int: Int = if (this.value >= Int.MinValue && this.value <= Int.MaxValue)
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34902
diff changeset
   309
      this.value.intValue
37969
3bf1fffcdd48 corrected range check once more
haftmann
parents: 37958
diff changeset
   310
    else error("Int value out of range: " + this.value.toString)
34899
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   311
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   312
  def +(that: Nat): Nat = new Nat(this.value + that.value)
37223
5226259b6fa2 corrected implementation
haftmann
parents: 37050
diff changeset
   313
  def -(that: Nat): Nat = Nat(this.value - that.value)
34899
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   314
  def *(that: Nat): Nat = new Nat(this.value * that.value)
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   315
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   316
  def /%(that: Nat): (Nat, Nat) = if (that.value == 0) (new Nat(0), this)
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   317
    else {
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   318
      val (k, l) = this.value /% that.value
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   319
      (new Nat(k), new Nat(l))
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   320
    }
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   321
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   322
  def <=(that: Nat): Boolean = this.value <= that.value
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   323
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   324
  def <(that: Nat): Boolean = this.value < that.value
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   325
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   326
}
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   327
*}
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   328
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   329
code_reserved Scala Nat
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   330
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   331
code_type nat
29793
86cac1fab613 changed name space policy for Haskell includes
haftmann
parents: 29657
diff changeset
   332
  (Haskell "Nat.Nat")
37878
d016aaead7a2 dropped superfluous prefixes
haftmann
parents: 37846
diff changeset
   333
  (Scala "Nat")
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   334
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   335
code_instance nat :: eq
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   336
  (Haskell -)
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   337
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   338
text {*
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   339
  Natural numerals.
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   340
*}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   341
32069
6d28bbd33e2c prefer code_inline over code_unfold; use code_unfold_post where appropriate
haftmann
parents: 31998
diff changeset
   342
lemma [code_unfold_post]:
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   343
  "nat (number_of i) = number_nat_inst.number_of_nat i"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   344
  -- {* this interacts as desired with @{thm nat_number_of_def} *}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   345
  by (simp add: number_nat_inst.number_of_nat)
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   346
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   347
setup {*
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   348
  fold (Numeral.add_code @{const_name number_nat_inst.number_of_nat}
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   349
    false Code_Printer.literal_positive_numeral) ["SML", "OCaml", "Haskell", "Scala"]
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   350
*}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   351
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   352
text {*
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   353
  Since natural numbers are implemented
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   354
  using integers in ML, the coercion function @{const "of_nat"} of type
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   355
  @{typ "nat \<Rightarrow> int"} is simply implemented by the identity function.
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37223
diff changeset
   356
  For the @{const nat} function for converting an integer to a natural
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   357
  number, we give a specific implementation using an ML function that
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   358
  returns its input value, provided that it is non-negative, and otherwise
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   359
  returns @{text "0"}.
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   360
*}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   361
32073
0a83608e21f1 additional preprocessor rule
haftmann
parents: 32069
diff changeset
   362
definition int :: "nat \<Rightarrow> int" where
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28522
diff changeset
   363
  [code del]: "int = of_nat"
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   364
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28522
diff changeset
   365
lemma int_code' [code]:
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   366
  "int (number_of l) = (if neg (number_of l \<Colon> int) then 0 else number_of l)"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   367
  unfolding int_nat_number_of [folded int_def] ..
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   368
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28522
diff changeset
   369
lemma nat_code' [code]:
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   370
  "nat (number_of l) = (if neg (number_of l \<Colon> int) then 0 else number_of l)"
28969
4ed63cdda799 change more lemmas to avoid using iszero
huffman
parents: 28694
diff changeset
   371
  unfolding nat_number_of_def number_of_is_id neg_def by simp
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   372
32069
6d28bbd33e2c prefer code_inline over code_unfold; use code_unfold_post where appropriate
haftmann
parents: 31998
diff changeset
   373
lemma of_nat_int [code_unfold_post]:
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   374
  "of_nat = int" by (simp add: int_def)
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   375
32073
0a83608e21f1 additional preprocessor rule
haftmann
parents: 32069
diff changeset
   376
lemma of_nat_aux_int [code_unfold]:
0a83608e21f1 additional preprocessor rule
haftmann
parents: 32069
diff changeset
   377
  "of_nat_aux (\<lambda>i. i + 1) k 0 = int k"
0a83608e21f1 additional preprocessor rule
haftmann
parents: 32069
diff changeset
   378
  by (simp add: int_def Nat.of_nat_code)
0a83608e21f1 additional preprocessor rule
haftmann
parents: 32069
diff changeset
   379
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   380
code_const int
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   381
  (SML "_")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   382
  (OCaml "_")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   383
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   384
consts_code
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   385
  int ("(_)")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   386
  nat ("\<module>nat")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   387
attach {*
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   388
fun nat i = if i < 0 then 0 else i;
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   389
*}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   390
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   391
code_const nat
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   392
  (SML "IntInf.max/ (/0,/ _)")
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   393
  (OCaml "Big'_int.max'_big'_int/ Big'_int.zero'_big'_int")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   394
  (Eval "Integer.max/ _/ 0")
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   395
35689
c3bef0c972d7 fixed typo
haftmann
parents: 35625
diff changeset
   396
text {* For Haskell and Scala, things are slightly different again. *}
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   397
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   398
code_const int and nat
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   399
  (Haskell "toInteger" and "fromInteger")
37878
d016aaead7a2 dropped superfluous prefixes
haftmann
parents: 37846
diff changeset
   400
  (Scala "!_.as'_BigInt" and "Nat")
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   401
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   402
text {* Conversion from and to code numerals. *}
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   403
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   404
code_const Code_Numeral.of_nat
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   405
  (SML "IntInf.toInt")
31377
a48f9ef9de15 OCaml builtin intergers are elusive; avoid
haftmann
parents: 31295
diff changeset
   406
  (OCaml "_")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   407
  (Haskell "!(fromInteger/ ./ toInteger)")
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   408
  (Scala "!Natural(_.as'_BigInt)")
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   409
  (Eval "_")
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   410
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   411
code_const Code_Numeral.nat_of
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   412
  (SML "IntInf.fromInt")
31377
a48f9ef9de15 OCaml builtin intergers are elusive; avoid
haftmann
parents: 31295
diff changeset
   413
  (OCaml "_")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   414
  (Haskell "!(fromInteger/ ./ toInteger)")
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   415
  (Scala "!Nat(_.as'_BigInt)")
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   416
  (Eval "_")
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   417
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   418
text {* Using target language arithmetic operations whenever appropriate *}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   419
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   420
code_const "op + \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   421
  (SML "IntInf.+ ((_), (_))")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   422
  (OCaml "Big'_int.add'_big'_int")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   423
  (Haskell infixl 6 "+")
34899
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   424
  (Scala infixl 7 "+")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   425
  (Eval infixl 8 "+")
34899
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   426
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   427
code_const "op - \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   428
  (Haskell infixl 6 "-")
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   429
  (Scala infixl 7 "-")
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   430
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   431
code_const "op * \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   432
  (SML "IntInf.* ((_), (_))")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   433
  (OCaml "Big'_int.mult'_big'_int")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   434
  (Haskell infixl 7 "*")
34899
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   435
  (Scala infixl 8 "*")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   436
  (Eval infixl 9 "*")
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   437
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   438
code_const divmod_nat
26009
b6a64fe38634 treating division by zero properly
haftmann
parents: 25967
diff changeset
   439
  (SML "IntInf.divMod/ ((_),/ (_))")
b6a64fe38634 treating division by zero properly
haftmann
parents: 25967
diff changeset
   440
  (OCaml "Big'_int.quomod'_big'_int")
b6a64fe38634 treating division by zero properly
haftmann
parents: 25967
diff changeset
   441
  (Haskell "divMod")
34899
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   442
  (Scala infixl 8 "/%")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   443
  (Eval "Integer.div'_mod")
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   444
28346
b8390cd56b8f discontinued special treatment of op = vs. eq_class.eq
haftmann
parents: 28228
diff changeset
   445
code_const "eq_class.eq \<Colon> nat \<Rightarrow> nat \<Rightarrow> bool"
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   446
  (SML "!((_ : IntInf.int) = _)")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   447
  (OCaml "Big'_int.eq'_big'_int")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   448
  (Haskell infixl 4 "==")
34899
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   449
  (Scala infixl 5 "==")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   450
  (Eval infixl 6 "=")
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   451
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   452
code_const "op \<le> \<Colon> nat \<Rightarrow> nat \<Rightarrow> bool"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   453
  (SML "IntInf.<= ((_), (_))")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   454
  (OCaml "Big'_int.le'_big'_int")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   455
  (Haskell infix 4 "<=")
34899
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   456
  (Scala infixl 4 "<=")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   457
  (Eval infixl 6 "<=")
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   458
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   459
code_const "op < \<Colon> nat \<Rightarrow> nat \<Rightarrow> bool"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   460
  (SML "IntInf.< ((_), (_))")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   461
  (OCaml "Big'_int.lt'_big'_int")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   462
  (Haskell infix 4 "<")
34899
8674bb6f727b added Scala setup
haftmann
parents: 34893
diff changeset
   463
  (Scala infixl 4 "<")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   464
  (Eval infixl 6 "<")
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   465
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   466
consts_code
28522
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 28423
diff changeset
   467
  "0::nat"                     ("0")
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 28423
diff changeset
   468
  "1::nat"                     ("1")
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   469
  Suc                          ("(_ +/ 1)")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   470
  "op + \<Colon>  nat \<Rightarrow> nat \<Rightarrow> nat"   ("(_ +/ _)")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   471
  "op * \<Colon>  nat \<Rightarrow> nat \<Rightarrow> nat"   ("(_ */ _)")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   472
  "op \<le> \<Colon>  nat \<Rightarrow> nat \<Rightarrow> bool"  ("(_ <=/ _)")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   473
  "op < \<Colon>  nat \<Rightarrow> nat \<Rightarrow> bool"  ("(_ </ _)")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   474
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   475
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27673
diff changeset
   476
text {* Evaluation *}
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27673
diff changeset
   477
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28522
diff changeset
   478
lemma [code, code del]:
32657
5f13912245ff Code_Eval(uation)
haftmann
parents: 32348
diff changeset
   479
  "(Code_Evaluation.term_of \<Colon> nat \<Rightarrow> term) = Code_Evaluation.term_of" ..
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27673
diff changeset
   480
32657
5f13912245ff Code_Eval(uation)
haftmann
parents: 32348
diff changeset
   481
code_const "Code_Evaluation.term_of \<Colon> nat \<Rightarrow> term"
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27673
diff changeset
   482
  (SML "HOLogic.mk'_number/ HOLogic.natT")
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27673
diff changeset
   483
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27673
diff changeset
   484
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   485
text {* Module names *}
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   486
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   487
code_modulename SML
33364
2bd12592c5e8 tuned code setup
haftmann
parents: 33343
diff changeset
   488
  Efficient_Nat Arith
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   489
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   490
code_modulename OCaml
33364
2bd12592c5e8 tuned code setup
haftmann
parents: 33343
diff changeset
   491
  Efficient_Nat Arith
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   492
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   493
code_modulename Haskell
33364
2bd12592c5e8 tuned code setup
haftmann
parents: 33343
diff changeset
   494
  Efficient_Nat Arith
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   495
36176
3fe7e97ccca8 replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
wenzelm
parents: 35689
diff changeset
   496
hide_const int
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   497
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   498
end