src/HOL/Library/Target_Numeral.thy
author Christian Sternagel
Wed Aug 29 12:23:14 2012 +0900 (2012-08-29)
changeset 49083 01081bca31b6
parent 48075 ec5e62b868eb
child 49834 b27bbb021df1
permissions -rw-r--r--
dropped ord and bot instance for list prefixes (use locale interpretation instead, which allows users to decide what order to use on lists)
huffman@47108
     1
theory Target_Numeral
huffman@47108
     2
imports Main Code_Nat
huffman@47108
     3
begin
huffman@47108
     4
huffman@47108
     5
subsection {* Type of target language numerals *}
huffman@47108
     6
huffman@47108
     7
typedef (open) int = "UNIV \<Colon> int set"
huffman@47108
     8
  morphisms int_of of_int ..
huffman@47108
     9
huffman@47108
    10
hide_type (open) int
huffman@47108
    11
hide_const (open) of_int
huffman@47108
    12
huffman@47108
    13
lemma int_eq_iff:
huffman@47108
    14
  "k = l \<longleftrightarrow> int_of k = int_of l"
huffman@47108
    15
  using int_of_inject [of k l] ..
huffman@47108
    16
huffman@47108
    17
lemma int_eqI:
huffman@47108
    18
  "int_of k = int_of l \<Longrightarrow> k = l"
huffman@47108
    19
  using int_eq_iff [of k l] by simp
huffman@47108
    20
huffman@47108
    21
lemma int_of_int [simp]:
huffman@47108
    22
  "int_of (Target_Numeral.of_int k) = k"
huffman@47108
    23
  using of_int_inverse [of k] by simp
huffman@47108
    24
huffman@47108
    25
lemma of_int_of [simp]:
huffman@47108
    26
  "Target_Numeral.of_int (int_of k) = k"
huffman@47108
    27
  using int_of_inverse [of k] by simp
huffman@47108
    28
huffman@47108
    29
hide_fact (open) int_eq_iff int_eqI
huffman@47108
    30
huffman@47108
    31
instantiation Target_Numeral.int :: ring_1
huffman@47108
    32
begin
huffman@47108
    33
huffman@47108
    34
definition
huffman@47108
    35
  "0 = Target_Numeral.of_int 0"
huffman@47108
    36
huffman@47108
    37
lemma int_of_zero [simp]:
huffman@47108
    38
  "int_of 0 = 0"
huffman@47108
    39
  by (simp add: zero_int_def)
huffman@47108
    40
huffman@47108
    41
definition
huffman@47108
    42
  "1 = Target_Numeral.of_int 1"
huffman@47108
    43
huffman@47108
    44
lemma int_of_one [simp]:
huffman@47108
    45
  "int_of 1 = 1"
huffman@47108
    46
  by (simp add: one_int_def)
huffman@47108
    47
huffman@47108
    48
definition
huffman@47108
    49
  "k + l = Target_Numeral.of_int (int_of k + int_of l)"
huffman@47108
    50
huffman@47108
    51
lemma int_of_plus [simp]:
huffman@47108
    52
  "int_of (k + l) = int_of k + int_of l"
huffman@47108
    53
  by (simp add: plus_int_def)
huffman@47108
    54
huffman@47108
    55
definition
huffman@47108
    56
  "- k = Target_Numeral.of_int (- int_of k)"
huffman@47108
    57
huffman@47108
    58
lemma int_of_uminus [simp]:
huffman@47108
    59
  "int_of (- k) = - int_of k"
huffman@47108
    60
  by (simp add: uminus_int_def)
huffman@47108
    61
huffman@47108
    62
definition
huffman@47108
    63
  "k - l = Target_Numeral.of_int (int_of k - int_of l)"
huffman@47108
    64
huffman@47108
    65
lemma int_of_minus [simp]:
huffman@47108
    66
  "int_of (k - l) = int_of k - int_of l"
huffman@47108
    67
  by (simp add: minus_int_def)
huffman@47108
    68
huffman@47108
    69
definition
huffman@47108
    70
  "k * l = Target_Numeral.of_int (int_of k * int_of l)"
huffman@47108
    71
huffman@47108
    72
lemma int_of_times [simp]:
huffman@47108
    73
  "int_of (k * l) = int_of k * int_of l"
huffman@47108
    74
  by (simp add: times_int_def)
huffman@47108
    75
huffman@47108
    76
instance proof
huffman@47108
    77
qed (auto simp add: Target_Numeral.int_eq_iff algebra_simps)
huffman@47108
    78
huffman@47108
    79
end
huffman@47108
    80
huffman@47108
    81
lemma int_of_of_nat [simp]:
huffman@47108
    82
  "int_of (of_nat n) = of_nat n"
huffman@47108
    83
  by (induct n) simp_all
huffman@47108
    84
huffman@47108
    85
definition nat_of :: "Target_Numeral.int \<Rightarrow> nat" where
huffman@47108
    86
  "nat_of k = Int.nat (int_of k)"
huffman@47108
    87
huffman@47108
    88
lemma nat_of_of_nat [simp]:
huffman@47108
    89
  "nat_of (of_nat n) = n"
huffman@47108
    90
  by (simp add: nat_of_def)
huffman@47108
    91
huffman@47108
    92
lemma int_of_of_int [simp]:
huffman@47108
    93
  "int_of (of_int k) = k"
huffman@47108
    94
  by (induct k) (simp_all, simp only: neg_numeral_def numeral_One int_of_uminus int_of_one)
huffman@47108
    95
huffman@47108
    96
lemma of_int_of_int [simp, code_abbrev]:
huffman@47108
    97
  "Target_Numeral.of_int = of_int"
huffman@47108
    98
  by rule (simp add: Target_Numeral.int_eq_iff)
huffman@47108
    99
huffman@47108
   100
lemma int_of_numeral [simp]:
huffman@47108
   101
  "int_of (numeral k) = numeral k"
huffman@47108
   102
  using int_of_of_int [of "numeral k"] by simp
huffman@47108
   103
huffman@47108
   104
lemma int_of_neg_numeral [simp]:
huffman@47108
   105
  "int_of (neg_numeral k) = neg_numeral k"
huffman@47108
   106
  by (simp only: neg_numeral_def int_of_uminus) simp
huffman@47108
   107
huffman@47108
   108
lemma int_of_sub [simp]:
huffman@47108
   109
  "int_of (Num.sub k l) = Num.sub k l"
huffman@47108
   110
  by (simp only: Num.sub_def int_of_minus int_of_numeral)
huffman@47108
   111
huffman@47108
   112
instantiation Target_Numeral.int :: "{ring_div, equal, linordered_idom}"
huffman@47108
   113
begin
huffman@47108
   114
huffman@47108
   115
definition
huffman@47108
   116
  "k div l = of_int (int_of k div int_of l)"
huffman@47108
   117
huffman@47108
   118
lemma int_of_div [simp]:
huffman@47108
   119
  "int_of (k div l) = int_of k div int_of l"
huffman@47108
   120
  by (simp add: div_int_def)
huffman@47108
   121
huffman@47108
   122
definition
huffman@47108
   123
  "k mod l = of_int (int_of k mod int_of l)"
huffman@47108
   124
huffman@47108
   125
lemma int_of_mod [simp]:
huffman@47108
   126
  "int_of (k mod l) = int_of k mod int_of l"
huffman@47108
   127
  by (simp add: mod_int_def)
huffman@47108
   128
huffman@47108
   129
definition
huffman@47108
   130
  "\<bar>k\<bar> = of_int \<bar>int_of k\<bar>"
huffman@47108
   131
huffman@47108
   132
lemma int_of_abs [simp]:
huffman@47108
   133
  "int_of \<bar>k\<bar> = \<bar>int_of k\<bar>"
huffman@47108
   134
  by (simp add: abs_int_def)
huffman@47108
   135
huffman@47108
   136
definition
huffman@47108
   137
  "sgn k = of_int (sgn (int_of k))"
huffman@47108
   138
huffman@47108
   139
lemma int_of_sgn [simp]:
huffman@47108
   140
  "int_of (sgn k) = sgn (int_of k)"
huffman@47108
   141
  by (simp add: sgn_int_def)
huffman@47108
   142
huffman@47108
   143
definition
huffman@47108
   144
  "k \<le> l \<longleftrightarrow> int_of k \<le> int_of l"
huffman@47108
   145
huffman@47108
   146
definition
huffman@47108
   147
  "k < l \<longleftrightarrow> int_of k < int_of l"
huffman@47108
   148
huffman@47108
   149
definition
huffman@47108
   150
  "HOL.equal k l \<longleftrightarrow> HOL.equal (int_of k) (int_of l)"
huffman@47108
   151
huffman@47108
   152
instance proof
huffman@47108
   153
qed (auto simp add: Target_Numeral.int_eq_iff algebra_simps
huffman@47108
   154
  less_eq_int_def less_int_def equal_int_def equal)
huffman@47108
   155
huffman@47108
   156
end
huffman@47108
   157
huffman@47108
   158
lemma int_of_min [simp]:
huffman@47108
   159
  "int_of (min k l) = min (int_of k) (int_of l)"
huffman@47108
   160
  by (simp add: min_def less_eq_int_def)
huffman@47108
   161
huffman@47108
   162
lemma int_of_max [simp]:
huffman@47108
   163
  "int_of (max k l) = max (int_of k) (int_of l)"
huffman@47108
   164
  by (simp add: max_def less_eq_int_def)
huffman@47108
   165
haftmann@47400
   166
lemma of_nat_nat_of [simp]:
haftmann@47400
   167
  "of_nat (nat_of k) = max 0 k"
haftmann@47400
   168
  by (simp add: nat_of_def Target_Numeral.int_eq_iff less_eq_int_def max_def)
haftmann@47400
   169
huffman@47108
   170
huffman@47108
   171
subsection {* Code theorems for target language numerals *}
huffman@47108
   172
huffman@47108
   173
text {* Constructors *}
huffman@47108
   174
huffman@47108
   175
definition Pos :: "num \<Rightarrow> Target_Numeral.int" where
huffman@47108
   176
  [simp, code_abbrev]: "Pos = numeral"
huffman@47108
   177
huffman@47108
   178
definition Neg :: "num \<Rightarrow> Target_Numeral.int" where
huffman@47108
   179
  [simp, code_abbrev]: "Neg = neg_numeral"
huffman@47108
   180
huffman@47108
   181
code_datatype "0::Target_Numeral.int" Pos Neg
huffman@47108
   182
huffman@47108
   183
huffman@47108
   184
text {* Auxiliary operations *}
huffman@47108
   185
huffman@47108
   186
definition dup :: "Target_Numeral.int \<Rightarrow> Target_Numeral.int" where
huffman@47108
   187
  [simp]: "dup k = k + k"
huffman@47108
   188
huffman@47108
   189
lemma dup_code [code]:
huffman@47108
   190
  "dup 0 = 0"
huffman@47108
   191
  "dup (Pos n) = Pos (Num.Bit0 n)"
huffman@47108
   192
  "dup (Neg n) = Neg (Num.Bit0 n)"
huffman@47108
   193
  unfolding Pos_def Neg_def neg_numeral_def
huffman@47108
   194
  by (simp_all add: numeral_Bit0)
huffman@47108
   195
huffman@47108
   196
definition sub :: "num \<Rightarrow> num \<Rightarrow> Target_Numeral.int" where
huffman@47108
   197
  [simp]: "sub m n = numeral m - numeral n"
huffman@47108
   198
huffman@47108
   199
lemma sub_code [code]:
huffman@47108
   200
  "sub Num.One Num.One = 0"
huffman@47108
   201
  "sub (Num.Bit0 m) Num.One = Pos (Num.BitM m)"
huffman@47108
   202
  "sub (Num.Bit1 m) Num.One = Pos (Num.Bit0 m)"
huffman@47108
   203
  "sub Num.One (Num.Bit0 n) = Neg (Num.BitM n)"
huffman@47108
   204
  "sub Num.One (Num.Bit1 n) = Neg (Num.Bit0 n)"
huffman@47108
   205
  "sub (Num.Bit0 m) (Num.Bit0 n) = dup (sub m n)"
huffman@47108
   206
  "sub (Num.Bit1 m) (Num.Bit1 n) = dup (sub m n)"
huffman@47108
   207
  "sub (Num.Bit1 m) (Num.Bit0 n) = dup (sub m n) + 1"
huffman@47108
   208
  "sub (Num.Bit0 m) (Num.Bit1 n) = dup (sub m n) - 1"
huffman@47108
   209
  unfolding sub_def dup_def numeral.simps Pos_def Neg_def
huffman@47108
   210
    neg_numeral_def numeral_BitM
huffman@47108
   211
  by (simp_all only: algebra_simps add.comm_neutral)
huffman@47108
   212
huffman@47108
   213
huffman@47108
   214
text {* Implementations *}
huffman@47108
   215
huffman@47108
   216
lemma one_int_code [code, code_unfold]:
huffman@47108
   217
  "1 = Pos Num.One"
huffman@47108
   218
  by simp
huffman@47108
   219
huffman@47108
   220
lemma plus_int_code [code]:
huffman@47108
   221
  "k + 0 = (k::Target_Numeral.int)"
huffman@47108
   222
  "0 + l = (l::Target_Numeral.int)"
huffman@47108
   223
  "Pos m + Pos n = Pos (m + n)"
huffman@47108
   224
  "Pos m + Neg n = sub m n"
huffman@47108
   225
  "Neg m + Pos n = sub n m"
huffman@47108
   226
  "Neg m + Neg n = Neg (m + n)"
huffman@47108
   227
  by simp_all
huffman@47108
   228
huffman@47108
   229
lemma uminus_int_code [code]:
huffman@47108
   230
  "uminus 0 = (0::Target_Numeral.int)"
huffman@47108
   231
  "uminus (Pos m) = Neg m"
huffman@47108
   232
  "uminus (Neg m) = Pos m"
huffman@47108
   233
  by simp_all
huffman@47108
   234
huffman@47108
   235
lemma minus_int_code [code]:
huffman@47108
   236
  "k - 0 = (k::Target_Numeral.int)"
huffman@47108
   237
  "0 - l = uminus (l::Target_Numeral.int)"
huffman@47108
   238
  "Pos m - Pos n = sub m n"
huffman@47108
   239
  "Pos m - Neg n = Pos (m + n)"
huffman@47108
   240
  "Neg m - Pos n = Neg (m + n)"
huffman@47108
   241
  "Neg m - Neg n = sub n m"
huffman@47108
   242
  by simp_all
huffman@47108
   243
huffman@47108
   244
lemma times_int_code [code]:
huffman@47108
   245
  "k * 0 = (0::Target_Numeral.int)"
huffman@47108
   246
  "0 * l = (0::Target_Numeral.int)"
huffman@47108
   247
  "Pos m * Pos n = Pos (m * n)"
huffman@47108
   248
  "Pos m * Neg n = Neg (m * n)"
huffman@47108
   249
  "Neg m * Pos n = Neg (m * n)"
huffman@47108
   250
  "Neg m * Neg n = Pos (m * n)"
huffman@47108
   251
  by simp_all
huffman@47108
   252
huffman@47108
   253
definition divmod :: "Target_Numeral.int \<Rightarrow> Target_Numeral.int \<Rightarrow> Target_Numeral.int \<times> Target_Numeral.int" where
huffman@47108
   254
  "divmod k l = (k div l, k mod l)"
huffman@47108
   255
huffman@47108
   256
lemma fst_divmod [simp]:
huffman@47108
   257
  "fst (divmod k l) = k div l"
huffman@47108
   258
  by (simp add: divmod_def)
huffman@47108
   259
huffman@47108
   260
lemma snd_divmod [simp]:
huffman@47108
   261
  "snd (divmod k l) = k mod l"
huffman@47108
   262
  by (simp add: divmod_def)
huffman@47108
   263
huffman@47108
   264
definition divmod_abs :: "Target_Numeral.int \<Rightarrow> Target_Numeral.int \<Rightarrow> Target_Numeral.int \<times> Target_Numeral.int" where
huffman@47108
   265
  "divmod_abs k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)"
huffman@47108
   266
huffman@47108
   267
lemma fst_divmod_abs [simp]:
huffman@47108
   268
  "fst (divmod_abs k l) = \<bar>k\<bar> div \<bar>l\<bar>"
huffman@47108
   269
  by (simp add: divmod_abs_def)
huffman@47108
   270
huffman@47108
   271
lemma snd_divmod_abs [simp]:
huffman@47108
   272
  "snd (divmod_abs k l) = \<bar>k\<bar> mod \<bar>l\<bar>"
huffman@47108
   273
  by (simp add: divmod_abs_def)
huffman@47108
   274
huffman@47108
   275
lemma divmod_abs_terminate_code [code]:
huffman@47108
   276
  "divmod_abs (Neg k) (Neg l) = divmod_abs (Pos k) (Pos l)"
huffman@47108
   277
  "divmod_abs (Neg k) (Pos l) = divmod_abs (Pos k) (Pos l)"
huffman@47108
   278
  "divmod_abs (Pos k) (Neg l) = divmod_abs (Pos k) (Pos l)"
huffman@47108
   279
  "divmod_abs j 0 = (0, \<bar>j\<bar>)"
huffman@47108
   280
  "divmod_abs 0 j = (0, 0)"
huffman@47108
   281
  by (simp_all add: prod_eq_iff)
huffman@47108
   282
huffman@47108
   283
lemma divmod_abs_rec_code [code]:
huffman@47108
   284
  "divmod_abs (Pos k) (Pos l) =
huffman@47108
   285
    (let j = sub k l in
huffman@47108
   286
       if j < 0 then (0, Pos k)
huffman@47108
   287
       else let (q, r) = divmod_abs j (Pos l) in (q + 1, r))"
huffman@47108
   288
  by (auto simp add: prod_eq_iff Target_Numeral.int_eq_iff Let_def prod_case_beta
huffman@47108
   289
    sub_non_negative sub_negative div_pos_pos_trivial mod_pos_pos_trivial div_pos_geq mod_pos_geq)
huffman@47108
   290
huffman@47108
   291
lemma divmod_code [code]: "divmod k l =
huffman@47108
   292
  (if k = 0 then (0, 0) else if l = 0 then (0, k) else
huffman@47108
   293
  (apsnd \<circ> times \<circ> sgn) l (if sgn k = sgn l
huffman@47108
   294
    then divmod_abs k l
huffman@47108
   295
    else (let (r, s) = divmod_abs k l in
huffman@47108
   296
      if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
huffman@47108
   297
proof -
huffman@47108
   298
  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"
huffman@47108
   299
    by (auto simp add: sgn_if)
huffman@47108
   300
  have aux2: "\<And>q::int. - int_of k = int_of l * q \<longleftrightarrow> int_of k = int_of l * - q" by auto
huffman@47108
   301
  show ?thesis
huffman@47108
   302
    by (simp add: prod_eq_iff Target_Numeral.int_eq_iff prod_case_beta aux1)
huffman@47159
   303
      (auto simp add: zdiv_zminus1_eq_if zmod_zminus1_eq_if div_minus_right mod_minus_right aux2)
huffman@47108
   304
qed
huffman@47108
   305
huffman@47108
   306
lemma div_int_code [code]:
huffman@47108
   307
  "k div l = fst (divmod k l)"
huffman@47108
   308
  by simp
huffman@47108
   309
huffman@47108
   310
lemma div_mod_code [code]:
huffman@47108
   311
  "k mod l = snd (divmod k l)"
huffman@47108
   312
  by simp
huffman@47108
   313
huffman@47108
   314
lemma equal_int_code [code]:
huffman@47108
   315
  "HOL.equal 0 (0::Target_Numeral.int) \<longleftrightarrow> True"
huffman@47108
   316
  "HOL.equal 0 (Pos l) \<longleftrightarrow> False"
huffman@47108
   317
  "HOL.equal 0 (Neg l) \<longleftrightarrow> False"
huffman@47108
   318
  "HOL.equal (Pos k) 0 \<longleftrightarrow> False"
huffman@47108
   319
  "HOL.equal (Pos k) (Pos l) \<longleftrightarrow> HOL.equal k l"
huffman@47108
   320
  "HOL.equal (Pos k) (Neg l) \<longleftrightarrow> False"
huffman@47108
   321
  "HOL.equal (Neg k) 0 \<longleftrightarrow> False"
huffman@47108
   322
  "HOL.equal (Neg k) (Pos l) \<longleftrightarrow> False"
huffman@47108
   323
  "HOL.equal (Neg k) (Neg l) \<longleftrightarrow> HOL.equal k l"
huffman@47108
   324
  by (simp_all add: equal Target_Numeral.int_eq_iff)
huffman@47108
   325
huffman@47108
   326
lemma equal_int_refl [code nbe]:
huffman@47108
   327
  "HOL.equal (k::Target_Numeral.int) k \<longleftrightarrow> True"
huffman@47108
   328
  by (fact equal_refl)
huffman@47108
   329
huffman@47108
   330
lemma less_eq_int_code [code]:
huffman@47108
   331
  "0 \<le> (0::Target_Numeral.int) \<longleftrightarrow> True"
huffman@47108
   332
  "0 \<le> Pos l \<longleftrightarrow> True"
huffman@47108
   333
  "0 \<le> Neg l \<longleftrightarrow> False"
huffman@47108
   334
  "Pos k \<le> 0 \<longleftrightarrow> False"
huffman@47108
   335
  "Pos k \<le> Pos l \<longleftrightarrow> k \<le> l"
huffman@47108
   336
  "Pos k \<le> Neg l \<longleftrightarrow> False"
huffman@47108
   337
  "Neg k \<le> 0 \<longleftrightarrow> True"
huffman@47108
   338
  "Neg k \<le> Pos l \<longleftrightarrow> True"
huffman@47108
   339
  "Neg k \<le> Neg l \<longleftrightarrow> l \<le> k"
huffman@47108
   340
  by (simp_all add: less_eq_int_def)
huffman@47108
   341
huffman@47108
   342
lemma less_int_code [code]:
huffman@47108
   343
  "0 < (0::Target_Numeral.int) \<longleftrightarrow> False"
huffman@47108
   344
  "0 < Pos l \<longleftrightarrow> True"
huffman@47108
   345
  "0 < Neg l \<longleftrightarrow> False"
huffman@47108
   346
  "Pos k < 0 \<longleftrightarrow> False"
huffman@47108
   347
  "Pos k < Pos l \<longleftrightarrow> k < l"
huffman@47108
   348
  "Pos k < Neg l \<longleftrightarrow> False"
huffman@47108
   349
  "Neg k < 0 \<longleftrightarrow> True"
huffman@47108
   350
  "Neg k < Pos l \<longleftrightarrow> True"
huffman@47108
   351
  "Neg k < Neg l \<longleftrightarrow> l < k"
huffman@47108
   352
  by (simp_all add: less_int_def)
huffman@47108
   353
huffman@47108
   354
lemma nat_of_code [code]:
huffman@47108
   355
  "nat_of (Neg k) = 0"
huffman@47108
   356
  "nat_of 0 = 0"
huffman@47108
   357
  "nat_of (Pos k) = nat_of_num k"
huffman@47108
   358
  by (simp_all add: nat_of_def nat_of_num_numeral)
huffman@47108
   359
huffman@47108
   360
lemma int_of_code [code]:
huffman@47108
   361
  "int_of (Neg k) = neg_numeral k"
huffman@47108
   362
  "int_of 0 = 0"
huffman@47108
   363
  "int_of (Pos k) = numeral k"
huffman@47108
   364
  by simp_all
huffman@47108
   365
huffman@47108
   366
lemma of_int_code [code]:
huffman@47108
   367
  "Target_Numeral.of_int (Int.Neg k) = neg_numeral k"
huffman@47108
   368
  "Target_Numeral.of_int 0 = 0"
huffman@47108
   369
  "Target_Numeral.of_int (Int.Pos k) = numeral k"
huffman@47108
   370
  by simp_all
huffman@47108
   371
huffman@47108
   372
definition num_of_int :: "Target_Numeral.int \<Rightarrow> num" where
huffman@47108
   373
  "num_of_int = num_of_nat \<circ> nat_of"
huffman@47108
   374
huffman@47108
   375
lemma num_of_int_code [code]:
huffman@47108
   376
  "num_of_int k = (if k \<le> 1 then Num.One
huffman@47108
   377
     else let
huffman@47108
   378
       (l, j) = divmod k 2;
huffman@47108
   379
       l' = num_of_int l + num_of_int l
huffman@47108
   380
     in if j = 0 then l' else l' + Num.One)"
huffman@47108
   381
proof -
huffman@47108
   382
  {
huffman@47108
   383
    assume "int_of k mod 2 = 1"
huffman@47108
   384
    then have "nat (int_of k mod 2) = nat 1" by simp
huffman@47108
   385
    moreover assume *: "1 < int_of k"
huffman@47108
   386
    ultimately have **: "nat (int_of k) mod 2 = 1" by (simp add: nat_mod_distrib)
huffman@47108
   387
    have "num_of_nat (nat (int_of k)) =
huffman@47108
   388
      num_of_nat (2 * (nat (int_of k) div 2) + nat (int_of k) mod 2)"
huffman@47108
   389
      by simp
huffman@47108
   390
    then have "num_of_nat (nat (int_of k)) =
huffman@47108
   391
      num_of_nat (nat (int_of k) div 2 + nat (int_of k) div 2 + nat (int_of k) mod 2)"
huffman@47217
   392
      by (simp add: mult_2)
huffman@47108
   393
    with ** have "num_of_nat (nat (int_of k)) =
huffman@47108
   394
      num_of_nat (nat (int_of k) div 2 + nat (int_of k) div 2 + 1)"
huffman@47108
   395
      by simp
huffman@47108
   396
  }
huffman@47108
   397
  note aux = this
huffman@47108
   398
  show ?thesis
huffman@47108
   399
    by (auto simp add: num_of_int_def nat_of_def Let_def prod_case_beta
huffman@47108
   400
      not_le Target_Numeral.int_eq_iff less_eq_int_def
huffman@47108
   401
      nat_mult_distrib nat_div_distrib num_of_nat_One num_of_nat_plus_distrib
huffman@47217
   402
       mult_2 [where 'a=nat] aux add_One)
huffman@47108
   403
qed
huffman@47108
   404
huffman@47108
   405
hide_const (open) int_of nat_of Pos Neg sub dup divmod_abs num_of_int
huffman@47108
   406
huffman@47108
   407
huffman@47108
   408
subsection {* Serializer setup for target language numerals *}
huffman@47108
   409
huffman@47108
   410
code_type Target_Numeral.int
huffman@47108
   411
  (SML "IntInf.int")
huffman@47108
   412
  (OCaml "Big'_int.big'_int")
huffman@47108
   413
  (Haskell "Integer")
huffman@47108
   414
  (Scala "BigInt")
huffman@47108
   415
  (Eval "int")
huffman@47108
   416
huffman@47108
   417
code_instance Target_Numeral.int :: equal
huffman@47108
   418
  (Haskell -)
huffman@47108
   419
huffman@47108
   420
code_const "0::Target_Numeral.int"
huffman@47108
   421
  (SML "0")
huffman@47108
   422
  (OCaml "Big'_int.zero'_big'_int")
huffman@47108
   423
  (Haskell "0")
huffman@47108
   424
  (Scala "BigInt(0)")
huffman@47108
   425
huffman@47108
   426
setup {*
huffman@47108
   427
  fold (Numeral.add_code @{const_name Target_Numeral.Pos}
huffman@47108
   428
    false Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
huffman@47108
   429
*}
huffman@47108
   430
huffman@47108
   431
setup {*
huffman@47108
   432
  fold (Numeral.add_code @{const_name Target_Numeral.Neg}
huffman@47108
   433
    true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
huffman@47108
   434
*}
huffman@47108
   435
huffman@47108
   436
code_const "plus :: Target_Numeral.int \<Rightarrow> _ \<Rightarrow> _"
huffman@47108
   437
  (SML "IntInf.+ ((_), (_))")
huffman@47108
   438
  (OCaml "Big'_int.add'_big'_int")
huffman@47108
   439
  (Haskell infixl 6 "+")
huffman@47108
   440
  (Scala infixl 7 "+")
huffman@47108
   441
  (Eval infixl 8 "+")
huffman@47108
   442
huffman@47108
   443
code_const "uminus :: Target_Numeral.int \<Rightarrow> _"
huffman@47108
   444
  (SML "IntInf.~")
huffman@47108
   445
  (OCaml "Big'_int.minus'_big'_int")
huffman@47108
   446
  (Haskell "negate")
huffman@47108
   447
  (Scala "!(- _)")
huffman@47108
   448
  (Eval "~/ _")
huffman@47108
   449
huffman@47108
   450
code_const "minus :: Target_Numeral.int \<Rightarrow> _"
huffman@47108
   451
  (SML "IntInf.- ((_), (_))")
huffman@47108
   452
  (OCaml "Big'_int.sub'_big'_int")
huffman@47108
   453
  (Haskell infixl 6 "-")
huffman@47108
   454
  (Scala infixl 7 "-")
huffman@47108
   455
  (Eval infixl 8 "-")
huffman@47108
   456
huffman@47108
   457
code_const Target_Numeral.dup
huffman@47108
   458
  (SML "IntInf.*/ (2,/ (_))")
huffman@47108
   459
  (OCaml "Big'_int.mult'_big'_int/ 2")
huffman@47108
   460
  (Haskell "!(2 * _)")
huffman@47108
   461
  (Scala "!(2 * _)")
huffman@47108
   462
  (Eval "!(2 * _)")
huffman@47108
   463
huffman@47108
   464
code_const Target_Numeral.sub
huffman@47108
   465
  (SML "!(raise/ Fail/ \"sub\")")
huffman@47108
   466
  (OCaml "failwith/ \"sub\"")
huffman@47108
   467
  (Haskell "error/ \"sub\"")
haftmann@48073
   468
  (Scala "!sys.error(\"sub\")")
huffman@47108
   469
huffman@47108
   470
code_const "times :: Target_Numeral.int \<Rightarrow> _ \<Rightarrow> _"
huffman@47108
   471
  (SML "IntInf.* ((_), (_))")
huffman@47108
   472
  (OCaml "Big'_int.mult'_big'_int")
huffman@47108
   473
  (Haskell infixl 7 "*")
huffman@47108
   474
  (Scala infixl 8 "*")
huffman@47108
   475
  (Eval infixl 9 "*")
huffman@47108
   476
huffman@47108
   477
code_const Target_Numeral.divmod_abs
huffman@47108
   478
  (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
huffman@47108
   479
  (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
huffman@47108
   480
  (Haskell "divMod/ (abs _)/ (abs _)")
huffman@47108
   481
  (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
huffman@47108
   482
  (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
huffman@47108
   483
huffman@47108
   484
code_const "HOL.equal :: Target_Numeral.int \<Rightarrow> _ \<Rightarrow> bool"
huffman@47108
   485
  (SML "!((_ : IntInf.int) = _)")
huffman@47108
   486
  (OCaml "Big'_int.eq'_big'_int")
huffman@47108
   487
  (Haskell infix 4 "==")
huffman@47108
   488
  (Scala infixl 5 "==")
huffman@47108
   489
  (Eval infixl 6 "=")
huffman@47108
   490
huffman@47108
   491
code_const "less_eq :: Target_Numeral.int \<Rightarrow> _ \<Rightarrow> bool"
huffman@47108
   492
  (SML "IntInf.<= ((_), (_))")
huffman@47108
   493
  (OCaml "Big'_int.le'_big'_int")
huffman@47108
   494
  (Haskell infix 4 "<=")
huffman@47108
   495
  (Scala infixl 4 "<=")
huffman@47108
   496
  (Eval infixl 6 "<=")
huffman@47108
   497
huffman@47108
   498
code_const "less :: Target_Numeral.int \<Rightarrow> _ \<Rightarrow> bool"
huffman@47108
   499
  (SML "IntInf.< ((_), (_))")
huffman@47108
   500
  (OCaml "Big'_int.lt'_big'_int")
huffman@47108
   501
  (Haskell infix 4 "<")
huffman@47108
   502
  (Scala infixl 4 "<")
huffman@47108
   503
  (Eval infixl 6 "<")
huffman@47108
   504
huffman@47108
   505
ML {*
huffman@47108
   506
structure Target_Numeral =
huffman@47108
   507
struct
huffman@47108
   508
huffman@47108
   509
val T = @{typ "Target_Numeral.int"};
huffman@47108
   510
huffman@47108
   511
end;
huffman@47108
   512
*}
huffman@47108
   513
huffman@47108
   514
code_reserved Eval Target_Numeral
huffman@47108
   515
huffman@47108
   516
code_const "Code_Evaluation.term_of \<Colon> Target_Numeral.int \<Rightarrow> term"
huffman@47108
   517
  (Eval "HOLogic.mk'_number/ Target'_Numeral.T")
huffman@47108
   518
huffman@47108
   519
code_modulename SML
huffman@47108
   520
  Target_Numeral Arith
huffman@47108
   521
huffman@47108
   522
code_modulename OCaml
huffman@47108
   523
  Target_Numeral Arith
huffman@47108
   524
huffman@47108
   525
code_modulename Haskell
huffman@47108
   526
  Target_Numeral Arith
huffman@47108
   527
huffman@47108
   528
huffman@47108
   529
subsection {* Implementation for @{typ int} *}
huffman@47108
   530
huffman@47108
   531
code_datatype Target_Numeral.int_of
huffman@47108
   532
huffman@47108
   533
lemma [code, code del]:
huffman@47108
   534
  "Target_Numeral.of_int = Target_Numeral.of_int" ..
huffman@47108
   535
huffman@47108
   536
lemma [code]:
huffman@47108
   537
  "Target_Numeral.of_int (Target_Numeral.int_of k) = k"
huffman@47108
   538
  by (simp add: Target_Numeral.int_eq_iff)
huffman@47108
   539
huffman@47108
   540
declare Int.Pos_def [code]
huffman@47108
   541
huffman@47108
   542
lemma [code_abbrev]:
huffman@47108
   543
  "Target_Numeral.int_of (Target_Numeral.Pos k) = Int.Pos k"
huffman@47108
   544
  by simp
huffman@47108
   545
huffman@47108
   546
declare Int.Neg_def [code]
huffman@47108
   547
huffman@47108
   548
lemma [code_abbrev]:
huffman@47108
   549
  "Target_Numeral.int_of (Target_Numeral.Neg k) = Int.Neg k"
huffman@47108
   550
  by simp
huffman@47108
   551
huffman@47108
   552
lemma [code]:
huffman@47108
   553
  "0 = Target_Numeral.int_of 0"
huffman@47108
   554
  by simp
huffman@47108
   555
huffman@47108
   556
lemma [code]:
huffman@47108
   557
  "1 = Target_Numeral.int_of 1"
huffman@47108
   558
  by simp
huffman@47108
   559
huffman@47108
   560
lemma [code]:
huffman@47108
   561
  "k + l = Target_Numeral.int_of (of_int k + of_int l)"
huffman@47108
   562
  by simp
huffman@47108
   563
huffman@47108
   564
lemma [code]:
huffman@47108
   565
  "- k = Target_Numeral.int_of (- of_int k)"
huffman@47108
   566
  by simp
huffman@47108
   567
huffman@47108
   568
lemma [code]:
huffman@47108
   569
  "k - l = Target_Numeral.int_of (of_int k - of_int l)"
huffman@47108
   570
  by simp
huffman@47108
   571
huffman@47108
   572
lemma [code]:
huffman@47108
   573
  "Int.dup k = Target_Numeral.int_of (Target_Numeral.dup (of_int k))"
huffman@47108
   574
  by simp
huffman@47108
   575
huffman@47108
   576
lemma [code, code del]:
huffman@47108
   577
  "Int.sub = Int.sub" ..
huffman@47108
   578
huffman@47108
   579
lemma [code]:
huffman@47108
   580
  "k * l = Target_Numeral.int_of (of_int k * of_int l)"
huffman@47108
   581
  by simp
huffman@47108
   582
huffman@47108
   583
lemma [code]:
huffman@47108
   584
  "pdivmod k l = map_pair Target_Numeral.int_of Target_Numeral.int_of
huffman@47108
   585
    (Target_Numeral.divmod_abs (of_int k) (of_int l))"
huffman@47108
   586
  by (simp add: prod_eq_iff pdivmod_def)
huffman@47108
   587
huffman@47108
   588
lemma [code]:
huffman@47108
   589
  "k div l = Target_Numeral.int_of (of_int k div of_int l)"
huffman@47108
   590
  by simp
huffman@47108
   591
huffman@47108
   592
lemma [code]:
huffman@47108
   593
  "k mod l = Target_Numeral.int_of (of_int k mod of_int l)"
huffman@47108
   594
  by simp
huffman@47108
   595
huffman@47108
   596
lemma [code]:
huffman@47108
   597
  "HOL.equal k l = HOL.equal (of_int k :: Target_Numeral.int) (of_int l)"
huffman@47108
   598
  by (simp add: equal Target_Numeral.int_eq_iff)
huffman@47108
   599
huffman@47108
   600
lemma [code]:
huffman@47108
   601
  "k \<le> l \<longleftrightarrow> (of_int k :: Target_Numeral.int) \<le> of_int l"
huffman@47108
   602
  by (simp add: less_eq_int_def)
huffman@47108
   603
huffman@47108
   604
lemma [code]:
huffman@47108
   605
  "k < l \<longleftrightarrow> (of_int k :: Target_Numeral.int) < of_int l"
huffman@47108
   606
  by (simp add: less_int_def)
huffman@47108
   607
huffman@47108
   608
lemma (in ring_1) of_int_code:
huffman@47108
   609
  "of_int k = (if k = 0 then 0
huffman@47108
   610
     else if k < 0 then - of_int (- k)
huffman@47108
   611
     else let
huffman@47108
   612
       (l, j) = divmod_int k 2;
huffman@47108
   613
       l' = 2 * of_int l
huffman@47108
   614
     in if j = 0 then l' else l' + 1)"
huffman@47108
   615
proof -
huffman@47108
   616
  from mod_div_equality have *: "of_int k = of_int (k div 2 * 2 + k mod 2)" by simp
huffman@47108
   617
  show ?thesis
huffman@47108
   618
    by (simp add: Let_def divmod_int_mod_div mod_2_not_eq_zero_eq_one_int
huffman@47108
   619
      of_int_add [symmetric]) (simp add: * mult_commute)
huffman@47108
   620
qed
huffman@47108
   621
huffman@47108
   622
declare of_int_code [code]
huffman@47108
   623
huffman@47108
   624
huffman@47108
   625
subsection {* Implementation for @{typ nat} *}
huffman@47108
   626
haftmann@47400
   627
definition Nat :: "Target_Numeral.int \<Rightarrow> nat" where
haftmann@47400
   628
  "Nat = Target_Numeral.nat_of"
haftmann@47400
   629
huffman@47108
   630
definition of_nat :: "nat \<Rightarrow> Target_Numeral.int" where
huffman@47108
   631
  [code_abbrev]: "of_nat = Nat.of_nat"
huffman@47108
   632
haftmann@47400
   633
hide_const (open) of_nat Nat
huffman@47108
   634
haftmann@47830
   635
lemma [code_unfold]:
haftmann@47830
   636
  "Int.nat (Target_Numeral.int_of k) = Target_Numeral.nat_of k"
haftmann@47830
   637
  by (simp add: nat_of_def)
haftmann@47830
   638
huffman@47108
   639
lemma int_of_nat [simp]:
huffman@47108
   640
  "Target_Numeral.int_of (Target_Numeral.of_nat n) = of_nat n"
huffman@47108
   641
  by (simp add: of_nat_def)
huffman@47108
   642
huffman@47108
   643
lemma [code abstype]:
haftmann@47400
   644
  "Target_Numeral.Nat (Target_Numeral.of_nat n) = n"
haftmann@47400
   645
  by (simp add: Nat_def nat_of_def)
haftmann@47400
   646
haftmann@47400
   647
lemma [code abstract]:
haftmann@47400
   648
  "Target_Numeral.of_nat (Target_Numeral.nat_of k) = max 0 k"
haftmann@47400
   649
  by (simp add: of_nat_def)
huffman@47108
   650
huffman@47108
   651
lemma [code_abbrev]:
huffman@47108
   652
  "nat (Int.Pos k) = nat_of_num k"
huffman@47108
   653
  by (simp add: nat_of_num_numeral)
huffman@47108
   654
huffman@47108
   655
lemma [code abstract]:
huffman@47108
   656
  "Target_Numeral.of_nat 0 = 0"
huffman@47108
   657
  by (simp add: Target_Numeral.int_eq_iff)
huffman@47108
   658
huffman@47108
   659
lemma [code abstract]:
huffman@47108
   660
  "Target_Numeral.of_nat 1 = 1"
huffman@47108
   661
  by (simp add: Target_Numeral.int_eq_iff)
huffman@47108
   662
huffman@47108
   663
lemma [code abstract]:
haftmann@48075
   664
  "Target_Numeral.of_nat (m + n) = of_nat m + of_nat n"
huffman@47108
   665
  by (simp add: Target_Numeral.int_eq_iff)
huffman@47108
   666
huffman@47108
   667
lemma [code abstract]:
haftmann@48075
   668
  "Target_Numeral.of_nat (Code_Nat.dup n) = Target_Numeral.dup (of_nat n)"
huffman@47108
   669
  by (simp add: Target_Numeral.int_eq_iff Code_Nat.dup_def)
huffman@47108
   670
huffman@47108
   671
lemma [code, code del]:
huffman@47108
   672
  "Code_Nat.sub = Code_Nat.sub" ..
huffman@47108
   673
huffman@47108
   674
lemma [code abstract]:
haftmann@48075
   675
  "Target_Numeral.of_nat (m - n) = max 0 (of_nat m - of_nat n)"
huffman@47108
   676
  by (simp add: Target_Numeral.int_eq_iff)
huffman@47108
   677
huffman@47108
   678
lemma [code abstract]:
haftmann@48075
   679
  "Target_Numeral.of_nat (m * n) = of_nat m * of_nat n"
huffman@47108
   680
  by (simp add: Target_Numeral.int_eq_iff of_nat_mult)
huffman@47108
   681
huffman@47108
   682
lemma [code abstract]:
haftmann@48075
   683
  "Target_Numeral.of_nat (m div n) = of_nat m div of_nat n"
huffman@47108
   684
  by (simp add: Target_Numeral.int_eq_iff zdiv_int)
huffman@47108
   685
huffman@47108
   686
lemma [code abstract]:
haftmann@48075
   687
  "Target_Numeral.of_nat (m mod n) = of_nat m mod of_nat n"
huffman@47108
   688
  by (simp add: Target_Numeral.int_eq_iff zmod_int)
huffman@47108
   689
huffman@47108
   690
lemma [code]:
huffman@47108
   691
  "Divides.divmod_nat m n = (m div n, m mod n)"
huffman@47108
   692
  by (simp add: prod_eq_iff)
huffman@47108
   693
huffman@47108
   694
lemma [code]:
huffman@47108
   695
  "HOL.equal m n = HOL.equal (of_nat m :: Target_Numeral.int) (of_nat n)"
huffman@47108
   696
  by (simp add: equal Target_Numeral.int_eq_iff)
huffman@47108
   697
huffman@47108
   698
lemma [code]:
huffman@47108
   699
  "m \<le> n \<longleftrightarrow> (of_nat m :: Target_Numeral.int) \<le> of_nat n"
huffman@47108
   700
  by (simp add: less_eq_int_def)
huffman@47108
   701
huffman@47108
   702
lemma [code]:
huffman@47108
   703
  "m < n \<longleftrightarrow> (of_nat m :: Target_Numeral.int) < of_nat n"
huffman@47108
   704
  by (simp add: less_int_def)
huffman@47108
   705
huffman@47108
   706
lemma num_of_nat_code [code]:
haftmann@48075
   707
  "num_of_nat = Target_Numeral.num_of_int \<circ> of_nat"
huffman@47108
   708
  by (simp add: fun_eq_iff num_of_int_def of_nat_def)
huffman@47108
   709
huffman@47108
   710
lemma (in semiring_1) of_nat_code:
huffman@47108
   711
  "of_nat n = (if n = 0 then 0
huffman@47108
   712
     else let
huffman@47108
   713
       (m, q) = divmod_nat n 2;
huffman@47108
   714
       m' = 2 * of_nat m
huffman@47108
   715
     in if q = 0 then m' else m' + 1)"
huffman@47108
   716
proof -
huffman@47108
   717
  from mod_div_equality have *: "of_nat n = of_nat (n div 2 * 2 + n mod 2)" by simp
huffman@47108
   718
  show ?thesis
huffman@47108
   719
    by (simp add: Let_def divmod_nat_div_mod mod_2_not_eq_zero_eq_one_nat
huffman@47108
   720
      of_nat_add [symmetric])
huffman@47108
   721
      (simp add: * mult_commute of_nat_mult add_commute)
huffman@47108
   722
qed
huffman@47108
   723
huffman@47108
   724
declare of_nat_code [code]
huffman@47108
   725
huffman@47108
   726
text {* Conversions between @{typ nat} and @{typ int} *}
huffman@47108
   727
huffman@47108
   728
definition int :: "nat \<Rightarrow> int" where
huffman@47108
   729
  [code_abbrev]: "int = of_nat"
huffman@47108
   730
huffman@47108
   731
hide_const (open) int
huffman@47108
   732
huffman@47108
   733
lemma [code]:
huffman@47108
   734
  "Target_Numeral.int n = Target_Numeral.int_of (of_nat n)"
huffman@47108
   735
  by (simp add: int_def)
huffman@47108
   736
huffman@47108
   737
lemma [code abstract]:
huffman@47108
   738
  "Target_Numeral.of_nat (nat k) = max 0 (Target_Numeral.of_int k)"
huffman@47108
   739
  by (simp add: of_nat_def of_int_of_nat max_def)
huffman@47108
   740
huffman@47108
   741
end
haftmann@47819
   742