src/HOL/Library/EfficientNat.thy
author haftmann
Tue, 09 May 2006 10:11:30 +0200
changeset 19603 9801b391c8b3
parent 19481 a6205c6203ea
child 19617 7cb4b67d4b97
permissions -rw-r--r--
added preprocs for CodegenTheorems
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
     1
(*  Title:      HOL/Library/EfficientNat.thy
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
     2
    ID:         $Id$
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
     4
*)
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
     5
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
     6
header {* Implementation of natural numbers by integers *}
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
     7
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
     8
theory EfficientNat
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
     9
imports Main
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    10
begin
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    11
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    12
text {*
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    13
When generating code for functions on natural numbers, the canonical
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    14
representation using @{term "0::nat"} and @{term "Suc"} is unsuitable for
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    15
computations involving large numbers. The efficiency of the generated
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    16
code can be improved drastically by implementing natural numbers by
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    17
integers. To do this, just include this theory.
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    18
*}
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    19
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    20
subsection {* Basic functions *}
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    21
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    22
text {*
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    23
The implementation of @{term "0::nat"} and @{term "Suc"} using the
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    24
ML integers is straightforward. Since natural numbers are implemented
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    25
using integers, the coercion function @{term "int"} of type
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    26
@{typ "nat \<Rightarrow> int"} is simply implemented by the identity function.
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    27
For the @{term "nat"} function for converting an integer to a natural
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    28
number, we give a specific implementation using an ML function that
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    29
returns its input value, provided that it is non-negative, and otherwise
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    30
returns @{text "0"}.
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    31
*}
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    32
16770
1f1b1fae30e4 Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents: 16295
diff changeset
    33
types_code
1f1b1fae30e4 Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents: 16295
diff changeset
    34
  nat ("int")
1f1b1fae30e4 Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents: 16295
diff changeset
    35
attach (term_of) {*
1f1b1fae30e4 Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents: 16295
diff changeset
    36
fun term_of_nat 0 = Const ("0", HOLogic.natT)
1f1b1fae30e4 Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents: 16295
diff changeset
    37
  | term_of_nat 1 = Const ("1", HOLogic.natT)
1f1b1fae30e4 Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents: 16295
diff changeset
    38
  | term_of_nat i = HOLogic.number_of_const HOLogic.natT $
19481
a6205c6203ea renamed HOLogic.mk_bin to mk_binum for consistency with dest_binum
paulson
parents: 19137
diff changeset
    39
      HOLogic.mk_binum (IntInf.fromInt i);
16770
1f1b1fae30e4 Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents: 16295
diff changeset
    40
*}
1f1b1fae30e4 Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents: 16295
diff changeset
    41
attach (test) {*
1f1b1fae30e4 Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents: 16295
diff changeset
    42
fun gen_nat i = random_range 0 i;
1f1b1fae30e4 Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents: 16295
diff changeset
    43
*}
1f1b1fae30e4 Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents: 16295
diff changeset
    44
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    45
consts_code
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    46
  0 :: nat ("0")
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    47
  Suc ("(_ + 1)")
16770
1f1b1fae30e4 Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents: 16295
diff changeset
    48
  nat ("\<module>nat")
1f1b1fae30e4 Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents: 16295
diff changeset
    49
attach {*
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    50
fun nat i = if i < 0 then 0 else i;
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    51
*}
16770
1f1b1fae30e4 Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents: 16295
diff changeset
    52
  int ("(_)")
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    53
18851
9502ce541f01 adaptions to codegen_package
haftmann
parents: 18757
diff changeset
    54
code_primconst nat
9502ce541f01 adaptions to codegen_package
haftmann
parents: 18757
diff changeset
    55
ml {*
9502ce541f01 adaptions to codegen_package
haftmann
parents: 18757
diff changeset
    56
fun nat i = if i < 0 then 0 : IntInf.int else i;
9502ce541f01 adaptions to codegen_package
haftmann
parents: 18757
diff changeset
    57
*}
9502ce541f01 adaptions to codegen_package
haftmann
parents: 18757
diff changeset
    58
haskell {*
9502ce541f01 adaptions to codegen_package
haftmann
parents: 18757
diff changeset
    59
nat i = if i < 0 then 0 else i
9502ce541f01 adaptions to codegen_package
haftmann
parents: 18757
diff changeset
    60
*}
9502ce541f01 adaptions to codegen_package
haftmann
parents: 18757
diff changeset
    61
9502ce541f01 adaptions to codegen_package
haftmann
parents: 18757
diff changeset
    62
code_syntax_tyco nat
9502ce541f01 adaptions to codegen_package
haftmann
parents: 18757
diff changeset
    63
  ml (target_atom "IntInf.int")
18757
f0d901bc0686 removed problematic keyword 'atom'
haftmann
parents: 18708
diff changeset
    64
  haskell (target_atom "Integer")
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 16900
diff changeset
    65
19137
f92919b141b2 change in codegen syntax
haftmann
parents: 19041
diff changeset
    66
code_syntax_const "0 :: nat"
18851
9502ce541f01 adaptions to codegen_package
haftmann
parents: 18757
diff changeset
    67
  ml (target_atom "(0:IntInf.int)")
18757
f0d901bc0686 removed problematic keyword 'atom'
haftmann
parents: 18708
diff changeset
    68
  haskell (target_atom "0")
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 16900
diff changeset
    69
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 16900
diff changeset
    70
code_syntax_const Suc
19041
1a8f08f9f8af some fixes
haftmann
parents: 18851
diff changeset
    71
  ml (target_atom "(__ + 1)")
1a8f08f9f8af some fixes
haftmann
parents: 18851
diff changeset
    72
  haskell (target_atom "(__ + 1)")
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 16900
diff changeset
    73
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    74
text {*
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    75
Case analysis on natural numbers is rephrased using a conditional
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    76
expression:
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    77
*}
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    78
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    79
lemma [code unfold]: "nat_case \<equiv> (\<lambda>f g n. if n = 0 then f else g (n - 1))"
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    80
  apply (rule eq_reflection ext)+
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    81
  apply (case_tac n)
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    82
  apply simp_all
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    83
  done
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    84
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    85
text {*
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    86
Most standard arithmetic functions on natural numbers are implemented
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    87
using their counterparts on the integers:
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    88
*}
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    89
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    90
lemma [code]: "m - n = nat (int m - int n)" by arith
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    91
lemma [code]: "m + n = nat (int m + int n)" by arith
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    92
lemma [code]: "m * n = nat (int m * int n)"
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    93
  by (simp add: zmult_int)
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    94
lemma [code]: "m div n = nat (int m div int n)"
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    95
  by (simp add: zdiv_int [symmetric])
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    96
lemma [code]: "m mod n = nat (int m mod int n)"
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    97
  by (simp add: zmod_int [symmetric])
16295
cd7a83dae4f9 Added code lemma for <
berghofe
parents: 15574
diff changeset
    98
lemma [code]: "(m < n) = (int m < int n)"
cd7a83dae4f9 Added code lemma for <
berghofe
parents: 15574
diff changeset
    99
  by simp
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   100
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   101
subsection {* Preprocessors *}
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   102
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   103
text {*
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   104
In contrast to @{term "Suc n"}, the term @{term "n + (1::nat)"} is no longer
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   105
a constructor term. Therefore, all occurrences of this term in a position
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   106
where a pattern is expected (i.e.\ on the left-hand side of a recursion
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   107
equation or in the arguments of an inductive relation in an introduction
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   108
rule) must be eliminated.
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   109
This can be accomplished by applying the following transformation rules:
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   110
*}
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   111
16900
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   112
theorem Suc_if_eq: "(\<And>n. f (Suc n) = h n) \<Longrightarrow> f 0 = g \<Longrightarrow>
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   113
  f n = (if n = 0 then g else h (n - 1))"
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   114
  by (case_tac n) simp_all
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   115
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   116
theorem Suc_clause: "(\<And>n. P n (Suc n)) \<Longrightarrow> n \<noteq> 0 \<Longrightarrow> P (n - 1) n"
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   117
  by (case_tac n) simp_all
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   118
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   119
text {*
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   120
The rules above are built into a preprocessor that is plugged into
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   121
the code generator. Since the preprocessor for introduction rules
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   122
does not know anything about modes, some of the modes that worked
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   123
for the canonical representation of natural numbers may no longer work.
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   124
*}
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   125
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   126
(*<*)
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   127
ML {*
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   128
val Suc_if_eq = thm "Suc_if_eq";
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   129
15396
0a36ccb79481 Preprocessors now transfer theorems to current theory in order to
berghofe
parents: 15323
diff changeset
   130
fun remove_suc thy thms =
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   131
  let
15396
0a36ccb79481 Preprocessors now transfer theorems to current theory in order to
berghofe
parents: 15323
diff changeset
   132
    val Suc_if_eq' = Thm.transfer thy Suc_if_eq;
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   133
    val vname = variant (map fst
16861
7446b4be013b tuned fold on terms;
wenzelm
parents: 16770
diff changeset
   134
      (fold (add_term_varnames o Thm.full_prop_of) thms [])) "x";
7446b4be013b tuned fold on terms;
wenzelm
parents: 16770
diff changeset
   135
    val cv = cterm_of Main.thy (Var ((vname, 0), HOLogic.natT));
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   136
    fun lhs_of th = snd (Thm.dest_comb
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   137
      (fst (Thm.dest_comb (snd (Thm.dest_comb (cprop_of th))))));
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   138
    fun rhs_of th = snd (Thm.dest_comb (snd (Thm.dest_comb (cprop_of th))));
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   139
    fun find_vars ct = (case term_of ct of
16900
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   140
        (Const ("Suc", _) $ Var _) => [(cv, snd (Thm.dest_comb ct))]
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   141
      | _ $ _ =>
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   142
        let val (ct1, ct2) = Thm.dest_comb ct
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   143
        in 
16900
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   144
          map (apfst (fn ct => Thm.capply ct ct2)) (find_vars ct1) @
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   145
          map (apfst (Thm.capply ct1)) (find_vars ct2)
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   146
        end
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   147
      | _ => []);
16900
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   148
    val eqs = List.concat (map
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   149
      (fn th => map (pair th) (find_vars (lhs_of th))) thms);
16900
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   150
    fun mk_thms (th, (ct, cv')) =
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   151
      let
16900
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   152
        val th' =
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   153
          Thm.implies_elim
15396
0a36ccb79481 Preprocessors now transfer theorems to current theory in order to
berghofe
parents: 15323
diff changeset
   154
           (Drule.fconv_rule (Thm.beta_conversion true)
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   155
             (Drule.instantiate'
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15396
diff changeset
   156
               [SOME (ctyp_of_term ct)] [SOME (Thm.cabs cv ct),
16900
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   157
                 SOME (Thm.cabs cv' (rhs_of th)), NONE, SOME cv']
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   158
               Suc_if_eq')) (Thm.forall_intr cv' th)
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   159
      in
16900
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   160
        case List.mapPartial (fn th'' =>
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   161
            SOME (th'', standard (Drule.freeze_all th'' RS th'))
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   162
          handle THM _ => NONE) thms of
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   163
            [] => NONE
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   164
          | thps =>
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   165
              let val (ths1, ths2) = ListPair.unzip thps
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   166
              in SOME (gen_rems eq_thm (thms, th :: ths1) @ ths2) end
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   167
      end
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   168
  in
16900
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   169
    case Library.get_first mk_thms eqs of
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   170
      NONE => thms
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   171
    | SOME x => remove_suc thy x
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   172
  end;
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   173
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   174
fun eqn_suc_preproc thy ths =
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   175
  let val dest = fst o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   176
  in
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   177
    if forall (can dest) ths andalso
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   178
      exists (fn th => "Suc" mem term_consts (dest th)) ths
15396
0a36ccb79481 Preprocessors now transfer theorems to current theory in order to
berghofe
parents: 15323
diff changeset
   179
    then remove_suc thy ths else ths
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   180
  end;
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   181
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   182
val Suc_clause = thm "Suc_clause";
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   183
15396
0a36ccb79481 Preprocessors now transfer theorems to current theory in order to
berghofe
parents: 15323
diff changeset
   184
fun remove_suc_clause thy thms =
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   185
  let
15396
0a36ccb79481 Preprocessors now transfer theorems to current theory in order to
berghofe
parents: 15323
diff changeset
   186
    val Suc_clause' = Thm.transfer thy Suc_clause;
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   187
    val vname = variant (map fst
16861
7446b4be013b tuned fold on terms;
wenzelm
parents: 16770
diff changeset
   188
      (fold (add_term_varnames o Thm.full_prop_of) thms [])) "x";
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15396
diff changeset
   189
    fun find_var (t as Const ("Suc", _) $ (v as Var _)) = SOME (t, v)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15396
diff changeset
   190
      | find_var (t $ u) = (case find_var t of NONE => find_var u | x => x)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15396
diff changeset
   191
      | find_var _ = NONE;
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   192
    fun find_thm th =
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   193
      let val th' = ObjectLogic.atomize_thm th
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   194
      in Option.map (pair (th, th')) (find_var (prop_of th')) end
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   195
  in
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   196
    case get_first find_thm thms of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15396
diff changeset
   197
      NONE => thms
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15396
diff changeset
   198
    | SOME ((th, th'), (Sucv, v)) =>
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   199
        let
16861
7446b4be013b tuned fold on terms;
wenzelm
parents: 16770
diff changeset
   200
          val cert = cterm_of (Thm.theory_of_thm th);
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   201
          val th'' = ObjectLogic.rulify (Thm.implies_elim
15396
0a36ccb79481 Preprocessors now transfer theorems to current theory in order to
berghofe
parents: 15323
diff changeset
   202
            (Drule.fconv_rule (Thm.beta_conversion true)
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   203
              (Drule.instantiate' []
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15396
diff changeset
   204
                [SOME (cert (lambda v (Abs ("x", HOLogic.natT,
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   205
                   abstract_over (Sucv,
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   206
                     HOLogic.dest_Trueprop (prop_of th')))))),
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15396
diff changeset
   207
                 SOME (cert v)] Suc_clause'))
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   208
            (Thm.forall_intr (cert v) th'))
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   209
        in
15396
0a36ccb79481 Preprocessors now transfer theorems to current theory in order to
berghofe
parents: 15323
diff changeset
   210
          remove_suc_clause thy (map (fn th''' =>
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   211
            if th''' = th then th'' else th''') thms)
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   212
        end
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   213
  end;
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   214
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   215
fun clause_suc_preproc thy ths =
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   216
  let val dest = fst o HOLogic.dest_mem o HOLogic.dest_Trueprop
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   217
  in
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   218
    if forall (can (dest o concl_of)) ths andalso
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   219
      exists (fn th => "Suc" mem foldr add_term_consts
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   220
        [] (List.mapPartial (try dest) (concl_of th :: prems_of th))) ths
15396
0a36ccb79481 Preprocessors now transfer theorems to current theory in order to
berghofe
parents: 15323
diff changeset
   221
    then remove_suc_clause thy ths else ths
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   222
  end;
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   223
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   224
val suc_preproc_setup =
19603
9801b391c8b3 added preprocs for CodegenTheorems
haftmann
parents: 19481
diff changeset
   225
  Codegen.add_preprocessor eqn_suc_preproc
9801b391c8b3 added preprocs for CodegenTheorems
haftmann
parents: 19481
diff changeset
   226
  #> Codegen.add_preprocessor clause_suc_preproc
9801b391c8b3 added preprocs for CodegenTheorems
haftmann
parents: 19481
diff changeset
   227
  #> Codegen.add_preprocessor eqn_suc_preproc
9801b391c8b3 added preprocs for CodegenTheorems
haftmann
parents: 19481
diff changeset
   228
  #> Codegen.add_preprocessor clause_suc_preproc
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   229
*}
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   230
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   231
setup suc_preproc_setup
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   232
(*>*)
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   233
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   234
end