src/HOL/Code_Numeral.thy
author wenzelm
Sun, 12 Mar 2017 18:50:02 +0100
changeset 65202 187277b77d50
parent 64994 6e4c05e8edbb
child 66190 a41435469559
permissions -rw-r--r--
suppress vacuous messages;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
     1
(*  Title:      HOL/Code_Numeral.thy
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
     3
*)
24999
haftmann
parents:
diff changeset
     4
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
     5
section \<open>Numeric types for code generation onto target language numerals only\<close>
24999
haftmann
parents:
diff changeset
     6
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
     7
theory Code_Numeral
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
     8
imports Nat_Transfer Divides Lifting
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
     9
begin
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    10
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
    11
subsection \<open>Type of target language integers\<close>
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    12
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60868
diff changeset
    13
typedef integer = "UNIV :: int set"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    14
  morphisms int_of_integer integer_of_int ..
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    15
59487
adaa430fc0f7 default abstypes and default abstract equations make technical (no_code) annotation superfluous
haftmann
parents: 58889
diff changeset
    16
setup_lifting type_definition_integer
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    17
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    18
lemma integer_eq_iff:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    19
  "k = l \<longleftrightarrow> int_of_integer k = int_of_integer l"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    20
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    21
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    22
lemma integer_eqI:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    23
  "int_of_integer k = int_of_integer l \<Longrightarrow> k = l"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    24
  using integer_eq_iff [of k l] by simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    25
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    26
lemma int_of_integer_integer_of_int [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    27
  "int_of_integer (integer_of_int k) = k"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    28
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    29
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    30
lemma integer_of_int_int_of_integer [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    31
  "integer_of_int (int_of_integer k) = k"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    32
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    33
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    34
instantiation integer :: ring_1
24999
haftmann
parents:
diff changeset
    35
begin
haftmann
parents:
diff changeset
    36
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    37
lift_definition zero_integer :: integer
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    38
  is "0 :: int"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    39
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    40
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    41
declare zero_integer.rep_eq [simp]
24999
haftmann
parents:
diff changeset
    42
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    43
lift_definition one_integer :: integer
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    44
  is "1 :: int"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    45
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    46
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    47
declare one_integer.rep_eq [simp]
24999
haftmann
parents:
diff changeset
    48
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    49
lift_definition plus_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    50
  is "plus :: int \<Rightarrow> int \<Rightarrow> int"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    51
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    52
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    53
declare plus_integer.rep_eq [simp]
24999
haftmann
parents:
diff changeset
    54
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    55
lift_definition uminus_integer :: "integer \<Rightarrow> integer"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    56
  is "uminus :: int \<Rightarrow> int"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    57
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    58
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    59
declare uminus_integer.rep_eq [simp]
24999
haftmann
parents:
diff changeset
    60
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    61
lift_definition minus_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    62
  is "minus :: int \<Rightarrow> int \<Rightarrow> int"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    63
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    64
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    65
declare minus_integer.rep_eq [simp]
24999
haftmann
parents:
diff changeset
    66
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    67
lift_definition times_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    68
  is "times :: int \<Rightarrow> int \<Rightarrow> int"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    69
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    70
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    71
declare times_integer.rep_eq [simp]
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
    72
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    73
instance proof
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    74
qed (transfer, simp add: algebra_simps)+
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    75
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    76
end
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    77
64241
430d74089d4d transfer rules for divides relation on integer and natural
haftmann
parents: 64178
diff changeset
    78
instance integer :: Rings.dvd ..
430d74089d4d transfer rules for divides relation on integer and natural
haftmann
parents: 64178
diff changeset
    79
430d74089d4d transfer rules for divides relation on integer and natural
haftmann
parents: 64178
diff changeset
    80
lemma [transfer_rule]:
430d74089d4d transfer rules for divides relation on integer and natural
haftmann
parents: 64178
diff changeset
    81
  "rel_fun pcr_integer (rel_fun pcr_integer HOL.iff) Rings.dvd Rings.dvd"
430d74089d4d transfer rules for divides relation on integer and natural
haftmann
parents: 64178
diff changeset
    82
  unfolding dvd_def by transfer_prover
430d74089d4d transfer rules for divides relation on integer and natural
haftmann
parents: 64178
diff changeset
    83
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    84
lemma [transfer_rule]:
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55736
diff changeset
    85
  "rel_fun HOL.eq pcr_integer (of_nat :: nat \<Rightarrow> int) (of_nat :: nat \<Rightarrow> integer)"
64178
12e6c3bbb488 transfer lifting rule for numeral
haftmann
parents: 63950
diff changeset
    86
  by (rule transfer_rule_of_nat) transfer_prover+
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    87
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    88
lemma [transfer_rule]:
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55736
diff changeset
    89
  "rel_fun HOL.eq pcr_integer (\<lambda>k :: int. k :: int) (of_int :: int \<Rightarrow> integer)"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    90
proof -
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55736
diff changeset
    91
  have "rel_fun HOL.eq pcr_integer (of_int :: int \<Rightarrow> int) (of_int :: int \<Rightarrow> integer)"
64178
12e6c3bbb488 transfer lifting rule for numeral
haftmann
parents: 63950
diff changeset
    92
    by (rule transfer_rule_of_int) transfer_prover+
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    93
  then show ?thesis by (simp add: id_def)
24999
haftmann
parents:
diff changeset
    94
qed
haftmann
parents:
diff changeset
    95
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    96
lemma [transfer_rule]:
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55736
diff changeset
    97
  "rel_fun HOL.eq pcr_integer (numeral :: num \<Rightarrow> int) (numeral :: num \<Rightarrow> integer)"
64178
12e6c3bbb488 transfer lifting rule for numeral
haftmann
parents: 63950
diff changeset
    98
  by (rule transfer_rule_numeral) transfer_prover+
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    99
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   100
lemma [transfer_rule]:
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55736
diff changeset
   101
  "rel_fun HOL.eq (rel_fun HOL.eq pcr_integer) (Num.sub :: _ \<Rightarrow> _ \<Rightarrow> int) (Num.sub :: _ \<Rightarrow> _ \<Rightarrow> integer)"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   102
  by (unfold Num.sub_def [abs_def]) transfer_prover
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   103
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   104
lemma int_of_integer_of_nat [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   105
  "int_of_integer (of_nat n) = of_nat n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   106
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   107
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   108
lift_definition integer_of_nat :: "nat \<Rightarrow> integer"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   109
  is "of_nat :: nat \<Rightarrow> int"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   110
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   111
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   112
lemma integer_of_nat_eq_of_nat [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   113
  "integer_of_nat = of_nat"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   114
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   115
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   116
lemma int_of_integer_integer_of_nat [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   117
  "int_of_integer (integer_of_nat n) = of_nat n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   118
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   119
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   120
lift_definition nat_of_integer :: "integer \<Rightarrow> nat"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   121
  is Int.nat
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   122
  .
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   123
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   124
lemma nat_of_integer_of_nat [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   125
  "nat_of_integer (of_nat n) = n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   126
  by transfer simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   127
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   128
lemma int_of_integer_of_int [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   129
  "int_of_integer (of_int k) = k"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   130
  by transfer simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   131
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   132
lemma nat_of_integer_integer_of_nat [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   133
  "nat_of_integer (integer_of_nat n) = n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   134
  by transfer simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   135
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   136
lemma integer_of_int_eq_of_int [simp, code_abbrev]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   137
  "integer_of_int = of_int"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   138
  by transfer (simp add: fun_eq_iff)
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   139
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   140
lemma of_int_integer_of [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   141
  "of_int (int_of_integer k) = (k :: integer)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   142
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   144
lemma int_of_integer_numeral [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   145
  "int_of_integer (numeral k) = numeral k"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   146
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   147
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   148
lemma int_of_integer_sub [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   149
  "int_of_integer (Num.sub k l) = Num.sub k l"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   150
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   151
61275
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   152
lift_definition integer_of_num :: "num \<Rightarrow> integer"
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   153
  is "numeral :: num \<Rightarrow> int"
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   154
  .
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   155
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   156
lemma integer_of_num [code]:
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   157
  "integer_of_num num.One = 1"
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   158
  "integer_of_num (num.Bit0 n) = (let k = integer_of_num n in k + k)"
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   159
  "integer_of_num (num.Bit1 n) = (let k = integer_of_num n in k + k + 1)"
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   160
  by (transfer, simp only: numeral.simps Let_def)+
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   161
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   162
lemma numeral_unfold_integer_of_num:
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   163
  "numeral = integer_of_num"
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   164
  by (simp add: integer_of_num_def map_fun_def fun_eq_iff)
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   165
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   166
lemma integer_of_num_triv:
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   167
  "integer_of_num Num.One = 1"
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   168
  "integer_of_num (Num.Bit0 Num.One) = 2"
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   169
  by (transfer, simp)+
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   170
64592
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   171
instantiation integer :: "{linordered_idom, equal}"
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   172
begin
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   173
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   174
lift_definition abs_integer :: "integer \<Rightarrow> integer"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   175
  is "abs :: int \<Rightarrow> int"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   176
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   177
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   178
declare abs_integer.rep_eq [simp]
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   179
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   180
lift_definition sgn_integer :: "integer \<Rightarrow> integer"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   181
  is "sgn :: int \<Rightarrow> int"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   182
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   183
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   184
declare sgn_integer.rep_eq [simp]
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   185
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   186
lift_definition less_eq_integer :: "integer \<Rightarrow> integer \<Rightarrow> bool"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   187
  is "less_eq :: int \<Rightarrow> int \<Rightarrow> bool"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   188
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   189
64592
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   190
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   191
lift_definition less_integer :: "integer \<Rightarrow> integer \<Rightarrow> bool"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   192
  is "less :: int \<Rightarrow> int \<Rightarrow> bool"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   193
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   194
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   195
lift_definition equal_integer :: "integer \<Rightarrow> integer \<Rightarrow> bool"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   196
  is "HOL.equal :: int \<Rightarrow> int \<Rightarrow> bool"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   197
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   198
64592
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   199
instance
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   200
  by standard (transfer, simp add: algebra_simps equal less_le_not_le [symmetric] mult_strict_right_mono linear)+
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   201
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   202
end
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   203
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   204
lemma [transfer_rule]:
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55736
diff changeset
   205
  "rel_fun pcr_integer (rel_fun pcr_integer pcr_integer) (min :: _ \<Rightarrow> _ \<Rightarrow> int) (min :: _ \<Rightarrow> _ \<Rightarrow> integer)"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   206
  by (unfold min_def [abs_def]) transfer_prover
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   207
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   208
lemma [transfer_rule]:
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55736
diff changeset
   209
  "rel_fun pcr_integer (rel_fun pcr_integer pcr_integer) (max :: _ \<Rightarrow> _ \<Rightarrow> int) (max :: _ \<Rightarrow> _ \<Rightarrow> integer)"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   210
  by (unfold max_def [abs_def]) transfer_prover
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   211
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   212
lemma int_of_integer_min [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   213
  "int_of_integer (min k l) = min (int_of_integer k) (int_of_integer l)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   214
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   215
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   216
lemma int_of_integer_max [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   217
  "int_of_integer (max k l) = max (int_of_integer k) (int_of_integer l)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   218
  by transfer rule
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   219
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   220
lemma nat_of_integer_non_positive [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   221
  "k \<le> 0 \<Longrightarrow> nat_of_integer k = 0"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   222
  by transfer simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   223
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   224
lemma of_nat_of_integer [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   225
  "of_nat (nat_of_integer k) = max 0 k"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   226
  by transfer auto
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   227
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64592
diff changeset
   228
instantiation integer :: normalization_semidom
64592
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   229
begin
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   230
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   231
lift_definition normalize_integer :: "integer \<Rightarrow> integer"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   232
  is "normalize :: int \<Rightarrow> int"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   233
  .
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   234
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   235
declare normalize_integer.rep_eq [simp]
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   236
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   237
lift_definition unit_factor_integer :: "integer \<Rightarrow> integer"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   238
  is "unit_factor :: int \<Rightarrow> int"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   239
  .
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   240
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   241
declare unit_factor_integer.rep_eq [simp]
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   242
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   243
lift_definition divide_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   244
  is "divide :: int \<Rightarrow> int \<Rightarrow> int"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   245
  .
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   246
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   247
declare divide_integer.rep_eq [simp]
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64592
diff changeset
   248
  
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64592
diff changeset
   249
instance
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64592
diff changeset
   250
  by (standard; transfer)
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64592
diff changeset
   251
    (auto simp add: mult_sgn_abs sgn_mult abs_eq_iff')
64592
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   252
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64592
diff changeset
   253
end
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64592
diff changeset
   254
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64592
diff changeset
   255
instantiation integer :: ring_div
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64592
diff changeset
   256
begin
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64592
diff changeset
   257
  
64592
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   258
lift_definition modulo_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   259
  is "modulo :: int \<Rightarrow> int \<Rightarrow> int"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   260
  .
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   261
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   262
declare modulo_integer.rep_eq [simp]
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   263
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   264
instance
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64592
diff changeset
   265
  by (standard; transfer) simp_all
64592
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   266
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   267
end
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   268
61275
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   269
instantiation integer :: semiring_numeral_div
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   270
begin
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   271
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   272
definition divmod_integer :: "num \<Rightarrow> num \<Rightarrow> integer \<times> integer"
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   273
where
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   274
  divmod_integer'_def: "divmod_integer m n = (numeral m div numeral n, numeral m mod numeral n)"
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   275
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   276
definition divmod_step_integer :: "num \<Rightarrow> integer \<times> integer \<Rightarrow> integer \<times> integer"
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   277
where
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   278
  "divmod_step_integer l qr = (let (q, r) = qr
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   279
    in if r \<ge> numeral l then (2 * q + 1, r - numeral l)
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   280
    else (2 * q, r))"
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   281
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   282
instance proof
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   283
  show "divmod m n = (numeral m div numeral n :: integer, numeral m mod numeral n)"
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   284
    for m n by (fact divmod_integer'_def)
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   285
  show "divmod_step l qr = (let (q, r) = qr
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   286
    in if r \<ge> numeral l then (2 * q + 1, r - numeral l)
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   287
    else (2 * q, r))" for l and qr :: "integer \<times> integer"
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   288
    by (fact divmod_step_integer_def)
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   289
qed (transfer,
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   290
  fact le_add_diff_inverse2
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   291
  semiring_numeral_div_class.div_less
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   292
  semiring_numeral_div_class.mod_less
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   293
  semiring_numeral_div_class.div_positive
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   294
  semiring_numeral_div_class.mod_less_eq_dividend
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   295
  semiring_numeral_div_class.pos_mod_bound
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   296
  semiring_numeral_div_class.pos_mod_sign
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   297
  semiring_numeral_div_class.mod_mult2_eq
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   298
  semiring_numeral_div_class.div_mult2_eq
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   299
  semiring_numeral_div_class.discrete)+
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   300
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   301
end
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   302
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   303
declare divmod_algorithm_code [where ?'a = integer,
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   304
  unfolded numeral_unfold_integer_of_num, unfolded integer_of_num_triv, 
053ec04ea866 monomorphization of divmod wrt. code generation avoids costly dictionary unpacking at runtime
haftmann
parents: 61274
diff changeset
   305
  code]
53069
d165213e3924 execution of int division by class semiring_numeral_div, replacing pdivmod by divmod_abs
haftmann
parents: 52435
diff changeset
   306
55427
ff54d22fe357 make integer_of_char and char_of_integer work with NBE and code_simp
Andreas Lochbihler
parents: 54489
diff changeset
   307
lemma integer_of_nat_0: "integer_of_nat 0 = 0"
ff54d22fe357 make integer_of_char and char_of_integer work with NBE and code_simp
Andreas Lochbihler
parents: 54489
diff changeset
   308
by transfer simp
ff54d22fe357 make integer_of_char and char_of_integer work with NBE and code_simp
Andreas Lochbihler
parents: 54489
diff changeset
   309
ff54d22fe357 make integer_of_char and char_of_integer work with NBE and code_simp
Andreas Lochbihler
parents: 54489
diff changeset
   310
lemma integer_of_nat_1: "integer_of_nat 1 = 1"
ff54d22fe357 make integer_of_char and char_of_integer work with NBE and code_simp
Andreas Lochbihler
parents: 54489
diff changeset
   311
by transfer simp
ff54d22fe357 make integer_of_char and char_of_integer work with NBE and code_simp
Andreas Lochbihler
parents: 54489
diff changeset
   312
ff54d22fe357 make integer_of_char and char_of_integer work with NBE and code_simp
Andreas Lochbihler
parents: 54489
diff changeset
   313
lemma integer_of_nat_numeral:
ff54d22fe357 make integer_of_char and char_of_integer work with NBE and code_simp
Andreas Lochbihler
parents: 54489
diff changeset
   314
  "integer_of_nat (numeral n) = numeral n"
ff54d22fe357 make integer_of_char and char_of_integer work with NBE and code_simp
Andreas Lochbihler
parents: 54489
diff changeset
   315
by transfer simp
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   316
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   317
subsection \<open>Code theorems for target language integers\<close>
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   318
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   319
text \<open>Constructors\<close>
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   320
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   321
definition Pos :: "num \<Rightarrow> integer"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   322
where
61274
0261eec37233 more selective preprocessing allows bare "numeral" occurences to be retained as real function in generated code
haftmann
parents: 61076
diff changeset
   323
  [simp, code_post]: "Pos = numeral"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   324
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   325
lemma [transfer_rule]:
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55736
diff changeset
   326
  "rel_fun HOL.eq pcr_integer numeral Pos"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   327
  by simp transfer_prover
30245
e67f42ac1157 consequent rewrite of index_size, size [index] to nat_of; support pseudo-primrec sepcifications with fun
haftmann
parents: 29823
diff changeset
   328
61274
0261eec37233 more selective preprocessing allows bare "numeral" occurences to be retained as real function in generated code
haftmann
parents: 61076
diff changeset
   329
lemma Pos_fold [code_unfold]:
0261eec37233 more selective preprocessing allows bare "numeral" occurences to be retained as real function in generated code
haftmann
parents: 61076
diff changeset
   330
  "numeral Num.One = Pos Num.One"
0261eec37233 more selective preprocessing allows bare "numeral" occurences to be retained as real function in generated code
haftmann
parents: 61076
diff changeset
   331
  "numeral (Num.Bit0 k) = Pos (Num.Bit0 k)"
0261eec37233 more selective preprocessing allows bare "numeral" occurences to be retained as real function in generated code
haftmann
parents: 61076
diff changeset
   332
  "numeral (Num.Bit1 k) = Pos (Num.Bit1 k)"
0261eec37233 more selective preprocessing allows bare "numeral" occurences to be retained as real function in generated code
haftmann
parents: 61076
diff changeset
   333
  by simp_all
0261eec37233 more selective preprocessing allows bare "numeral" occurences to be retained as real function in generated code
haftmann
parents: 61076
diff changeset
   334
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   335
definition Neg :: "num \<Rightarrow> integer"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   336
where
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 53069
diff changeset
   337
  [simp, code_abbrev]: "Neg n = - Pos n"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   338
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   339
lemma [transfer_rule]:
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55736
diff changeset
   340
  "rel_fun HOL.eq pcr_integer (\<lambda>n. - numeral n) Neg"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 53069
diff changeset
   341
  by (simp add: Neg_def [abs_def]) transfer_prover
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   342
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   343
code_datatype "0::integer" Pos Neg
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   344
64994
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   345
  
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   346
text \<open>A further pair of constructors for generated computations\<close>
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   347
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   348
context
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   349
begin  
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   350
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   351
qualified definition positive :: "num \<Rightarrow> integer"
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   352
  where [simp]: "positive = numeral"
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   353
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   354
qualified definition negative :: "num \<Rightarrow> integer"
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   355
  where [simp]: "negative = uminus \<circ> numeral"
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   356
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   357
lemma [code_computation_unfold]:
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   358
  "numeral = positive"
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   359
  "Pos = positive"
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   360
  "Neg = negative"
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   361
  by (simp_all add: fun_eq_iff)
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   362
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   363
end
6e4c05e8edbb computation preprocessing rules to allow literals as input for computations
haftmann
parents: 64848
diff changeset
   364
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   365
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   366
text \<open>Auxiliary operations\<close>
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   367
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   368
lift_definition dup :: "integer \<Rightarrow> integer"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   369
  is "\<lambda>k::int. k + k"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   370
  .
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   371
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   372
lemma dup_code [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   373
  "dup 0 = 0"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   374
  "dup (Pos n) = Pos (Num.Bit0 n)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   375
  "dup (Neg n) = Neg (Num.Bit0 n)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 53069
diff changeset
   376
  by (transfer, simp only: numeral_Bit0 minus_add_distrib)+
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   377
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   378
lift_definition sub :: "num \<Rightarrow> num \<Rightarrow> integer"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   379
  is "\<lambda>m n. numeral m - numeral n :: int"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   380
  .
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   381
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   382
lemma sub_code [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   383
  "sub Num.One Num.One = 0"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   384
  "sub (Num.Bit0 m) Num.One = Pos (Num.BitM m)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   385
  "sub (Num.Bit1 m) Num.One = Pos (Num.Bit0 m)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   386
  "sub Num.One (Num.Bit0 n) = Neg (Num.BitM n)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   387
  "sub Num.One (Num.Bit1 n) = Neg (Num.Bit0 n)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   388
  "sub (Num.Bit0 m) (Num.Bit0 n) = dup (sub m n)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   389
  "sub (Num.Bit1 m) (Num.Bit1 n) = dup (sub m n)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   390
  "sub (Num.Bit1 m) (Num.Bit0 n) = dup (sub m n) + 1"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   391
  "sub (Num.Bit0 m) (Num.Bit1 n) = dup (sub m n) - 1"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   392
  by (transfer, simp add: dbl_def dbl_inc_def dbl_dec_def)+
28351
abfc66969d1f non left-linear equations for nbe
haftmann
parents: 28346
diff changeset
   393
24999
haftmann
parents:
diff changeset
   394
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   395
text \<open>Implementations\<close>
24999
haftmann
parents:
diff changeset
   396
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   397
lemma one_integer_code [code, code_unfold]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   398
  "1 = Pos Num.One"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   399
  by simp
24999
haftmann
parents:
diff changeset
   400
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   401
lemma plus_integer_code [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   402
  "k + 0 = (k::integer)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   403
  "0 + l = (l::integer)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   404
  "Pos m + Pos n = Pos (m + n)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   405
  "Pos m + Neg n = sub m n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   406
  "Neg m + Pos n = sub n m"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   407
  "Neg m + Neg n = Neg (m + n)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   408
  by (transfer, simp)+
24999
haftmann
parents:
diff changeset
   409
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   410
lemma uminus_integer_code [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   411
  "uminus 0 = (0::integer)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   412
  "uminus (Pos m) = Neg m"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   413
  "uminus (Neg m) = Pos m"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   414
  by simp_all
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   415
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   416
lemma minus_integer_code [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   417
  "k - 0 = (k::integer)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   418
  "0 - l = uminus (l::integer)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   419
  "Pos m - Pos n = sub m n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   420
  "Pos m - Neg n = Pos (m + n)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   421
  "Neg m - Pos n = Neg (m + n)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   422
  "Neg m - Neg n = sub n m"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   423
  by (transfer, simp)+
46028
9f113cdf3d66 attribute code_abbrev superseedes code_unfold_post
haftmann
parents: 45694
diff changeset
   424
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   425
lemma abs_integer_code [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   426
  "\<bar>k\<bar> = (if (k::integer) < 0 then - k else k)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   427
  by simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46664
diff changeset
   428
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   429
lemma sgn_integer_code [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   430
  "sgn k = (if k = 0 then 0 else if (k::integer) < 0 then - 1 else 1)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46664
diff changeset
   431
  by simp
46028
9f113cdf3d66 attribute code_abbrev superseedes code_unfold_post
haftmann
parents: 45694
diff changeset
   432
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   433
lemma times_integer_code [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   434
  "k * 0 = (0::integer)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   435
  "0 * l = (0::integer)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   436
  "Pos m * Pos n = Pos (m * n)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   437
  "Pos m * Neg n = Neg (m * n)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   438
  "Neg m * Pos n = Neg (m * n)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   439
  "Neg m * Neg n = Pos (m * n)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   440
  by simp_all
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   441
64592
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   442
lemma normalize_integer_code [code]:
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   443
  "normalize = (abs :: integer \<Rightarrow> integer)"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   444
  by transfer simp
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   445
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   446
lemma unit_factor_integer_code [code]:
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   447
  "unit_factor = (sgn :: integer \<Rightarrow> integer)"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   448
  by transfer simp
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   449
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   450
definition divmod_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer \<times> integer"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   451
where
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   452
  "divmod_integer k l = (k div l, k mod l)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   453
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   454
lemma fst_divmod [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   455
  "fst (divmod_integer k l) = k div l"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   456
  by (simp add: divmod_integer_def)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   457
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   458
lemma snd_divmod [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   459
  "snd (divmod_integer k l) = k mod l"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   460
  by (simp add: divmod_integer_def)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   461
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   462
definition divmod_abs :: "integer \<Rightarrow> integer \<Rightarrow> integer \<times> integer"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   463
where
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   464
  "divmod_abs k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   465
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   466
lemma fst_divmod_abs [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   467
  "fst (divmod_abs k l) = \<bar>k\<bar> div \<bar>l\<bar>"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   468
  by (simp add: divmod_abs_def)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   469
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   470
lemma snd_divmod_abs [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   471
  "snd (divmod_abs k l) = \<bar>k\<bar> mod \<bar>l\<bar>"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   472
  by (simp add: divmod_abs_def)
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   473
53069
d165213e3924 execution of int division by class semiring_numeral_div, replacing pdivmod by divmod_abs
haftmann
parents: 52435
diff changeset
   474
lemma divmod_abs_code [code]:
d165213e3924 execution of int division by class semiring_numeral_div, replacing pdivmod by divmod_abs
haftmann
parents: 52435
diff changeset
   475
  "divmod_abs (Pos k) (Pos l) = divmod k l"
d165213e3924 execution of int division by class semiring_numeral_div, replacing pdivmod by divmod_abs
haftmann
parents: 52435
diff changeset
   476
  "divmod_abs (Neg k) (Neg l) = divmod k l"
d165213e3924 execution of int division by class semiring_numeral_div, replacing pdivmod by divmod_abs
haftmann
parents: 52435
diff changeset
   477
  "divmod_abs (Neg k) (Pos l) = divmod k l"
d165213e3924 execution of int division by class semiring_numeral_div, replacing pdivmod by divmod_abs
haftmann
parents: 52435
diff changeset
   478
  "divmod_abs (Pos k) (Neg l) = divmod k l"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   479
  "divmod_abs j 0 = (0, \<bar>j\<bar>)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   480
  "divmod_abs 0 j = (0, 0)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   481
  by (simp_all add: prod_eq_iff)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   482
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   483
lemma divmod_integer_code [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   484
  "divmod_integer k l =
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   485
    (if k = 0 then (0, 0) else if l = 0 then (0, k) else
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   486
    (apsnd \<circ> times \<circ> sgn) l (if sgn k = sgn l
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   487
      then divmod_abs k l
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   488
      else (let (r, s) = divmod_abs k l in
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   489
        if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   490
proof -
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   491
  have aux1: "\<And>k l::int. sgn k = sgn l \<longleftrightarrow> k = 0 \<and> l = 0 \<or> 0 < l \<and> 0 < k \<or> l < 0 \<and> k < 0"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   492
    by (auto simp add: sgn_if)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   493
  have aux2: "\<And>q::int. - int_of_integer k = int_of_integer l * q \<longleftrightarrow> int_of_integer k = int_of_integer l * - q" by auto
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   494
  show ?thesis
55414
eab03e9cee8a renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents: 54489
diff changeset
   495
    by (simp add: prod_eq_iff integer_eq_iff case_prod_beta aux1)
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   496
      (auto simp add: zdiv_zminus1_eq_if zmod_zminus1_eq_if div_minus_right mod_minus_right aux2)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   497
qed
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   498
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   499
lemma div_integer_code [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   500
  "k div l = fst (divmod_integer k l)"
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   501
  by simp
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   502
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   503
lemma mod_integer_code [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   504
  "k mod l = snd (divmod_integer k l)"
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   505
  by simp
24999
haftmann
parents:
diff changeset
   506
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   507
lemma equal_integer_code [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   508
  "HOL.equal 0 (0::integer) \<longleftrightarrow> True"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   509
  "HOL.equal 0 (Pos l) \<longleftrightarrow> False"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   510
  "HOL.equal 0 (Neg l) \<longleftrightarrow> False"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   511
  "HOL.equal (Pos k) 0 \<longleftrightarrow> False"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   512
  "HOL.equal (Pos k) (Pos l) \<longleftrightarrow> HOL.equal k l"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   513
  "HOL.equal (Pos k) (Neg l) \<longleftrightarrow> False"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   514
  "HOL.equal (Neg k) 0 \<longleftrightarrow> False"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   515
  "HOL.equal (Neg k) (Pos l) \<longleftrightarrow> False"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   516
  "HOL.equal (Neg k) (Neg l) \<longleftrightarrow> HOL.equal k l"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   517
  by (simp_all add: equal)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   518
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   519
lemma equal_integer_refl [code nbe]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   520
  "HOL.equal (k::integer) k \<longleftrightarrow> True"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   521
  by (fact equal_refl)
31266
55e70b6d812e added lemma about 0 - 1
haftmann
parents: 31205
diff changeset
   522
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   523
lemma less_eq_integer_code [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   524
  "0 \<le> (0::integer) \<longleftrightarrow> True"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   525
  "0 \<le> Pos l \<longleftrightarrow> True"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   526
  "0 \<le> Neg l \<longleftrightarrow> False"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   527
  "Pos k \<le> 0 \<longleftrightarrow> False"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   528
  "Pos k \<le> Pos l \<longleftrightarrow> k \<le> l"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   529
  "Pos k \<le> Neg l \<longleftrightarrow> False"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   530
  "Neg k \<le> 0 \<longleftrightarrow> True"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   531
  "Neg k \<le> Pos l \<longleftrightarrow> True"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   532
  "Neg k \<le> Neg l \<longleftrightarrow> l \<le> k"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   533
  by simp_all
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   534
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   535
lemma less_integer_code [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   536
  "0 < (0::integer) \<longleftrightarrow> False"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   537
  "0 < Pos l \<longleftrightarrow> True"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   538
  "0 < Neg l \<longleftrightarrow> False"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   539
  "Pos k < 0 \<longleftrightarrow> False"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   540
  "Pos k < Pos l \<longleftrightarrow> k < l"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   541
  "Pos k < Neg l \<longleftrightarrow> False"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   542
  "Neg k < 0 \<longleftrightarrow> True"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   543
  "Neg k < Pos l \<longleftrightarrow> True"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   544
  "Neg k < Neg l \<longleftrightarrow> l < k"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   545
  by simp_all
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   546
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   547
lift_definition num_of_integer :: "integer \<Rightarrow> num"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   548
  is "num_of_nat \<circ> nat"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   549
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   550
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   551
lemma num_of_integer_code [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   552
  "num_of_integer k = (if k \<le> 1 then Num.One
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   553
     else let
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   554
       (l, j) = divmod_integer k 2;
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   555
       l' = num_of_integer l;
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   556
       l'' = l' + l'
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   557
     in if j = 0 then l'' else l'' + Num.One)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   558
proof -
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   559
  {
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   560
    assume "int_of_integer k mod 2 = 1"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   561
    then have "nat (int_of_integer k mod 2) = nat 1" by simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   562
    moreover assume *: "1 < int_of_integer k"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   563
    ultimately have **: "nat (int_of_integer k) mod 2 = 1" by (simp add: nat_mod_distrib)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   564
    have "num_of_nat (nat (int_of_integer k)) =
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   565
      num_of_nat (2 * (nat (int_of_integer k) div 2) + nat (int_of_integer k) mod 2)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   566
      by simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   567
    then have "num_of_nat (nat (int_of_integer k)) =
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   568
      num_of_nat (nat (int_of_integer k) div 2 + nat (int_of_integer k) div 2 + nat (int_of_integer k) mod 2)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   569
      by (simp add: mult_2)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   570
    with ** have "num_of_nat (nat (int_of_integer k)) =
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   571
      num_of_nat (nat (int_of_integer k) div 2 + nat (int_of_integer k) div 2 + 1)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   572
      by simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   573
  }
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   574
  note aux = this
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   575
  show ?thesis
55414
eab03e9cee8a renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents: 54489
diff changeset
   576
    by (auto simp add: num_of_integer_def nat_of_integer_def Let_def case_prod_beta
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   577
      not_le integer_eq_iff less_eq_integer_def
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   578
      nat_mult_distrib nat_div_distrib num_of_nat_One num_of_nat_plus_distrib
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   579
       mult_2 [where 'a=nat] aux add_One)
25918
haftmann
parents: 25767
diff changeset
   580
qed
haftmann
parents: 25767
diff changeset
   581
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   582
lemma nat_of_integer_code [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   583
  "nat_of_integer k = (if k \<le> 0 then 0
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   584
     else let
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   585
       (l, j) = divmod_integer k 2;
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   586
       l' = nat_of_integer l;
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   587
       l'' = l' + l'
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   588
     in if j = 0 then l'' else l'' + 1)"
33340
a165b97f3658 moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
haftmann
parents: 33318
diff changeset
   589
proof -
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   590
  obtain j where "k = integer_of_int j"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   591
  proof
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   592
    show "k = integer_of_int (int_of_integer k)" by simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   593
  qed
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   594
  moreover have "2 * (j div 2) = j - j mod 2"
64246
15d1ee6e847b eliminated irregular aliasses
haftmann
parents: 64241
diff changeset
   595
    by (simp add: minus_mod_eq_mult_div [symmetric] mult.commute)
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   596
  ultimately show ?thesis
63950
cdc1e59aa513 syntactic type class for operation mod named after mod;
haftmann
parents: 63174
diff changeset
   597
    by (auto simp add: split_def Let_def modulo_integer_def nat_of_integer_def not_le
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   598
      nat_add_distrib [symmetric] Suc_nat_eq_nat_zadd1)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   599
      (auto simp add: mult_2 [symmetric])
33340
a165b97f3658 moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
haftmann
parents: 33318
diff changeset
   600
qed
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   601
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   602
lemma int_of_integer_code [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   603
  "int_of_integer k = (if k < 0 then - (int_of_integer (- k))
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   604
     else if k = 0 then 0
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   605
     else let
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   606
       (l, j) = divmod_integer k 2;
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   607
       l' = 2 * int_of_integer l
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   608
     in if j = 0 then l' else l' + 1)"
64246
15d1ee6e847b eliminated irregular aliasses
haftmann
parents: 64241
diff changeset
   609
  by (auto simp add: split_def Let_def integer_eq_iff minus_mod_eq_mult_div [symmetric])
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   610
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   611
lemma integer_of_int_code [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   612
  "integer_of_int k = (if k < 0 then - (integer_of_int (- k))
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   613
     else if k = 0 then 0
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   614
     else let
60868
dd18c33c001e direct bootstrap of integer division from natural division
haftmann
parents: 60758
diff changeset
   615
       l = 2 * integer_of_int (k div 2);
dd18c33c001e direct bootstrap of integer division from natural division
haftmann
parents: 60758
diff changeset
   616
       j = k mod 2
dd18c33c001e direct bootstrap of integer division from natural division
haftmann
parents: 60758
diff changeset
   617
     in if j = 0 then l else l + 1)"
64246
15d1ee6e847b eliminated irregular aliasses
haftmann
parents: 64241
diff changeset
   618
  by (auto simp add: split_def Let_def integer_eq_iff minus_mod_eq_mult_div [symmetric])
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   619
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   620
hide_const (open) Pos Neg sub dup divmod_abs
46547
d1dcb91a512e use qualified constant names instead of suffixes (from Florian Haftmann)
huffman
parents: 46028
diff changeset
   621
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   622
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   623
subsection \<open>Serializer setup for target language integers\<close>
24999
haftmann
parents:
diff changeset
   624
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   625
code_reserved Eval int Integer abs
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   626
52435
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   627
code_printing
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   628
  type_constructor integer \<rightharpoonup>
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   629
    (SML) "IntInf.int"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   630
    and (OCaml) "Big'_int.big'_int"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   631
    and (Haskell) "Integer"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   632
    and (Scala) "BigInt"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   633
    and (Eval) "int"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   634
| class_instance integer :: equal \<rightharpoonup>
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   635
    (Haskell) -
24999
haftmann
parents:
diff changeset
   636
52435
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   637
code_printing
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   638
  constant "0::integer" \<rightharpoonup>
58400
d0d3c30806b4 always annotate potentially polymorphic Haskell numerals
haftmann
parents: 58399
diff changeset
   639
    (SML) "!(0/ :/ IntInf.int)"
52435
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   640
    and (OCaml) "Big'_int.zero'_big'_int"
58400
d0d3c30806b4 always annotate potentially polymorphic Haskell numerals
haftmann
parents: 58399
diff changeset
   641
    and (Haskell) "!(0/ ::/ Integer)"
52435
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   642
    and (Scala) "BigInt(0)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46664
diff changeset
   643
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   644
setup \<open>
58399
haftmann
parents: 58390
diff changeset
   645
  fold (fn target =>
haftmann
parents: 58390
diff changeset
   646
    Numeral.add_code @{const_name Code_Numeral.Pos} I Code_Printer.literal_numeral target
haftmann
parents: 58390
diff changeset
   647
    #> Numeral.add_code @{const_name Code_Numeral.Neg} (op ~) Code_Printer.literal_numeral target)
haftmann
parents: 58390
diff changeset
   648
    ["SML", "OCaml", "Haskell", "Scala"]
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   649
\<close>
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   650
52435
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   651
code_printing
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   652
  constant "plus :: integer \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   653
    (SML) "IntInf.+ ((_), (_))"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   654
    and (OCaml) "Big'_int.add'_big'_int"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   655
    and (Haskell) infixl 6 "+"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   656
    and (Scala) infixl 7 "+"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   657
    and (Eval) infixl 8 "+"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   658
| constant "uminus :: integer \<Rightarrow> _" \<rightharpoonup>
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   659
    (SML) "IntInf.~"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   660
    and (OCaml) "Big'_int.minus'_big'_int"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   661
    and (Haskell) "negate"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   662
    and (Scala) "!(- _)"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   663
    and (Eval) "~/ _"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   664
| constant "minus :: integer \<Rightarrow> _" \<rightharpoonup>
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   665
    (SML) "IntInf.- ((_), (_))"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   666
    and (OCaml) "Big'_int.sub'_big'_int"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   667
    and (Haskell) infixl 6 "-"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   668
    and (Scala) infixl 7 "-"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   669
    and (Eval) infixl 8 "-"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   670
| constant Code_Numeral.dup \<rightharpoonup>
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   671
    (SML) "IntInf.*/ (2,/ (_))"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   672
    and (OCaml) "Big'_int.mult'_big'_int/ (Big'_int.big'_int'_of'_int/ 2)"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   673
    and (Haskell) "!(2 * _)"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   674
    and (Scala) "!(2 * _)"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   675
    and (Eval) "!(2 * _)"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   676
| constant Code_Numeral.sub \<rightharpoonup>
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   677
    (SML) "!(raise/ Fail/ \"sub\")"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   678
    and (OCaml) "failwith/ \"sub\""
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   679
    and (Haskell) "error/ \"sub\""
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   680
    and (Scala) "!sys.error(\"sub\")"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   681
| constant "times :: integer \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   682
    (SML) "IntInf.* ((_), (_))"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   683
    and (OCaml) "Big'_int.mult'_big'_int"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   684
    and (Haskell) infixl 7 "*"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   685
    and (Scala) infixl 8 "*"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   686
    and (Eval) infixl 9 "*"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   687
| constant Code_Numeral.divmod_abs \<rightharpoonup>
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   688
    (SML) "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   689
    and (OCaml) "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   690
    and (Haskell) "divMod/ (abs _)/ (abs _)"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   691
    and (Scala) "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   692
    and (Eval) "Integer.div'_mod/ (abs _)/ (abs _)"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   693
| constant "HOL.equal :: integer \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   694
    (SML) "!((_ : IntInf.int) = _)"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   695
    and (OCaml) "Big'_int.eq'_big'_int"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   696
    and (Haskell) infix 4 "=="
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   697
    and (Scala) infixl 5 "=="
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   698
    and (Eval) infixl 6 "="
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   699
| constant "less_eq :: integer \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   700
    (SML) "IntInf.<= ((_), (_))"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   701
    and (OCaml) "Big'_int.le'_big'_int"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   702
    and (Haskell) infix 4 "<="
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   703
    and (Scala) infixl 4 "<="
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   704
    and (Eval) infixl 6 "<="
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   705
| constant "less :: integer \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   706
    (SML) "IntInf.< ((_), (_))"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   707
    and (OCaml) "Big'_int.lt'_big'_int"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   708
    and (Haskell) infix 4 "<"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   709
    and (Scala) infixl 4 "<"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   710
    and (Eval) infixl 6 "<"
61857
542f2c6da692 add serialisation for abs on integer to target language operation
Andreas Lochbihler
parents: 61275
diff changeset
   711
| constant "abs :: integer \<Rightarrow> _" \<rightharpoonup>
542f2c6da692 add serialisation for abs on integer to target language operation
Andreas Lochbihler
parents: 61275
diff changeset
   712
    (SML) "IntInf.abs"
542f2c6da692 add serialisation for abs on integer to target language operation
Andreas Lochbihler
parents: 61275
diff changeset
   713
    and (OCaml) "Big'_int.abs'_big'_int"
542f2c6da692 add serialisation for abs on integer to target language operation
Andreas Lochbihler
parents: 61275
diff changeset
   714
    and (Haskell) "Prelude.abs"
542f2c6da692 add serialisation for abs on integer to target language operation
Andreas Lochbihler
parents: 61275
diff changeset
   715
    and (Scala) "_.abs"
542f2c6da692 add serialisation for abs on integer to target language operation
Andreas Lochbihler
parents: 61275
diff changeset
   716
    and (Eval) "abs"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   717
52435
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   718
code_identifier
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51375
diff changeset
   719
  code_module Code_Numeral \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
46547
d1dcb91a512e use qualified constant names instead of suffixes (from Florian Haftmann)
huffman
parents: 46028
diff changeset
   720
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   721
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   722
subsection \<open>Type of target language naturals\<close>
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   723
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60868
diff changeset
   724
typedef natural = "UNIV :: nat set"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   725
  morphisms nat_of_natural natural_of_nat ..
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   726
59487
adaa430fc0f7 default abstypes and default abstract equations make technical (no_code) annotation superfluous
haftmann
parents: 58889
diff changeset
   727
setup_lifting type_definition_natural
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   728
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   729
lemma natural_eq_iff [termination_simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   730
  "m = n \<longleftrightarrow> nat_of_natural m = nat_of_natural n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   731
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   732
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   733
lemma natural_eqI:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   734
  "nat_of_natural m = nat_of_natural n \<Longrightarrow> m = n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   735
  using natural_eq_iff [of m n] by simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   736
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   737
lemma nat_of_natural_of_nat_inverse [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   738
  "nat_of_natural (natural_of_nat n) = n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   739
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   740
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   741
lemma natural_of_nat_of_natural_inverse [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   742
  "natural_of_nat (nat_of_natural n) = n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   743
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   744
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   745
instantiation natural :: "{comm_monoid_diff, semiring_1}"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   746
begin
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   747
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   748
lift_definition zero_natural :: natural
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   749
  is "0 :: nat"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   750
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   751
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   752
declare zero_natural.rep_eq [simp]
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   753
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   754
lift_definition one_natural :: natural
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   755
  is "1 :: nat"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   756
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   757
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   758
declare one_natural.rep_eq [simp]
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   759
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   760
lift_definition plus_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   761
  is "plus :: nat \<Rightarrow> nat \<Rightarrow> nat"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   762
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   763
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   764
declare plus_natural.rep_eq [simp]
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   765
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   766
lift_definition minus_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   767
  is "minus :: nat \<Rightarrow> nat \<Rightarrow> nat"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   768
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   769
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   770
declare minus_natural.rep_eq [simp]
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   771
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   772
lift_definition times_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   773
  is "times :: nat \<Rightarrow> nat \<Rightarrow> nat"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   774
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   775
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   776
declare times_natural.rep_eq [simp]
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   777
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   778
instance proof
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   779
qed (transfer, simp add: algebra_simps)+
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   780
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   781
end
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   782
64241
430d74089d4d transfer rules for divides relation on integer and natural
haftmann
parents: 64178
diff changeset
   783
instance natural :: Rings.dvd ..
430d74089d4d transfer rules for divides relation on integer and natural
haftmann
parents: 64178
diff changeset
   784
430d74089d4d transfer rules for divides relation on integer and natural
haftmann
parents: 64178
diff changeset
   785
lemma [transfer_rule]:
430d74089d4d transfer rules for divides relation on integer and natural
haftmann
parents: 64178
diff changeset
   786
  "rel_fun pcr_natural (rel_fun pcr_natural HOL.iff) Rings.dvd Rings.dvd"
430d74089d4d transfer rules for divides relation on integer and natural
haftmann
parents: 64178
diff changeset
   787
  unfolding dvd_def by transfer_prover
430d74089d4d transfer rules for divides relation on integer and natural
haftmann
parents: 64178
diff changeset
   788
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   789
lemma [transfer_rule]:
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55736
diff changeset
   790
  "rel_fun HOL.eq pcr_natural (\<lambda>n::nat. n) (of_nat :: nat \<Rightarrow> natural)"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   791
proof -
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55736
diff changeset
   792
  have "rel_fun HOL.eq pcr_natural (of_nat :: nat \<Rightarrow> nat) (of_nat :: nat \<Rightarrow> natural)"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   793
    by (unfold of_nat_def [abs_def]) transfer_prover
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   794
  then show ?thesis by (simp add: id_def)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   795
qed
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   796
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   797
lemma [transfer_rule]:
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55736
diff changeset
   798
  "rel_fun HOL.eq pcr_natural (numeral :: num \<Rightarrow> nat) (numeral :: num \<Rightarrow> natural)"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   799
proof -
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55736
diff changeset
   800
  have "rel_fun HOL.eq pcr_natural (numeral :: num \<Rightarrow> nat) (\<lambda>n. of_nat (numeral n))"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   801
    by transfer_prover
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   802
  then show ?thesis by simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   803
qed
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   804
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   805
lemma nat_of_natural_of_nat [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   806
  "nat_of_natural (of_nat n) = n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   807
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   808
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   809
lemma natural_of_nat_of_nat [simp, code_abbrev]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   810
  "natural_of_nat = of_nat"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   811
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   812
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   813
lemma of_nat_of_natural [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   814
  "of_nat (nat_of_natural n) = n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   815
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   816
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   817
lemma nat_of_natural_numeral [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   818
  "nat_of_natural (numeral k) = numeral k"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   819
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   820
64592
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   821
instantiation natural :: "{linordered_semiring, equal}"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   822
begin
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   823
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   824
lift_definition less_eq_natural :: "natural \<Rightarrow> natural \<Rightarrow> bool"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   825
  is "less_eq :: nat \<Rightarrow> nat \<Rightarrow> bool"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   826
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   827
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   828
declare less_eq_natural.rep_eq [termination_simp]
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   829
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   830
lift_definition less_natural :: "natural \<Rightarrow> natural \<Rightarrow> bool"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   831
  is "less :: nat \<Rightarrow> nat \<Rightarrow> bool"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   832
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   833
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   834
declare less_natural.rep_eq [termination_simp]
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   835
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   836
lift_definition equal_natural :: "natural \<Rightarrow> natural \<Rightarrow> bool"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   837
  is "HOL.equal :: nat \<Rightarrow> nat \<Rightarrow> bool"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   838
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   839
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   840
instance proof
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   841
qed (transfer, simp add: algebra_simps equal less_le_not_le [symmetric] linear)+
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   842
24999
haftmann
parents:
diff changeset
   843
end
46664
1f6c140f9c72 moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
haftmann
parents: 46638
diff changeset
   844
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   845
lemma [transfer_rule]:
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55736
diff changeset
   846
  "rel_fun pcr_natural (rel_fun pcr_natural pcr_natural) (min :: _ \<Rightarrow> _ \<Rightarrow> nat) (min :: _ \<Rightarrow> _ \<Rightarrow> natural)"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   847
  by (unfold min_def [abs_def]) transfer_prover
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   848
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   849
lemma [transfer_rule]:
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55736
diff changeset
   850
  "rel_fun pcr_natural (rel_fun pcr_natural pcr_natural) (max :: _ \<Rightarrow> _ \<Rightarrow> nat) (max :: _ \<Rightarrow> _ \<Rightarrow> natural)"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   851
  by (unfold max_def [abs_def]) transfer_prover
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   852
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   853
lemma nat_of_natural_min [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   854
  "nat_of_natural (min k l) = min (nat_of_natural k) (nat_of_natural l)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   855
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   856
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   857
lemma nat_of_natural_max [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   858
  "nat_of_natural (max k l) = max (nat_of_natural k) (nat_of_natural l)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   859
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   860
64592
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   861
instantiation natural :: "{semiring_div, normalization_semidom}"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   862
begin
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   863
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   864
lift_definition normalize_natural :: "natural \<Rightarrow> natural"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   865
  is "normalize :: nat \<Rightarrow> nat"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   866
  .
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   867
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   868
declare normalize_natural.rep_eq [simp]
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   869
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   870
lift_definition unit_factor_natural :: "natural \<Rightarrow> natural"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   871
  is "unit_factor :: nat \<Rightarrow> nat"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   872
  .
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   873
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   874
declare unit_factor_natural.rep_eq [simp]
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   875
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   876
lift_definition divide_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   877
  is "divide :: nat \<Rightarrow> nat \<Rightarrow> nat"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   878
  .
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   879
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   880
declare divide_natural.rep_eq [simp]
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   881
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   882
lift_definition modulo_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   883
  is "modulo :: nat \<Rightarrow> nat \<Rightarrow> nat"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   884
  .
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   885
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   886
declare modulo_natural.rep_eq [simp]
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   887
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   888
instance
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   889
  by standard (transfer, auto simp add: algebra_simps unit_factor_nat_def gr0_conv_Suc)+
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   890
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   891
end
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
   892
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   893
lift_definition natural_of_integer :: "integer \<Rightarrow> natural"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   894
  is "nat :: int \<Rightarrow> nat"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   895
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   896
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   897
lift_definition integer_of_natural :: "natural \<Rightarrow> integer"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   898
  is "of_nat :: nat \<Rightarrow> int"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   899
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   900
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   901
lemma natural_of_integer_of_natural [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   902
  "natural_of_integer (integer_of_natural n) = n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   903
  by transfer simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   904
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   905
lemma integer_of_natural_of_integer [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   906
  "integer_of_natural (natural_of_integer k) = max 0 k"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   907
  by transfer auto
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   908
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   909
lemma int_of_integer_of_natural [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   910
  "int_of_integer (integer_of_natural n) = of_nat (nat_of_natural n)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   911
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   912
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   913
lemma integer_of_natural_of_nat [simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   914
  "integer_of_natural (of_nat n) = of_nat n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   915
  by transfer rule
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   916
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   917
lemma [measure_function]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   918
  "is_measure nat_of_natural"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   919
  by (rule is_measure_trivial)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   920
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   921
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   922
subsection \<open>Inductive representation of target language naturals\<close>
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   923
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   924
lift_definition Suc :: "natural \<Rightarrow> natural"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   925
  is Nat.Suc
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   926
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   927
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   928
declare Suc.rep_eq [simp]
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   929
58306
117ba6cbe414 renamed 'rep_datatype' to 'old_rep_datatype' (HOL)
blanchet
parents: 57512
diff changeset
   930
old_rep_datatype "0::natural" Suc
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   931
  by (transfer, fact nat.induct nat.inject nat.distinct)+
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   932
55416
dd7992d4a61a adapted theories to 'xxx_case' to 'case_xxx'
blanchet
parents: 55414
diff changeset
   933
lemma natural_cases [case_names nat, cases type: natural]:
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   934
  fixes m :: natural
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   935
  assumes "\<And>n. m = of_nat n \<Longrightarrow> P"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   936
  shows P
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   937
  using assms by transfer blast
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   938
58390
b74d8470b98e keep obsolete interpretations in Main, to avoid merge trouble
blanchet
parents: 58379
diff changeset
   939
lemma [simp, code]: "size_natural = nat_of_natural"
b74d8470b98e keep obsolete interpretations in Main, to avoid merge trouble
blanchet
parents: 58379
diff changeset
   940
proof (rule ext)
b74d8470b98e keep obsolete interpretations in Main, to avoid merge trouble
blanchet
parents: 58379
diff changeset
   941
  fix n
b74d8470b98e keep obsolete interpretations in Main, to avoid merge trouble
blanchet
parents: 58379
diff changeset
   942
  show "size_natural n = nat_of_natural n"
b74d8470b98e keep obsolete interpretations in Main, to avoid merge trouble
blanchet
parents: 58379
diff changeset
   943
    by (induct n) simp_all
b74d8470b98e keep obsolete interpretations in Main, to avoid merge trouble
blanchet
parents: 58379
diff changeset
   944
qed
58379
c044539a2bda reintroduced an instantiation of 'size' for 'numerals'
blanchet
parents: 58377
diff changeset
   945
58390
b74d8470b98e keep obsolete interpretations in Main, to avoid merge trouble
blanchet
parents: 58379
diff changeset
   946
lemma [simp, code]: "size = nat_of_natural"
b74d8470b98e keep obsolete interpretations in Main, to avoid merge trouble
blanchet
parents: 58379
diff changeset
   947
proof (rule ext)
b74d8470b98e keep obsolete interpretations in Main, to avoid merge trouble
blanchet
parents: 58379
diff changeset
   948
  fix n
b74d8470b98e keep obsolete interpretations in Main, to avoid merge trouble
blanchet
parents: 58379
diff changeset
   949
  show "size n = nat_of_natural n"
b74d8470b98e keep obsolete interpretations in Main, to avoid merge trouble
blanchet
parents: 58379
diff changeset
   950
    by (induct n) simp_all
b74d8470b98e keep obsolete interpretations in Main, to avoid merge trouble
blanchet
parents: 58379
diff changeset
   951
qed
58379
c044539a2bda reintroduced an instantiation of 'size' for 'numerals'
blanchet
parents: 58377
diff changeset
   952
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   953
lemma natural_decr [termination_simp]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   954
  "n \<noteq> 0 \<Longrightarrow> nat_of_natural n - Nat.Suc 0 < nat_of_natural n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   955
  by transfer simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   956
58379
c044539a2bda reintroduced an instantiation of 'size' for 'numerals'
blanchet
parents: 58377
diff changeset
   957
lemma natural_zero_minus_one: "(0::natural) - 1 = 0"
c044539a2bda reintroduced an instantiation of 'size' for 'numerals'
blanchet
parents: 58377
diff changeset
   958
  by (rule zero_diff)
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   959
58379
c044539a2bda reintroduced an instantiation of 'size' for 'numerals'
blanchet
parents: 58377
diff changeset
   960
lemma Suc_natural_minus_one: "Suc n - 1 = n"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   961
  by transfer simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   962
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   963
hide_const (open) Suc
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   964
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   965
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   966
subsection \<open>Code refinement for target language naturals\<close>
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   967
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   968
lift_definition Nat :: "integer \<Rightarrow> natural"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   969
  is nat
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   970
  .
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   971
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   972
lemma [code_post]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   973
  "Nat 0 = 0"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   974
  "Nat 1 = 1"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   975
  "Nat (numeral k) = numeral k"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   976
  by (transfer, simp)+
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   977
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   978
lemma [code abstype]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   979
  "Nat (integer_of_natural n) = n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   980
  by transfer simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   981
63174
57c0d60e491c do not export abstract constructors in code_reflect
haftmann
parents: 61857
diff changeset
   982
lemma [code]:
57c0d60e491c do not export abstract constructors in code_reflect
haftmann
parents: 61857
diff changeset
   983
  "natural_of_nat n = natural_of_integer (integer_of_nat n)"
57c0d60e491c do not export abstract constructors in code_reflect
haftmann
parents: 61857
diff changeset
   984
  by transfer simp
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   985
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   986
lemma [code abstract]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   987
  "integer_of_natural (natural_of_integer k) = max 0 k"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   988
  by simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   989
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   990
lemma [code_abbrev]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   991
  "natural_of_integer (Code_Numeral.Pos k) = numeral k"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   992
  by transfer simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   993
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   994
lemma [code abstract]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   995
  "integer_of_natural 0 = 0"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   996
  by transfer simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   997
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   998
lemma [code abstract]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
   999
  "integer_of_natural 1 = 1"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1000
  by transfer simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1001
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1002
lemma [code abstract]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1003
  "integer_of_natural (Code_Numeral.Suc n) = integer_of_natural n + 1"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1004
  by transfer simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1005
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1006
lemma [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1007
  "nat_of_natural = nat_of_integer \<circ> integer_of_natural"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1008
  by transfer (simp add: fun_eq_iff)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1009
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1010
lemma [code, code_unfold]:
55416
dd7992d4a61a adapted theories to 'xxx_case' to 'case_xxx'
blanchet
parents: 55414
diff changeset
  1011
  "case_natural f g n = (if n = 0 then f else g (n - 1))"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1012
  by (cases n rule: natural.exhaust) (simp_all, simp add: Suc_def)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1013
55642
63beb38e9258 adapted to renaming of datatype 'cases' and 'recs' to 'case' and 'rec'
blanchet
parents: 55428
diff changeset
  1014
declare natural.rec [code del]
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1015
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1016
lemma [code abstract]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1017
  "integer_of_natural (m + n) = integer_of_natural m + integer_of_natural n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1018
  by transfer simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1019
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1020
lemma [code abstract]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1021
  "integer_of_natural (m - n) = max 0 (integer_of_natural m - integer_of_natural n)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1022
  by transfer simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1023
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1024
lemma [code abstract]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1025
  "integer_of_natural (m * n) = integer_of_natural m * integer_of_natural n"
64592
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1026
  by transfer simp
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1027
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1028
lemma [code]:
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1029
  "normalize n = n" for n :: natural
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1030
  by transfer simp
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1031
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1032
lemma [code]:
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1033
  "unit_factor n = of_bool (n \<noteq> 0)" for n :: natural
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1034
proof (cases "n = 0")
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1035
  case True
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1036
  then show ?thesis
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1037
    by simp
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1038
next
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1039
  case False
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1040
  then have "unit_factor n = 1"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1041
  proof transfer
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1042
    fix n :: nat
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1043
    assume "n \<noteq> 0"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1044
    then obtain m where "n = Suc m"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1045
      by (cases n) auto
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1046
    then show "unit_factor n = 1"
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1047
      by simp
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1048
  qed
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1049
  with False show ?thesis
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1050
    by simp
7759f1766189 more fine-grained type class hierarchy for div and mod
haftmann
parents: 64246
diff changeset
  1051
qed
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1052
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1053
lemma [code abstract]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1054
  "integer_of_natural (m div n) = integer_of_natural m div integer_of_natural n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1055
  by transfer (simp add: zdiv_int)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1056
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1057
lemma [code abstract]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1058
  "integer_of_natural (m mod n) = integer_of_natural m mod integer_of_natural n"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1059
  by transfer (simp add: zmod_int)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1060
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1061
lemma [code]:
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1062
  "HOL.equal m n \<longleftrightarrow> HOL.equal (integer_of_natural m) (integer_of_natural n)"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1063
  by transfer (simp add: equal)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1064
58379
c044539a2bda reintroduced an instantiation of 'size' for 'numerals'
blanchet
parents: 58377
diff changeset
  1065
lemma [code nbe]: "HOL.equal n (n::natural) \<longleftrightarrow> True"
c044539a2bda reintroduced an instantiation of 'size' for 'numerals'
blanchet
parents: 58377
diff changeset
  1066
  by (rule equal_class.equal_refl)
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1067
58379
c044539a2bda reintroduced an instantiation of 'size' for 'numerals'
blanchet
parents: 58377
diff changeset
  1068
lemma [code]: "m \<le> n \<longleftrightarrow> integer_of_natural m \<le> integer_of_natural n"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1069
  by transfer simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1070
58379
c044539a2bda reintroduced an instantiation of 'size' for 'numerals'
blanchet
parents: 58377
diff changeset
  1071
lemma [code]: "m < n \<longleftrightarrow> integer_of_natural m < integer_of_natural n"
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1072
  by transfer simp
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1073
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1074
hide_const (open) Nat
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1075
55736
f1ed1e9cd080 unregister lifting setup following the best practice of Lifting
kuncar
parents: 55642
diff changeset
  1076
lifting_update integer.lifting
f1ed1e9cd080 unregister lifting setup following the best practice of Lifting
kuncar
parents: 55642
diff changeset
  1077
lifting_forget integer.lifting
f1ed1e9cd080 unregister lifting setup following the best practice of Lifting
kuncar
parents: 55642
diff changeset
  1078
f1ed1e9cd080 unregister lifting setup following the best practice of Lifting
kuncar
parents: 55642
diff changeset
  1079
lifting_update natural.lifting
f1ed1e9cd080 unregister lifting setup following the best practice of Lifting
kuncar
parents: 55642
diff changeset
  1080
lifting_forget natural.lifting
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1081
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1082
code_reflect Code_Numeral
63174
57c0d60e491c do not export abstract constructors in code_reflect
haftmann
parents: 61857
diff changeset
  1083
  datatypes natural
57c0d60e491c do not export abstract constructors in code_reflect
haftmann
parents: 61857
diff changeset
  1084
  functions "Code_Numeral.Suc" "0 :: natural" "1 :: natural"
57c0d60e491c do not export abstract constructors in code_reflect
haftmann
parents: 61857
diff changeset
  1085
    "plus :: natural \<Rightarrow> _" "minus :: natural \<Rightarrow> _"
57c0d60e491c do not export abstract constructors in code_reflect
haftmann
parents: 61857
diff changeset
  1086
    "times :: natural \<Rightarrow> _" "divide :: natural \<Rightarrow> _"
63950
cdc1e59aa513 syntactic type class for operation mod named after mod;
haftmann
parents: 63174
diff changeset
  1087
    "modulo :: natural \<Rightarrow> _"
63174
57c0d60e491c do not export abstract constructors in code_reflect
haftmann
parents: 61857
diff changeset
  1088
    integer_of_natural natural_of_integer
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1089
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  1090
end