src/HOL/Library/Efficient_Nat.thy
author wenzelm
Wed, 31 Dec 2008 18:53:16 +0100
changeset 29270 0eade173f77e
parent 29258 bce03c644efb
child 29287 5b0bfd63b5da
permissions -rw-r--r--
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
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
28694
4e9edaef64dc slightly tuned
haftmann
parents: 28683
diff changeset
     8
imports Code_Index Code_Integer
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
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    14
  @{term "Suc"} is unsuitable for computations involving large
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
28522
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 28423
diff changeset
    29
lemma zero_nat_code [code, code inline]:
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
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    32
lemmas [code post] = zero_nat_code [symmetric]
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    33
28522
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 28423
diff changeset
    34
lemma one_nat_code [code, code inline]:
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    35
  "1 = (Numeral1 :: nat)"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    36
  by simp
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    37
lemmas [code post] = one_nat_code [symmetric]
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    38
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    39
lemma Suc_code [code]:
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    40
  "Suc n = n + 1"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    41
  by simp
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    42
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    43
lemma plus_nat_code [code]:
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    44
  "n + m = nat (of_nat n + of_nat m)"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    45
  by simp
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    46
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    47
lemma minus_nat_code [code]:
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    48
  "n - m = nat (of_nat n - of_nat m)"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    49
  by simp
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    50
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    51
lemma times_nat_code [code]:
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    52
  "n * m = nat (of_nat n * of_nat m)"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    53
  unfolding of_nat_mult [symmetric] by simp
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    54
26009
b6a64fe38634 treating division by zero properly
haftmann
parents: 25967
diff changeset
    55
text {* Specialized @{term "op div \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"} 
b6a64fe38634 treating division by zero properly
haftmann
parents: 25967
diff changeset
    56
  and @{term "op mod \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"} operations. *}
b6a64fe38634 treating division by zero properly
haftmann
parents: 25967
diff changeset
    57
28694
4e9edaef64dc slightly tuned
haftmann
parents: 28683
diff changeset
    58
definition divmod_aux ::  "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat" where
28522
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 28423
diff changeset
    59
  [code del]: "divmod_aux = divmod"
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    60
28522
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 28423
diff changeset
    61
lemma [code]:
26100
fbc60cd02ae2 using only an relation predicate to construct div and mod
haftmann
parents: 26009
diff changeset
    62
  "divmod n m = (if m = 0 then (0, n) else divmod_aux n m)"
fbc60cd02ae2 using only an relation predicate to construct div and mod
haftmann
parents: 26009
diff changeset
    63
  unfolding divmod_aux_def divmod_div_mod by simp
26009
b6a64fe38634 treating division by zero properly
haftmann
parents: 25967
diff changeset
    64
26100
fbc60cd02ae2 using only an relation predicate to construct div and mod
haftmann
parents: 26009
diff changeset
    65
lemma divmod_aux_code [code]:
fbc60cd02ae2 using only an relation predicate to construct div and mod
haftmann
parents: 26009
diff changeset
    66
  "divmod_aux n m = (nat (of_nat n div of_nat m), nat (of_nat n mod of_nat m))"
fbc60cd02ae2 using only an relation predicate to construct div and mod
haftmann
parents: 26009
diff changeset
    67
  unfolding divmod_aux_def divmod_div_mod zdiv_int [symmetric] zmod_int [symmetric] by simp
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    68
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    69
lemma eq_nat_code [code]:
28351
abfc66969d1f non left-linear equations for nbe
haftmann
parents: 28346
diff changeset
    70
  "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
    71
  by (simp add: eq)
abfc66969d1f non left-linear equations for nbe
haftmann
parents: 28346
diff changeset
    72
abfc66969d1f non left-linear equations for nbe
haftmann
parents: 28346
diff changeset
    73
lemma eq_nat_refl [code nbe]:
abfc66969d1f non left-linear equations for nbe
haftmann
parents: 28346
diff changeset
    74
  "eq_class.eq (n::nat) n \<longleftrightarrow> True"
abfc66969d1f non left-linear equations for nbe
haftmann
parents: 28346
diff changeset
    75
  by (rule HOL.eq_refl)
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    76
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    77
lemma less_eq_nat_code [code]:
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    78
  "n \<le> m \<longleftrightarrow> (of_nat n \<Colon> int) \<le> of_nat m"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    79
  by simp
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    80
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    81
lemma less_nat_code [code]:
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    82
  "n < m \<longleftrightarrow> (of_nat n \<Colon> int) < of_nat m"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    83
  by simp
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    84
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    85
subsection {* Case analysis *}
23854
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 {*
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    88
  Case analysis on natural numbers is rephrased using a conditional
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    89
  expression:
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    90
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    91
28522
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 28423
diff changeset
    92
lemma [code, code unfold]:
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    93
  "nat_case = (\<lambda>f g n. if n = 0 then f else g (n - 1))"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
    94
  by (auto simp add: expand_fun_eq dest!: gr0_implies_Suc)
25615
b337edd55a07 target language div and mod
haftmann
parents: 25488
diff changeset
    95
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    96
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    97
subsection {* Preprocessors *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    98
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    99
text {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   100
  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
   101
  a constructor term. Therefore, all occurrences of this term in a position
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   102
  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
   103
  equation or in the arguments of an inductive relation in an introduction
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   104
  rule) must be eliminated.
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   105
  This can be accomplished by applying the following transformation rules:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   106
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   107
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   108
lemma Suc_if_eq: "(\<And>n. f (Suc n) = h n) \<Longrightarrow> f 0 = g \<Longrightarrow>
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   109
  f n = (if n = 0 then g else h (n - 1))"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   110
  by (case_tac n) simp_all
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   111
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   112
lemma Suc_clause: "(\<And>n. P n (Suc n)) \<Longrightarrow> n \<noteq> 0 \<Longrightarrow> P (n - 1) n"
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   113
  by (case_tac n) simp_all
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   114
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   115
text {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   116
  The rules above are built into a preprocessor that is plugged into
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   117
  the code generator. Since the preprocessor for introduction rules
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   118
  does not know anything about modes, some of the modes that worked
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   119
  for the canonical representation of natural numbers may no longer work.
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   120
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   121
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   122
(*<*)
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   123
setup {*
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   124
let
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   125
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   126
fun remove_suc thy thms =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   127
  let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   128
    val vname = Name.variant (map fst
29258
bce03c644efb canonical Term.add_var_names;
wenzelm
parents: 28969
diff changeset
   129
      (fold (Term.add_var_names o Thm.full_prop_of) thms [])) "x";
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   130
    val cv = cterm_of thy (Var ((vname, 0), HOLogic.natT));
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   131
    fun lhs_of th = snd (Thm.dest_comb
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   132
      (fst (Thm.dest_comb (snd (Thm.dest_comb (cprop_of th))))));
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   133
    fun rhs_of th = snd (Thm.dest_comb (snd (Thm.dest_comb (cprop_of th))));
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   134
    fun find_vars ct = (case term_of ct of
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   135
        (Const ("Suc", _) $ Var _) => [(cv, snd (Thm.dest_comb ct))]
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   136
      | _ $ _ =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   137
        let val (ct1, ct2) = Thm.dest_comb ct
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   138
        in 
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   139
          map (apfst (fn ct => Thm.capply ct ct2)) (find_vars ct1) @
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   140
          map (apfst (Thm.capply ct1)) (find_vars ct2)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   141
        end
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   142
      | _ => []);
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   143
    val eqs = maps
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   144
      (fn th => map (pair th) (find_vars (lhs_of th))) thms;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   145
    fun mk_thms (th, (ct, cv')) =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   146
      let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   147
        val th' =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   148
          Thm.implies_elim
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   149
           (Conv.fconv_rule (Thm.beta_conversion true)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   150
             (Drule.instantiate'
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   151
               [SOME (ctyp_of_term ct)] [SOME (Thm.cabs cv ct),
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   152
                 SOME (Thm.cabs cv' (rhs_of th)), NONE, SOME cv']
24222
a8a28c15c5cc new structure for code generator modules
haftmann
parents: 24195
diff changeset
   153
               @{thm Suc_if_eq})) (Thm.forall_intr cv' th)
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   154
      in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   155
        case map_filter (fn th'' =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   156
            SOME (th'', singleton
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   157
              (Variable.trade (K (fn [th'''] => [th''' RS th'])) (Variable.thm_context th'')) th'')
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   158
          handle THM _ => NONE) thms of
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   159
            [] => NONE
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   160
          | thps =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   161
              let val (ths1, ths2) = split_list thps
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   162
              in SOME (subtract Thm.eq_thm (th :: ths1) thms @ ths2) end
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   163
      end
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   164
  in case get_first mk_thms eqs of
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   165
      NONE => thms
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   166
    | SOME x => remove_suc thy x
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   167
  end;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   168
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   169
fun eqn_suc_preproc thy ths =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   170
  let
24222
a8a28c15c5cc new structure for code generator modules
haftmann
parents: 24195
diff changeset
   171
    val dest = fst o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of;
29270
0eade173f77e moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents: 29258
diff changeset
   172
    fun contains_suc t = member (op =) (OldTerm.term_consts t) @{const_name Suc};
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   173
  in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   174
    if forall (can dest) ths andalso
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   175
      exists (contains_suc o dest) ths
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   176
    then remove_suc thy ths else ths
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   177
  end;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   178
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   179
fun remove_suc_clause thy thms =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   180
  let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   181
    val vname = Name.variant (map fst
29258
bce03c644efb canonical Term.add_var_names;
wenzelm
parents: 28969
diff changeset
   182
      (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
   183
    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
   184
      | 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
   185
      | find_var _ = NONE;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   186
    fun find_thm th =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   187
      let val th' = Conv.fconv_rule ObjectLogic.atomize th
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   188
      in Option.map (pair (th, th')) (find_var (prop_of th')) end
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   189
  in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   190
    case get_first find_thm thms of
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   191
      NONE => thms
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   192
    | SOME ((th, th'), (Sucv, v)) =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   193
        let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   194
          val cert = cterm_of (Thm.theory_of_thm th);
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   195
          val th'' = ObjectLogic.rulify (Thm.implies_elim
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   196
            (Conv.fconv_rule (Thm.beta_conversion true)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   197
              (Drule.instantiate' []
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   198
                [SOME (cert (lambda v (Abs ("x", HOLogic.natT,
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   199
                   abstract_over (Sucv,
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   200
                     HOLogic.dest_Trueprop (prop_of th')))))),
24222
a8a28c15c5cc new structure for code generator modules
haftmann
parents: 24195
diff changeset
   201
                 SOME (cert v)] @{thm Suc_clause}))
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   202
            (Thm.forall_intr (cert v) th'))
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   203
        in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   204
          remove_suc_clause thy (map (fn th''' =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   205
            if (op = o pairself prop_of) (th''', th) then th'' else th''') thms)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   206
        end
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   207
  end;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   208
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   209
fun clause_suc_preproc thy ths =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   210
  let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   211
    val dest = fst o HOLogic.dest_mem o HOLogic.dest_Trueprop
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   212
  in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   213
    if forall (can (dest o concl_of)) ths andalso
29270
0eade173f77e moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents: 29258
diff changeset
   214
      exists (fn th => member (op =) (foldr OldTerm.add_term_consts
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   215
        [] (map_filter (try dest) (concl_of th :: prems_of th))) "Suc") ths
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   216
    then remove_suc_clause thy ths else ths
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   217
  end;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   218
28423
9fc3befd8191 clarified codegen interfaces
haftmann
parents: 28351
diff changeset
   219
fun lift f thy eqns1 =
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   220
  let
28522
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 28423
diff changeset
   221
    val eqns2 = burrow_fst Drule.zero_var_indexes_list eqns1;
28423
9fc3befd8191 clarified codegen interfaces
haftmann
parents: 28351
diff changeset
   222
    val thms3 = try (map fst
9fc3befd8191 clarified codegen interfaces
haftmann
parents: 28351
diff changeset
   223
      #> map (fn thm => thm RS @{thm meta_eq_to_obj_eq})
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   224
      #> f thy
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   225
      #> map (fn thm => thm RS @{thm eq_reflection})
28423
9fc3befd8191 clarified codegen interfaces
haftmann
parents: 28351
diff changeset
   226
      #> map (Conv.fconv_rule Drule.beta_eta_conversion)) eqns2;
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   227
    val thms4 = Option.map Drule.zero_var_indexes_list thms3;
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   228
  in case thms4
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   229
   of NONE => NONE
28423
9fc3befd8191 clarified codegen interfaces
haftmann
parents: 28351
diff changeset
   230
    | SOME thms4 => if Thm.eq_thms (map fst eqns2, thms4)
28522
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 28423
diff changeset
   231
        then NONE else SOME (map (apfst (AxClass.overload thy) o Code_Unit.mk_eqn thy) thms4)
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   232
  end
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   233
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   234
in
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   235
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   236
  Codegen.add_preprocessor eqn_suc_preproc
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   237
  #> Codegen.add_preprocessor clause_suc_preproc
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   238
  #> Code.add_functrans ("eqn_Suc", lift eqn_suc_preproc)
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   239
  #> Code.add_functrans ("clause_Suc", lift clause_suc_preproc)
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   240
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   241
end;
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   242
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   243
(*>*)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   244
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27557
diff changeset
   245
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   246
subsection {* Target language setup *}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   247
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   248
text {*
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   249
  For ML, we map @{typ nat} to target language integers, where we
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   250
  assert that values are always non-negative.
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   251
*}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   252
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   253
code_type nat
27496
f56684dd75a5 fix: using IntInf.int for SML
haftmann
parents: 27368
diff changeset
   254
  (SML "IntInf.int")
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   255
  (OCaml "Big'_int.big'_int")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   256
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   257
types_code
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   258
  nat ("int")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   259
attach (term_of) {*
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   260
val term_of_nat = HOLogic.mk_number HOLogic.natT;
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   261
*}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   262
attach (test) {*
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   263
fun gen_nat i =
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   264
  let val n = random_range 0 i
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   265
  in (n, fn () => term_of_nat n) end;
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   266
*}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   267
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   268
text {*
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   269
  For Haskell we define our own @{typ nat} type.  The reason
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   270
  is that we have to distinguish type class instances
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   271
  for @{typ nat} and @{typ int}.
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   272
*}
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   273
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   274
code_include Haskell "Nat" {*
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   275
newtype Nat = Nat Integer deriving (Show, Eq);
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   276
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   277
instance Num Nat where {
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   278
  fromInteger k = Nat (if k >= 0 then k else 0);
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   279
  Nat n + Nat m = Nat (n + m);
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   280
  Nat n - Nat m = fromInteger (n - m);
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   281
  Nat n * Nat m = Nat (n * m);
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   282
  abs n = n;
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   283
  signum _ = 1;
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   284
  negate n = error "negate Nat";
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   285
};
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   286
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   287
instance Ord Nat where {
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   288
  Nat n <= Nat m = n <= m;
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   289
  Nat n < Nat m = n < m;
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   290
};
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   291
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   292
instance Real Nat where {
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   293
  toRational (Nat n) = toRational n;
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   294
};
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   295
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   296
instance Enum Nat where {
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   297
  toEnum k = fromInteger (toEnum k);
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   298
  fromEnum (Nat n) = fromEnum n;
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   299
};
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   300
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   301
instance Integral Nat where {
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   302
  toInteger (Nat n) = n;
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   303
  divMod n m = quotRem n m;
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   304
  quotRem (Nat n) (Nat m) = (Nat k, Nat l) where (k, l) = quotRem n m;
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   305
};
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   306
*}
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   307
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   308
code_reserved Haskell Nat
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   309
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   310
code_type nat
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   311
  (Haskell "Nat")
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   312
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   313
code_instance nat :: eq
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   314
  (Haskell -)
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   315
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   316
text {*
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   317
  Natural numerals.
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   318
*}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   319
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   320
lemma [code inline, symmetric, code post]:
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   321
  "nat (number_of i) = number_nat_inst.number_of_nat i"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   322
  -- {* this interacts as desired with @{thm nat_number_of_def} *}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   323
  by (simp add: number_nat_inst.number_of_nat)
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   324
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   325
setup {*
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   326
  fold (Numeral.add_code @{const_name number_nat_inst.number_of_nat}
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   327
    true false) ["SML", "OCaml", "Haskell"]
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   328
*}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   329
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   330
text {*
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   331
  Since natural numbers are implemented
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   332
  using integers in ML, the coercion function @{const "of_nat"} of type
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   333
  @{typ "nat \<Rightarrow> int"} is simply implemented by the identity function.
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   334
  For the @{const "nat"} function for converting an integer to a natural
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   335
  number, we give a specific implementation using an ML function that
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   336
  returns its input value, provided that it is non-negative, and otherwise
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   337
  returns @{text "0"}.
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   338
*}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   339
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   340
definition
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   341
  int :: "nat \<Rightarrow> int"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   342
where
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28522
diff changeset
   343
  [code del]: "int = of_nat"
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   344
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28522
diff changeset
   345
lemma int_code' [code]:
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   346
  "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
   347
  unfolding int_nat_number_of [folded int_def] ..
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   348
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28522
diff changeset
   349
lemma nat_code' [code]:
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   350
  "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
   351
  unfolding nat_number_of_def number_of_is_id neg_def by simp
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   352
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   353
lemma of_nat_int [code unfold]:
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   354
  "of_nat = int" by (simp add: int_def)
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   355
declare of_nat_int [symmetric, code post]
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   356
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   357
code_const int
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   358
  (SML "_")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   359
  (OCaml "_")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   360
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   361
consts_code
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   362
  int ("(_)")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   363
  nat ("\<module>nat")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   364
attach {*
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   365
fun nat i = if i < 0 then 0 else i;
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   366
*}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   367
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   368
code_const nat
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   369
  (SML "IntInf.max/ (/0,/ _)")
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   370
  (OCaml "Big'_int.max'_big'_int/ Big'_int.zero'_big'_int")
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   371
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   372
text {* For Haskell, things are slightly different again. *}
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   373
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   374
code_const int and nat
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   375
  (Haskell "toInteger" and "fromInteger")
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   376
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   377
text {* Conversion from and to indices. *}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   378
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   379
code_const index_of_nat
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   380
  (SML "IntInf.toInt")
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   381
  (OCaml "Big'_int.int'_of'_big'_int")
27673
52056ddac194 fixed code generator setup
haftmann
parents: 27609
diff changeset
   382
  (Haskell "fromEnum")
25967
dd602eb20f3f fixed and tuned
haftmann
parents: 25931
diff changeset
   383
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   384
code_const nat_of_index
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   385
  (SML "IntInf.fromInt")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   386
  (OCaml "Big'_int.big'_int'_of'_int")
27673
52056ddac194 fixed code generator setup
haftmann
parents: 27609
diff changeset
   387
  (Haskell "toEnum")
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   388
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   389
text {* Using target language arithmetic operations whenever appropriate *}
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   390
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   391
code_const "op + \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   392
  (SML "IntInf.+ ((_), (_))")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   393
  (OCaml "Big'_int.add'_big'_int")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   394
  (Haskell infixl 6 "+")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   395
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   396
code_const "op * \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   397
  (SML "IntInf.* ((_), (_))")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   398
  (OCaml "Big'_int.mult'_big'_int")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   399
  (Haskell infixl 7 "*")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   400
26100
fbc60cd02ae2 using only an relation predicate to construct div and mod
haftmann
parents: 26009
diff changeset
   401
code_const divmod_aux
26009
b6a64fe38634 treating division by zero properly
haftmann
parents: 25967
diff changeset
   402
  (SML "IntInf.divMod/ ((_),/ (_))")
b6a64fe38634 treating division by zero properly
haftmann
parents: 25967
diff changeset
   403
  (OCaml "Big'_int.quomod'_big'_int")
b6a64fe38634 treating division by zero properly
haftmann
parents: 25967
diff changeset
   404
  (Haskell "divMod")
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   405
28346
b8390cd56b8f discontinued special treatment of op = vs. eq_class.eq
haftmann
parents: 28228
diff changeset
   406
code_const "eq_class.eq \<Colon> nat \<Rightarrow> nat \<Rightarrow> bool"
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   407
  (SML "!((_ : IntInf.int) = _)")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   408
  (OCaml "Big'_int.eq'_big'_int")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   409
  (Haskell infixl 4 "==")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   410
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   411
code_const "op \<le> \<Colon> nat \<Rightarrow> nat \<Rightarrow> bool"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   412
  (SML "IntInf.<= ((_), (_))")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   413
  (OCaml "Big'_int.le'_big'_int")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   414
  (Haskell infix 4 "<=")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   415
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   416
code_const "op < \<Colon> nat \<Rightarrow> nat \<Rightarrow> bool"
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   417
  (SML "IntInf.< ((_), (_))")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   418
  (OCaml "Big'_int.lt'_big'_int")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   419
  (Haskell infix 4 "<")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   420
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   421
consts_code
28522
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 28423
diff changeset
   422
  "0::nat"                     ("0")
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 28423
diff changeset
   423
  "1::nat"                     ("1")
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   424
  Suc                          ("(_ +/ 1)")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   425
  "op + \<Colon>  nat \<Rightarrow> nat \<Rightarrow> nat"   ("(_ +/ _)")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   426
  "op * \<Colon>  nat \<Rightarrow> nat \<Rightarrow> nat"   ("(_ */ _)")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   427
  "op \<le> \<Colon>  nat \<Rightarrow> nat \<Rightarrow> bool"  ("(_ <=/ _)")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   428
  "op < \<Colon>  nat \<Rightarrow> nat \<Rightarrow> bool"  ("(_ </ _)")
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   429
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   430
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27673
diff changeset
   431
text {* Evaluation *}
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27673
diff changeset
   432
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28522
diff changeset
   433
lemma [code, code del]:
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27673
diff changeset
   434
  "(Code_Eval.term_of \<Colon> nat \<Rightarrow> term) = Code_Eval.term_of" ..
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27673
diff changeset
   435
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27673
diff changeset
   436
code_const "Code_Eval.term_of \<Colon> nat \<Rightarrow> term"
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27673
diff changeset
   437
  (SML "HOLogic.mk'_number/ HOLogic.natT")
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27673
diff changeset
   438
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27673
diff changeset
   439
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   440
text {* Module names *}
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   441
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   442
code_modulename SML
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   443
  Nat Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   444
  Divides Integer
28683
59c01ec6cb8d more clever module name aliasses for code generation
haftmann
parents: 28562
diff changeset
   445
  Ring_and_Field Integer
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   446
  Efficient_Nat Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   447
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   448
code_modulename OCaml
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   449
  Nat Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   450
  Divides Integer
28683
59c01ec6cb8d more clever module name aliasses for code generation
haftmann
parents: 28562
diff changeset
   451
  Ring_and_Field Integer
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   452
  Efficient_Nat Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   453
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   454
code_modulename Haskell
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   455
  Nat Integer
24195
haftmann
parents: 23854
diff changeset
   456
  Divides Integer
28683
59c01ec6cb8d more clever module name aliasses for code generation
haftmann
parents: 28562
diff changeset
   457
  Ring_and_Field Integer
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   458
  Efficient_Nat Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   459
25931
b1d1ab3e5a2e streamlined and improved
haftmann
parents: 25919
diff changeset
   460
hide const int
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   461
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   462
end