src/HOL/Library/Efficient_Nat.thy
author haftmann
Tue, 25 Sep 2007 21:08:34 +0200
changeset 24715 f96d86cdbe5a
parent 24630 351a308ab58d
child 24994 c385c4eabb3b
permissions -rw-r--r--
Efficient_Nat and Pretty_Int integrated with ML_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
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
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    35
  int_of_nat :: "nat \<Rightarrow> int" where
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
    36
  "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]:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    77
  "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
    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
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    85
  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
    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
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
   143
lemma [code, 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
   144
  unfolding int_of_nat_def by simp
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
   145
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
   146
lemma [code func, code inline]: "(m \<le> n) \<longleftrightarrow> (int_of_nat m \<le> int_of_nat n)"
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
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
   168
lemma ml_int_of_nat_code [code func, code inline]:
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
   169
  "ml_int_of_nat n = ml_int_of_int (int_of_nat n)"
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
   170
  unfolding ml_int_of_nat_def int_of_nat_def by simp
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   171
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
   172
lemma nat_of_mlt_int_code [code func, code inline]:
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
   173
  "nat_of_ml_int k = nat (int_of_ml_int k)"
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
   174
  unfolding nat_of_ml_int_def by simp
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   175
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   176
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   177
subsection {* Code generator setup for basic functions *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   178
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   179
text {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   180
  @{typ nat} is no longer a datatype but embedded into the integers.
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   181
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   182
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   183
code_type nat
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24423
diff changeset
   184
  (SML "int")
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   185
  (OCaml "Big'_int.big'_int")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   186
  (Haskell "Integer")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   187
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   188
types_code
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   189
  nat ("int")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   190
attach (term_of) {*
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24423
diff changeset
   191
val term_of_nat = HOLogic.mk_number HOLogic.natT;
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   192
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   193
attach (test) {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   194
fun gen_nat i = random_range 0 i;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   195
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   196
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   197
consts_code
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   198
  "0 \<Colon> nat" ("0")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   199
  Suc ("(_ + 1)")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   200
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   201
text {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   202
  Since natural numbers are implemented
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   203
  using integers, the coercion function @{const "int"} of type
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   204
  @{typ "nat \<Rightarrow> int"} is simply implemented by the identity function,
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   205
  likewise @{const nat_of_int} of type @{typ "int \<Rightarrow> nat"}.
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   206
  For the @{const "nat"} function for converting an integer to a natural
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   207
  number, we give a specific implementation using an ML function that
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   208
  returns its input value, provided that it is non-negative, and otherwise
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   209
  returns @{text "0"}.
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   210
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   211
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   212
consts_code
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
   213
  int_of_nat ("(_)")
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   214
  nat ("\<module>nat")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   215
attach {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   216
fun nat i = if i < 0 then 0 else i;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   217
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   218
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
   219
code_const int_of_nat
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   220
  (SML "_")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   221
  (OCaml "_")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   222
  (Haskell "_")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   223
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   224
code_const nat_of_int
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   225
  (SML "_")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   226
  (OCaml "_")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   227
  (Haskell "_")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   228
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   229
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   230
subsection {* Preprocessors *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   231
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   232
text {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   233
  Natural numerals should be expressed using @{const nat_of_int}.
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   234
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   235
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   236
lemmas [code inline del] = nat_number_of_def
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   237
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   238
ML {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   239
fun nat_of_int_of_number_of thy cts =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   240
  let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   241
    val simplify_less = Simplifier.rewrite 
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   242
      (HOL_basic_ss addsimps (@{thms less_numeral_code} @ @{thms less_eq_numeral_code}));
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   243
    fun mk_rew (t, ty) =
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24423
diff changeset
   244
      if ty = HOLogic.natT andalso 0 <= HOLogic.dest_numeral t then
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   245
        Thm.capply @{cterm "(op \<le>) Numeral.Pls"} (Thm.cterm_of thy t)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   246
        |> simplify_less
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   247
        |> (fn thm => @{thm nat_of_int_of_number_of_aux} OF [thm])
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   248
        |> (fn thm => @{thm nat_of_int_of_number_of} OF [thm])
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   249
        |> (fn thm => @{thm eq_reflection} OF [thm])
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   250
        |> SOME
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   251
      else NONE
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   252
  in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   253
    fold (HOLogic.add_numerals o Thm.term_of) cts []
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   254
    |> map_filter mk_rew
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   255
  end;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   256
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   257
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   258
setup {*
24222
a8a28c15c5cc new structure for code generator modules
haftmann
parents: 24195
diff changeset
   259
  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
   260
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   261
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   262
text {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   263
  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
   264
  a constructor term. Therefore, all occurrences of this term in a position
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   265
  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
   266
  equation or in the arguments of an inductive relation in an introduction
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   267
  rule) must be eliminated.
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   268
  This can be accomplished by applying the following transformation rules:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   269
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   270
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   271
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
   272
  f n = (if n = 0 then g else h (n - 1))"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   273
  by (case_tac n) simp_all
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   274
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   275
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
   276
  by (case_tac n) simp_all
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   277
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   278
text {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   279
  The rules above are built into a preprocessor that is plugged into
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   280
  the code generator. Since the preprocessor for introduction rules
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   281
  does not know anything about modes, some of the modes that worked
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   282
  for the canonical representation of natural numbers may no longer work.
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   283
*}
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
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   287
ML {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   288
fun remove_suc thy thms =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   289
  let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   290
    val vname = Name.variant (map fst
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   291
      (fold (Term.add_varnames o Thm.full_prop_of) thms [])) "x";
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   292
    val cv = cterm_of thy (Var ((vname, 0), HOLogic.natT));
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   293
    fun lhs_of th = snd (Thm.dest_comb
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   294
      (fst (Thm.dest_comb (snd (Thm.dest_comb (cprop_of th))))));
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   295
    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
   296
    fun find_vars ct = (case term_of ct of
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   297
        (Const ("Suc", _) $ Var _) => [(cv, snd (Thm.dest_comb ct))]
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   298
      | _ $ _ =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   299
        let val (ct1, ct2) = Thm.dest_comb ct
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   300
        in 
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   301
          map (apfst (fn ct => Thm.capply ct ct2)) (find_vars ct1) @
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   302
          map (apfst (Thm.capply ct1)) (find_vars ct2)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   303
        end
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   304
      | _ => []);
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   305
    val eqs = maps
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   306
      (fn th => map (pair th) (find_vars (lhs_of th))) thms;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   307
    fun mk_thms (th, (ct, cv')) =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   308
      let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   309
        val th' =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   310
          Thm.implies_elim
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   311
           (Conv.fconv_rule (Thm.beta_conversion true)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   312
             (Drule.instantiate'
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   313
               [SOME (ctyp_of_term ct)] [SOME (Thm.cabs cv ct),
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   314
                 SOME (Thm.cabs cv' (rhs_of th)), NONE, SOME cv']
24222
a8a28c15c5cc new structure for code generator modules
haftmann
parents: 24195
diff changeset
   315
               @{thm Suc_if_eq})) (Thm.forall_intr cv' th)
23854
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 map_filter (fn th'' =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   318
            SOME (th'', singleton
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   319
              (Variable.trade (K (fn [th'''] => [th''' RS th'])) (Variable.thm_context th'')) th'')
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   320
          handle THM _ => NONE) thms of
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   321
            [] => NONE
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   322
          | thps =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   323
              let val (ths1, ths2) = split_list thps
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   324
              in SOME (subtract Thm.eq_thm (th :: ths1) thms @ ths2) end
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   325
      end
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   326
  in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   327
    case get_first mk_thms eqs of
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   328
      NONE => thms
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   329
    | SOME x => remove_suc thy x
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   330
  end;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   331
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   332
fun eqn_suc_preproc thy ths =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   333
  let
24222
a8a28c15c5cc new structure for code generator modules
haftmann
parents: 24195
diff changeset
   334
    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
   335
    fun contains_suc t = member (op =) (term_consts t) @{const_name Suc};
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   336
  in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   337
    if forall (can dest) ths andalso
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   338
      exists (contains_suc o dest) ths
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   339
    then remove_suc thy ths else ths
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
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   342
fun remove_suc_clause thy thms =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   343
  let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   344
    val vname = Name.variant (map fst
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   345
      (fold (Term.add_varnames o Thm.full_prop_of) thms [])) "x";
24222
a8a28c15c5cc new structure for code generator modules
haftmann
parents: 24195
diff changeset
   346
    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
   347
      | 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
   348
      | find_var _ = NONE;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   349
    fun find_thm th =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   350
      let val th' = Conv.fconv_rule ObjectLogic.atomize th
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   351
      in Option.map (pair (th, th')) (find_var (prop_of th')) end
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   352
  in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   353
    case get_first find_thm thms of
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   354
      NONE => thms
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   355
    | SOME ((th, th'), (Sucv, v)) =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   356
        let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   357
          val cert = cterm_of (Thm.theory_of_thm th);
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   358
          val th'' = ObjectLogic.rulify (Thm.implies_elim
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   359
            (Conv.fconv_rule (Thm.beta_conversion true)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   360
              (Drule.instantiate' []
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   361
                [SOME (cert (lambda v (Abs ("x", HOLogic.natT,
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   362
                   abstract_over (Sucv,
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   363
                     HOLogic.dest_Trueprop (prop_of th')))))),
24222
a8a28c15c5cc new structure for code generator modules
haftmann
parents: 24195
diff changeset
   364
                 SOME (cert v)] @{thm Suc_clause}))
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   365
            (Thm.forall_intr (cert v) th'))
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   366
        in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   367
          remove_suc_clause thy (map (fn th''' =>
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   368
            if (op = o pairself prop_of) (th''', th) then th'' else th''') thms)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   369
        end
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
fun clause_suc_preproc thy ths =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   373
  let
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   374
    val dest = fst o HOLogic.dest_mem o HOLogic.dest_Trueprop
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   375
  in
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   376
    if forall (can (dest o concl_of)) ths andalso
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   377
      exists (fn th => member (op =) (foldr add_term_consts
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   378
        [] (map_filter (try dest) (concl_of th :: prems_of th))) "Suc") ths
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   379
    then remove_suc_clause thy ths else ths
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   380
  end;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   381
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   382
fun lift_obj_eq f thy =
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   383
  map (fn thm => thm RS @{thm meta_eq_to_obj_eq})
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   384
  #> f thy
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   385
  #> map (fn thm => thm RS @{thm eq_reflection})
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   386
  #> map (Conv.fconv_rule Drule.beta_eta_conversion)
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
setup {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   390
  Codegen.add_preprocessor eqn_suc_preproc
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   391
  #> Codegen.add_preprocessor clause_suc_preproc
24222
a8a28c15c5cc new structure for code generator modules
haftmann
parents: 24195
diff changeset
   392
  #> Code.add_preproc ("eqn_Suc", lift_obj_eq eqn_suc_preproc)
a8a28c15c5cc new structure for code generator modules
haftmann
parents: 24195
diff changeset
   393
  #> Code.add_preproc ("clause_Suc", lift_obj_eq clause_suc_preproc)
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   394
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   395
(*>*)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   396
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   397
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   398
subsection {* Module names *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   399
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   400
code_modulename SML
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   401
  Nat Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   402
  Divides Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   403
  Efficient_Nat Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   404
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   405
code_modulename OCaml
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   406
  Nat Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   407
  Divides Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   408
  Efficient_Nat Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   409
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   410
code_modulename Haskell
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   411
  Nat Integer
24195
haftmann
parents: 23854
diff changeset
   412
  Divides Integer
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   413
  Efficient_Nat Integer
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   414
24715
f96d86cdbe5a Efficient_Nat and Pretty_Int integrated with ML_Int
haftmann
parents: 24630
diff changeset
   415
hide const nat_of_int int_of_nat
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   416
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   417
end