src/HOL/Library/Word.thy
author wenzelm
Fri Nov 17 02:20:03 2006 +0100 (2006-11-17)
changeset 21404 eb85850d3eb7
parent 21210 c17fd2df4e9e
child 22059 f72cdc0a0af4
permissions -rw-r--r--
more robust syntax for definition/abbreviation/notation;
skalberg@14494
     1
(*  Title:      HOL/Library/Word.thy
skalberg@14494
     2
    ID:         $Id$
skalberg@14494
     3
    Author:     Sebastian Skalberg (TU Muenchen)
skalberg@14494
     4
*)
skalberg@14494
     5
wenzelm@14706
     6
header {* Binary Words *}
wenzelm@14589
     7
nipkow@15131
     8
theory Word
nipkow@15140
     9
imports Main
haftmann@16417
    10
uses "word_setup.ML"
nipkow@15131
    11
begin
skalberg@14494
    12
skalberg@14494
    13
subsection {* Auxilary Lemmas *}
skalberg@14494
    14
skalberg@14494
    15
lemma max_le [intro!]: "[| x \<le> z; y \<le> z |] ==> max x y \<le> z"
skalberg@14494
    16
  by (simp add: max_def)
skalberg@14494
    17
skalberg@14494
    18
lemma max_mono:
paulson@15067
    19
  fixes x :: "'a::linorder"
skalberg@14494
    20
  assumes mf: "mono f"
paulson@15067
    21
  shows       "max (f x) (f y) \<le> f (max x y)"
skalberg@14494
    22
proof -
skalberg@14494
    23
  from mf and le_maxI1 [of x y]
skalberg@14494
    24
  have fx: "f x \<le> f (max x y)"
skalberg@14494
    25
    by (rule monoD)
skalberg@14494
    26
  from mf and le_maxI2 [of y x]
skalberg@14494
    27
  have fy: "f y \<le> f (max x y)"
skalberg@14494
    28
    by (rule monoD)
skalberg@14494
    29
  from fx and fy
skalberg@14494
    30
  show "max (f x) (f y) \<le> f (max x y)"
skalberg@14494
    31
    by auto
skalberg@14494
    32
qed
skalberg@14494
    33
paulson@15067
    34
declare zero_le_power [intro]
paulson@15067
    35
    and zero_less_power [intro]
skalberg@14494
    36
skalberg@14494
    37
lemma int_nat_two_exp: "2 ^ k = int (2 ^ k)"
berghofe@15325
    38
  by (simp add: zpower_int [symmetric])
skalberg@14494
    39
wenzelm@14589
    40
subsection {* Bits *}
skalberg@14494
    41
skalberg@14494
    42
datatype bit
skalberg@14494
    43
  = Zero ("\<zero>")
skalberg@14494
    44
  | One ("\<one>")
skalberg@14494
    45
skalberg@14494
    46
consts
berghofe@15325
    47
  bitval :: "bit => nat"
skalberg@14494
    48
skalberg@14494
    49
primrec
skalberg@14494
    50
  "bitval \<zero> = 0"
skalberg@14494
    51
  "bitval \<one> = 1"
skalberg@14494
    52
skalberg@14494
    53
consts
skalberg@14494
    54
  bitnot :: "bit => bit"
skalberg@14494
    55
  bitand :: "bit => bit => bit" (infixr "bitand" 35)
skalberg@14494
    56
  bitor  :: "bit => bit => bit" (infixr "bitor"  30)
skalberg@14494
    57
  bitxor :: "bit => bit => bit" (infixr "bitxor" 30)
skalberg@14494
    58
wenzelm@21210
    59
notation (xsymbols)
wenzelm@21404
    60
  bitnot ("\<not>\<^sub>b _" [40] 40) and
wenzelm@21404
    61
  bitand (infixr "\<and>\<^sub>b" 35) and
wenzelm@21404
    62
  bitor  (infixr "\<or>\<^sub>b" 30) and
wenzelm@19736
    63
  bitxor (infixr "\<oplus>\<^sub>b" 30)
skalberg@14494
    64
wenzelm@21210
    65
notation (HTML output)
wenzelm@21404
    66
  bitnot ("\<not>\<^sub>b _" [40] 40) and
wenzelm@21404
    67
  bitand (infixr "\<and>\<^sub>b" 35) and
wenzelm@21404
    68
  bitor  (infixr "\<or>\<^sub>b" 30) and
wenzelm@19736
    69
  bitxor (infixr "\<oplus>\<^sub>b" 30)
kleing@14565
    70
skalberg@14494
    71
primrec
skalberg@14494
    72
  bitnot_zero: "(bitnot \<zero>) = \<one>"
skalberg@14494
    73
  bitnot_one : "(bitnot \<one>)  = \<zero>"
skalberg@14494
    74
skalberg@14494
    75
primrec
skalberg@14494
    76
  bitand_zero: "(\<zero> bitand y) = \<zero>"
skalberg@14494
    77
  bitand_one:  "(\<one> bitand y) = y"
skalberg@14494
    78
skalberg@14494
    79
primrec
skalberg@14494
    80
  bitor_zero: "(\<zero> bitor y) = y"
skalberg@14494
    81
  bitor_one:  "(\<one> bitor y) = \<one>"
skalberg@14494
    82
skalberg@14494
    83
primrec
skalberg@14494
    84
  bitxor_zero: "(\<zero> bitxor y) = y"
skalberg@14494
    85
  bitxor_one:  "(\<one> bitxor y) = (bitnot y)"
skalberg@14494
    86
skalberg@17650
    87
lemma bitnot_bitnot [simp]: "(bitnot (bitnot b)) = b"
skalberg@14494
    88
  by (cases b,simp_all)
skalberg@14494
    89
skalberg@17650
    90
lemma bitand_cancel [simp]: "(b bitand b) = b"
skalberg@14494
    91
  by (cases b,simp_all)
skalberg@14494
    92
skalberg@17650
    93
lemma bitor_cancel [simp]: "(b bitor b) = b"
skalberg@14494
    94
  by (cases b,simp_all)
skalberg@14494
    95
skalberg@17650
    96
lemma bitxor_cancel [simp]: "(b bitxor b) = \<zero>"
skalberg@14494
    97
  by (cases b,simp_all)
skalberg@14494
    98
wenzelm@14589
    99
subsection {* Bit Vectors *}
skalberg@14494
   100
skalberg@14494
   101
text {* First, a couple of theorems expressing case analysis and
skalberg@14494
   102
induction principles for bit vectors. *}
skalberg@14494
   103
skalberg@14494
   104
lemma bit_list_cases:
skalberg@14494
   105
  assumes empty: "w = [] ==> P w"
skalberg@14494
   106
  and     zero:  "!!bs. w = \<zero> # bs ==> P w"
skalberg@14494
   107
  and     one:   "!!bs. w = \<one> # bs ==> P w"
skalberg@14494
   108
  shows   "P w"
skalberg@14494
   109
proof (cases w)
skalberg@14494
   110
  assume "w = []"
skalberg@14494
   111
  thus ?thesis
skalberg@14494
   112
    by (rule empty)
skalberg@14494
   113
next
skalberg@14494
   114
  fix b bs
skalberg@14494
   115
  assume [simp]: "w = b # bs"
skalberg@14494
   116
  show "P w"
skalberg@14494
   117
  proof (cases b)
skalberg@14494
   118
    assume "b = \<zero>"
skalberg@14494
   119
    hence "w = \<zero> # bs"
skalberg@14494
   120
      by simp
skalberg@14494
   121
    thus ?thesis
skalberg@14494
   122
      by (rule zero)
skalberg@14494
   123
  next
skalberg@14494
   124
    assume "b = \<one>"
skalberg@14494
   125
    hence "w = \<one> # bs"
skalberg@14494
   126
      by simp
skalberg@14494
   127
    thus ?thesis
skalberg@14494
   128
      by (rule one)
skalberg@14494
   129
  qed
skalberg@14494
   130
qed
skalberg@14494
   131
skalberg@14494
   132
lemma bit_list_induct:
skalberg@14494
   133
  assumes empty: "P []"
skalberg@14494
   134
  and     zero:  "!!bs. P bs ==> P (\<zero>#bs)"
skalberg@14494
   135
  and     one:   "!!bs. P bs ==> P (\<one>#bs)"
skalberg@14494
   136
  shows   "P w"
skalberg@14494
   137
proof (induct w,simp_all add: empty)
skalberg@14494
   138
  fix b bs
skalberg@14494
   139
  assume [intro!]: "P bs"
skalberg@14494
   140
  show "P (b#bs)"
skalberg@14494
   141
    by (cases b,auto intro!: zero one)
skalberg@14494
   142
qed
skalberg@14494
   143
wenzelm@19736
   144
definition
wenzelm@21404
   145
  bv_msb :: "bit list => bit" where
wenzelm@19736
   146
  "bv_msb w = (if w = [] then \<zero> else hd w)"
wenzelm@21404
   147
wenzelm@21404
   148
definition
wenzelm@21404
   149
  bv_extend :: "[nat,bit,bit list]=>bit list" where
wenzelm@19736
   150
  "bv_extend i b w = (replicate (i - length w) b) @ w"
wenzelm@21404
   151
wenzelm@21404
   152
definition
wenzelm@21404
   153
  bv_not :: "bit list => bit list" where
wenzelm@19736
   154
  "bv_not w = map bitnot w"
skalberg@14494
   155
skalberg@14494
   156
lemma bv_length_extend [simp]: "length w \<le> i ==> length (bv_extend i b w) = i"
skalberg@14494
   157
  by (simp add: bv_extend_def)
skalberg@14494
   158
skalberg@17650
   159
lemma bv_not_Nil [simp]: "bv_not [] = []"
skalberg@14494
   160
  by (simp add: bv_not_def)
skalberg@14494
   161
skalberg@17650
   162
lemma bv_not_Cons [simp]: "bv_not (b#bs) = (bitnot b) # bv_not bs"
skalberg@14494
   163
  by (simp add: bv_not_def)
skalberg@14494
   164
skalberg@17650
   165
lemma bv_not_bv_not [simp]: "bv_not (bv_not w) = w"
skalberg@14494
   166
  by (rule bit_list_induct [of _ w],simp_all)
skalberg@14494
   167
skalberg@17650
   168
lemma bv_msb_Nil [simp]: "bv_msb [] = \<zero>"
skalberg@14494
   169
  by (simp add: bv_msb_def)
skalberg@14494
   170
skalberg@17650
   171
lemma bv_msb_Cons [simp]: "bv_msb (b#bs) = b"
skalberg@14494
   172
  by (simp add: bv_msb_def)
skalberg@14494
   173
skalberg@17650
   174
lemma bv_msb_bv_not [simp]: "0 < length w ==> bv_msb (bv_not w) = (bitnot (bv_msb w))"
skalberg@14494
   175
  by (cases w,simp_all)
skalberg@14494
   176
skalberg@17650
   177
lemma bv_msb_one_length [simp,intro]: "bv_msb w = \<one> ==> 0 < length w"
skalberg@14494
   178
  by (cases w,simp_all)
skalberg@14494
   179
skalberg@17650
   180
lemma length_bv_not [simp]: "length (bv_not w) = length w"
skalberg@14494
   181
  by (induct w,simp_all)
skalberg@14494
   182
wenzelm@19736
   183
definition
wenzelm@21404
   184
  bv_to_nat :: "bit list => nat" where
wenzelm@19736
   185
  "bv_to_nat = foldl (%bn b. 2 * bn + bitval b) 0"
skalberg@14494
   186
skalberg@17650
   187
lemma bv_to_nat_Nil [simp]: "bv_to_nat [] = 0"
skalberg@14494
   188
  by (simp add: bv_to_nat_def)
skalberg@14494
   189
skalberg@14494
   190
lemma bv_to_nat_helper [simp]: "bv_to_nat (b # bs) = bitval b * 2 ^ length bs + bv_to_nat bs"
skalberg@14494
   191
proof -
berghofe@15325
   192
  let ?bv_to_nat' = "foldl (\<lambda>bn b. 2 * bn + bitval b)"
berghofe@15325
   193
  have helper: "\<And>base. ?bv_to_nat' base bs = base * 2 ^ length bs + ?bv_to_nat' 0 bs"
berghofe@15325
   194
  proof (induct bs)
berghofe@15325
   195
    case Nil show ?case by simp
berghofe@15325
   196
  next
berghofe@15325
   197
    case (Cons x xs base)
berghofe@15325
   198
    show ?case
skalberg@14494
   199
      apply (simp only: foldl.simps)
berghofe@15325
   200
      apply (subst Cons [of "2 * base + bitval x"])
skalberg@14494
   201
      apply simp
berghofe@15325
   202
      apply (subst Cons [of "bitval x"])
berghofe@15325
   203
      apply (simp add: add_mult_distrib)
skalberg@14494
   204
      done
skalberg@14494
   205
  qed
berghofe@15325
   206
  show ?thesis by (simp add: bv_to_nat_def) (rule helper)
skalberg@14494
   207
qed
skalberg@14494
   208
skalberg@14494
   209
lemma bv_to_nat0 [simp]: "bv_to_nat (\<zero>#bs) = bv_to_nat bs"
skalberg@14494
   210
  by simp
skalberg@14494
   211
skalberg@14494
   212
lemma bv_to_nat1 [simp]: "bv_to_nat (\<one>#bs) = 2 ^ length bs + bv_to_nat bs"
skalberg@14494
   213
  by simp
skalberg@14494
   214
skalberg@14494
   215
lemma bv_to_nat_upper_range: "bv_to_nat w < 2 ^ length w"
skalberg@14494
   216
proof (induct w,simp_all)
skalberg@14494
   217
  fix b bs
skalberg@14494
   218
  assume "bv_to_nat bs < 2 ^ length bs"
skalberg@14494
   219
  show "bitval b * 2 ^ length bs + bv_to_nat bs < 2 * 2 ^ length bs"
skalberg@14494
   220
  proof (cases b,simp_all)
skalberg@14494
   221
    have "bv_to_nat bs < 2 ^ length bs"
skalberg@14494
   222
      .
skalberg@14494
   223
    also have "... < 2 * 2 ^ length bs"
skalberg@14494
   224
      by auto
skalberg@14494
   225
    finally show "bv_to_nat bs < 2 * 2 ^ length bs"
skalberg@14494
   226
      by simp
skalberg@14494
   227
  next
skalberg@14494
   228
    have "bv_to_nat bs < 2 ^ length bs"
skalberg@14494
   229
      .
skalberg@14494
   230
    hence "2 ^ length bs + bv_to_nat bs < 2 ^ length bs + 2 ^ length bs"
skalberg@14494
   231
      by arith
skalberg@14494
   232
    also have "... = 2 * (2 ^ length bs)"
skalberg@14494
   233
      by simp
skalberg@14494
   234
    finally show "bv_to_nat bs < 2 ^ length bs"
skalberg@14494
   235
      by simp
skalberg@14494
   236
  qed
skalberg@14494
   237
qed
skalberg@14494
   238
skalberg@17650
   239
lemma bv_extend_longer [simp]:
skalberg@14494
   240
  assumes wn: "n \<le> length w"
skalberg@14494
   241
  shows       "bv_extend n b w = w"
skalberg@14494
   242
  by (simp add: bv_extend_def wn)
skalberg@14494
   243
skalberg@17650
   244
lemma bv_extend_shorter [simp]:
skalberg@14494
   245
  assumes wn: "length w < n"
skalberg@14494
   246
  shows       "bv_extend n b w = bv_extend n b (b#w)"
skalberg@14494
   247
proof -
skalberg@14494
   248
  from wn
skalberg@14494
   249
  have s: "n - Suc (length w) + 1 = n - length w"
skalberg@14494
   250
    by arith
skalberg@14494
   251
  have "bv_extend n b w = replicate (n - length w) b @ w"
skalberg@14494
   252
    by (simp add: bv_extend_def)
skalberg@14494
   253
  also have "... = replicate (n - Suc (length w) + 1) b @ w"
skalberg@14494
   254
    by (subst s,rule)
skalberg@14494
   255
  also have "... = (replicate (n - Suc (length w)) b @ replicate 1 b) @ w"
skalberg@14494
   256
    by (subst replicate_add,rule)
skalberg@14494
   257
  also have "... = replicate (n - Suc (length w)) b @ b # w"
skalberg@14494
   258
    by simp
skalberg@14494
   259
  also have "... = bv_extend n b (b#w)"
skalberg@14494
   260
    by (simp add: bv_extend_def)
skalberg@14494
   261
  finally show "bv_extend n b w = bv_extend n b (b#w)"
skalberg@14494
   262
    .
skalberg@14494
   263
qed
skalberg@14494
   264
skalberg@14494
   265
consts
skalberg@14494
   266
  rem_initial :: "bit => bit list => bit list"
skalberg@14494
   267
skalberg@14494
   268
primrec
skalberg@14494
   269
  "rem_initial b [] = []"
skalberg@14494
   270
  "rem_initial b (x#xs) = (if b = x then rem_initial b xs else x#xs)"
skalberg@14494
   271
skalberg@14494
   272
lemma rem_initial_length: "length (rem_initial b w) \<le> length w"
skalberg@14494
   273
  by (rule bit_list_induct [of _ w],simp_all (no_asm),safe,simp_all)
skalberg@14494
   274
skalberg@14494
   275
lemma rem_initial_equal:
skalberg@14494
   276
  assumes p: "length (rem_initial b w) = length w"
skalberg@14494
   277
  shows      "rem_initial b w = w"
skalberg@14494
   278
proof -
skalberg@14494
   279
  have "length (rem_initial b w) = length w --> rem_initial b w = w"
skalberg@14494
   280
  proof (induct w,simp_all,clarify)
skalberg@14494
   281
    fix xs
skalberg@14494
   282
    assume "length (rem_initial b xs) = length xs --> rem_initial b xs = xs"
skalberg@14494
   283
    assume f: "length (rem_initial b xs) = Suc (length xs)"
skalberg@14494
   284
    with rem_initial_length [of b xs]
skalberg@14494
   285
    show "rem_initial b xs = b#xs"
skalberg@14494
   286
      by auto
skalberg@14494
   287
  qed
skalberg@14494
   288
  thus ?thesis
skalberg@14494
   289
    ..
skalberg@14494
   290
qed
skalberg@14494
   291
skalberg@14494
   292
lemma bv_extend_rem_initial: "bv_extend (length w) b (rem_initial b w) = w"
skalberg@14494
   293
proof (induct w,simp_all,safe)
skalberg@14494
   294
  fix xs
skalberg@14494
   295
  assume ind: "bv_extend (length xs) b (rem_initial b xs) = xs"
skalberg@14494
   296
  from rem_initial_length [of b xs]
skalberg@14494
   297
  have [simp]: "Suc (length xs) - length (rem_initial b xs) = 1 + (length xs - length (rem_initial b xs))"
skalberg@14494
   298
    by arith
skalberg@14494
   299
  have "bv_extend (Suc (length xs)) b (rem_initial b xs) = replicate (Suc (length xs) - length (rem_initial b xs)) b @ (rem_initial b xs)"
skalberg@14494
   300
    by (simp add: bv_extend_def)
skalberg@14494
   301
  also have "... = replicate (1 + (length xs - length (rem_initial b xs))) b @ rem_initial b xs"
skalberg@14494
   302
    by simp
skalberg@14494
   303
  also have "... = (replicate 1 b @ replicate (length xs - length (rem_initial b xs)) b) @ rem_initial b xs"
skalberg@14494
   304
    by (subst replicate_add,rule refl)
skalberg@14494
   305
  also have "... = b # bv_extend (length xs) b (rem_initial b xs)"
skalberg@14494
   306
    by (auto simp add: bv_extend_def [symmetric])
skalberg@14494
   307
  also have "... = b # xs"
skalberg@14494
   308
    by (simp add: ind)
skalberg@14494
   309
  finally show "bv_extend (Suc (length xs)) b (rem_initial b xs) = b # xs"
skalberg@14494
   310
    .
skalberg@14494
   311
qed
skalberg@14494
   312
skalberg@14494
   313
lemma rem_initial_append1:
skalberg@14494
   314
  assumes "rem_initial b xs ~= []"
skalberg@14494
   315
  shows   "rem_initial b (xs @ ys) = rem_initial b xs @ ys"
skalberg@14494
   316
proof -
skalberg@14494
   317
  have "rem_initial b xs ~= [] --> rem_initial b (xs @ ys) = rem_initial b xs @ ys" (is "?P xs ys")
skalberg@14494
   318
    by (induct xs,auto)
skalberg@14494
   319
  thus ?thesis
skalberg@14494
   320
    ..
skalberg@14494
   321
qed
skalberg@14494
   322
skalberg@14494
   323
lemma rem_initial_append2:
skalberg@14494
   324
  assumes "rem_initial b xs = []"
skalberg@14494
   325
  shows   "rem_initial b (xs @ ys) = rem_initial b ys"
skalberg@14494
   326
proof -
skalberg@14494
   327
  have "rem_initial b xs = [] --> rem_initial b (xs @ ys) = rem_initial b ys" (is "?P xs ys")
skalberg@14494
   328
    by (induct xs,auto)
skalberg@14494
   329
  thus ?thesis
skalberg@14494
   330
    ..
skalberg@14494
   331
qed
skalberg@14494
   332
wenzelm@19736
   333
definition
wenzelm@21404
   334
  norm_unsigned :: "bit list => bit list" where
wenzelm@19736
   335
  "norm_unsigned = rem_initial \<zero>"
skalberg@14494
   336
skalberg@17650
   337
lemma norm_unsigned_Nil [simp]: "norm_unsigned [] = []"
skalberg@14494
   338
  by (simp add: norm_unsigned_def)
skalberg@14494
   339
skalberg@17650
   340
lemma norm_unsigned_Cons0 [simp]: "norm_unsigned (\<zero>#bs) = norm_unsigned bs"
skalberg@14494
   341
  by (simp add: norm_unsigned_def)
skalberg@14494
   342
skalberg@17650
   343
lemma norm_unsigned_Cons1 [simp]: "norm_unsigned (\<one>#bs) = \<one>#bs"
skalberg@14494
   344
  by (simp add: norm_unsigned_def)
skalberg@14494
   345
skalberg@17650
   346
lemma norm_unsigned_idem [simp]: "norm_unsigned (norm_unsigned w) = norm_unsigned w"
skalberg@14494
   347
  by (rule bit_list_induct [of _ w],simp_all)
skalberg@14494
   348
skalberg@14494
   349
consts
berghofe@15325
   350
  nat_to_bv_helper :: "nat => bit list => bit list"
skalberg@14494
   351
berghofe@15325
   352
recdef nat_to_bv_helper "measure (\<lambda>n. n)"
berghofe@15325
   353
  "nat_to_bv_helper n = (%bs. (if n = 0 then bs
skalberg@14494
   354
                               else nat_to_bv_helper (n div 2) ((if n mod 2 = 0 then \<zero> else \<one>)#bs)))"
skalberg@14494
   355
wenzelm@19736
   356
definition
wenzelm@21404
   357
  nat_to_bv :: "nat => bit list" where
wenzelm@19736
   358
  "nat_to_bv n = nat_to_bv_helper n []"
skalberg@14494
   359
skalberg@14494
   360
lemma nat_to_bv0 [simp]: "nat_to_bv 0 = []"
skalberg@14494
   361
  by (simp add: nat_to_bv_def)
skalberg@14494
   362
skalberg@14494
   363
lemmas [simp del] = nat_to_bv_helper.simps
skalberg@14494
   364
skalberg@14494
   365
lemma n_div_2_cases:
berghofe@15325
   366
  assumes zero: "(n::nat) = 0 ==> R"
skalberg@14494
   367
  and     div : "[| n div 2 < n ; 0 < n |] ==> R"
skalberg@14494
   368
  shows         "R"
skalberg@14494
   369
proof (cases "n = 0")
skalberg@14494
   370
  assume "n = 0"
skalberg@14494
   371
  thus R
skalberg@14494
   372
    by (rule zero)
skalberg@14494
   373
next
skalberg@14494
   374
  assume "n ~= 0"
berghofe@15325
   375
  hence nn0: "0 < n"
skalberg@14494
   376
    by simp
skalberg@14494
   377
  hence "n div 2 < n"
skalberg@14494
   378
    by arith
skalberg@14494
   379
  from this and nn0
skalberg@14494
   380
  show R
skalberg@14494
   381
    by (rule div)
skalberg@14494
   382
qed
skalberg@14494
   383
skalberg@14494
   384
lemma int_wf_ge_induct:
skalberg@14494
   385
  assumes base:  "P (k::int)"
skalberg@14494
   386
  and     ind :  "!!i. (!!j. [| k \<le> j ; j < i |] ==> P j) ==> P i"
skalberg@14494
   387
  and     valid: "k \<le> i"
skalberg@14494
   388
  shows          "P i"
skalberg@14494
   389
proof -
skalberg@14494
   390
  have a: "\<forall> j. k \<le> j \<and> j < i --> P j"
skalberg@14494
   391
  proof (rule int_ge_induct)
skalberg@14494
   392
    show "k \<le> i"
skalberg@14494
   393
      .
skalberg@14494
   394
  next
skalberg@14494
   395
    show "\<forall> j. k \<le> j \<and> j < k --> P j"
skalberg@14494
   396
      by auto
skalberg@14494
   397
  next
skalberg@14494
   398
    fix i
skalberg@14494
   399
    assume "k \<le> i"
skalberg@14494
   400
    assume a: "\<forall> j. k \<le> j \<and> j < i --> P j"
skalberg@14494
   401
    have pi: "P i"
skalberg@14494
   402
    proof (rule ind)
skalberg@14494
   403
      fix j
skalberg@14494
   404
      assume "k \<le> j" and "j < i"
skalberg@14494
   405
      with a
skalberg@14494
   406
      show "P j"
wenzelm@19736
   407
        by auto
skalberg@14494
   408
    qed
skalberg@14494
   409
    show "\<forall> j. k \<le> j \<and> j < i + 1 --> P j"
skalberg@14494
   410
    proof auto
skalberg@14494
   411
      fix j
skalberg@14494
   412
      assume kj: "k \<le> j"
skalberg@14494
   413
      assume ji: "j \<le> i"
skalberg@14494
   414
      show "P j"
skalberg@14494
   415
      proof (cases "j = i")
wenzelm@19736
   416
        assume "j = i"
wenzelm@19736
   417
        with pi
wenzelm@19736
   418
        show "P j"
wenzelm@19736
   419
          by simp
skalberg@14494
   420
      next
wenzelm@19736
   421
        assume "j ~= i"
wenzelm@19736
   422
        with ji
wenzelm@19736
   423
        have "j < i"
wenzelm@19736
   424
          by simp
wenzelm@19736
   425
        with kj and a
wenzelm@19736
   426
        show "P j"
wenzelm@19736
   427
          by blast
skalberg@14494
   428
      qed
skalberg@14494
   429
    qed
skalberg@14494
   430
  qed
skalberg@14494
   431
  show "P i"
skalberg@14494
   432
  proof (rule ind)
skalberg@14494
   433
    fix j
skalberg@14494
   434
    assume "k \<le> j" and "j < i"
skalberg@14494
   435
    with a
skalberg@14494
   436
    show "P j"
skalberg@14494
   437
      by auto
skalberg@14494
   438
  qed
skalberg@14494
   439
qed
skalberg@14494
   440
skalberg@14494
   441
lemma unfold_nat_to_bv_helper:
berghofe@15325
   442
  "nat_to_bv_helper b l = nat_to_bv_helper b [] @ l"
skalberg@14494
   443
proof -
skalberg@14494
   444
  have "\<forall>l. nat_to_bv_helper b l = nat_to_bv_helper b [] @ l"
berghofe@15325
   445
  proof (induct b rule: less_induct)
skalberg@14494
   446
    fix n
berghofe@15325
   447
    assume ind: "!!j. j < n \<Longrightarrow> \<forall> l. nat_to_bv_helper j l = nat_to_bv_helper j [] @ l"
skalberg@14494
   448
    show "\<forall>l. nat_to_bv_helper n l = nat_to_bv_helper n [] @ l"
skalberg@14494
   449
    proof
skalberg@14494
   450
      fix l
skalberg@14494
   451
      show "nat_to_bv_helper n l = nat_to_bv_helper n [] @ l"
skalberg@14494
   452
      proof (cases "n < 0")
wenzelm@19736
   453
        assume "n < 0"
wenzelm@19736
   454
        thus ?thesis
wenzelm@19736
   455
          by (simp add: nat_to_bv_helper.simps)
skalberg@14494
   456
      next
wenzelm@19736
   457
        assume "~n < 0"
wenzelm@19736
   458
        show ?thesis
wenzelm@19736
   459
        proof (rule n_div_2_cases [of n])
wenzelm@19736
   460
          assume [simp]: "n = 0"
wenzelm@19736
   461
          show ?thesis
wenzelm@19736
   462
            apply (simp only: nat_to_bv_helper.simps [of n])
wenzelm@19736
   463
            apply simp
wenzelm@19736
   464
            done
wenzelm@19736
   465
        next
wenzelm@19736
   466
          assume n2n: "n div 2 < n"
wenzelm@19736
   467
          assume [simp]: "0 < n"
wenzelm@19736
   468
          hence n20: "0 \<le> n div 2"
wenzelm@19736
   469
            by arith
wenzelm@19736
   470
          from ind [of "n div 2"] and n2n n20
wenzelm@19736
   471
          have ind': "\<forall>l. nat_to_bv_helper (n div 2) l = nat_to_bv_helper (n div 2) [] @ l"
wenzelm@19736
   472
            by blast
wenzelm@19736
   473
          show ?thesis
wenzelm@19736
   474
            apply (simp only: nat_to_bv_helper.simps [of n])
wenzelm@19736
   475
            apply (cases "n=0")
wenzelm@19736
   476
            apply simp
wenzelm@19736
   477
            apply (simp only: if_False)
wenzelm@19736
   478
            apply simp
wenzelm@19736
   479
            apply (subst spec [OF ind',of "\<zero>#l"])
wenzelm@19736
   480
            apply (subst spec [OF ind',of "\<one>#l"])
wenzelm@19736
   481
            apply (subst spec [OF ind',of "[\<one>]"])
wenzelm@19736
   482
            apply (subst spec [OF ind',of "[\<zero>]"])
wenzelm@19736
   483
            apply simp
wenzelm@19736
   484
            done
wenzelm@19736
   485
        qed
skalberg@14494
   486
      qed
skalberg@14494
   487
    qed
skalberg@14494
   488
  qed
skalberg@14494
   489
  thus ?thesis
skalberg@14494
   490
    ..
skalberg@14494
   491
qed
skalberg@14494
   492
skalberg@14494
   493
lemma nat_to_bv_non0 [simp]: "0 < n ==> nat_to_bv n = nat_to_bv (n div 2) @ [if n mod 2 = 0 then \<zero> else \<one>]"
skalberg@14494
   494
proof -
skalberg@14494
   495
  assume [simp]: "0 < n"
skalberg@14494
   496
  show ?thesis
skalberg@14494
   497
    apply (subst nat_to_bv_def [of n])
paulson@15481
   498
    apply (simp only: nat_to_bv_helper.simps [of n])
skalberg@14494
   499
    apply (subst unfold_nat_to_bv_helper)
skalberg@14494
   500
    using prems
skalberg@14494
   501
    apply simp
skalberg@14494
   502
    apply (subst nat_to_bv_def [of "n div 2"])
skalberg@14494
   503
    apply auto
skalberg@14494
   504
    done
skalberg@14494
   505
qed
skalberg@14494
   506
skalberg@14494
   507
lemma bv_to_nat_dist_append: "bv_to_nat (l1 @ l2) = bv_to_nat l1 * 2 ^ length l2 + bv_to_nat l2"
skalberg@14494
   508
proof -
skalberg@14494
   509
  have "\<forall>l2. bv_to_nat (l1 @ l2) = bv_to_nat l1 * 2 ^ length l2 + bv_to_nat l2"
skalberg@14494
   510
  proof (induct l1,simp_all)
skalberg@14494
   511
    fix x xs
skalberg@14494
   512
    assume ind: "\<forall>l2. bv_to_nat (xs @ l2) = bv_to_nat xs * 2 ^ length l2 + bv_to_nat l2"
skalberg@14494
   513
    show "\<forall>l2. bitval x * 2 ^ (length xs + length l2) + bv_to_nat xs * 2 ^ length l2 = (bitval x * 2 ^ length xs + bv_to_nat xs) * 2 ^ length l2"
skalberg@14494
   514
    proof
skalberg@14494
   515
      fix l2
skalberg@14494
   516
      show "bitval x * 2 ^ (length xs + length l2) + bv_to_nat xs * 2 ^ length l2 = (bitval x * 2 ^ length xs + bv_to_nat xs) * 2 ^ length l2"
skalberg@14494
   517
      proof -
wenzelm@19736
   518
        have "(2::nat) ^ (length xs + length l2) = 2 ^ length xs * 2 ^ length l2"
wenzelm@19736
   519
          by (induct "length xs",simp_all)
wenzelm@19736
   520
        hence "bitval x * 2 ^ (length xs + length l2) + bv_to_nat xs * 2 ^ length l2 =
wenzelm@19736
   521
          bitval x * 2 ^ length xs * 2 ^ length l2 + bv_to_nat xs * 2 ^ length l2"
wenzelm@19736
   522
          by simp
wenzelm@19736
   523
        also have "... = (bitval x * 2 ^ length xs + bv_to_nat xs) * 2 ^ length l2"
wenzelm@19736
   524
          by (simp add: ring_distrib)
wenzelm@19736
   525
        finally show ?thesis .
skalberg@14494
   526
      qed
skalberg@14494
   527
    qed
skalberg@14494
   528
  qed
skalberg@14494
   529
  thus ?thesis
skalberg@14494
   530
    ..
skalberg@14494
   531
qed
skalberg@14494
   532
berghofe@15325
   533
lemma bv_nat_bv [simp]: "bv_to_nat (nat_to_bv n) = n"
berghofe@15325
   534
proof (induct n rule: less_induct)
berghofe@15325
   535
  fix n
berghofe@15325
   536
  assume ind: "!!j. j < n \<Longrightarrow> bv_to_nat (nat_to_bv j) = j"
berghofe@15325
   537
  show "bv_to_nat (nat_to_bv n) = n"
berghofe@15325
   538
  proof (rule n_div_2_cases [of n])
berghofe@15325
   539
    assume [simp]: "n = 0"
berghofe@15325
   540
    show ?thesis
berghofe@15325
   541
      by simp
berghofe@15325
   542
  next
berghofe@15325
   543
    assume nn: "n div 2 < n"
berghofe@15325
   544
    assume n0: "0 < n"
berghofe@15325
   545
    from ind and nn
berghofe@15325
   546
    have ind': "bv_to_nat (nat_to_bv (n div 2)) = n div 2"
berghofe@15325
   547
      by blast
berghofe@15325
   548
    from n0 have n0': "n \<noteq> 0"
berghofe@15325
   549
      by simp
berghofe@15325
   550
    show ?thesis
berghofe@15325
   551
      apply (subst nat_to_bv_def)
paulson@15481
   552
      apply (simp only: nat_to_bv_helper.simps [of n])
berghofe@15325
   553
      apply (simp only: n0' if_False)
berghofe@15325
   554
      apply (subst unfold_nat_to_bv_helper)
berghofe@15325
   555
      apply (subst bv_to_nat_dist_append)
berghofe@15325
   556
      apply (fold nat_to_bv_def)
berghofe@15325
   557
      apply (simp add: ind' split del: split_if)
berghofe@15325
   558
      apply (cases "n mod 2 = 0")
skalberg@14494
   559
      proof simp_all
wenzelm@19736
   560
        assume "n mod 2 = 0"
wenzelm@19736
   561
        with mod_div_equality [of n 2]
wenzelm@19736
   562
        show "n div 2 * 2 = n"
wenzelm@19736
   563
          by simp
skalberg@14494
   564
      next
wenzelm@19736
   565
        assume "n mod 2 = Suc 0"
wenzelm@19736
   566
        with mod_div_equality [of n 2]
wenzelm@19736
   567
        show "Suc (n div 2 * 2) = n"
wenzelm@19736
   568
          by simp
skalberg@14494
   569
      qed
skalberg@14494
   570
  qed
skalberg@14494
   571
qed
skalberg@14494
   572
skalberg@17650
   573
lemma bv_to_nat_type [simp]: "bv_to_nat (norm_unsigned w) = bv_to_nat w"
skalberg@14494
   574
  by (rule bit_list_induct,simp_all)
skalberg@14494
   575
skalberg@17650
   576
lemma length_norm_unsigned_le [simp]: "length (norm_unsigned w) \<le> length w"
skalberg@14494
   577
  by (rule bit_list_induct,simp_all)
skalberg@14494
   578
skalberg@14494
   579
lemma bv_to_nat_rew_msb: "bv_msb w = \<one> ==> bv_to_nat w = 2 ^ (length w - 1) + bv_to_nat (tl w)"
skalberg@14494
   580
  by (rule bit_list_cases [of w],simp_all)
skalberg@14494
   581
skalberg@14494
   582
lemma norm_unsigned_result: "norm_unsigned xs = [] \<or> bv_msb (norm_unsigned xs) = \<one>"
skalberg@14494
   583
proof (rule length_induct [of _ xs])
skalberg@14494
   584
  fix xs :: "bit list"
skalberg@14494
   585
  assume ind: "\<forall>ys. length ys < length xs --> norm_unsigned ys = [] \<or> bv_msb (norm_unsigned ys) = \<one>"
skalberg@14494
   586
  show "norm_unsigned xs = [] \<or> bv_msb (norm_unsigned xs) = \<one>"
skalberg@14494
   587
  proof (rule bit_list_cases [of xs],simp_all)
skalberg@14494
   588
    fix bs
skalberg@14494
   589
    assume [simp]: "xs = \<zero>#bs"
skalberg@14494
   590
    from ind
skalberg@14494
   591
    have "length bs < length xs --> norm_unsigned bs = [] \<or> bv_msb (norm_unsigned bs) = \<one>"
skalberg@14494
   592
      ..
skalberg@14494
   593
    thus "norm_unsigned bs = [] \<or> bv_msb (norm_unsigned bs) = \<one>"
skalberg@14494
   594
      by simp
skalberg@14494
   595
  qed
skalberg@14494
   596
qed
skalberg@14494
   597
skalberg@14494
   598
lemma norm_empty_bv_to_nat_zero:
skalberg@14494
   599
  assumes nw: "norm_unsigned w = []"
skalberg@14494
   600
  shows       "bv_to_nat w = 0"
skalberg@14494
   601
proof -
skalberg@14494
   602
  have "bv_to_nat w = bv_to_nat (norm_unsigned w)"
skalberg@14494
   603
    by simp
skalberg@14494
   604
  also have "... = bv_to_nat []"
skalberg@14494
   605
    by (subst nw,rule)
skalberg@14494
   606
  also have "... = 0"
skalberg@14494
   607
    by simp
skalberg@14494
   608
  finally show ?thesis .
skalberg@14494
   609
qed
skalberg@14494
   610
skalberg@14494
   611
lemma bv_to_nat_lower_limit:
skalberg@14494
   612
  assumes w0: "0 < bv_to_nat w"
skalberg@14494
   613
  shows         "2 ^ (length (norm_unsigned w) - 1) \<le> bv_to_nat w"
skalberg@14494
   614
proof -
skalberg@14494
   615
  from w0 and norm_unsigned_result [of w]
skalberg@14494
   616
  have msbw: "bv_msb (norm_unsigned w) = \<one>"
skalberg@14494
   617
    by (auto simp add: norm_empty_bv_to_nat_zero)
skalberg@14494
   618
  have "2 ^ (length (norm_unsigned w) - 1) \<le> bv_to_nat (norm_unsigned w)"
skalberg@14494
   619
    by (subst bv_to_nat_rew_msb [OF msbw],simp)
skalberg@14494
   620
  thus ?thesis
skalberg@14494
   621
    by simp
skalberg@14494
   622
qed
skalberg@14494
   623
skalberg@14494
   624
lemmas [simp del] = nat_to_bv_non0
skalberg@14494
   625
skalberg@14494
   626
lemma norm_unsigned_length [intro!]: "length (norm_unsigned w) \<le> length w"
skalberg@14494
   627
  by (subst norm_unsigned_def,rule rem_initial_length)
skalberg@14494
   628
skalberg@14494
   629
lemma norm_unsigned_equal: "length (norm_unsigned w) = length w ==> norm_unsigned w = w"
skalberg@14494
   630
  by (simp add: norm_unsigned_def,rule rem_initial_equal)
skalberg@14494
   631
skalberg@14494
   632
lemma bv_extend_norm_unsigned: "bv_extend (length w) \<zero> (norm_unsigned w) = w"
skalberg@14494
   633
  by (simp add: norm_unsigned_def,rule bv_extend_rem_initial)
skalberg@14494
   634
skalberg@14494
   635
lemma norm_unsigned_append1 [simp]: "norm_unsigned xs \<noteq> [] ==> norm_unsigned (xs @ ys) = norm_unsigned xs @ ys"
skalberg@14494
   636
  by (simp add: norm_unsigned_def,rule rem_initial_append1)
skalberg@14494
   637
skalberg@14494
   638
lemma norm_unsigned_append2 [simp]: "norm_unsigned xs = [] ==> norm_unsigned (xs @ ys) = norm_unsigned ys"
skalberg@14494
   639
  by (simp add: norm_unsigned_def,rule rem_initial_append2)
skalberg@14494
   640
berghofe@15325
   641
lemma bv_to_nat_zero_imp_empty [rule_format]:
berghofe@15325
   642
  "bv_to_nat w = 0 \<longrightarrow> norm_unsigned w = []"
berghofe@15325
   643
  by (rule bit_list_induct [of _ w],simp_all)
skalberg@14494
   644
skalberg@14494
   645
lemma bv_to_nat_nzero_imp_nempty:
skalberg@14494
   646
  assumes "bv_to_nat w \<noteq> 0"
skalberg@14494
   647
  shows   "norm_unsigned w \<noteq> []"
skalberg@14494
   648
proof -
skalberg@14494
   649
  have "bv_to_nat w \<noteq> 0 --> norm_unsigned w \<noteq> []"
skalberg@14494
   650
    by (rule bit_list_induct [of _ w],simp_all)
skalberg@14494
   651
  thus ?thesis
skalberg@14494
   652
    ..
skalberg@14494
   653
qed
skalberg@14494
   654
skalberg@14494
   655
lemma nat_helper1:
skalberg@14494
   656
  assumes ass: "nat_to_bv (bv_to_nat w) = norm_unsigned w"
skalberg@14494
   657
  shows        "nat_to_bv (2 * bv_to_nat w + bitval x) = norm_unsigned (w @ [x])"
skalberg@14494
   658
proof (cases x)
skalberg@14494
   659
  assume [simp]: "x = \<one>"
skalberg@14494
   660
  show ?thesis
skalberg@14494
   661
    apply (simp add: nat_to_bv_non0)
skalberg@14494
   662
    apply safe
skalberg@14494
   663
  proof -
skalberg@14494
   664
    fix q
berghofe@15325
   665
    assume "Suc (2 * bv_to_nat w) = 2 * q"
skalberg@14494
   666
    hence orig: "(2 * bv_to_nat w + 1) mod 2 = 2 * q mod 2" (is "?lhs = ?rhs")
skalberg@14494
   667
      by simp
skalberg@14494
   668
    have "?lhs = (1 + 2 * bv_to_nat w) mod 2"
skalberg@14494
   669
      by (simp add: add_commute)
skalberg@14494
   670
    also have "... = 1"
berghofe@15325
   671
      by (subst mod_add1_eq) simp
skalberg@14494
   672
    finally have eq1: "?lhs = 1" .
skalberg@14494
   673
    have "?rhs  = 0"
skalberg@14494
   674
      by simp
skalberg@14494
   675
    with orig and eq1
berghofe@15325
   676
    show "nat_to_bv (Suc (2 * bv_to_nat w) div 2) @ [\<zero>] = norm_unsigned (w @ [\<one>])"
skalberg@14494
   677
      by simp
skalberg@14494
   678
  next
berghofe@15325
   679
    have "nat_to_bv (Suc (2 * bv_to_nat w) div 2) @ [\<one>] = nat_to_bv ((1 + 2 * bv_to_nat w) div 2) @ [\<one>]"
skalberg@14494
   680
      by (simp add: add_commute)
skalberg@14494
   681
    also have "... = nat_to_bv (bv_to_nat w) @ [\<one>]"
berghofe@15325
   682
      by (subst div_add1_eq,simp)
skalberg@14494
   683
    also have "... = norm_unsigned w @ [\<one>]"
skalberg@14494
   684
      by (subst ass,rule refl)
skalberg@14494
   685
    also have "... = norm_unsigned (w @ [\<one>])"
skalberg@14494
   686
      by (cases "norm_unsigned w",simp_all)
berghofe@15325
   687
    finally show "nat_to_bv (Suc (2 * bv_to_nat w) div 2) @ [\<one>] = norm_unsigned (w @ [\<one>])"
skalberg@14494
   688
      .
skalberg@14494
   689
  qed
skalberg@14494
   690
next
skalberg@14494
   691
  assume [simp]: "x = \<zero>"
skalberg@14494
   692
  show ?thesis
skalberg@14494
   693
  proof (cases "bv_to_nat w = 0")
skalberg@14494
   694
    assume "bv_to_nat w = 0"
skalberg@14494
   695
    thus ?thesis
skalberg@14494
   696
      by (simp add: bv_to_nat_zero_imp_empty)
skalberg@14494
   697
  next
skalberg@14494
   698
    assume "bv_to_nat w \<noteq> 0"
skalberg@14494
   699
    thus ?thesis
skalberg@14494
   700
      apply simp
skalberg@14494
   701
      apply (subst nat_to_bv_non0)
skalberg@14494
   702
      apply simp
skalberg@14494
   703
      apply auto
skalberg@14494
   704
      apply (subst ass)
skalberg@14494
   705
      apply (cases "norm_unsigned w")
skalberg@14494
   706
      apply (simp_all add: norm_empty_bv_to_nat_zero)
skalberg@14494
   707
      done
skalberg@14494
   708
  qed
skalberg@14494
   709
qed
skalberg@14494
   710
skalberg@14494
   711
lemma nat_helper2: "nat_to_bv (2 ^ length xs + bv_to_nat xs) = \<one> # xs"
skalberg@14494
   712
proof -
skalberg@14494
   713
  have "\<forall>xs. nat_to_bv (2 ^ length (rev xs) + bv_to_nat (rev xs)) = \<one> # (rev xs)" (is "\<forall>xs. ?P xs")
skalberg@14494
   714
  proof
skalberg@14494
   715
    fix xs
skalberg@14494
   716
    show "?P xs"
skalberg@14494
   717
    proof (rule length_induct [of _ xs])
skalberg@14494
   718
      fix xs :: "bit list"
skalberg@14494
   719
      assume ind: "\<forall>ys. length ys < length xs --> ?P ys"
skalberg@14494
   720
      show "?P xs"
skalberg@14494
   721
      proof (cases xs)
wenzelm@19736
   722
        assume [simp]: "xs = []"
wenzelm@19736
   723
        show ?thesis
wenzelm@19736
   724
          by (simp add: nat_to_bv_non0)
skalberg@14494
   725
      next
wenzelm@19736
   726
        fix y ys
wenzelm@19736
   727
        assume [simp]: "xs = y # ys"
wenzelm@19736
   728
        show ?thesis
wenzelm@19736
   729
          apply simp
wenzelm@19736
   730
          apply (subst bv_to_nat_dist_append)
wenzelm@19736
   731
          apply simp
wenzelm@19736
   732
        proof -
wenzelm@19736
   733
          have "nat_to_bv (2 * 2 ^ length ys + (bv_to_nat (rev ys) * 2 + bitval y)) =
wenzelm@19736
   734
            nat_to_bv (2 * (2 ^ length ys + bv_to_nat (rev ys)) + bitval y)"
wenzelm@19736
   735
            by (simp add: add_ac mult_ac)
wenzelm@19736
   736
          also have "... = nat_to_bv (2 * (bv_to_nat (\<one>#rev ys)) + bitval y)"
wenzelm@19736
   737
            by simp
wenzelm@19736
   738
          also have "... = norm_unsigned (\<one>#rev ys) @ [y]"
wenzelm@19736
   739
          proof -
wenzelm@19736
   740
            from ind
wenzelm@19736
   741
            have "nat_to_bv (2 ^ length (rev ys) + bv_to_nat (rev ys)) = \<one> # rev ys"
wenzelm@19736
   742
              by auto
wenzelm@19736
   743
            hence [simp]: "nat_to_bv (2 ^ length ys + bv_to_nat (rev ys)) = \<one> # rev ys"
wenzelm@19736
   744
              by simp
wenzelm@19736
   745
            show ?thesis
wenzelm@19736
   746
              apply (subst nat_helper1)
wenzelm@19736
   747
              apply simp_all
wenzelm@19736
   748
              done
wenzelm@19736
   749
          qed
wenzelm@19736
   750
          also have "... = (\<one>#rev ys) @ [y]"
wenzelm@19736
   751
            by simp
wenzelm@19736
   752
          also have "... = \<one> # rev ys @ [y]"
wenzelm@19736
   753
            by simp
wenzelm@19736
   754
          finally show "nat_to_bv (2 * 2 ^ length ys + (bv_to_nat (rev ys) * 2 + bitval y)) = \<one> # rev ys @ [y]"
wenzelm@19736
   755
            .
wenzelm@19736
   756
        qed
skalberg@14494
   757
      qed
skalberg@14494
   758
    qed
skalberg@14494
   759
  qed
skalberg@14494
   760
  hence "nat_to_bv (2 ^ length (rev (rev xs)) + bv_to_nat (rev (rev xs))) = \<one> # rev (rev xs)"
skalberg@14494
   761
    ..
skalberg@14494
   762
  thus ?thesis
skalberg@14494
   763
    by simp
skalberg@14494
   764
qed
skalberg@14494
   765
skalberg@14494
   766
lemma nat_bv_nat [simp]: "nat_to_bv (bv_to_nat w) = norm_unsigned w"
skalberg@14494
   767
proof (rule bit_list_induct [of _ w],simp_all)
skalberg@14494
   768
  fix xs
skalberg@14494
   769
  assume "nat_to_bv (bv_to_nat xs) = norm_unsigned xs"
skalberg@14494
   770
  have "bv_to_nat xs = bv_to_nat (norm_unsigned xs)"
skalberg@14494
   771
    by simp
skalberg@14494
   772
  have "bv_to_nat xs < 2 ^ length xs"
skalberg@14494
   773
    by (rule bv_to_nat_upper_range)
skalberg@14494
   774
  show "nat_to_bv (2 ^ length xs + bv_to_nat xs) = \<one> # xs"
skalberg@14494
   775
    by (rule nat_helper2)
skalberg@14494
   776
qed
skalberg@14494
   777
skalberg@14494
   778
lemma bv_to_nat_qinj:
skalberg@14494
   779
  assumes one: "bv_to_nat xs = bv_to_nat ys"
skalberg@14494
   780
  and     len: "length xs = length ys"
skalberg@14494
   781
  shows        "xs = ys"
skalberg@14494
   782
proof -
skalberg@14494
   783
  from one
skalberg@14494
   784
  have "nat_to_bv (bv_to_nat xs) = nat_to_bv (bv_to_nat ys)"
skalberg@14494
   785
    by simp
skalberg@14494
   786
  hence xsys: "norm_unsigned xs = norm_unsigned ys"
skalberg@14494
   787
    by simp
skalberg@14494
   788
  have "xs = bv_extend (length xs) \<zero> (norm_unsigned xs)"
skalberg@14494
   789
    by (simp add: bv_extend_norm_unsigned)
skalberg@14494
   790
  also have "... = bv_extend (length ys) \<zero> (norm_unsigned ys)"
skalberg@14494
   791
    by (simp add: xsys len)
skalberg@14494
   792
  also have "... = ys"
skalberg@14494
   793
    by (simp add: bv_extend_norm_unsigned)
skalberg@14494
   794
  finally show ?thesis .
skalberg@14494
   795
qed
skalberg@14494
   796
skalberg@14494
   797
lemma norm_unsigned_nat_to_bv [simp]:
berghofe@15325
   798
  "norm_unsigned (nat_to_bv n) = nat_to_bv n"
skalberg@14494
   799
proof -
skalberg@14494
   800
  have "norm_unsigned (nat_to_bv n) = nat_to_bv (bv_to_nat (norm_unsigned (nat_to_bv n)))"
skalberg@14494
   801
    by (subst nat_bv_nat,simp)
skalberg@14494
   802
  also have "... = nat_to_bv n"
skalberg@14494
   803
    by simp
skalberg@14494
   804
  finally show ?thesis .
skalberg@14494
   805
qed
skalberg@14494
   806
skalberg@14494
   807
lemma length_nat_to_bv_upper_limit:
skalberg@14494
   808
  assumes nk: "n \<le> 2 ^ k - 1"
skalberg@14494
   809
  shows       "length (nat_to_bv n) \<le> k"
berghofe@15325
   810
proof (cases "n = 0")
berghofe@15325
   811
  case True
skalberg@14494
   812
  thus ?thesis
skalberg@14494
   813
    by (simp add: nat_to_bv_def nat_to_bv_helper.simps)
skalberg@14494
   814
next
berghofe@15325
   815
  case False
berghofe@15325
   816
  hence n0: "0 < n" by simp
skalberg@14494
   817
  show ?thesis
skalberg@14494
   818
  proof (rule ccontr)
skalberg@14494
   819
    assume "~ length (nat_to_bv n) \<le> k"
skalberg@14494
   820
    hence "k < length (nat_to_bv n)"
skalberg@14494
   821
      by simp
skalberg@14494
   822
    hence "k \<le> length (nat_to_bv n) - 1"
skalberg@14494
   823
      by arith
berghofe@15325
   824
    hence "(2::nat) ^ k \<le> 2 ^ (length (nat_to_bv n) - 1)"
skalberg@14494
   825
      by simp
skalberg@14494
   826
    also have "... = 2 ^ (length (norm_unsigned (nat_to_bv n)) - 1)"
berghofe@15325
   827
      by simp
skalberg@14494
   828
    also have "... \<le> bv_to_nat (nat_to_bv n)"
berghofe@15325
   829
      by (rule bv_to_nat_lower_limit,simp add: n0)
skalberg@14494
   830
    also have "... = n"
berghofe@15325
   831
      by simp
skalberg@14494
   832
    finally have "2 ^ k \<le> n" .
skalberg@14494
   833
    with n0
skalberg@14494
   834
    have "2 ^ k - 1 < n"
skalberg@14494
   835
      by arith
skalberg@14494
   836
    with nk
skalberg@14494
   837
    show False
skalberg@14494
   838
      by simp
skalberg@14494
   839
  qed
skalberg@14494
   840
qed
skalberg@14494
   841
skalberg@14494
   842
lemma length_nat_to_bv_lower_limit:
skalberg@14494
   843
  assumes nk: "2 ^ k \<le> n"
skalberg@14494
   844
  shows       "k < length (nat_to_bv n)"
skalberg@14494
   845
proof (rule ccontr)
skalberg@14494
   846
  assume "~ k < length (nat_to_bv n)"
skalberg@14494
   847
  hence lnk: "length (nat_to_bv n) \<le> k"
skalberg@14494
   848
    by simp
skalberg@14494
   849
  have "n = bv_to_nat (nat_to_bv n)"
skalberg@14494
   850
    by simp
skalberg@14494
   851
  also have "... < 2 ^ length (nat_to_bv n)"
skalberg@14494
   852
    by (rule bv_to_nat_upper_range)
skalberg@14494
   853
  also from lnk have "... \<le> 2 ^ k"
skalberg@14494
   854
    by simp
skalberg@14494
   855
  finally have "n < 2 ^ k" .
skalberg@14494
   856
  with nk
skalberg@14494
   857
  show False
skalberg@14494
   858
    by simp
skalberg@14494
   859
qed
skalberg@14494
   860
wenzelm@14589
   861
subsection {* Unsigned Arithmetic Operations *}
skalberg@14494
   862
wenzelm@19736
   863
definition
wenzelm@21404
   864
  bv_add :: "[bit list, bit list ] => bit list" where
wenzelm@19736
   865
  "bv_add w1 w2 = nat_to_bv (bv_to_nat w1 + bv_to_nat w2)"
skalberg@14494
   866
skalberg@17650
   867
lemma bv_add_type1 [simp]: "bv_add (norm_unsigned w1) w2 = bv_add w1 w2"
skalberg@14494
   868
  by (simp add: bv_add_def)
skalberg@14494
   869
skalberg@17650
   870
lemma bv_add_type2 [simp]: "bv_add w1 (norm_unsigned w2) = bv_add w1 w2"
skalberg@14494
   871
  by (simp add: bv_add_def)
skalberg@14494
   872
skalberg@17650
   873
lemma bv_add_returntype [simp]: "norm_unsigned (bv_add w1 w2) = bv_add w1 w2"
berghofe@15325
   874
  by (simp add: bv_add_def)
skalberg@14494
   875
skalberg@14494
   876
lemma bv_add_length: "length (bv_add w1 w2) \<le> Suc (max (length w1) (length w2))"
skalberg@14494
   877
proof (unfold bv_add_def,rule length_nat_to_bv_upper_limit)
skalberg@14494
   878
  from bv_to_nat_upper_range [of w1] and bv_to_nat_upper_range [of w2]
skalberg@14494
   879
  have "bv_to_nat w1 + bv_to_nat w2 \<le> (2 ^ length w1 - 1) + (2 ^ length w2 - 1)"
skalberg@14494
   880
    by arith
skalberg@14494
   881
  also have "... \<le> max (2 ^ length w1 - 1) (2 ^ length w2 - 1) + max (2 ^ length w1 - 1) (2 ^ length w2 - 1)"
skalberg@14494
   882
    by (rule add_mono,safe intro!: le_maxI1 le_maxI2)
skalberg@14494
   883
  also have "... = 2 * max (2 ^ length w1 - 1) (2 ^ length w2 - 1)"
skalberg@14494
   884
    by simp
skalberg@14494
   885
  also have "... \<le> 2 ^ Suc (max (length w1) (length w2)) - 2"
skalberg@14494
   886
  proof (cases "length w1 \<le> length w2")
berghofe@15325
   887
    assume w1w2: "length w1 \<le> length w2"
berghofe@15325
   888
    hence "(2::nat) ^ length w1 \<le> 2 ^ length w2"
skalberg@14494
   889
      by simp
berghofe@15325
   890
    hence "(2::nat) ^ length w1 - 1 \<le> 2 ^ length w2 - 1"
skalberg@14494
   891
      by arith
berghofe@15325
   892
    with w1w2 show ?thesis
berghofe@15325
   893
      by (simp add: diff_mult_distrib2 split: split_max)
skalberg@14494
   894
  next
skalberg@14494
   895
    assume [simp]: "~ (length w1 \<le> length w2)"
berghofe@15325
   896
    have "~ ((2::nat) ^ length w1 - 1 \<le> 2 ^ length w2 - 1)"
skalberg@14494
   897
    proof
berghofe@15325
   898
      assume "(2::nat) ^ length w1 - 1 \<le> 2 ^ length w2 - 1"
berghofe@15325
   899
      hence "((2::nat) ^ length w1 - 1) + 1 \<le> (2 ^ length w2 - 1) + 1"
wenzelm@19736
   900
        by (rule add_right_mono)
berghofe@15325
   901
      hence "(2::nat) ^ length w1 \<le> 2 ^ length w2"
wenzelm@19736
   902
        by simp
skalberg@14494
   903
      hence "length w1 \<le> length w2"
wenzelm@19736
   904
        by simp
skalberg@14494
   905
      thus False
wenzelm@19736
   906
        by simp
skalberg@14494
   907
    qed
skalberg@14494
   908
    thus ?thesis
berghofe@15325
   909
      by (simp add: diff_mult_distrib2 split: split_max)
skalberg@14494
   910
  qed
skalberg@14494
   911
  finally show "bv_to_nat w1 + bv_to_nat w2 \<le> 2 ^ Suc (max (length w1) (length w2)) - 1"
skalberg@14494
   912
    by arith
skalberg@14494
   913
qed
skalberg@14494
   914
wenzelm@19736
   915
definition
wenzelm@21404
   916
  bv_mult :: "[bit list, bit list ] => bit list" where
wenzelm@19736
   917
  "bv_mult w1 w2 = nat_to_bv (bv_to_nat w1 * bv_to_nat w2)"
skalberg@14494
   918
skalberg@17650
   919
lemma bv_mult_type1 [simp]: "bv_mult (norm_unsigned w1) w2 = bv_mult w1 w2"
skalberg@14494
   920
  by (simp add: bv_mult_def)
skalberg@14494
   921
skalberg@17650
   922
lemma bv_mult_type2 [simp]: "bv_mult w1 (norm_unsigned w2) = bv_mult w1 w2"
skalberg@14494
   923
  by (simp add: bv_mult_def)
skalberg@14494
   924
skalberg@17650
   925
lemma bv_mult_returntype [simp]: "norm_unsigned (bv_mult w1 w2) = bv_mult w1 w2"
berghofe@15325
   926
  by (simp add: bv_mult_def)
skalberg@14494
   927
skalberg@14494
   928
lemma bv_mult_length: "length (bv_mult w1 w2) \<le> length w1 + length w2"
skalberg@14494
   929
proof (unfold bv_mult_def,rule length_nat_to_bv_upper_limit)
skalberg@14494
   930
  from bv_to_nat_upper_range [of w1] and bv_to_nat_upper_range [of w2]
skalberg@14494
   931
  have h: "bv_to_nat w1 \<le> 2 ^ length w1 - 1 \<and> bv_to_nat w2 \<le> 2 ^ length w2 - 1"
skalberg@14494
   932
    by arith
skalberg@14494
   933
  have "bv_to_nat w1 * bv_to_nat w2 \<le> (2 ^ length w1 - 1) * (2 ^ length w2 - 1)"
skalberg@14494
   934
    apply (cut_tac h)
skalberg@14494
   935
    apply (rule mult_mono)
skalberg@14494
   936
    apply auto
skalberg@14494
   937
    done
skalberg@14494
   938
  also have "... < 2 ^ length w1 * 2 ^ length w2"
skalberg@14494
   939
    by (rule mult_strict_mono,auto)
skalberg@14494
   940
  also have "... = 2 ^ (length w1 + length w2)"
skalberg@14494
   941
    by (simp add: power_add)
skalberg@14494
   942
  finally show "bv_to_nat w1 * bv_to_nat w2 \<le> 2 ^ (length w1 + length w2) - 1"
skalberg@14494
   943
    by arith
skalberg@14494
   944
qed
skalberg@14494
   945
wenzelm@14589
   946
subsection {* Signed Vectors *}
skalberg@14494
   947
skalberg@14494
   948
consts
skalberg@14494
   949
  norm_signed :: "bit list => bit list"
skalberg@14494
   950
skalberg@14494
   951
primrec
skalberg@14494
   952
  norm_signed_Nil: "norm_signed [] = []"
skalberg@14494
   953
  norm_signed_Cons: "norm_signed (b#bs) = (case b of \<zero> => if norm_unsigned bs = [] then [] else b#norm_unsigned bs | \<one> => b#rem_initial b bs)"
skalberg@14494
   954
skalberg@17650
   955
lemma norm_signed0 [simp]: "norm_signed [\<zero>] = []"
skalberg@14494
   956
  by simp
skalberg@14494
   957
skalberg@17650
   958
lemma norm_signed1 [simp]: "norm_signed [\<one>] = [\<one>]"
skalberg@14494
   959
  by simp
skalberg@14494
   960
skalberg@17650
   961
lemma norm_signed01 [simp]: "norm_signed (\<zero>#\<one>#xs) = \<zero>#\<one>#xs"
skalberg@14494
   962
  by simp
skalberg@14494
   963
skalberg@17650
   964
lemma norm_signed00 [simp]: "norm_signed (\<zero>#\<zero>#xs) = norm_signed (\<zero>#xs)"
skalberg@14494
   965
  by simp
skalberg@14494
   966
skalberg@17650
   967
lemma norm_signed10 [simp]: "norm_signed (\<one>#\<zero>#xs) = \<one>#\<zero>#xs"
skalberg@14494
   968
  by simp
skalberg@14494
   969
skalberg@17650
   970
lemma norm_signed11 [simp]: "norm_signed (\<one>#\<one>#xs) = norm_signed (\<one>#xs)"
skalberg@14494
   971
  by simp
skalberg@14494
   972
skalberg@14494
   973
lemmas [simp del] = norm_signed_Cons
skalberg@14494
   974
wenzelm@19736
   975
definition
wenzelm@21404
   976
  int_to_bv :: "int => bit list" where
wenzelm@19736
   977
  "int_to_bv n = (if 0 \<le> n
berghofe@15325
   978
                 then norm_signed (\<zero>#nat_to_bv (nat n))
wenzelm@19736
   979
                 else norm_signed (bv_not (\<zero>#nat_to_bv (nat (-n- 1)))))"
skalberg@14494
   980
berghofe@15325
   981
lemma int_to_bv_ge0 [simp]: "0 \<le> n ==> int_to_bv n = norm_signed (\<zero> # nat_to_bv (nat n))"
skalberg@14494
   982
  by (simp add: int_to_bv_def)
skalberg@14494
   983
berghofe@15325
   984
lemma int_to_bv_lt0 [simp]: "n < 0 ==> int_to_bv n = norm_signed (bv_not (\<zero>#nat_to_bv (nat (-n- 1))))"
skalberg@14494
   985
  by (simp add: int_to_bv_def)
skalberg@14494
   986
skalberg@17650
   987
lemma norm_signed_idem [simp]: "norm_signed (norm_signed w) = norm_signed w"
skalberg@14494
   988
proof (rule bit_list_induct [of _ w],simp_all)
skalberg@14494
   989
  fix xs
skalberg@14494
   990
  assume "norm_signed (norm_signed xs) = norm_signed xs"
skalberg@14494
   991
  show "norm_signed (norm_signed (\<zero>#xs)) = norm_signed (\<zero>#xs)"
skalberg@14494
   992
  proof (rule bit_list_cases [of xs],simp_all)
skalberg@14494
   993
    fix ys
skalberg@14494
   994
    assume [symmetric,simp]: "xs = \<zero>#ys"
skalberg@14494
   995
    show "norm_signed (norm_signed (\<zero>#ys)) = norm_signed (\<zero>#ys)"
skalberg@14494
   996
      by simp
skalberg@14494
   997
  qed
skalberg@14494
   998
next
skalberg@14494
   999
  fix xs
skalberg@14494
  1000
  assume "norm_signed (norm_signed xs) = norm_signed xs"
skalberg@14494
  1001
  show "norm_signed (norm_signed (\<one>#xs)) = norm_signed (\<one>#xs)"
skalberg@14494
  1002
  proof (rule bit_list_cases [of xs],simp_all)
skalberg@14494
  1003
    fix ys
skalberg@14494
  1004
    assume [symmetric,simp]: "xs = \<one>#ys"
skalberg@14494
  1005
    show "norm_signed (norm_signed (\<one>#ys)) = norm_signed (\<one>#ys)"
skalberg@14494
  1006
      by simp
skalberg@14494
  1007
  qed
skalberg@14494
  1008
qed
skalberg@14494
  1009
wenzelm@19736
  1010
definition
wenzelm@21404
  1011
  bv_to_int :: "bit list => int" where
wenzelm@19736
  1012
  "bv_to_int w =
wenzelm@19736
  1013
    (case bv_msb w of \<zero> => int (bv_to_nat w)
wenzelm@19736
  1014
    | \<one> => - int (bv_to_nat (bv_not w) + 1))"
skalberg@14494
  1015
skalberg@17650
  1016
lemma bv_to_int_Nil [simp]: "bv_to_int [] = 0"
skalberg@14494
  1017
  by (simp add: bv_to_int_def)
skalberg@14494
  1018
skalberg@17650
  1019
lemma bv_to_int_Cons0 [simp]: "bv_to_int (\<zero>#bs) = int (bv_to_nat bs)"
skalberg@14494
  1020
  by (simp add: bv_to_int_def)
skalberg@14494
  1021
skalberg@17650
  1022
lemma bv_to_int_Cons1 [simp]: "bv_to_int (\<one>#bs) = - int (bv_to_nat (bv_not bs) + 1)"
skalberg@14494
  1023
  by (simp add: bv_to_int_def)
skalberg@14494
  1024
skalberg@17650
  1025
lemma bv_to_int_type [simp]: "bv_to_int (norm_signed w) = bv_to_int w"
skalberg@14494
  1026
proof (rule bit_list_induct [of _ w],simp_all)
skalberg@14494
  1027
  fix xs
skalberg@14494
  1028
  assume ind: "bv_to_int (norm_signed xs) = bv_to_int xs"
berghofe@15325
  1029
  show "bv_to_int (norm_signed (\<zero>#xs)) = int (bv_to_nat xs)"
skalberg@14494
  1030
  proof (rule bit_list_cases [of xs],simp_all)
skalberg@14494
  1031
    fix ys
skalberg@14494
  1032
    assume [simp]: "xs = \<zero>#ys"
skalberg@14494
  1033
    from ind
berghofe@15325
  1034
    show "bv_to_int (norm_signed (\<zero>#ys)) = int (bv_to_nat ys)"
skalberg@14494
  1035
      by simp
skalberg@14494
  1036
  qed
skalberg@14494
  1037
next
skalberg@14494
  1038
  fix xs
skalberg@14494
  1039
  assume ind: "bv_to_int (norm_signed xs) = bv_to_int xs"
berghofe@15325
  1040
  show "bv_to_int (norm_signed (\<one>#xs)) = -1 - int (bv_to_nat (bv_not xs))"
skalberg@14494
  1041
  proof (rule bit_list_cases [of xs],simp_all)
skalberg@14494
  1042
    fix ys
skalberg@14494
  1043
    assume [simp]: "xs = \<one>#ys"
skalberg@14494
  1044
    from ind
berghofe@15325
  1045
    show "bv_to_int (norm_signed (\<one>#ys)) = -1 - int (bv_to_nat (bv_not ys))"
skalberg@14494
  1046
      by simp
skalberg@14494
  1047
  qed
skalberg@14494
  1048
qed
skalberg@14494
  1049
skalberg@14494
  1050
lemma bv_to_int_upper_range: "bv_to_int w < 2 ^ (length w - 1)"
skalberg@14494
  1051
proof (rule bit_list_cases [of w],simp_all)
skalberg@14494
  1052
  fix bs
berghofe@15325
  1053
  from bv_to_nat_upper_range
berghofe@15325
  1054
  show "int (bv_to_nat bs) < 2 ^ length bs"
berghofe@15325
  1055
    by (simp add: int_nat_two_exp)
skalberg@14494
  1056
next
skalberg@14494
  1057
  fix bs
berghofe@15325
  1058
  have "-1 - int (bv_to_nat (bv_not bs)) \<le> 0"
berghofe@15325
  1059
    by simp
skalberg@14494
  1060
  also have "... < 2 ^ length bs"
skalberg@14494
  1061
    by (induct bs,simp_all)
berghofe@15325
  1062
  finally show "-1 - int (bv_to_nat (bv_not bs)) < 2 ^ length bs"
skalberg@14494
  1063
    .
skalberg@14494
  1064
qed
skalberg@14494
  1065
skalberg@14494
  1066
lemma bv_to_int_lower_range: "- (2 ^ (length w - 1)) \<le> bv_to_int w"
skalberg@14494
  1067
proof (rule bit_list_cases [of w],simp_all)
skalberg@14494
  1068
  fix bs :: "bit list"
skalberg@14494
  1069
  have "- (2 ^ length bs) \<le> (0::int)"
skalberg@14494
  1070
    by (induct bs,simp_all)
berghofe@15325
  1071
  also have "... \<le> int (bv_to_nat bs)"
skalberg@14494
  1072
    by simp
berghofe@15325
  1073
  finally show "- (2 ^ length bs) \<le> int (bv_to_nat bs)"
skalberg@14494
  1074
    .
skalberg@14494
  1075
next
skalberg@14494
  1076
  fix bs
skalberg@14494
  1077
  from bv_to_nat_upper_range [of "bv_not bs"]
berghofe@15325
  1078
  show "- (2 ^ length bs) \<le> -1 - int (bv_to_nat (bv_not bs))"
berghofe@15325
  1079
    by (simp add: int_nat_two_exp)
skalberg@14494
  1080
qed
skalberg@14494
  1081
skalberg@14494
  1082
lemma int_bv_int [simp]: "int_to_bv (bv_to_int w) = norm_signed w"
skalberg@14494
  1083
proof (rule bit_list_cases [of w],simp)
skalberg@14494
  1084
  fix xs
skalberg@14494
  1085
  assume [simp]: "w = \<zero>#xs"
skalberg@14494
  1086
  show ?thesis
skalberg@14494
  1087
    apply simp
skalberg@14494
  1088
    apply (subst norm_signed_Cons [of "\<zero>" "xs"])
skalberg@14494
  1089
    apply simp
skalberg@14494
  1090
    using norm_unsigned_result [of xs]
skalberg@14494
  1091
    apply safe
skalberg@14494
  1092
    apply (rule bit_list_cases [of "norm_unsigned xs"])
skalberg@14494
  1093
    apply simp_all
skalberg@14494
  1094
    done
skalberg@14494
  1095
next
skalberg@14494
  1096
  fix xs
skalberg@14494
  1097
  assume [simp]: "w = \<one>#xs"
skalberg@14494
  1098
  show ?thesis
berghofe@15325
  1099
    apply (simp del: int_to_bv_lt0)
skalberg@14494
  1100
    apply (rule bit_list_induct [of _ xs])
skalberg@14494
  1101
    apply simp
skalberg@14494
  1102
    apply (subst int_to_bv_lt0)
berghofe@15325
  1103
    apply (subgoal_tac "- int (bv_to_nat (bv_not (\<zero> # bs))) + -1 < 0 + 0")
skalberg@14494
  1104
    apply simp
skalberg@14494
  1105
    apply (rule add_le_less_mono)
skalberg@14494
  1106
    apply simp
skalberg@14494
  1107
    apply simp
skalberg@14494
  1108
    apply (simp del: bv_to_nat1 bv_to_nat_helper)
skalberg@14494
  1109
    apply simp
skalberg@14494
  1110
    done
skalberg@14494
  1111
qed
skalberg@14494
  1112
skalberg@14494
  1113
lemma bv_int_bv [simp]: "bv_to_int (int_to_bv i) = i"
skalberg@14494
  1114
  by (cases "0 \<le> i",simp_all)
skalberg@14494
  1115
skalberg@14494
  1116
lemma bv_msb_norm [simp]: "bv_msb (norm_signed w) = bv_msb w"
skalberg@14494
  1117
  by (rule bit_list_cases [of w],simp_all add: norm_signed_Cons)
skalberg@14494
  1118
skalberg@14494
  1119
lemma norm_signed_length: "length (norm_signed w) \<le> length w"
skalberg@14494
  1120
  apply (cases w,simp_all)
skalberg@14494
  1121
  apply (subst norm_signed_Cons)
skalberg@14494
  1122
  apply (case_tac "a",simp_all)
skalberg@14494
  1123
  apply (rule rem_initial_length)
skalberg@14494
  1124
  done
skalberg@14494
  1125
skalberg@14494
  1126
lemma norm_signed_equal: "length (norm_signed w) = length w ==> norm_signed w = w"
skalberg@14494
  1127
proof (rule bit_list_cases [of w],simp_all)
skalberg@14494
  1128
  fix xs
skalberg@14494
  1129
  assume "length (norm_signed (\<zero>#xs)) = Suc (length xs)"
skalberg@14494
  1130
  thus "norm_signed (\<zero>#xs) = \<zero>#xs"
skalberg@14494
  1131
    apply (simp add: norm_signed_Cons)
skalberg@14494
  1132
    apply safe
skalberg@14494
  1133
    apply simp_all
skalberg@14494
  1134
    apply (rule norm_unsigned_equal)
skalberg@14494
  1135
    apply assumption
skalberg@14494
  1136
    done
skalberg@14494
  1137
next
skalberg@14494
  1138
  fix xs
skalberg@14494
  1139
  assume "length (norm_signed (\<one>#xs)) = Suc (length xs)"
skalberg@14494
  1140
  thus "norm_signed (\<one>#xs) = \<one>#xs"
skalberg@14494
  1141
    apply (simp add: norm_signed_Cons)
skalberg@14494
  1142
    apply (rule rem_initial_equal)
skalberg@14494
  1143
    apply assumption
skalberg@14494
  1144
    done
skalberg@14494
  1145
qed
skalberg@14494
  1146
skalberg@14494
  1147
lemma bv_extend_norm_signed: "bv_msb w = b ==> bv_extend (length w) b (norm_signed w) = w"
skalberg@14494
  1148
proof (rule bit_list_cases [of w],simp_all)
skalberg@14494
  1149
  fix xs
skalberg@14494
  1150
  show "bv_extend (Suc (length xs)) \<zero> (norm_signed (\<zero>#xs)) = \<zero>#xs"
skalberg@14494
  1151
  proof (simp add: norm_signed_list_def,auto)
skalberg@14494
  1152
    assume "norm_unsigned xs = []"
skalberg@14494
  1153
    hence xx: "rem_initial \<zero> xs = []"
skalberg@14494
  1154
      by (simp add: norm_unsigned_def)
skalberg@14494
  1155
    have "bv_extend (Suc (length xs)) \<zero> (\<zero>#rem_initial \<zero> xs) = \<zero>#xs"
skalberg@14494
  1156
      apply (simp add: bv_extend_def replicate_app_Cons_same)
skalberg@14494
  1157
      apply (fold bv_extend_def)
skalberg@14494
  1158
      apply (rule bv_extend_rem_initial)
skalberg@14494
  1159
      done
skalberg@14494
  1160
    thus "bv_extend (Suc (length xs)) \<zero> [\<zero>] = \<zero>#xs"
skalberg@14494
  1161
      by (simp add: xx)
skalberg@14494
  1162
  next
skalberg@14494
  1163
    show "bv_extend (Suc (length xs)) \<zero> (\<zero>#norm_unsigned xs) = \<zero>#xs"
skalberg@14494
  1164
      apply (simp add: norm_unsigned_def)
skalberg@14494
  1165
      apply (simp add: bv_extend_def replicate_app_Cons_same)
skalberg@14494
  1166
      apply (fold bv_extend_def)
skalberg@14494
  1167
      apply (rule bv_extend_rem_initial)
skalberg@14494
  1168
      done
skalberg@14494
  1169
  qed
skalberg@14494
  1170
next
skalberg@14494
  1171
  fix xs
skalberg@14494
  1172
  show "bv_extend (Suc (length xs)) \<one> (norm_signed (\<one>#xs)) = \<one>#xs"
skalberg@14494
  1173
    apply (simp add: norm_signed_Cons)
skalberg@14494
  1174
    apply (simp add: bv_extend_def replicate_app_Cons_same)
skalberg@14494
  1175
    apply (fold bv_extend_def)
skalberg@14494
  1176
    apply (rule bv_extend_rem_initial)
skalberg@14494
  1177
    done
skalberg@14494
  1178
qed
skalberg@14494
  1179
skalberg@14494
  1180
lemma bv_to_int_qinj:
skalberg@14494
  1181
  assumes one: "bv_to_int xs = bv_to_int ys"
skalberg@14494
  1182
  and     len: "length xs = length ys"
skalberg@14494
  1183
  shows        "xs = ys"
skalberg@14494
  1184
proof -
skalberg@14494
  1185
  from one
skalberg@14494
  1186
  have "int_to_bv (bv_to_int xs) = int_to_bv (bv_to_int ys)"
skalberg@14494
  1187
    by simp
skalberg@14494
  1188
  hence xsys: "norm_signed xs = norm_signed ys"
skalberg@14494
  1189
    by simp
skalberg@14494
  1190
  hence xsys': "bv_msb xs = bv_msb ys"
skalberg@14494
  1191
  proof -
skalberg@14494
  1192
    have "bv_msb xs = bv_msb (norm_signed xs)"
skalberg@14494
  1193
      by simp
skalberg@14494
  1194
    also have "... = bv_msb (norm_signed ys)"
skalberg@14494
  1195
      by (simp add: xsys)
skalberg@14494
  1196
    also have "... = bv_msb ys"
skalberg@14494
  1197
      by simp
skalberg@14494
  1198
    finally show ?thesis .
skalberg@14494
  1199
  qed
skalberg@14494
  1200
  have "xs = bv_extend (length xs) (bv_msb xs) (norm_signed xs)"
skalberg@14494
  1201
    by (simp add: bv_extend_norm_signed)
skalberg@14494
  1202
  also have "... = bv_extend (length ys) (bv_msb ys) (norm_signed ys)"
skalberg@14494
  1203
    by (simp add: xsys xsys' len)
skalberg@14494
  1204
  also have "... = ys"
skalberg@14494
  1205
    by (simp add: bv_extend_norm_signed)
skalberg@14494
  1206
  finally show ?thesis .
skalberg@14494
  1207
qed
skalberg@14494
  1208
skalberg@17650
  1209
lemma int_to_bv_returntype [simp]: "norm_signed (int_to_bv w) = int_to_bv w"
skalberg@14494
  1210
  by (simp add: int_to_bv_def)
skalberg@14494
  1211
skalberg@14494
  1212
lemma bv_to_int_msb0: "0 \<le> bv_to_int w1 ==> bv_msb w1 = \<zero>"
berghofe@15325
  1213
  by (rule bit_list_cases,simp_all)
skalberg@14494
  1214
skalberg@14494
  1215
lemma bv_to_int_msb1: "bv_to_int w1 < 0 ==> bv_msb w1 = \<one>"
berghofe@15325
  1216
  by (rule bit_list_cases,simp_all)
skalberg@14494
  1217
skalberg@14494
  1218
lemma bv_to_int_lower_limit_gt0:
skalberg@14494
  1219
  assumes w0: "0 < bv_to_int w"
skalberg@14494
  1220
  shows       "2 ^ (length (norm_signed w) - 2) \<le> bv_to_int w"
skalberg@14494
  1221
proof -
skalberg@14494
  1222
  from w0
skalberg@14494
  1223
  have "0 \<le> bv_to_int w"
skalberg@14494
  1224
    by simp
skalberg@14494
  1225
  hence [simp]: "bv_msb w = \<zero>"
skalberg@14494
  1226
    by (rule bv_to_int_msb0)
skalberg@14494
  1227
  have "2 ^ (length (norm_signed w) - 2) \<le> bv_to_int (norm_signed w)"
skalberg@14494
  1228
  proof (rule bit_list_cases [of w])
skalberg@14494
  1229
    assume "w = []"
skalberg@14494
  1230
    with w0
skalberg@14494
  1231
    show ?thesis
skalberg@14494
  1232
      by simp
skalberg@14494
  1233
  next
skalberg@14494
  1234
    fix w'
skalberg@14494
  1235
    assume weq: "w = \<zero> # w'"
skalberg@14494
  1236
    thus ?thesis
skalberg@14494
  1237
    proof (simp add: norm_signed_Cons,safe)
skalberg@14494
  1238
      assume "norm_unsigned w' = []"
skalberg@14494
  1239
      with weq and w0
skalberg@14494
  1240
      show False
wenzelm@19736
  1241
        by (simp add: norm_empty_bv_to_nat_zero)
skalberg@14494
  1242
    next
skalberg@14494
  1243
      assume w'0: "norm_unsigned w' \<noteq> []"
skalberg@14494
  1244
      have "0 < bv_to_nat w'"
skalberg@14494
  1245
      proof (rule ccontr)
wenzelm@19736
  1246
        assume "~ (0 < bv_to_nat w')"
wenzelm@19736
  1247
        hence "bv_to_nat w' = 0"
wenzelm@19736
  1248
          by arith
wenzelm@19736
  1249
        hence "norm_unsigned w' = []"
wenzelm@19736
  1250
          by (simp add: bv_to_nat_zero_imp_empty)
wenzelm@19736
  1251
        with w'0
wenzelm@19736
  1252
        show False
wenzelm@19736
  1253
          by simp
skalberg@14494
  1254
      qed
skalberg@14494
  1255
      with bv_to_nat_lower_limit [of w']
berghofe@15325
  1256
      show "2 ^ (length (norm_unsigned w') - Suc 0) \<le> int (bv_to_nat w')"
wenzelm@19736
  1257
        by (simp add: int_nat_two_exp)
skalberg@14494
  1258
    qed
skalberg@14494
  1259
  next
skalberg@14494
  1260
    fix w'
skalberg@14494
  1261
    assume "w = \<one> # w'"
skalberg@14494
  1262
    from w0
skalberg@14494
  1263
    have "bv_msb w = \<zero>"
skalberg@14494
  1264
      by simp
skalberg@14494
  1265
    with prems
skalberg@14494
  1266
    show ?thesis
skalberg@14494
  1267
      by simp
skalberg@14494
  1268
  qed
skalberg@14494
  1269
  also have "...  = bv_to_int w"
skalberg@14494
  1270
    by simp
skalberg@14494
  1271
  finally show ?thesis .
skalberg@14494
  1272
qed
skalberg@14494
  1273
skalberg@14494
  1274
lemma norm_signed_result: "norm_signed w = [] \<or> norm_signed w = [\<one>] \<or> bv_msb (norm_signed w) \<noteq> bv_msb (tl (norm_signed w))"
skalberg@14494
  1275
  apply (rule bit_list_cases [of w],simp_all)
skalberg@14494
  1276
  apply (case_tac "bs",simp_all)
skalberg@14494
  1277
  apply (case_tac "a",simp_all)
skalberg@14494
  1278
  apply (simp add: norm_signed_Cons)
skalberg@14494
  1279
  apply safe
skalberg@14494
  1280
  apply simp
skalberg@14494
  1281
proof -
skalberg@14494
  1282
  fix l
skalberg@14494
  1283
  assume msb: "\<zero> = bv_msb (norm_unsigned l)"
skalberg@14494
  1284
  assume "norm_unsigned l \<noteq> []"
skalberg@14494
  1285
  with norm_unsigned_result [of l]
skalberg@14494
  1286
  have "bv_msb (norm_unsigned l) = \<one>"
skalberg@14494
  1287
    by simp
skalberg@14494
  1288
  with msb
skalberg@14494
  1289
  show False
skalberg@14494
  1290
    by simp
skalberg@14494
  1291
next
skalberg@14494
  1292
  fix xs
skalberg@14494
  1293
  assume p: "\<one> = bv_msb (tl (norm_signed (\<one> # xs)))"
skalberg@14494
  1294
  have "\<one> \<noteq> bv_msb (tl (norm_signed (\<one> # xs)))"
skalberg@14494
  1295
    by (rule bit_list_induct [of _ xs],simp_all)
skalberg@14494
  1296
  with p
skalberg@14494
  1297
  show False
skalberg@14494
  1298
    by simp
skalberg@14494
  1299
qed
skalberg@14494
  1300
skalberg@14494
  1301
lemma bv_to_int_upper_limit_lem1:
skalberg@14494
  1302
  assumes w0: "bv_to_int w < -1"
skalberg@14494
  1303
  shows       "bv_to_int w < - (2 ^ (length (norm_signed w) - 2))"
skalberg@14494
  1304
proof -
skalberg@14494
  1305
  from w0
skalberg@14494
  1306
  have "bv_to_int w < 0"
skalberg@14494
  1307
    by simp
skalberg@14494
  1308
  hence msbw [simp]: "bv_msb w = \<one>"
skalberg@14494
  1309
    by (rule bv_to_int_msb1)
skalberg@14494
  1310
  have "bv_to_int w = bv_to_int (norm_signed w)"
skalberg@14494
  1311
    by simp
skalberg@14494
  1312
  also from norm_signed_result [of w]
skalberg@14494
  1313
  have "... < - (2 ^ (length (norm_signed w) - 2))"
skalberg@14494
  1314
  proof (safe)
skalberg@14494
  1315
    assume "norm_signed w = []"
skalberg@14494
  1316
    hence "bv_to_int (norm_signed w) = 0"
skalberg@14494
  1317
      by simp
skalberg@14494
  1318
    with w0
skalberg@14494
  1319
    show ?thesis
skalberg@14494
  1320
      by simp
skalberg@14494
  1321
  next
skalberg@14494
  1322
    assume "norm_signed w = [\<one>]"
skalberg@14494
  1323
    hence "bv_to_int (norm_signed w) = -1"
skalberg@14494
  1324
      by simp
skalberg@14494
  1325
    with w0
skalberg@14494
  1326
    show ?thesis
skalberg@14494
  1327
      by simp
skalberg@14494
  1328
  next
skalberg@14494
  1329
    assume "bv_msb (norm_signed w) \<noteq> bv_msb (tl (norm_signed w))"
skalberg@14494
  1330
    hence msb_tl: "\<one> \<noteq> bv_msb (tl (norm_signed w))"
skalberg@14494
  1331
      by simp
skalberg@14494
  1332
    show "bv_to_int (norm_signed w) < - (2 ^ (length (norm_signed w) - 2))"
skalberg@14494
  1333
    proof (rule bit_list_cases [of "norm_signed w"])
skalberg@14494
  1334
      assume "norm_signed w = []"
skalberg@14494
  1335
      hence "bv_to_int (norm_signed w) = 0"
wenzelm@19736
  1336
        by simp
skalberg@14494
  1337
      with w0
skalberg@14494
  1338
      show ?thesis
wenzelm@19736
  1339
        by simp
skalberg@14494
  1340
    next
skalberg@14494
  1341
      fix w'
skalberg@14494
  1342
      assume nw: "norm_signed w = \<zero> # w'"
skalberg@14494
  1343
      from msbw
skalberg@14494
  1344
      have "bv_msb (norm_signed w) = \<one>"
wenzelm@19736
  1345
        by simp
skalberg@14494
  1346
      with nw
skalberg@14494
  1347
      show ?thesis
wenzelm@19736
  1348
        by simp
skalberg@14494
  1349
    next
skalberg@14494
  1350
      fix w'
skalberg@14494
  1351
      assume weq: "norm_signed w = \<one> # w'"
skalberg@14494
  1352
      show ?thesis
skalberg@14494
  1353
      proof (rule bit_list_cases [of w'])
wenzelm@19736
  1354
        assume w'eq: "w' = []"
wenzelm@19736
  1355
        from w0
wenzelm@19736
  1356
        have "bv_to_int (norm_signed w) < -1"
wenzelm@19736
  1357
          by simp
wenzelm@19736
  1358
        with w'eq and weq
wenzelm@19736
  1359
        show ?thesis
wenzelm@19736
  1360
          by simp
skalberg@14494
  1361
      next
wenzelm@19736
  1362
        fix w''
wenzelm@19736
  1363
        assume w'eq: "w' = \<zero> # w''"
wenzelm@19736
  1364
        show ?thesis
wenzelm@19736
  1365
          apply (simp add: weq w'eq)
wenzelm@19736
  1366
          apply (subgoal_tac "- int (bv_to_nat (bv_not w'')) + -1 < 0 + 0")
wenzelm@19736
  1367
          apply (simp add: int_nat_two_exp)
wenzelm@19736
  1368
          apply (rule add_le_less_mono)
wenzelm@19736
  1369
          apply simp_all
wenzelm@19736
  1370
          done
skalberg@14494
  1371
      next
wenzelm@19736
  1372
        fix w''
wenzelm@19736
  1373
        assume w'eq: "w' = \<one> # w''"
wenzelm@19736
  1374
        with weq and msb_tl
wenzelm@19736
  1375
        show ?thesis
wenzelm@19736
  1376
          by simp
skalberg@14494
  1377
      qed
skalberg@14494
  1378
    qed
skalberg@14494
  1379
  qed
skalberg@14494
  1380
  finally show ?thesis .
skalberg@14494
  1381
qed
skalberg@14494
  1382
skalberg@14494
  1383
lemma length_int_to_bv_upper_limit_gt0:
skalberg@14494
  1384
  assumes w0: "0 < i"
skalberg@14494
  1385
  and     wk: "i \<le> 2 ^ (k - 1) - 1"
skalberg@14494
  1386
  shows       "length (int_to_bv i) \<le> k"
skalberg@14494
  1387
proof (rule ccontr)
skalberg@14494
  1388
  from w0 wk
skalberg@14494
  1389
  have k1: "1 < k"
webertj@20217
  1390
    by (cases "k - 1",simp_all)
skalberg@14494
  1391
  assume "~ length (int_to_bv i) \<le> k"
skalberg@14494
  1392
  hence "k < length (int_to_bv i)"
skalberg@14494
  1393
    by simp
skalberg@14494
  1394
  hence "k \<le> length (int_to_bv i) - 1"
skalberg@14494
  1395
    by arith
skalberg@14494
  1396
  hence a: "k - 1 \<le> length (int_to_bv i) - 2"
skalberg@14494
  1397
    by arith
paulson@15067
  1398
  hence "(2::int) ^ (k - 1) \<le> 2 ^ (length (int_to_bv i) - 2)" by simp
skalberg@14494
  1399
  also have "... \<le> i"
skalberg@14494
  1400
  proof -
skalberg@14494
  1401
    have "2 ^ (length (norm_signed (int_to_bv i)) - 2) \<le> bv_to_int (int_to_bv i)"
skalberg@14494
  1402
    proof (rule bv_to_int_lower_limit_gt0)
skalberg@14494
  1403
      from w0
skalberg@14494
  1404
      show "0 < bv_to_int (int_to_bv i)"
wenzelm@19736
  1405
        by simp
skalberg@14494
  1406
    qed
skalberg@14494
  1407
    thus ?thesis
skalberg@14494
  1408
      by simp
skalberg@14494
  1409
  qed
skalberg@14494
  1410
  finally have "2 ^ (k - 1) \<le> i" .
skalberg@14494
  1411
  with wk
skalberg@14494
  1412
  show False
skalberg@14494
  1413
    by simp
skalberg@14494
  1414
qed
skalberg@14494
  1415
skalberg@14494
  1416
lemma pos_length_pos:
skalberg@14494
  1417
  assumes i0: "0 < bv_to_int w"
skalberg@14494
  1418
  shows       "0 < length w"
skalberg@14494
  1419
proof -
skalberg@14494
  1420
  from norm_signed_result [of w]
skalberg@14494
  1421
  have "0 < length (norm_signed w)"
skalberg@14494
  1422
  proof (auto)
skalberg@14494
  1423
    assume ii: "norm_signed w = []"
skalberg@14494
  1424
    have "bv_to_int (norm_signed w) = 0"
skalberg@14494
  1425
      by (subst ii,simp)
skalberg@14494
  1426
    hence "bv_to_int w = 0"
skalberg@14494
  1427
      by simp
skalberg@14494
  1428
    with i0
skalberg@14494
  1429
    show False
skalberg@14494
  1430
      by simp
skalberg@14494
  1431
  next
skalberg@14494
  1432
    assume ii: "norm_signed w = []"
skalberg@14494
  1433
    assume jj: "bv_msb w \<noteq> \<zero>"
skalberg@14494
  1434
    have "\<zero> = bv_msb (norm_signed w)"
skalberg@14494
  1435
      by (subst ii,simp)
skalberg@14494
  1436
    also have "... \<noteq> \<zero>"
skalberg@14494
  1437
      by (simp add: jj)
skalberg@14494
  1438
    finally show False by simp
skalberg@14494
  1439
  qed
skalberg@14494
  1440
  also have "... \<le> length w"
skalberg@14494
  1441
    by (rule norm_signed_length)
skalberg@14494
  1442
  finally show ?thesis
skalberg@14494
  1443
    .
skalberg@14494
  1444
qed
skalberg@14494
  1445
skalberg@14494
  1446
lemma neg_length_pos:
skalberg@14494
  1447
  assumes i0: "bv_to_int w < -1"
skalberg@14494
  1448
  shows       "0 < length w"
skalberg@14494
  1449
proof -
skalberg@14494
  1450
  from norm_signed_result [of w]
skalberg@14494
  1451
  have "0 < length (norm_signed w)"
skalberg@14494
  1452
  proof (auto)
skalberg@14494
  1453
    assume ii: "norm_signed w = []"
skalberg@14494
  1454
    have "bv_to_int (norm_signed w) = 0"
skalberg@14494
  1455
      by (subst ii,simp)
skalberg@14494
  1456
    hence "bv_to_int w = 0"
skalberg@14494
  1457
      by simp
skalberg@14494
  1458
    with i0
skalberg@14494
  1459
    show False
skalberg@14494
  1460
      by simp
skalberg@14494
  1461
  next
skalberg@14494
  1462
    assume ii: "norm_signed w = []"
skalberg@14494
  1463
    assume jj: "bv_msb w \<noteq> \<zero>"
skalberg@14494
  1464
    have "\<zero> = bv_msb (norm_signed w)"
skalberg@14494
  1465
      by (subst ii,simp)
skalberg@14494
  1466
    also have "... \<noteq> \<zero>"
skalberg@14494
  1467
      by (simp add: jj)
skalberg@14494
  1468
    finally show False by simp
skalberg@14494
  1469
  qed
skalberg@14494
  1470
  also have "... \<le> length w"
skalberg@14494
  1471
    by (rule norm_signed_length)
skalberg@14494
  1472
  finally show ?thesis
skalberg@14494
  1473
    .
skalberg@14494
  1474
qed
skalberg@14494
  1475
skalberg@14494
  1476
lemma length_int_to_bv_lower_limit_gt0:
skalberg@14494
  1477
  assumes wk: "2 ^ (k - 1) \<le> i"
skalberg@14494
  1478
  shows       "k < length (int_to_bv i)"
skalberg@14494
  1479
proof (rule ccontr)
skalberg@14494
  1480
  have "0 < (2::int) ^ (k - 1)"
paulson@15067
  1481
    by (rule zero_less_power,simp)
skalberg@14494
  1482
  also have "... \<le> i"
skalberg@14494
  1483
    by (rule wk)
skalberg@14494
  1484
  finally have i0: "0 < i"
skalberg@14494
  1485
    .
skalberg@14494
  1486
  have lii0: "0 < length (int_to_bv i)"
skalberg@14494
  1487
    apply (rule pos_length_pos)
skalberg@14494
  1488
    apply (simp,rule i0)
skalberg@14494
  1489
    done
skalberg@14494
  1490
  assume "~ k < length (int_to_bv i)"
skalberg@14494
  1491
  hence "length (int_to_bv i) \<le> k"
skalberg@14494
  1492
    by simp
skalberg@14494
  1493
  with lii0
skalberg@14494
  1494
  have a: "length (int_to_bv i) - 1 \<le> k - 1"
skalberg@14494
  1495
    by arith
skalberg@14494
  1496
  have "i < 2 ^ (length (int_to_bv i) - 1)"
skalberg@14494
  1497
  proof -
skalberg@14494
  1498
    have "i = bv_to_int (int_to_bv i)"
skalberg@14494
  1499
      by simp
skalberg@14494
  1500
    also have "... < 2 ^ (length (int_to_bv i) - 1)"
skalberg@14494
  1501
      by (rule bv_to_int_upper_range)
skalberg@14494
  1502
    finally show ?thesis .
skalberg@14494
  1503
  qed
paulson@15067
  1504
  also have "(2::int) ^ (length (int_to_bv i) - 1) \<le> 2 ^ (k - 1)" using a
paulson@15067
  1505
         by simp
skalberg@14494
  1506
  finally have "i < 2 ^ (k - 1)" .
skalberg@14494
  1507
  with wk
skalberg@14494
  1508
  show False
skalberg@14494
  1509
    by simp
skalberg@14494
  1510
qed
skalberg@14494
  1511
skalberg@14494
  1512
lemma length_int_to_bv_upper_limit_lem1:
skalberg@14494
  1513
  assumes w1: "i < -1"
skalberg@14494
  1514
  and     wk: "- (2 ^ (k - 1)) \<le> i"
skalberg@14494
  1515
  shows       "length (int_to_bv i) \<le> k"
skalberg@14494
  1516
proof (rule ccontr)
skalberg@14494
  1517
  from w1 wk
skalberg@14494
  1518
  have k1: "1 < k"
webertj@20217
  1519
    by (cases "k - 1",simp_all)
skalberg@14494
  1520
  assume "~ length (int_to_bv i) \<le> k"
skalberg@14494
  1521
  hence "k < length (int_to_bv i)"
skalberg@14494
  1522
    by simp
skalberg@14494
  1523
  hence "k \<le> length (int_to_bv i) - 1"
skalberg@14494
  1524
    by arith
skalberg@14494
  1525
  hence a: "k - 1 \<le> length (int_to_bv i) - 2"
skalberg@14494
  1526
    by arith
skalberg@14494
  1527
  have "i < - (2 ^ (length (int_to_bv i) - 2))"
skalberg@14494
  1528
  proof -
skalberg@14494
  1529
    have "i = bv_to_int (int_to_bv i)"
skalberg@14494
  1530
      by simp
skalberg@14494
  1531
    also have "... < - (2 ^ (length (norm_signed (int_to_bv i)) - 2))"
skalberg@14494
  1532
      by (rule bv_to_int_upper_limit_lem1,simp,rule w1)
skalberg@14494
  1533
    finally show ?thesis by simp
skalberg@14494
  1534
  qed
skalberg@14494
  1535
  also have "... \<le> -(2 ^ (k - 1))"
skalberg@14494
  1536
  proof -
paulson@15067
  1537
    have "(2::int) ^ (k - 1) \<le> 2 ^ (length (int_to_bv i) - 2)" using a
paulson@15067
  1538
      by simp
skalberg@14494
  1539
    thus ?thesis
skalberg@14494
  1540
      by simp
skalberg@14494
  1541
  qed
skalberg@14494
  1542
  finally have "i < -(2 ^ (k - 1))" .
skalberg@14494
  1543
  with wk
skalberg@14494
  1544
  show False
skalberg@14494
  1545
    by simp
skalberg@14494
  1546
qed
skalberg@14494
  1547
skalberg@14494
  1548
lemma length_int_to_bv_lower_limit_lem1:
skalberg@14494
  1549
  assumes wk: "i < -(2 ^ (k - 1))"
skalberg@14494
  1550
  shows       "k < length (int_to_bv i)"
skalberg@14494
  1551
proof (rule ccontr)
skalberg@14494
  1552
  from wk
skalberg@14494
  1553
  have "i \<le> -(2 ^ (k - 1)) - 1"
skalberg@14494
  1554
    by simp
skalberg@14494
  1555
  also have "... < -1"
skalberg@14494
  1556
  proof -
skalberg@14494
  1557
    have "0 < (2::int) ^ (k - 1)"
paulson@15067
  1558
      by (rule zero_less_power,simp)
skalberg@14494
  1559
    hence "-((2::int) ^ (k - 1)) < 0"
skalberg@14494
  1560
      by simp
skalberg@14494
  1561
    thus ?thesis by simp
skalberg@14494
  1562
  qed
skalberg@14494
  1563
  finally have i1: "i < -1" .
skalberg@14494
  1564
  have lii0: "0 < length (int_to_bv i)"
skalberg@14494
  1565
    apply (rule neg_length_pos)
skalberg@14494
  1566
    apply (simp,rule i1)
skalberg@14494
  1567
    done
skalberg@14494
  1568
  assume "~ k < length (int_to_bv i)"
skalberg@14494
  1569
  hence "length (int_to_bv i) \<le> k"
skalberg@14494
  1570
    by simp
skalberg@14494
  1571
  with lii0
skalberg@14494
  1572
  have a: "length (int_to_bv i) - 1 \<le> k - 1"
skalberg@14494
  1573
    by arith
paulson@15067
  1574
  hence "(2::int) ^ (length (int_to_bv i) - 1) \<le> 2 ^ (k - 1)" by simp
skalberg@14494
  1575
  hence "-((2::int) ^ (k - 1)) \<le> - (2 ^ (length (int_to_bv i) - 1))"
skalberg@14494
  1576
    by simp
skalberg@14494
  1577
  also have "... \<le> i"
skalberg@14494
  1578
  proof -
skalberg@14494
  1579
    have "- (2 ^ (length (int_to_bv i) - 1)) \<le> bv_to_int (int_to_bv i)"
skalberg@14494
  1580
      by (rule bv_to_int_lower_range)
skalberg@14494
  1581
    also have "... = i"
skalberg@14494
  1582
      by simp
skalberg@14494
  1583
    finally show ?thesis .
skalberg@14494
  1584
  qed
skalberg@14494
  1585
  finally have "-(2 ^ (k - 1)) \<le> i" .
skalberg@14494
  1586
  with wk
skalberg@14494
  1587
  show False
skalberg@14494
  1588
    by simp
skalberg@14494
  1589
qed
skalberg@14494
  1590
wenzelm@14589
  1591
subsection {* Signed Arithmetic Operations *}
skalberg@14494
  1592
wenzelm@14589
  1593
subsubsection {* Conversion from unsigned to signed *}
skalberg@14494
  1594
wenzelm@19736
  1595
definition
wenzelm@21404
  1596
  utos :: "bit list => bit list" where
wenzelm@19736
  1597
  "utos w = norm_signed (\<zero> # w)"
skalberg@14494
  1598
skalberg@17650
  1599
lemma utos_type [simp]: "utos (norm_unsigned w) = utos w"
skalberg@14494
  1600
  by (simp add: utos_def norm_signed_Cons)
skalberg@14494
  1601
skalberg@17650
  1602
lemma utos_returntype [simp]: "norm_signed (utos w) = utos w"
skalberg@14494
  1603
  by (simp add: utos_def)
skalberg@14494
  1604
skalberg@14494
  1605
lemma utos_length: "length (utos w) \<le> Suc (length w)"
skalberg@14494
  1606
  by (simp add: utos_def norm_signed_Cons)
skalberg@14494
  1607
berghofe@15325
  1608
lemma bv_to_int_utos: "bv_to_int (utos w) = int (bv_to_nat w)"
skalberg@14494
  1609
proof (simp add: utos_def norm_signed_Cons,safe)
skalberg@14494
  1610
  assume "norm_unsigned w = []"
skalberg@14494
  1611
  hence "bv_to_nat (norm_unsigned w) = 0"
skalberg@14494
  1612
    by simp
skalberg@14494
  1613
  thus "bv_to_nat w = 0"
skalberg@14494
  1614
    by simp
skalberg@14494
  1615
qed
skalberg@14494
  1616
wenzelm@14589
  1617
subsubsection {* Unary minus *}
skalberg@14494
  1618
wenzelm@19736
  1619
definition
wenzelm@21404
  1620
  bv_uminus :: "bit list => bit list" where
wenzelm@19736
  1621
  "bv_uminus w = int_to_bv (- bv_to_int w)"
skalberg@14494
  1622
skalberg@17650
  1623
lemma bv_uminus_type [simp]: "bv_uminus (norm_signed w) = bv_uminus w"
skalberg@14494
  1624
  by (simp add: bv_uminus_def)
skalberg@14494
  1625
skalberg@17650
  1626
lemma bv_uminus_returntype [simp]: "norm_signed (bv_uminus w) = bv_uminus w"
skalberg@14494
  1627
  by (simp add: bv_uminus_def)
skalberg@14494
  1628
skalberg@14494
  1629
lemma bv_uminus_length: "length (bv_uminus w) \<le> Suc (length w)"
skalberg@14494
  1630
proof -
skalberg@14494
  1631
  have "1 < -bv_to_int w \<or> -bv_to_int w = 1 \<or> -bv_to_int w = 0 \<or> -bv_to_int w = -1 \<or> -bv_to_int w < -1"
skalberg@14494
  1632
    by arith
skalberg@14494
  1633
  thus ?thesis
skalberg@14494
  1634
  proof safe
skalberg@14494
  1635
    assume p: "1 < - bv_to_int w"
skalberg@14494
  1636
    have lw: "0 < length w"
skalberg@14494
  1637
      apply (rule neg_length_pos)
skalberg@14494
  1638
      using p
skalberg@14494
  1639
      apply simp
skalberg@14494
  1640
      done
skalberg@14494
  1641
    show ?thesis
skalberg@14494
  1642
    proof (simp add: bv_uminus_def,rule length_int_to_bv_upper_limit_gt0,simp_all)
skalberg@14494
  1643
      from prems
skalberg@14494
  1644
      show "bv_to_int w < 0"
wenzelm@19736
  1645
        by simp
skalberg@14494
  1646
    next
skalberg@14494
  1647
      have "-(2^(length w - 1)) \<le> bv_to_int w"
wenzelm@19736
  1648
        by (rule bv_to_int_lower_range)
skalberg@14494
  1649
      hence "- bv_to_int w \<le> 2^(length w - 1)"
wenzelm@19736
  1650
        by simp
skalberg@14494
  1651
      also from lw have "... < 2 ^ length w"
wenzelm@19736
  1652
        by simp
skalberg@14494
  1653
      finally show "- bv_to_int w < 2 ^ length w"
wenzelm@19736
  1654
        by simp
skalberg@14494
  1655
    qed
skalberg@14494
  1656
  next
skalberg@14494
  1657
    assume p: "- bv_to_int w = 1"
skalberg@14494
  1658
    hence lw: "0 < length w"
skalberg@14494
  1659
      by (cases w,simp_all)
skalberg@14494
  1660
    from p
skalberg@14494
  1661
    show ?thesis
skalberg@14494
  1662
      apply (simp add: bv_uminus_def)
skalberg@14494
  1663
      using lw
skalberg@14494
  1664
      apply (simp (no_asm) add: nat_to_bv_non0)
skalberg@14494
  1665
      done
skalberg@14494
  1666
  next
skalberg@14494
  1667
    assume "- bv_to_int w = 0"
skalberg@14494
  1668
    thus ?thesis
skalberg@14494
  1669
      by (simp add: bv_uminus_def)
skalberg@14494
  1670
  next
skalberg@14494
  1671
    assume p: "- bv_to_int w = -1"
skalberg@14494
  1672
    thus ?thesis
skalberg@14494
  1673
      by (simp add: bv_uminus_def)
skalberg@14494
  1674
  next
skalberg@14494
  1675
    assume p: "- bv_to_int w < -1"
skalberg@14494
  1676
    show ?thesis
skalberg@14494
  1677
      apply (simp add: bv_uminus_def)
skalberg@14494
  1678
      apply (rule length_int_to_bv_upper_limit_lem1)
skalberg@14494
  1679
      apply (rule p)
skalberg@14494
  1680
      apply simp
skalberg@14494
  1681
    proof -
skalberg@14494
  1682
      have "bv_to_int w < 2 ^ (length w - 1)"
wenzelm@19736
  1683
        by (rule bv_to_int_upper_range)
paulson@15067
  1684
      also have "... \<le> 2 ^ length w" by simp
skalberg@14494
  1685
      finally show "bv_to_int w \<le> 2 ^ length w"
wenzelm@19736
  1686
        by simp
skalberg@14494
  1687
    qed
skalberg@14494
  1688
  qed
skalberg@14494
  1689
qed
skalberg@14494
  1690
skalberg@14494
  1691
lemma bv_uminus_length_utos: "length (bv_uminus (utos w)) \<le> Suc (length w)"
skalberg@14494
  1692
proof -
skalberg@14494
  1693
  have "-bv_to_int (utos w) = 0 \<or> -bv_to_int (utos w) = -1 \<or> -bv_to_int (utos w) < -1"
skalberg@14494
  1694
    apply (simp add: bv_to_int_utos)
skalberg@14494
  1695
    by arith
skalberg@14494
  1696
  thus ?thesis
skalberg@14494
  1697
  proof safe
skalberg@14494
  1698
    assume "-bv_to_int (utos w) = 0"
skalberg@14494
  1699
    thus ?thesis
skalberg@14494
  1700
      by (simp add: bv_uminus_def)
skalberg@14494
  1701
  next
skalberg@14494
  1702
    assume "-bv_to_int (utos w) = -1"
skalberg@14494
  1703
    thus ?thesis
skalberg@14494
  1704
      by (simp add: bv_uminus_def)
skalberg@14494
  1705
  next
skalberg@14494
  1706
    assume p: "-bv_to_int (utos w) < -1"
skalberg@14494
  1707
    show ?thesis
skalberg@14494
  1708
      apply (simp add: bv_uminus_def)
skalberg@14494
  1709
      apply (rule length_int_to_bv_upper_limit_lem1)
skalberg@14494
  1710
      apply (rule p)
skalberg@14494
  1711
      apply (simp add: bv_to_int_utos)
skalberg@14494
  1712
      using bv_to_nat_upper_range [of w]
berghofe@15325
  1713
      apply (simp add: int_nat_two_exp)
skalberg@14494
  1714
      done
skalberg@14494
  1715
  qed
skalberg@14494
  1716
qed
skalberg@14494
  1717
wenzelm@19736
  1718
definition
wenzelm@21404
  1719
  bv_sadd :: "[bit list, bit list ] => bit list" where
wenzelm@19736
  1720
  "bv_sadd w1 w2 = int_to_bv (bv_to_int w1 + bv_to_int w2)"
skalberg@14494
  1721
skalberg@17650
  1722
lemma bv_sadd_type1 [simp]: "bv_sadd (norm_signed w1) w2 = bv_sadd w1 w2"
skalberg@14494
  1723
  by (simp add: bv_sadd_def)
skalberg@14494
  1724
skalberg@17650
  1725
lemma bv_sadd_type2 [simp]: "bv_sadd w1 (norm_signed w2) = bv_sadd w1 w2"
skalberg@14494
  1726
  by (simp add: bv_sadd_def)
skalberg@14494
  1727
skalberg@17650
  1728
lemma bv_sadd_returntype [simp]: "norm_signed (bv_sadd w1 w2) = bv_sadd w1 w2"
skalberg@14494
  1729
  by (simp add: bv_sadd_def)
skalberg@14494
  1730
skalberg@14494
  1731
lemma adder_helper:
skalberg@14494
  1732
  assumes lw: "0 < max (length w1) (length w2)"
skalberg@14494
  1733
  shows   "((2::int) ^ (length w1 - 1)) + (2 ^ (length w2 - 1)) \<le> 2 ^ max (length w1) (length w2)"
skalberg@14494
  1734
proof -
skalberg@14494
  1735
  have "((2::int) ^ (length w1 - 1)) + (2 ^ (length w2 - 1)) \<le> 2 ^ (max (length w1) (length w2) - 1) + 2 ^ (max (length w1) (length w2) - 1)"
skalberg@14494
  1736
    apply (cases "length w1 \<le> length w2")
skalberg@14494
  1737
    apply (auto simp add: max_def)
skalberg@14494
  1738
    done
skalberg@14494
  1739
  also have "... = 2 ^ max (length w1) (length w2)"
skalberg@14494
  1740
  proof -
skalberg@14494
  1741
    from lw
skalberg@14494
  1742
    show ?thesis
skalberg@14494
  1743
      apply simp
skalberg@14494
  1744
      apply (subst power_Suc [symmetric])
skalberg@14494
  1745
      apply (simp del: power.simps)
skalberg@14494
  1746
      done
skalberg@14494
  1747
  qed
skalberg@14494
  1748
  finally show ?thesis .
skalberg@14494
  1749
qed
skalberg@14494
  1750
skalberg@14494
  1751
lemma bv_sadd_length: "length (bv_sadd w1 w2) \<le> Suc (max (length w1) (length w2))"
skalberg@14494
  1752
proof -
skalberg@14494
  1753
  let ?Q = "bv_to_int w1 + bv_to_int w2"
skalberg@14494
  1754
skalberg@14494
  1755
  have helper: "?Q \<noteq> 0 ==> 0 < max (length w1) (length w2)"
skalberg@14494
  1756
  proof -
skalberg@14494
  1757
    assume p: "?Q \<noteq> 0"
skalberg@14494
  1758
    show "0 < max (length w1) (length w2)"
skalberg@14494
  1759
    proof (simp add: less_max_iff_disj,rule)
skalberg@14494
  1760
      assume [simp]: "w1 = []"
skalberg@14494
  1761
      show "w2 \<noteq> []"
skalberg@14494
  1762
      proof (rule ccontr,simp)
wenzelm@19736
  1763
        assume [simp]: "w2 = []"
wenzelm@19736
  1764
        from p
wenzelm@19736
  1765
        show False
wenzelm@19736
  1766
          by simp
skalberg@14494
  1767
      qed
skalberg@14494
  1768
    qed
skalberg@14494
  1769
  qed
skalberg@14494
  1770
skalberg@14494
  1771
  have "0 < ?Q \<or> ?Q = 0 \<or> ?Q = -1 \<or> ?Q < -1"
skalberg@14494
  1772
    by arith
skalberg@14494
  1773
  thus ?thesis
skalberg@14494
  1774
  proof safe
skalberg@14494
  1775
    assume "?Q = 0"
skalberg@14494
  1776
    thus ?thesis
skalberg@14494
  1777
      by (simp add: bv_sadd_def)
skalberg@14494
  1778
  next
skalberg@14494
  1779
    assume "?Q = -1"
skalberg@14494
  1780
    thus ?thesis
skalberg@14494
  1781
      by (simp add: bv_sadd_def)
skalberg@14494
  1782
  next
skalberg@14494
  1783
    assume p: "0 < ?Q"
skalberg@14494
  1784
    show ?thesis
skalberg@14494
  1785
      apply (simp add: bv_sadd_def)
skalberg@14494
  1786
      apply (rule length_int_to_bv_upper_limit_gt0)
skalberg@14494
  1787
      apply (rule p)
skalberg@14494
  1788
    proof simp
skalberg@14494
  1789
      from bv_to_int_upper_range [of w2]
skalberg@14494
  1790
      have "bv_to_int w2 \<le> 2 ^ (length w2 - 1)"
wenzelm@19736
  1791
        by simp
skalberg@14494
  1792
      with bv_to_int_upper_range [of w1]
skalberg@14494
  1793
      have "bv_to_int w1 + bv_to_int w2 < (2 ^ (length w1 - 1)) + (2 ^ (length w2 - 1))"
wenzelm@19736
  1794
        by (rule zadd_zless_mono)
skalberg@14494
  1795
      also have "... \<le> 2 ^ max (length w1) (length w2)"
wenzelm@19736
  1796
        apply (rule adder_helper)
wenzelm@19736
  1797
        apply (rule helper)
wenzelm@19736
  1798
        using p
wenzelm@19736
  1799
        apply simp
wenzelm@19736
  1800
        done
skalberg@14494
  1801
      finally show "?Q < 2 ^ max (length w1) (length w2)"
wenzelm@19736
  1802
        .
skalberg@14494
  1803
    qed
skalberg@14494
  1804
  next
skalberg@14494
  1805
    assume p: "?Q < -1"
skalberg@14494
  1806
    show ?thesis
skalberg@14494
  1807
      apply (simp add: bv_sadd_def)
skalberg@14494
  1808
      apply (rule length_int_to_bv_upper_limit_lem1,simp_all)
skalberg@14494
  1809
      apply (rule p)
skalberg@14494
  1810
    proof -
skalberg@14494
  1811
      have "(2 ^ (length w1 - 1)) + 2 ^ (length w2 - 1) \<le> (2::int) ^ max (length w1) (length w2)"
wenzelm@19736
  1812
        apply (rule adder_helper)
wenzelm@19736
  1813
        apply (rule helper)
wenzelm@19736
  1814
        using p
wenzelm@19736
  1815
        apply simp
wenzelm@19736
  1816
        done
skalberg@14494
  1817
      hence "-((2::int) ^ max (length w1) (length w2)) \<le> - (2 ^ (length w1 - 1)) + -(2 ^ (length w2 - 1))"
wenzelm@19736
  1818
        by simp
skalberg@14494
  1819
      also have "- (2 ^ (length w1 - 1)) + -(2 ^ (length w2 - 1)) \<le> ?Q"
wenzelm@19736
  1820
        apply (rule add_mono)
wenzelm@19736
  1821
        apply (rule bv_to_int_lower_range [of w1])
wenzelm@19736
  1822
        apply (rule bv_to_int_lower_range [of w2])
wenzelm@19736
  1823
        done
skalberg@14494
  1824
      finally show "- (2^max (length w1) (length w2)) \<le> ?Q" .
skalberg@14494
  1825
    qed
skalberg@14494
  1826
  qed
skalberg@14494
  1827
qed
skalberg@14494
  1828
wenzelm@19736
  1829
definition
wenzelm@21404
  1830
  bv_sub :: "[bit list, bit list] => bit list" where
wenzelm@19736
  1831
  "bv_sub w1 w2 = bv_sadd w1 (bv_uminus w2)"
skalberg@14494
  1832
skalberg@17650
  1833
lemma bv_sub_type1 [simp]: "bv_sub (norm_signed w1) w2 = bv_sub w1 w2"
skalberg@14494
  1834
  by (simp add: bv_sub_def)
skalberg@14494
  1835
skalberg@17650
  1836
lemma bv_sub_type2 [simp]: "bv_sub w1 (norm_signed w2) = bv_sub w1 w2"
skalberg@14494
  1837
  by (simp add: bv_sub_def)
skalberg@14494
  1838
skalberg@17650
  1839
lemma bv_sub_returntype [simp]: "norm_signed (bv_sub w1 w2) = bv_sub w1 w2"
skalberg@14494
  1840
  by (simp add: bv_sub_def)
skalberg@14494
  1841
skalberg@14494
  1842
lemma bv_sub_length: "length (bv_sub w1 w2) \<le> Suc (max (length w1) (length w2))"
skalberg@14494
  1843
proof (cases "bv_to_int w2 = 0")
skalberg@14494
  1844
  assume p: "bv_to_int w2 = 0"
skalberg@14494
  1845
  show ?thesis
skalberg@14494
  1846
  proof (simp add: bv_sub_def bv_sadd_def bv_uminus_def p)
skalberg@14494
  1847
    have "length (norm_signed w1) \<le> length w1"
skalberg@14494
  1848
      by (rule norm_signed_length)
skalberg@14494
  1849
    also have "... \<le> max (length w1) (length w2)"
skalberg@14494
  1850
      by (rule le_maxI1)
skalberg@14494
  1851
    also have "... \<le> Suc (max (length w1) (length w2))"
skalberg@14494
  1852
      by arith
skalberg@14494
  1853
    finally show "length (norm_signed w1) \<le> Suc (max (length w1) (length w2))"
skalberg@14494
  1854
      .
skalberg@14494
  1855
  qed
skalberg@14494
  1856
next
skalberg@14494
  1857
  assume "bv_to_int w2 \<noteq> 0"
skalberg@14494
  1858
  hence "0 < length w2"
skalberg@14494
  1859
    by (cases w2,simp_all)
skalberg@14494
  1860
  hence lmw: "0 < max (length w1) (length w2)"
skalberg@14494
  1861
    by arith
skalberg@14494
  1862
skalberg@14494
  1863
  let ?Q = "bv_to_int w1 - bv_to_int w2"
skalberg@14494
  1864
skalberg@14494
  1865
  have "0 < ?Q \<or> ?Q = 0 \<or> ?Q = -1 \<or> ?Q < -1"
skalberg@14494
  1866
    by arith
skalberg@14494
  1867
  thus ?thesis
skalberg@14494
  1868
  proof safe
skalberg@14494
  1869
    assume "?Q = 0"
skalberg@14494
  1870
    thus ?thesis
skalberg@14494
  1871
      by (simp add: bv_sub_def bv_sadd_def bv_uminus_def)
skalberg@14494
  1872
  next
skalberg@14494
  1873
    assume "?Q = -1"
skalberg@14494
  1874
    thus ?thesis
skalberg@14494
  1875
      by (simp add: bv_sub_def bv_sadd_def bv_uminus_def)
skalberg@14494
  1876
  next
skalberg@14494
  1877
    assume p: "0 < ?Q"
skalberg@14494
  1878
    show ?thesis
skalberg@14494
  1879
      apply (simp add: bv_sub_def bv_sadd_def bv_uminus_def)
skalberg@14494
  1880
      apply (rule length_int_to_bv_upper_limit_gt0)
skalberg@14494
  1881
      apply (rule p)
skalberg@14494
  1882
    proof simp
skalberg@14494
  1883
      from bv_to_int_lower_range [of w2]
skalberg@14494
  1884
      have v2: "- bv_to_int w2 \<le> 2 ^ (length w2 - 1)"
wenzelm@19736
  1885
        by simp
skalberg@14494
  1886
      have "bv_to_int w1 + - bv_to_int w2 < (2 ^ (length w1 - 1)) + (2 ^ (length w2 - 1))"
wenzelm@19736
  1887
        apply (rule zadd_zless_mono)
wenzelm@19736
  1888
        apply (rule bv_to_int_upper_range [of w1])
wenzelm@19736
  1889
        apply (rule v2)
wenzelm@19736
  1890
        done
skalberg@14494
  1891
      also have "... \<le> 2 ^ max (length w1) (length w2)"
wenzelm@19736
  1892
        apply (rule adder_helper)
wenzelm@19736
  1893
        apply (rule lmw)
wenzelm@19736
  1894
        done
skalberg@14494
  1895
      finally show "?Q < 2 ^ max (length w1) (length w2)"
wenzelm@19736
  1896
        by simp
skalberg@14494
  1897
    qed
skalberg@14494
  1898
  next
skalberg@14494
  1899
    assume p: "?Q < -1"
skalberg@14494
  1900
    show ?thesis
skalberg@14494
  1901
      apply (simp add: bv_sub_def bv_sadd_def bv_uminus_def)
skalberg@14494
  1902
      apply (rule length_int_to_bv_upper_limit_lem1)
skalberg@14494
  1903
      apply (rule p)
skalberg@14494
  1904
    proof simp
skalberg@14494
  1905
      have "(2 ^ (length w1 - 1)) + 2 ^ (length w2 - 1) \<le> (2::int) ^ max (length w1) (length w2)"
wenzelm@19736
  1906
        apply (rule adder_helper)
wenzelm@19736
  1907
        apply (rule lmw)
wenzelm@19736
  1908
        done
skalberg@14494
  1909
      hence "-((2::int) ^ max (length w1) (length w2)) \<le> - (2 ^ (length w1 - 1)) + -(2 ^ (length w2 - 1))"
wenzelm@19736
  1910
        by simp
skalberg@14494
  1911
      also have "- (2 ^ (length w1 - 1)) + -(2 ^ (length w2 - 1)) \<le> bv_to_int w1 + -bv_to_int w2"
wenzelm@19736
  1912
        apply (rule add_mono)
wenzelm@19736
  1913
        apply (rule bv_to_int_lower_range [of w1])
wenzelm@19736
  1914
        using bv_to_int_upper_range [of w2]
wenzelm@19736
  1915
        apply simp
wenzelm@19736
  1916
        done
skalberg@14494
  1917
      finally show "- (2^max (length w1) (length w2)) \<le> ?Q"
wenzelm@19736
  1918
        by simp
skalberg@14494
  1919
    qed
skalberg@14494
  1920
  qed
skalberg@14494
  1921
qed
skalberg@14494
  1922
wenzelm@19736
  1923
definition
wenzelm@21404
  1924
  bv_smult :: "[bit list, bit list] => bit list" where
wenzelm@19736
  1925
  "bv_smult w1 w2 = int_to_bv (bv_to_int w1 * bv_to_int w2)"
skalberg@14494
  1926
skalberg@17650
  1927
lemma bv_smult_type1 [simp]: "bv_smult (norm_signed w1) w2 = bv_smult w1 w2"
skalberg@14494
  1928
  by (simp add: bv_smult_def)
skalberg@14494
  1929
skalberg@17650
  1930
lemma bv_smult_type2 [simp]: "bv_smult w1 (norm_signed w2) = bv_smult w1 w2"
skalberg@14494
  1931
  by (simp add: bv_smult_def)
skalberg@14494
  1932
skalberg@17650
  1933
lemma bv_smult_returntype [simp]: "norm_signed (bv_smult w1 w2) = bv_smult w1 w2"
skalberg@14494
  1934
  by (simp add: bv_smult_def)
skalberg@14494
  1935
skalberg@14494
  1936
lemma bv_smult_length: "length (bv_smult w1 w2) \<le> length w1 + length w2"
skalberg@14494
  1937
proof -
skalberg@14494
  1938
  let ?Q = "bv_to_int w1 * bv_to_int w2"
skalberg@14494
  1939
skalberg@14494
  1940
  have lmw: "?Q \<noteq> 0 ==> 0 < length w1 \<and> 0 < length w2"
skalberg@14494
  1941
    by auto
skalberg@14494
  1942
skalberg@14494
  1943
  have "0 < ?Q \<or> ?Q = 0 \<or> ?Q = -1 \<or> ?Q < -1"
skalberg@14494
  1944
    by arith
skalberg@14494
  1945
  thus ?thesis
skalberg@14494
  1946
  proof (safe dest!: iffD1 [OF mult_eq_0_iff])
skalberg@14494
  1947
    assume "bv_to_int w1 = 0"
skalberg@14494
  1948
    thus ?thesis
skalberg@14494
  1949
      by (simp add: bv_smult_def)
skalberg@14494
  1950
  next
skalberg@14494
  1951
    assume "bv_to_int w2 = 0"
skalberg@14494
  1952
    thus ?thesis
skalberg@14494
  1953
      by (simp add: bv_smult_def)
skalberg@14494
  1954
  next
skalberg@14494
  1955
    assume p: "?Q = -1"
skalberg@14494
  1956
    show ?thesis
skalberg@14494
  1957
      apply (simp add: bv_smult_def p)
skalberg@14494
  1958
      apply (cut_tac lmw)
skalberg@14494
  1959
      apply arith
skalberg@14494
  1960
      using p
skalberg@14494
  1961
      apply simp
skalberg@14494
  1962
      done
skalberg@14494
  1963
  next
skalberg@14494
  1964
    assume p: "0 < ?Q"
skalberg@14494
  1965
    thus ?thesis
skalberg@14494
  1966
    proof (simp add: zero_less_mult_iff,safe)
skalberg@14494
  1967
      assume bi1: "0 < bv_to_int w1"
skalberg@14494
  1968
      assume bi2: "0 < bv_to_int w2"
skalberg@14494
  1969
      show ?thesis
wenzelm@19736
  1970
        apply (simp add: bv_smult_def)
wenzelm@19736
  1971
        apply (rule length_int_to_bv_upper_limit_gt0)
wenzelm@19736
  1972
        apply (rule p)
skalberg@14494
  1973
      proof simp
wenzelm@19736
  1974
        have "?Q < 2 ^ (length w1 - 1) * 2 ^ (length w2 - 1)"
wenzelm@19736
  1975
          apply (rule mult_strict_mono)
wenzelm@19736
  1976
          apply (rule bv_to_int_upper_range)
wenzelm@19736
  1977
          apply (rule bv_to_int_upper_range)
wenzelm@19736
  1978
          apply (rule zero_less_power)
wenzelm@19736
  1979
          apply simp
wenzelm@19736
  1980
          using bi2
wenzelm@19736
  1981
          apply simp
wenzelm@19736
  1982
          done
wenzelm@19736
  1983
        also have "... \<le> 2 ^ (length w1 + length w2 - Suc 0)"
wenzelm@19736
  1984
          apply simp
wenzelm@19736
  1985
          apply (subst zpower_zadd_distrib [symmetric])
wenzelm@19736
  1986
          apply simp
wenzelm@19736
  1987
          done
wenzelm@19736
  1988
        finally show "?Q < 2 ^ (length w1 + length w2 - Suc 0)"
wenzelm@19736
  1989
          .
skalberg@14494
  1990
      qed
skalberg@14494
  1991
    next
skalberg@14494
  1992
      assume bi1: "bv_to_int w1 < 0"
skalberg@14494
  1993
      assume bi2: "bv_to_int w2 < 0"
skalberg@14494
  1994
      show ?thesis
wenzelm@19736
  1995
        apply (simp add: bv_smult_def)
wenzelm@19736
  1996
        apply (rule length_int_to_bv_upper_limit_gt0)
wenzelm@19736
  1997
        apply (rule p)
skalberg@14494
  1998
      proof simp
wenzelm@19736
  1999
        have "-bv_to_int w1 * -bv_to_int w2 \<le> 2 ^ (length w1 - 1) * 2 ^(length w2 - 1)"
wenzelm@19736
  2000
          apply (rule mult_mono)
wenzelm@19736
  2001
          using bv_to_int_lower_range [of w1]
wenzelm@19736
  2002
          apply simp
wenzelm@19736
  2003
          using bv_to_int_lower_range [of w2]
wenzelm@19736
  2004
          apply simp
wenzelm@19736
  2005
          apply (rule zero_le_power,simp)
wenzelm@19736
  2006
          using bi2
wenzelm@19736
  2007
          apply simp
wenzelm@19736
  2008
          done
wenzelm@19736
  2009
        hence "?Q \<le> 2 ^ (length w1 - 1) * 2 ^(length w2 - 1)"
wenzelm@19736
  2010
          by simp
wenzelm@19736
  2011
        also have "... < 2 ^ (length w1 + length w2 - Suc 0)"
wenzelm@19736
  2012
          apply simp
wenzelm@19736
  2013
          apply (subst zpower_zadd_distrib [symmetric])
wenzelm@19736
  2014
          apply simp
wenzelm@19736
  2015
          apply (cut_tac lmw)
wenzelm@19736
  2016
          apply arith
wenzelm@19736
  2017
          apply (cut_tac p)
wenzelm@19736
  2018
          apply arith
wenzelm@19736
  2019
          done
wenzelm@19736
  2020
        finally show "?Q < 2 ^ (length w1 + length w2 - Suc 0)" .
skalberg@14494
  2021
      qed
skalberg@14494
  2022
    qed
skalberg@14494
  2023
  next
skalberg@14494
  2024
    assume p: "?Q < -1"
skalberg@14494
  2025
    show ?thesis
skalberg@14494
  2026
      apply (subst bv_smult_def)
skalberg@14494
  2027
      apply (rule length_int_to_bv_upper_limit_lem1)
skalberg@14494
  2028
      apply (rule p)
skalberg@14494
  2029
    proof simp
skalberg@14494
  2030
      have "(2::int) ^ (length w1 - 1) * 2 ^(length w2 - 1) \<le> 2 ^ (length w1 + length w2 - Suc 0)"
wenzelm@19736
  2031
        apply simp
wenzelm@19736
  2032
        apply (subst zpower_zadd_distrib [symmetric])
wenzelm@19736
  2033
        apply simp
wenzelm@19736
  2034
        done
skalberg@14494
  2035
      hence "-((2::int) ^ (length w1 + length w2 - Suc 0)) \<le> -(2^(length w1 - 1) * 2 ^ (length w2 - 1))"
wenzelm@19736
  2036
        by simp
skalberg@14494
  2037
      also have "... \<le> ?Q"
skalberg@14494
  2038
      proof -
wenzelm@19736
  2039
        from p
wenzelm@19736
  2040
        have q: "bv_to_int w1 * bv_to_int w2 < 0"
wenzelm@19736
  2041
          by simp
wenzelm@19736
  2042
        thus ?thesis
wenzelm@19736
  2043
        proof (simp add: mult_less_0_iff,safe)
wenzelm@19736
  2044
          assume bi1: "0 < bv_to_int w1"
wenzelm@19736
  2045
          assume bi2: "bv_to_int w2 < 0"
wenzelm@19736
  2046
          have "-bv_to_int w2 * bv_to_int w1 \<le> ((2::int)^(length w2 - 1)) * (2 ^ (length w1 - 1))"
wenzelm@19736
  2047
            apply (rule mult_mono)
wenzelm@19736
  2048
            using bv_to_int_lower_range [of w2]
wenzelm@19736
  2049
            apply simp
wenzelm@19736
  2050
            using bv_to_int_upper_range [of w1]
wenzelm@19736
  2051
            apply simp
wenzelm@19736
  2052
            apply (rule zero_le_power,simp)
wenzelm@19736
  2053
            using bi1
wenzelm@19736
  2054
            apply simp
wenzelm@19736
  2055
            done
wenzelm@19736
  2056
          hence "-?Q \<le> ((2::int)^(length w1 - 1)) * (2 ^ (length w2 - 1))"
wenzelm@19736
  2057
            by (simp add: zmult_ac)
wenzelm@19736
  2058
          thus "-(((2::int)^(length w1 - Suc 0)) * (2 ^ (length w2 - Suc 0))) \<le> ?Q"
wenzelm@19736
  2059
            by simp
wenzelm@19736
  2060
        next
wenzelm@19736
  2061
          assume bi1: "bv_to_int w1 < 0"
wenzelm@19736
  2062
          assume bi2: "0 < bv_to_int w2"
wenzelm@19736
  2063
          have "-bv_to_int w1 * bv_to_int w2 \<le> ((2::int)^(length w1 - 1)) * (2 ^ (length w2 - 1))"
wenzelm@19736
  2064
            apply (rule mult_mono)
wenzelm@19736
  2065
            using bv_to_int_lower_range [of w1]
wenzelm@19736
  2066
            apply simp
wenzelm@19736
  2067
            using bv_to_int_upper_range [of w2]
wenzelm@19736
  2068
            apply simp
wenzelm@19736
  2069
            apply (rule zero_le_power,simp)
wenzelm@19736
  2070
            using bi2
wenzelm@19736
  2071
            apply simp
wenzelm@19736
  2072
            done
wenzelm@19736
  2073
          hence "-?Q \<le> ((2::int)^(length w1 - 1)) * (2 ^ (length w2 - 1))"
wenzelm@19736
  2074
            by (simp add: zmult_ac)
wenzelm@19736
  2075
          thus "-(((2::int)^(length w1 - Suc 0)) * (2 ^ (length w2 - Suc 0))) \<le> ?Q"
wenzelm@19736
  2076
            by simp
wenzelm@19736
  2077
        qed
skalberg@14494
  2078
      qed
skalberg@14494
  2079
      finally show "-(2 ^ (length w1 + length w2 - Suc 0)) \<le> ?Q"
wenzelm@19736
  2080
        .
skalberg@14494
  2081
    qed
skalberg@14494
  2082
  qed
skalberg@14494
  2083
qed
skalberg@14494
  2084
skalberg@14494
  2085
lemma bv_msb_one: "bv_msb w = \<one> ==> 0 < bv_to_nat w"
berghofe@15325
  2086
  by (cases w,simp_all)
skalberg@14494
  2087
skalberg@14494
  2088
lemma bv_smult_length_utos: "length (bv_smult (utos w1) w2) \<le> length w1 + length w2"
skalberg@14494
  2089
proof -
skalberg@14494
  2090
  let ?Q = "bv_to_int (utos w1) * bv_to_int w2"
skalberg@14494
  2091
skalberg@14494
  2092
  have lmw: "?Q \<noteq> 0 ==> 0 < length (utos w1) \<and> 0 < length w2"
skalberg@14494
  2093
    by auto
skalberg@14494
  2094
skalberg@14494
  2095
  have "0 < ?Q \<or> ?Q = 0 \<or> ?Q = -1 \<or> ?Q < -1"
skalberg@14494
  2096
    by arith
skalberg@14494
  2097
  thus ?thesis
skalberg@14494
  2098
  proof (safe dest!: iffD1 [OF mult_eq_0_iff])
skalberg@14494
  2099
    assume "bv_to_int (utos w1) = 0"
skalberg@14494
  2100
    thus ?thesis
skalberg@14494
  2101
      by (simp add: bv_smult_def)
skalberg@14494
  2102
  next
skalberg@14494
  2103
    assume "bv_to_int w2 = 0"
skalberg@14494
  2104
    thus ?thesis
skalberg@14494
  2105
      by (simp add: bv_smult_def)
skalberg@14494
  2106
  next
skalberg@14494
  2107
    assume p: "0 < ?Q"
skalberg@14494
  2108
    thus ?thesis
skalberg@14494
  2109
    proof (simp add: zero_less_mult_iff,safe)
skalberg@14494
  2110
      assume biw2: "0 < bv_to_int w2"
skalberg@14494
  2111
      show ?thesis
wenzelm@19736
  2112
        apply (simp add: bv_smult_def)
wenzelm@19736
  2113
        apply (rule length_int_to_bv_upper_limit_gt0)
wenzelm@19736
  2114
        apply (rule p)
skalberg@14494
  2115
      proof simp
wenzelm@19736
  2116
        have "?Q < 2 ^ length w1 * 2 ^ (length w2 - 1)"
wenzelm@19736
  2117
          apply (rule mult_strict_mono)
wenzelm@19736
  2118
          apply (simp add: bv_to_int_utos int_nat_two_exp)
wenzelm@19736
  2119
          apply (rule bv_to_nat_upper_range)
wenzelm@19736
  2120
          apply (rule bv_to_int_upper_range)
wenzelm@19736
  2121
          apply (rule zero_less_power,simp)
wenzelm@19736
  2122
          using biw2
wenzelm@19736
  2123
          apply simp
wenzelm@19736
  2124
          done
wenzelm@19736
  2125
        also have "... \<le> 2 ^ (length w1 + length w2 - Suc 0)"
wenzelm@19736
  2126
          apply simp
wenzelm@19736
  2127
          apply (subst zpower_zadd_distrib [symmetric])
wenzelm@19736
  2128
          apply simp
wenzelm@19736
  2129
          apply (cut_tac lmw)
wenzelm@19736
  2130
          apply arith
wenzelm@19736
  2131
          using p
wenzelm@19736
  2132
          apply auto
wenzelm@19736
  2133
          done
wenzelm@19736
  2134
        finally show "?Q < 2 ^ (length w1 + length w2 - Suc 0)"
wenzelm@19736
  2135
          .
skalberg@14494
  2136
      qed
skalberg@14494
  2137
    next
skalberg@14494
  2138
      assume "bv_to_int (utos w1) < 0"
skalberg@14494
  2139
      thus ?thesis
wenzelm@19736
  2140
        by (simp add: bv_to_int_utos)
skalberg@14494
  2141
    qed
skalberg@14494
  2142
  next
skalberg@14494
  2143
    assume p: "?Q = -1"
skalberg@14494
  2144
    thus ?thesis
skalberg@14494
  2145
      apply (simp add: bv_smult_def)
skalberg@14494
  2146
      apply (cut_tac lmw)
skalberg@14494
  2147
      apply arith
skalberg@14494
  2148
      apply simp
skalberg@14494
  2149
      done
skalberg@14494
  2150
  next
skalberg@14494
  2151
    assume p: "?Q < -1"
skalberg@14494
  2152
    show ?thesis
skalberg@14494
  2153
      apply (subst bv_smult_def)
skalberg@14494
  2154
      apply (rule length_int_to_bv_upper_limit_lem1)
skalberg@14494
  2155
      apply (rule p)
skalberg@14494
  2156
    proof simp
skalberg@14494
  2157
      have "(2::int) ^ length w1 * 2 ^(length w2 - 1) \<le> 2 ^ (length w1 + length w2 - Suc 0)"
wenzelm@19736
  2158
        apply simp
wenzelm@19736
  2159
        apply (subst zpower_zadd_distrib [symmetric])
wenzelm@19736
  2160
        apply simp
wenzelm@19736
  2161
        apply (cut_tac lmw)
wenzelm@19736
  2162
        apply arith
wenzelm@19736
  2163
        apply (cut_tac p)
wenzelm@19736
  2164
        apply arith
wenzelm@19736
  2165
        done
skalberg@14494
  2166
      hence "-((2::int) ^ (length w1 + length w2 - Suc 0)) \<le> -(2^ length w1 * 2 ^ (length w2 - 1))"
wenzelm@19736
  2167
        by simp
skalberg@14494
  2168
      also have "... \<le> ?Q"
skalberg@14494
  2169
      proof -
wenzelm@19736
  2170
        from p
wenzelm@19736
  2171
        have q: "bv_to_int (utos w1) * bv_to_int w2 < 0"
wenzelm@19736
  2172
          by simp
wenzelm@19736
  2173
        thus ?thesis
wenzelm@19736
  2174
        proof (simp add: mult_less_0_iff,safe)
wenzelm@19736
  2175
          assume bi1: "0 < bv_to_int (utos w1)"
wenzelm@19736
  2176
          assume bi2: "bv_to_int w2 < 0"
wenzelm@19736
  2177
          have "-bv_to_int w2 * bv_to_int (utos w1) \<le> ((2::int)^(length w2 - 1)) * (2 ^ length w1)"
wenzelm@19736
  2178
            apply (rule mult_mono)
wenzelm@19736
  2179
            using bv_to_int_lower_range [of w2]
wenzelm@19736
  2180
            apply simp
wenzelm@19736
  2181
            apply (simp add: bv_to_int_utos)
wenzelm@19736
  2182
            using bv_to_nat_upper_range [of w1]
wenzelm@19736
  2183
            apply (simp add: int_nat_two_exp)
wenzelm@19736
  2184
            apply (rule zero_le_power,simp)
wenzelm@19736
  2185
            using bi1
wenzelm@19736
  2186
            apply simp
wenzelm@19736
  2187
            done
wenzelm@19736
  2188
          hence "-?Q \<le> ((2::int)^length w1) * (2 ^ (length w2 - 1))"
wenzelm@19736
  2189
            by (simp add: zmult_ac)
wenzelm@19736
  2190
          thus "-(((2::int)^length w1) * (2 ^ (length w2 - Suc 0))) \<le> ?Q"
wenzelm@19736
  2191
            by simp
wenzelm@19736
  2192
        next
wenzelm@19736
  2193
          assume bi1: "bv_to_int (utos w1) < 0"
wenzelm@19736
  2194
          thus "-(((2::int)^length w1) * (2 ^ (length w2 - Suc 0))) \<le> ?Q"
wenzelm@19736
  2195
            by (simp add: bv_to_int_utos)
wenzelm@19736
  2196
        qed
skalberg@14494
  2197
      qed
skalberg@14494
  2198
      finally show "-(2 ^ (length w1 + length w2 - Suc 0)) \<le> ?Q"
wenzelm@19736
  2199
        .
skalberg@14494
  2200
    qed
skalberg@14494
  2201
  qed
skalberg@14494
  2202
qed
skalberg@14494
  2203
skalberg@14494
  2204
lemma bv_smult_sym: "bv_smult w1 w2 = bv_smult w2 w1"
skalberg@14494
  2205
  by (simp add: bv_smult_def zmult_ac)
skalberg@14494
  2206
wenzelm@14589
  2207
subsection {* Structural operations *}
skalberg@14494
  2208
wenzelm@19736
  2209
definition
wenzelm@21404
  2210
  bv_select :: "[bit list,nat] => bit" where
wenzelm@19736
  2211
  "bv_select w i = w ! (length w - 1 - i)"
wenzelm@21404
  2212
wenzelm@21404
  2213
definition
wenzelm@21404
  2214
  bv_chop :: "[bit list,nat] => bit list * bit list" where
wenzelm@19736
  2215
  "bv_chop w i = (let len = length w in (take (len - i) w,drop (len - i) w))"
wenzelm@21404
  2216
wenzelm@21404
  2217
definition
wenzelm@21404
  2218
  bv_slice :: "[bit list,nat*nat] => bit list" where
wenzelm@19736
  2219
  "bv_slice w = (\<lambda>(b,e). fst (bv_chop (snd (bv_chop w (b+1))) e))"
skalberg@14494
  2220
skalberg@14494
  2221
lemma bv_select_rev:
skalberg@14494
  2222
  assumes notnull: "n < length w"
skalberg@14494
  2223
  shows            "bv_select w n = rev w ! n"
skalberg@14494
  2224
proof -
skalberg@14494
  2225
  have "\<forall>n. n < length w --> bv_select w n = rev w ! n"
skalberg@14494
  2226
  proof (rule length_induct [of _ w],auto simp add: bv_select_def)
skalberg@14494
  2227
    fix xs :: "bit list"
skalberg@14494
  2228
    fix n
skalberg@14494
  2229
    assume ind: "\<forall>ys::bit list. length ys < length xs --> (\<forall>n. n < length ys --> ys ! (length ys - Suc n) = rev ys ! n)"
skalberg@14494
  2230
    assume notx: "n < length xs"
skalberg@14494
  2231
    show "xs ! (length xs - Suc n) = rev xs ! n"
skalberg@14494
  2232
    proof (cases xs)
skalberg@14494
  2233
      assume "xs = []"
skalberg@14494
  2234
      with notx
skalberg@14494
  2235
      show ?thesis
wenzelm@19736
  2236
        by simp
skalberg@14494
  2237
    next
skalberg@14494
  2238
      fix y ys
skalberg@14494
  2239
      assume [simp]: "xs = y # ys"
skalberg@14494
  2240
      show ?thesis
skalberg@14494
  2241
      proof (auto simp add: nth_append)
wenzelm@19736
  2242
        assume noty: "n < length ys"
wenzelm@19736
  2243
        from spec [OF ind,of ys]
wenzelm@19736
  2244
        have "\<forall>n. n < length ys --> ys ! (length ys - Suc n) = rev ys ! n"
wenzelm@19736
  2245
          by simp
wenzelm@19736
  2246
        hence "n < length ys --> ys ! (length ys - Suc n) = rev ys ! n"
wenzelm@19736
  2247
          ..
wenzelm@19736
  2248
        hence "ys ! (length ys - Suc n) = rev ys ! n"
wenzelm@19736
  2249
          ..
wenzelm@19736
  2250
        thus "(y # ys) ! (length ys - n) = rev ys ! n"
wenzelm@19736
  2251
          by (simp add: nth_Cons' noty linorder_not_less [symmetric])
skalberg@14494
  2252
      next
wenzelm@19736
  2253
        assume "~ n < length ys"
wenzelm@19736
  2254
        hence x: "length ys \<le> n"
wenzelm@19736
  2255
          by simp
wenzelm@19736
  2256
        from notx
wenzelm@19736
  2257
        have "n < Suc (length ys)"
wenzelm@19736
  2258
          by simp
wenzelm@19736
  2259
        hence "n \<le> length ys"
wenzelm@19736
  2260
          by simp
wenzelm@19736
  2261
        with x
wenzelm@19736
  2262
        have "length ys = n"
wenzelm@19736
  2263
          by simp
wenzelm@19736
  2264
        thus "y = [y] ! (n - length ys)"
wenzelm@19736
  2265
          by simp
skalberg@14494
  2266
      qed
skalberg@14494
  2267
    qed
skalberg@14494
  2268
  qed
skalberg@14494
  2269
  hence "n < length w --> bv_select w n = rev w ! n"
skalberg@14494
  2270
    ..
skalberg@14494
  2271
  thus ?thesis
skalberg@14494
  2272
    ..
skalberg@14494
  2273
qed
skalberg@14494
  2274
skalberg@14494
  2275
lemma bv_chop_append: "bv_chop (w1 @ w2) (length w2) = (w1,w2)"
skalberg@14494
  2276
  by (simp add: bv_chop_def Let_def)
skalberg@14494
  2277
skalberg@14494
  2278
lemma append_bv_chop_id: "fst (bv_chop w l) @ snd (bv_chop w l) = w"
skalberg@14494
  2279
  by (simp add: bv_chop_def Let_def)
skalberg@14494
  2280
skalberg@14494
  2281
lemma bv_chop_length_fst [simp]: "length (fst (bv_chop w i)) = length w - i"
webertj@20217
  2282
  by (simp add: bv_chop_def Let_def)
skalberg@14494
  2283
skalberg@14494
  2284
lemma bv_chop_length_snd [simp]: "length (snd (bv_chop w i)) = min i (length w)"
webertj@20217
  2285
  by (simp add: bv_chop_def Let_def)
skalberg@14494
  2286
skalberg@14494
  2287
lemma bv_slice_length [simp]: "[| j \<le> i; i < length w |] ==> length (bv_slice w (i,j)) = i - j + 1"
webertj@20217
  2288
  by (auto simp add: bv_slice_def)
skalberg@14494
  2289
wenzelm@19736
  2290
definition
wenzelm@21404
  2291
  length_nat :: "nat => nat" where
wenzelm@19736
  2292
  "length_nat x = (LEAST n. x < 2 ^ n)"
skalberg@14494
  2293
skalberg@14494
  2294
lemma length_nat: "length (nat_to_bv n) = length_nat n"
skalberg@14494
  2295
  apply (simp add: length_nat_def)
skalberg@14494
  2296
  apply (rule Least_equality [symmetric])
skalberg@14494
  2297
  prefer 2
skalberg@14494
  2298
  apply (rule length_nat_to_bv_upper_limit)
skalberg@14494
  2299
  apply arith
skalberg@14494
  2300
  apply (rule ccontr)
skalberg@14494
  2301
proof -
skalberg@14494
  2302
  assume "~ n < 2 ^ length (nat_to_bv n)"
skalberg@14494
  2303
  hence "2 ^ length (nat_to_bv n) \<le> n"
skalberg@14494
  2304
    by simp
skalberg@14494
  2305
  hence "length (nat_to_bv n) < length (nat_to_bv n)"
skalberg@14494
  2306
    by (rule length_nat_to_bv_lower_limit)
skalberg@14494
  2307
  thus False
skalberg@14494
  2308
    by simp
skalberg@14494
  2309
qed
skalberg@14494
  2310
skalberg@14494
  2311
lemma length_nat_0 [simp]: "length_nat 0 = 0"
skalberg@14494
  2312
  by (simp add: length_nat_def Least_equality)
skalberg@14494
  2313
skalberg@14494
  2314
lemma length_nat_non0:
skalberg@14494
  2315
  assumes n0: "0 < n"
skalberg@14494
  2316
  shows       "length_nat n = Suc (length_nat (n div 2))"
skalberg@14494
  2317
  apply (simp add: length_nat [symmetric])
skalberg@14494
  2318
  apply (subst nat_to_bv_non0 [of n])
skalberg@14494
  2319
  apply (simp_all add: n0)
skalberg@14494
  2320
  done
skalberg@14494
  2321
wenzelm@19736
  2322
definition
wenzelm@21404
  2323
  length_int :: "int => nat" where
wenzelm@19736
  2324
  "length_int x =
wenzelm@19736
  2325
    (if 0 < x then Suc (length_nat (nat x))
wenzelm@19736
  2326
    else if x = 0 then 0
wenzelm@19736
  2327
    else Suc (length_nat (nat (-x - 1))))"
skalberg@14494
  2328
skalberg@14494
  2329
lemma length_int: "length (int_to_bv i) = length_int i"
skalberg@14494
  2330
proof (cases "0 < i")
skalberg@14494
  2331
  assume i0: "0 < i"
berghofe@15325
  2332
  hence "length (int_to_bv i) = length (norm_signed (\<zero> # norm_unsigned (nat_to_bv (nat i))))"
skalberg@14494
  2333
    by simp
berghofe@15325
  2334
  also from norm_unsigned_result [of "nat_to_bv (nat i)"]
berghofe@15325
  2335
  have "... = Suc (length_nat (nat i))"
skalberg@14494
  2336
    apply safe
berghofe@15325
  2337
    apply (simp del: norm_unsigned_nat_to_bv)
skalberg@14494
  2338
    apply (drule norm_empty_bv_to_nat_zero)
skalberg@14494
  2339
    using prems
skalberg@14494
  2340
    apply simp
berghofe@15325
  2341
    apply (cases "norm_unsigned (nat_to_bv (nat i))")
berghofe@15325
  2342
    apply (drule norm_empty_bv_to_nat_zero [of "nat_to_bv (nat i)"])
skalberg@14494
  2343
    using prems
skalberg@14494
  2344
    apply simp
skalberg@14494
  2345
    apply simp
skalberg@14494
  2346
    using prems
skalberg@14494
  2347
    apply (simp add: length_nat [symmetric])
skalberg@14494
  2348
    done
skalberg@14494
  2349
  finally show ?thesis
skalberg@14494
  2350
    using i0
skalberg@14494
  2351
    by (simp add: length_int_def)
skalberg@14494
  2352
next
skalberg@14494
  2353
  assume "~ 0 < i"
skalberg@14494
  2354
  hence i0: "i \<le> 0"
skalberg@14494
  2355
    by simp
skalberg@14494
  2356
  show ?thesis
skalberg@14494
  2357
  proof (cases "i = 0")
skalberg@14494
  2358
    assume "i = 0"
skalberg@14494
  2359
    thus ?thesis
skalberg@14494
  2360
      by (simp add: length_int_def)
skalberg@14494
  2361
  next
skalberg@14494
  2362
    assume "i \<noteq> 0"
skalberg@14494
  2363
    with i0
skalberg@14494
  2364
    have i0: "i < 0"
skalberg@14494
  2365
      by simp
berghofe@15325
  2366
    hence "length (int_to_bv i) = length (norm_signed (\<one> # bv_not (norm_unsigned (nat_to_bv (nat (- i) - 1)))))"
berghofe@15325
  2367
      by (simp add: int_to_bv_def nat_diff_distrib)
berghofe@15325
  2368
    also from norm_unsigned_result [of "nat_to_bv (nat (- i) - 1)"]
berghofe@15325
  2369
    have "... = Suc (length_nat (nat (- i) - 1))"
skalberg@14494
  2370
      apply safe
berghofe@15325
  2371
      apply (simp del: norm_unsigned_nat_to_bv)
berghofe@15325
  2372
      apply (drule norm_empty_bv_to_nat_zero [of "nat_to_bv (nat (-i) - Suc 0)"])
skalberg@14494
  2373
      using prems
skalberg@14494
  2374
      apply simp
skalberg@14494
  2375
      apply (cases "- i - 1 = 0")
skalberg@14494
  2376
      apply simp
skalberg@14494
  2377
      apply (simp add: length_nat [symmetric])
berghofe@15325
  2378
      apply (cases "norm_unsigned (nat_to_bv (nat (- i) - 1))")
skalberg@14494
  2379
      apply simp
skalberg@14494
  2380
      apply simp
skalberg@14494
  2381
      done
skalberg@14494
  2382
    finally
skalberg@14494
  2383
    show ?thesis
skalberg@14494
  2384
      using i0
berghofe@15325
  2385
      by (simp add: length_int_def nat_diff_distrib del: int_to_bv_lt0)
skalberg@14494
  2386
  qed
skalberg@14494
  2387
qed
skalberg@14494
  2388
skalberg@14494
  2389
lemma length_int_0 [simp]: "length_int 0 = 0"
skalberg@14494
  2390
  by (simp add: length_int_def)
skalberg@14494
  2391
berghofe@15325
  2392
lemma length_int_gt0: "0 < i ==> length_int i = Suc (length_nat (nat i))"
skalberg@14494
  2393
  by (simp add: length_int_def)
skalberg@14494
  2394
berghofe@15325
  2395
lemma length_int_lt0: "i < 0 ==> length_int i = Suc (length_nat (nat (- i) - 1))"
berghofe@15325
  2396
  by (simp add: length_int_def nat_diff_distrib)
skalberg@14494
  2397
skalberg@14494
  2398
lemma bv_chopI: "[| w = w1 @ w2 ; i = length w2 |] ==> bv_chop w i = (w1,w2)"
skalberg@14494
  2399
  by (simp add: bv_chop_def Let_def)
skalberg@14494
  2400
skalberg@14494
  2401
lemma bv_sliceI: "[| j \<le> i ; i < length w ; w = w1 @ w2 @ w3 ; Suc i = length w2 + j ; j = length w3  |] ==> bv_slice w (i,j) = w2"
skalberg@14494
  2402
  apply (simp add: bv_slice_def)
skalberg@14494
  2403
  apply (subst bv_chopI [of "w1 @ w2 @ w3" w1 "w2 @ w3"])
skalberg@14494
  2404
  apply simp
skalberg@14494
  2405
  apply simp
skalberg@14494
  2406
  apply simp
skalberg@14494
  2407
  apply (subst bv_chopI [of "w2 @ w3" w2 w3],simp_all)
skalberg@14494
  2408
  done
skalberg@14494
  2409
skalberg@14494
  2410
lemma bv_slice_bv_slice:
skalberg@14494
  2411
  assumes ki: "k \<le> i"
skalberg@14494
  2412
  and     ij: "i \<le> j"
skalberg@14494
  2413
  and     jl: "j \<le> l"
skalberg@14494
  2414
  and     lw: "l < length w"
skalberg@14494
  2415
  shows       "bv_slice w (j,i) = bv_slice (bv_slice w (l,k)) (j-k,i-k)"
skalberg@14494
  2416
proof -
skalberg@14494
  2417
  def w1  == "fst (bv_chop w (Suc l))"
wenzelm@19736
  2418
  and w2  == "fst (bv_chop (snd (bv_chop w (Suc l))) (Suc j))"
wenzelm@19736
  2419
  and w3  == "fst (bv_chop (snd (bv_chop (snd (bv_chop w (Suc l))) (Suc j))) i)"
wenzelm@19736
  2420
  and w4  == "fst (bv_chop (snd (bv_chop (snd (bv_chop (snd (bv_chop w (Suc l))) (Suc j))) i)) k)"
wenzelm@19736
  2421
  and w5  == "snd (bv_chop (snd (bv_chop (snd (bv_chop (snd (bv_chop w (Suc l))) (Suc j))) i)) k)"
wenzelm@19736
  2422
  note w_defs = this
skalberg@14494
  2423
skalberg@14494
  2424
  have w_def: "w = w1 @ w2 @ w3 @ w4 @ w5"
skalberg@14494
  2425
    by (simp add: w_defs append_bv_chop_id)
skalberg@14494
  2426
skalberg@14494
  2427
  from ki ij jl lw
skalberg@14494
  2428
  show ?thesis
paulson@15488
  2429
    appl