src/HOL/Library/Efficient_Nat.thy
author haftmann
Thu, 19 Jul 2007 21:47:39 +0200
changeset 23854 688a8a7bcd4e
child 24195 7d1a16c77f7c
permissions -rw-r--r--
uniform naming conventions for CG theories
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
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     2
    ID:         $Id$
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     4
*)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     5
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     6
header {* Implementation of natural numbers by integers *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     7
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     8
theory Efficient_Nat
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     9
imports Main Pretty_Int
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    10
begin
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    11
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    12
text {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    13
When generating code for functions on natural numbers, the canonical
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    14
representation using @{term "0::nat"} and @{term "Suc"} is unsuitable for
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    15
computations involving large numbers. The efficiency of the generated
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    16
code can be improved drastically by implementing natural numbers by
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    17
integers. To do this, just include this theory.
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    18
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    19
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    20
subsection {* Logical rewrites *}
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
  An int-to-nat conversion
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    24
  restricted to non-negative ints (in contrast to @{const nat}).
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    25
  Note that this restriction has no logical relevance and
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    26
  is just a kind of proof hint -- nothing prevents you from 
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    27
  writing nonsense like @{term "nat_of_int (-4)"}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    28
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    29
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    30
definition
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    31
  nat_of_int :: "int \<Rightarrow> nat" where
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    32
  "k \<ge> 0 \<Longrightarrow> nat_of_int k = nat k"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    33
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    34
definition
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    35
  int' :: "nat \<Rightarrow> int" where
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    36
  "int' n = of_nat n"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    37
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    38
lemma int'_Suc [simp]: "int' (Suc n) = 1 + int' n"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    39
unfolding int'_def by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    40
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    41
lemma int'_add: "int' (m + n) = int' m + int' n"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    42
unfolding int'_def by (rule of_nat_add)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    43
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    44
lemma int'_mult: "int' (m * n) = int' m * int' n"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    45
unfolding int'_def by (rule of_nat_mult)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    46
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    47
lemma nat_of_int_of_number_of:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    48
  fixes k
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    49
  assumes "k \<ge> 0"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    50
  shows "number_of k = nat_of_int (number_of k)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    51
  unfolding nat_of_int_def [OF assms] nat_number_of_def number_of_is_id ..
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    52
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    53
lemma nat_of_int_of_number_of_aux:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    54
  fixes k
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    55
  assumes "Numeral.Pls \<le> k \<equiv> True"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    56
  shows "k \<ge> 0"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    57
  using assms unfolding Pls_def by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    58
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    59
lemma nat_of_int_int:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    60
  "nat_of_int (int' n) = n"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    61
  using nat_of_int_def int'_def by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    62
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    63
lemma eq_nat_of_int: "int' n = x \<Longrightarrow> n = nat_of_int x"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    64
by (erule subst, simp only: nat_of_int_int)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    65
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    66
text {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    67
  Case analysis on natural numbers is rephrased using a conditional
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    68
  expression:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    69
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    70
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    71
lemma [code unfold, code inline del]:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    72
  "nat_case \<equiv> (\<lambda>f g n. if n = 0 then f else g (n - 1))"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    73
proof -
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    74
  have rewrite: "\<And>f g n. nat_case f g n = (if n = 0 then f else g (n - 1))"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    75
  proof -
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    76
    fix f g n
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    77
    show "nat_case f g n = (if n = 0 then f else g (n - 1))"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    78
      by (cases n) simp_all
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    79
  qed
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    80
  show "nat_case \<equiv> (\<lambda>f g n. if n = 0 then f else g (n - 1))"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    81
    by (rule eq_reflection ext rewrite)+ 
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    82
qed
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    83
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    84
lemma [code inline]:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    85
  "nat_case = (\<lambda>f g n. if n = 0 then f else g (nat_of_int (int' n - 1)))"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    86
proof (rule ext)+
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    87
  fix f g n
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    88
  show "nat_case f g n = (if n = 0 then f else g (nat_of_int (int' n - 1)))"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    89
  by (cases n) (simp_all add: nat_of_int_int)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    90
qed
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    91
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    92
text {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    93
  Most standard arithmetic functions on natural numbers are implemented
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    94
  using their counterparts on the integers:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    95
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    96
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    97
lemma [code func]: "0 = nat_of_int 0"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    98
  by (simp add: nat_of_int_def)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    99
lemma [code func, code inline]:  "1 = nat_of_int 1"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   100
  by (simp add: nat_of_int_def)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   101
lemma [code func]: "Suc n = nat_of_int (int' n + 1)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   102
  by (simp add: eq_nat_of_int)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   103
lemma [code]: "m + n = nat (int' m + int' n)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   104
  by (simp add: int'_def nat_eq_iff2)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   105
lemma [code func, code inline]: "m + n = nat_of_int (int' m + int' n)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   106
  by (simp add: eq_nat_of_int int'_add)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   107
lemma [code, code inline]: "m - n = nat (int' m - int' n)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   108
  by (simp add: int'_def nat_eq_iff2 of_nat_diff)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   109
lemma [code]: "m * n = nat (int' m * int' n)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   110
  unfolding int'_def
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   111
  by (simp add: of_nat_mult [symmetric] del: of_nat_mult)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   112
lemma [code func, code inline]: "m * n = nat_of_int (int' m * int' n)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   113
  by (simp add: eq_nat_of_int int'_mult)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   114
lemma [code]: "m div n = nat (int' m div int' n)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   115
  unfolding int'_def zdiv_int [symmetric] by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   116
lemma [code func]: "m div n = fst (Divides.divmod m n)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   117
  unfolding divmod_def by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   118
lemma [code]: "m mod n = nat (int' m mod int' n)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   119
  unfolding int'_def zmod_int [symmetric] by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   120
lemma [code func]: "m mod n = snd (Divides.divmod m n)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   121
  unfolding divmod_def by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   122
lemma [code, code inline]: "(m < n) \<longleftrightarrow> (int' m < int' n)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   123
  unfolding int'_def by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   124
lemma [code func, code inline]: "(m \<le> n) \<longleftrightarrow> (int' m \<le> int' n)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   125
  unfolding int'_def by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   126
lemma [code func, code inline]: "m = n \<longleftrightarrow> int' m = int' n"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   127
  unfolding int'_def by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   128
lemma [code func]: "nat k = (if k < 0 then 0 else nat_of_int k)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   129
proof (cases "k < 0")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   130
  case True then show ?thesis by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   131
next
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   132
  case False then show ?thesis by (simp add: nat_of_int_def)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   133
qed
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   134
lemma [code func]:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   135
  "int_aux n i = (if int' n = 0 then i else int_aux (nat_of_int (int' n - 1)) (i + 1))"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   136
proof -
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   137
  have "0 < n \<Longrightarrow> int' n = 1 + int' (nat_of_int (int' n - 1))"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   138
  proof -
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   139
    assume prem: "n > 0"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   140
    then have "int' n - 1 \<ge> 0" unfolding int'_def by auto
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   141
    then have "nat_of_int (int' n - 1) = nat (int' n - 1)" by (simp add: nat_of_int_def)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   142
    with prem show "int' n = 1 + int' (nat_of_int (int' n - 1))" unfolding int'_def by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   143
  qed
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   144
  then show ?thesis unfolding int_aux_def int'_def by auto
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   145
qed
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   146
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   147
lemma div_nat_code [code func]:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   148
  "m div k = nat_of_int (fst (divAlg (int' m, int' k)))"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   149
  unfolding div_def [symmetric] int'_def zdiv_int [symmetric]
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   150
  unfolding int'_def [symmetric] nat_of_int_int ..
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   151
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   152
lemma mod_nat_code [code func]:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   153
  "m mod k = nat_of_int (snd (divAlg (int' m, int' k)))"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   154
  unfolding mod_def [symmetric] int'_def zmod_int [symmetric]
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   155
  unfolding int'_def [symmetric] nat_of_int_int ..
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   156
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   157
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   158
subsection {* Code generator setup for basic functions *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   159
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   160
text {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   161
  @{typ nat} is no longer a datatype but embedded into the integers.
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   162
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   163
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   164
code_datatype nat_of_int
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   165
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   166
code_type nat
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   167
  (SML "IntInf.int")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   168
  (OCaml "Big'_int.big'_int")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   169
  (Haskell "Integer")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   170
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   171
types_code
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   172
  nat ("int")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   173
attach (term_of) {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   174
val term_of_nat = HOLogic.mk_number HOLogic.natT o IntInf.fromInt;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   175
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   176
attach (test) {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   177
fun gen_nat i = random_range 0 i;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   178
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   179
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   180
consts_code
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   181
  "0 \<Colon> nat" ("0")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   182
  Suc ("(_ + 1)")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   183
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   184
text {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   185
  Since natural numbers are implemented
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   186
  using integers, the coercion function @{const "int"} of type
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   187
  @{typ "nat \<Rightarrow> int"} is simply implemented by the identity function,
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   188
  likewise @{const nat_of_int} of type @{typ "int \<Rightarrow> nat"}.
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   189
  For the @{const "nat"} function for converting an integer to a natural
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   190
  number, we give a specific implementation using an ML function that
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   191
  returns its input value, provided that it is non-negative, and otherwise
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   192
  returns @{text "0"}.
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   193
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   194
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   195
consts_code
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   196
  int' ("(_)")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   197
  nat ("\<module>nat")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   198
attach {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   199
fun nat i = if i < 0 then 0 else i;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   200
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   201
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   202
code_const int'
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   203
  (SML "_")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   204
  (OCaml "_")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   205
  (Haskell "_")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   206
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   207
code_const nat_of_int
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   208
  (SML "_")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   209
  (OCaml "_")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   210
  (Haskell "_")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   211
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   212
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   213
subsection {* Preprocessors *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   214
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   215
text {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   216
  Natural numerals should be expressed using @{const nat_of_int}.
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   217
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   218
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   219
lemmas [code inline del] = nat_number_of_def
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   220
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   221
ML {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   222
fun nat_of_int_of_number_of thy cts =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   223
  let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   224
    val simplify_less = Simplifier.rewrite 
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   225
      (HOL_basic_ss addsimps (@{thms less_numeral_code} @ @{thms less_eq_numeral_code}));
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   226
    fun mk_rew (t, ty) =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   227
      if ty = HOLogic.natT andalso IntInf.<= (0, HOLogic.dest_numeral t) then
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   228
        Thm.capply @{cterm "(op \<le>) Numeral.Pls"} (Thm.cterm_of thy t)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   229
        |> simplify_less
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   230
        |> (fn thm => @{thm nat_of_int_of_number_of_aux} OF [thm])
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   231
        |> (fn thm => @{thm nat_of_int_of_number_of} OF [thm])
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   232
        |> (fn thm => @{thm eq_reflection} OF [thm])
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   233
        |> SOME
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   234
      else NONE
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   235
  in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   236
    fold (HOLogic.add_numerals o Thm.term_of) cts []
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   237
    |> map_filter mk_rew
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   238
  end;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   239
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   240
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   241
setup {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   242
  CodegenData.add_inline_proc ("nat_of_int_of_number_of", nat_of_int_of_number_of)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   243
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   244
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   245
text {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   246
  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
   247
  a constructor term. Therefore, all occurrences of this term in a position
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   248
  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
   249
  equation or in the arguments of an inductive relation in an introduction
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   250
  rule) must be eliminated.
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   251
  This can be accomplished by applying the following transformation rules:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   252
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   253
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   254
theorem Suc_if_eq: "(\<And>n. f (Suc n) = h n) \<Longrightarrow> f 0 = g \<Longrightarrow>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   255
  f n = (if n = 0 then g else h (n - 1))"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   256
  by (case_tac n) simp_all
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   257
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   258
theorem Suc_clause: "(\<And>n. P n (Suc n)) \<Longrightarrow> n \<noteq> 0 \<Longrightarrow> P (n - 1) n"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   259
  by (case_tac n) simp_all
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   260
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   261
text {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   262
  The rules above are built into a preprocessor that is plugged into
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   263
  the code generator. Since the preprocessor for introduction rules
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   264
  does not know anything about modes, some of the modes that worked
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   265
  for the canonical representation of natural numbers may no longer work.
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   266
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   267
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   268
(*<*)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   269
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   270
ML {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   271
local
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   272
  val Suc_if_eq = thm "Suc_if_eq";
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   273
  val Suc_clause = thm "Suc_clause";
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   274
  fun contains_suc t = member (op =) (term_consts t) "Suc";
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   275
in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   276
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   277
fun remove_suc thy thms =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   278
  let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   279
    val Suc_if_eq' = Thm.transfer thy Suc_if_eq;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   280
    val vname = Name.variant (map fst
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   281
      (fold (Term.add_varnames o Thm.full_prop_of) thms [])) "x";
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   282
    val cv = cterm_of thy (Var ((vname, 0), HOLogic.natT));
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   283
    fun lhs_of th = snd (Thm.dest_comb
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   284
      (fst (Thm.dest_comb (snd (Thm.dest_comb (cprop_of th))))));
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   285
    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
   286
    fun find_vars ct = (case term_of ct of
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   287
        (Const ("Suc", _) $ Var _) => [(cv, snd (Thm.dest_comb ct))]
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   288
      | _ $ _ =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   289
        let val (ct1, ct2) = Thm.dest_comb ct
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   290
        in 
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   291
          map (apfst (fn ct => Thm.capply ct ct2)) (find_vars ct1) @
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   292
          map (apfst (Thm.capply ct1)) (find_vars ct2)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   293
        end
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   294
      | _ => []);
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   295
    val eqs = maps
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   296
      (fn th => map (pair th) (find_vars (lhs_of th))) thms;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   297
    fun mk_thms (th, (ct, cv')) =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   298
      let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   299
        val th' =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   300
          Thm.implies_elim
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   301
           (Conv.fconv_rule (Thm.beta_conversion true)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   302
             (Drule.instantiate'
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   303
               [SOME (ctyp_of_term ct)] [SOME (Thm.cabs cv ct),
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   304
                 SOME (Thm.cabs cv' (rhs_of th)), NONE, SOME cv']
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   305
               Suc_if_eq')) (Thm.forall_intr cv' th)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   306
      in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   307
        case map_filter (fn th'' =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   308
            SOME (th'', singleton
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   309
              (Variable.trade (K (fn [th'''] => [th''' RS th'])) (Variable.thm_context th'')) th'')
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   310
          handle THM _ => NONE) thms of
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   311
            [] => NONE
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   312
          | thps =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   313
              let val (ths1, ths2) = split_list thps
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   314
              in SOME (subtract Thm.eq_thm (th :: ths1) thms @ ths2) end
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   315
      end
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   316
  in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   317
    case get_first mk_thms eqs of
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   318
      NONE => thms
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   319
    | SOME x => remove_suc thy x
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   320
  end;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   321
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   322
fun eqn_suc_preproc thy ths =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   323
  let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   324
    val dest = fst o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   325
  in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   326
    if forall (can dest) ths andalso
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   327
      exists (contains_suc o dest) ths
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   328
    then remove_suc thy ths else ths
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   329
  end;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   330
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   331
fun remove_suc_clause thy thms =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   332
  let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   333
    val Suc_clause' = Thm.transfer thy Suc_clause;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   334
    val vname = Name.variant (map fst
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   335
      (fold (Term.add_varnames o Thm.full_prop_of) thms [])) "x";
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   336
    fun find_var (t as Const ("Suc", _) $ (v as Var _)) = SOME (t, v)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   337
      | 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
   338
      | find_var _ = NONE;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   339
    fun find_thm th =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   340
      let val th' = Conv.fconv_rule ObjectLogic.atomize th
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   341
      in Option.map (pair (th, th')) (find_var (prop_of th')) end
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   342
  in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   343
    case get_first find_thm thms of
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   344
      NONE => thms
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   345
    | SOME ((th, th'), (Sucv, v)) =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   346
        let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   347
          val cert = cterm_of (Thm.theory_of_thm th);
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   348
          val th'' = ObjectLogic.rulify (Thm.implies_elim
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   349
            (Conv.fconv_rule (Thm.beta_conversion true)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   350
              (Drule.instantiate' []
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   351
                [SOME (cert (lambda v (Abs ("x", HOLogic.natT,
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   352
                   abstract_over (Sucv,
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   353
                     HOLogic.dest_Trueprop (prop_of th')))))),
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   354
                 SOME (cert v)] Suc_clause'))
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   355
            (Thm.forall_intr (cert v) th'))
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   356
        in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   357
          remove_suc_clause thy (map (fn th''' =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   358
            if (op = o pairself prop_of) (th''', th) then th'' else th''') thms)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   359
        end
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   360
  end;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   361
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   362
fun clause_suc_preproc thy ths =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   363
  let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   364
    val dest = fst o HOLogic.dest_mem o HOLogic.dest_Trueprop
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   365
  in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   366
    if forall (can (dest o concl_of)) ths andalso
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   367
      exists (fn th => member (op =) (foldr add_term_consts
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   368
        [] (map_filter (try dest) (concl_of th :: prems_of th))) "Suc") ths
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   369
    then remove_suc_clause thy ths else ths
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   370
  end;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   371
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   372
end; (*local*)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   373
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   374
fun lift_obj_eq f thy =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   375
  map (fn thm => thm RS @{thm meta_eq_to_obj_eq})
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   376
  #> f thy
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   377
  #> map (fn thm => thm RS @{thm eq_reflection})
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   378
  #> map (Conv.fconv_rule Drule.beta_eta_conversion)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   379
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   380
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   381
setup {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   382
  Codegen.add_preprocessor eqn_suc_preproc
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   383
  #> Codegen.add_preprocessor clause_suc_preproc
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   384
  #> CodegenData.add_preproc ("eqn_Suc", lift_obj_eq eqn_suc_preproc)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   385
  #> CodegenData.add_preproc ("clause_Suc", lift_obj_eq clause_suc_preproc)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   386
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   387
(*>*)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   388
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   389
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   390
subsection {* Module names *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   391
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   392
code_modulename SML
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   393
  Nat Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   394
  Divides Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   395
  Efficient_Nat Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   396
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   397
code_modulename OCaml
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   398
  Nat Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   399
  Divides Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   400
  Efficient_Nat Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   401
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   402
code_modulename Haskell
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   403
  Nat Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   404
  Efficient_Nat Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   405
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   406
hide const nat_of_int int'
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   407
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   408
end