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