src/HOL/Library/EfficientNat.thy
author avigad
Mon, 25 Jul 2005 18:54:49 +0200
changeset 16908 d374530bfaaa
parent 16900 e294033d1c0f
child 18702 7dc7dcd63224
permissions -rw-r--r--
Added two new theories to HOL/Library: SetsAndFunctions.thy and BigO.thy
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 $
1f1b1fae30e4 Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents: 16295
diff changeset
    39
      HOLogic.mk_bin (IntInf.fromInt i);
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
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    54
text {*
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    55
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
    56
expression:
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    57
*}
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    58
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    59
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
    60
  apply (rule eq_reflection ext)+
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    61
  apply (case_tac n)
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    62
  apply simp_all
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    63
  done
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    64
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    65
text {*
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    66
Most standard arithmetic functions on natural numbers are implemented
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    67
using their counterparts on the integers:
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    68
*}
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    69
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    70
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
    71
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
    72
lemma [code]: "m * n = nat (int m * int n)"
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    73
  by (simp add: zmult_int)
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    74
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
    75
  by (simp add: zdiv_int [symmetric])
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    76
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
    77
  by (simp add: zmod_int [symmetric])
16295
cd7a83dae4f9 Added code lemma for <
berghofe
parents: 15574
diff changeset
    78
lemma [code]: "(m < n) = (int m < int n)"
cd7a83dae4f9 Added code lemma for <
berghofe
parents: 15574
diff changeset
    79
  by simp
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    80
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    81
subsection {* Preprocessors *}
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    82
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    83
text {*
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    84
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
    85
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
    86
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
    87
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
    88
rule) must be eliminated.
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    89
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
    90
*}
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    91
16900
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
    92
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
    93
  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
    94
  by (case_tac n) simp_all
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    95
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    96
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
    97
  by (case_tac n) simp_all
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    98
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
    99
text {*
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   100
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
   101
the code generator. Since the preprocessor for introduction rules
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   102
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
   103
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
   104
*}
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   105
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   106
(*<*)
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   107
ML {*
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   108
val Suc_if_eq = thm "Suc_if_eq";
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   109
15396
0a36ccb79481 Preprocessors now transfer theorems to current theory in order to
berghofe
parents: 15323
diff changeset
   110
fun remove_suc thy thms =
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   111
  let
15396
0a36ccb79481 Preprocessors now transfer theorems to current theory in order to
berghofe
parents: 15323
diff changeset
   112
    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
   113
    val vname = variant (map fst
16861
7446b4be013b tuned fold on terms;
wenzelm
parents: 16770
diff changeset
   114
      (fold (add_term_varnames o Thm.full_prop_of) thms [])) "x";
7446b4be013b tuned fold on terms;
wenzelm
parents: 16770
diff changeset
   115
    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
   116
    fun lhs_of th = snd (Thm.dest_comb
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   117
      (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
   118
    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
   119
    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
   120
        (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
   121
      | _ $ _ =>
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   122
        let val (ct1, ct2) = Thm.dest_comb ct
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   123
        in 
16900
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   124
          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
   125
          map (apfst (Thm.capply ct1)) (find_vars ct2)
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   126
        end
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   127
      | _ => []);
16900
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   128
    val eqs = List.concat (map
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   129
      (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
   130
    fun mk_thms (th, (ct, cv')) =
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   131
      let
16900
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   132
        val th' =
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   133
          Thm.implies_elim
15396
0a36ccb79481 Preprocessors now transfer theorems to current theory in order to
berghofe
parents: 15323
diff changeset
   134
           (Drule.fconv_rule (Thm.beta_conversion true)
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   135
             (Drule.instantiate'
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15396
diff changeset
   136
               [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
   137
                 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
   138
               Suc_if_eq')) (Thm.forall_intr cv' th)
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   139
      in
16900
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   140
        case List.mapPartial (fn th'' =>
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   141
            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
   142
          handle THM _ => NONE) thms of
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   143
            [] => NONE
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   144
          | thps =>
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   145
              let val (ths1, ths2) = ListPair.unzip thps
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   146
              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
   147
      end
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   148
  in
16900
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   149
    case Library.get_first mk_thms eqs of
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   150
      NONE => thms
e294033d1c0f Rewrote function remove_suc, since it failed on some equations
berghofe
parents: 16861
diff changeset
   151
    | SOME x => remove_suc thy x
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   152
  end;
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   153
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   154
fun eqn_suc_preproc thy ths =
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   155
  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
   156
  in
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   157
    if forall (can dest) ths andalso
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   158
      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
   159
    then remove_suc thy ths else ths
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   160
  end;
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   161
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   162
val Suc_clause = thm "Suc_clause";
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   163
15396
0a36ccb79481 Preprocessors now transfer theorems to current theory in order to
berghofe
parents: 15323
diff changeset
   164
fun remove_suc_clause thy thms =
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   165
  let
15396
0a36ccb79481 Preprocessors now transfer theorems to current theory in order to
berghofe
parents: 15323
diff changeset
   166
    val Suc_clause' = Thm.transfer thy Suc_clause;
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   167
    val vname = variant (map fst
16861
7446b4be013b tuned fold on terms;
wenzelm
parents: 16770
diff changeset
   168
      (fold (add_term_varnames o Thm.full_prop_of) thms [])) "x";
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15396
diff changeset
   169
    fun find_var (t as Const ("Suc", _) $ (v as Var _)) = SOME (t, v)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15396
diff changeset
   170
      | 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
   171
      | find_var _ = NONE;
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   172
    fun find_thm th =
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   173
      let val th' = ObjectLogic.atomize_thm th
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   174
      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
   175
  in
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   176
    case get_first find_thm thms of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15396
diff changeset
   177
      NONE => thms
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15396
diff changeset
   178
    | SOME ((th, th'), (Sucv, v)) =>
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   179
        let
16861
7446b4be013b tuned fold on terms;
wenzelm
parents: 16770
diff changeset
   180
          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
   181
          val th'' = ObjectLogic.rulify (Thm.implies_elim
15396
0a36ccb79481 Preprocessors now transfer theorems to current theory in order to
berghofe
parents: 15323
diff changeset
   182
            (Drule.fconv_rule (Thm.beta_conversion true)
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   183
              (Drule.instantiate' []
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15396
diff changeset
   184
                [SOME (cert (lambda v (Abs ("x", HOLogic.natT,
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   185
                   abstract_over (Sucv,
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   186
                     HOLogic.dest_Trueprop (prop_of th')))))),
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15396
diff changeset
   187
                 SOME (cert v)] Suc_clause'))
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   188
            (Thm.forall_intr (cert v) th'))
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   189
        in
15396
0a36ccb79481 Preprocessors now transfer theorems to current theory in order to
berghofe
parents: 15323
diff changeset
   190
          remove_suc_clause thy (map (fn th''' =>
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   191
            if th''' = th then th'' else th''') thms)
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   192
        end
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   193
  end;
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   194
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   195
fun clause_suc_preproc thy ths =
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   196
  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
   197
  in
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   198
    if forall (can (dest o concl_of)) ths andalso
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   199
      exists (fn th => "Suc" mem foldr add_term_consts
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   200
        [] (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
   201
    then remove_suc_clause thy ths else ths
15323
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   202
  end;
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   203
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   204
val suc_preproc_setup =
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   205
  [Codegen.add_preprocessor eqn_suc_preproc,
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   206
   Codegen.add_preprocessor clause_suc_preproc];
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   207
*}
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   208
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   209
setup suc_preproc_setup
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   210
(*>*)
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   211
6c10fe1c0e17 Code generator plug-in for implementing natural numbers by integers.
berghofe
parents:
diff changeset
   212
end