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