src/HOL/Library/Word.thy
author paulson
Wed Feb 02 18:06:25 2005 +0100 (2005-02-02)
changeset 15488 7c638a46dcbb
parent 15481 fc075ae929e4
child 15620 8ccdc8bc66a2
permissions -rw-r--r--
tidying of some subst/simplesubst proofs
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
nipkow@15131
    10
files "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
skalberg@14494
    59
syntax (xsymbols)
skalberg@14494
    60
  bitnot :: "bit => bit"        ("\<not>\<^sub>b _" [40] 40)
skalberg@14494
    61
  bitand :: "bit => bit => bit" (infixr "\<and>\<^sub>b" 35)
skalberg@14494
    62
  bitor  :: "bit => bit => bit" (infixr "\<or>\<^sub>b" 30)
skalberg@14494
    63
  bitxor :: "bit => bit => bit" (infixr "\<oplus>\<^sub>b" 30)
skalberg@14494
    64
kleing@14565
    65
syntax (HTML output)
kleing@14565
    66
  bitnot :: "bit => bit"        ("\<not>\<^sub>b _" [40] 40)
kleing@14565
    67
  bitand :: "bit => bit => bit" (infixr "\<and>\<^sub>b" 35)
kleing@14565
    68
  bitor  :: "bit => bit => bit" (infixr "\<or>\<^sub>b" 30)
kleing@14565
    69
  bitxor :: "bit => bit => bit" (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@14494
    87
lemma [simp]: "(bitnot (bitnot b)) = b"
skalberg@14494
    88
  by (cases b,simp_all)
skalberg@14494
    89
skalberg@14494
    90
lemma [simp]: "(b bitand b) = b"
skalberg@14494
    91
  by (cases b,simp_all)
skalberg@14494
    92
skalberg@14494
    93
lemma [simp]: "(b bitor b) = b"
skalberg@14494
    94
  by (cases b,simp_all)
skalberg@14494
    95
skalberg@14494
    96
lemma [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
skalberg@14494
   144
constdefs
skalberg@14494
   145
  bv_msb :: "bit list => bit"
skalberg@14494
   146
  "bv_msb w == if w = [] then \<zero> else hd w"
skalberg@14494
   147
  bv_extend :: "[nat,bit,bit list]=>bit list"
skalberg@14494
   148
  "bv_extend i b w == (replicate (i - length w) b) @ w"
skalberg@14494
   149
  bv_not :: "bit list => bit list"
skalberg@14494
   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@14494
   155
lemma [simp]: "bv_not [] = []"
skalberg@14494
   156
  by (simp add: bv_not_def)
skalberg@14494
   157
skalberg@14494
   158
lemma [simp]: "bv_not (b#bs) = (bitnot b) # bv_not bs"
skalberg@14494
   159
  by (simp add: bv_not_def)
skalberg@14494
   160
skalberg@14494
   161
lemma [simp]: "bv_not (bv_not w) = w"
skalberg@14494
   162
  by (rule bit_list_induct [of _ w],simp_all)
skalberg@14494
   163
skalberg@14494
   164
lemma [simp]: "bv_msb [] = \<zero>"
skalberg@14494
   165
  by (simp add: bv_msb_def)
skalberg@14494
   166
skalberg@14494
   167
lemma [simp]: "bv_msb (b#bs) = b"
skalberg@14494
   168
  by (simp add: bv_msb_def)
skalberg@14494
   169
skalberg@14494
   170
lemma [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@14494
   173
lemma [simp,intro]: "bv_msb w = \<one> ==> 0 < length w"
skalberg@14494
   174
  by (cases w,simp_all)
skalberg@14494
   175
skalberg@14494
   176
lemma [simp]: "length (bv_not w) = length w"
skalberg@14494
   177
  by (induct w,simp_all)
skalberg@14494
   178
skalberg@14494
   179
constdefs
berghofe@15325
   180
  bv_to_nat :: "bit list => nat"
berghofe@15325
   181
  "bv_to_nat == foldl (%bn b. 2 * bn + bitval b) 0"
skalberg@14494
   182
skalberg@14494
   183
lemma [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@14494
   235
lemma [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@14494
   240
lemma [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
skalberg@14494
   329
constdefs
skalberg@14494
   330
  norm_unsigned :: "bit list => bit list"
skalberg@14494
   331
  "norm_unsigned == rem_initial \<zero>"
skalberg@14494
   332
skalberg@14494
   333
lemma [simp]: "norm_unsigned [] = []"
skalberg@14494
   334
  by (simp add: norm_unsigned_def)
skalberg@14494
   335
skalberg@14494
   336
lemma [simp]: "norm_unsigned (\<zero>#bs) = norm_unsigned bs"
skalberg@14494
   337
  by (simp add: norm_unsigned_def)
skalberg@14494
   338
skalberg@14494
   339
lemma [simp]: "norm_unsigned (\<one>#bs) = \<one>#bs"
skalberg@14494
   340
  by (simp add: norm_unsigned_def)
skalberg@14494
   341
skalberg@14494
   342
lemma [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
skalberg@14494
   352
constdefs
berghofe@15325
   353
  nat_to_bv :: "nat => bit list"
skalberg@14494
   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"
skalberg@14494
   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")
skalberg@14494
   412
	assume "j = i"
skalberg@14494
   413
	with pi
skalberg@14494
   414
	show "P j"
skalberg@14494
   415
	  by simp
skalberg@14494
   416
      next
skalberg@14494
   417
	assume "j ~= i"
skalberg@14494
   418
	with ji
skalberg@14494
   419
	have "j < i"
skalberg@14494
   420
	  by simp
skalberg@14494
   421
	with kj and a
skalberg@14494
   422
	show "P j"
skalberg@14494
   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")
skalberg@14494
   449
	assume "n < 0"
skalberg@14494
   450
	thus ?thesis
skalberg@14494
   451
	  by (simp add: nat_to_bv_helper.simps)
skalberg@14494
   452
      next
skalberg@14494
   453
	assume "~n < 0"
skalberg@14494
   454
	show ?thesis
skalberg@14494
   455
	proof (rule n_div_2_cases [of n])
skalberg@14494
   456
	  assume [simp]: "n = 0"
skalberg@14494
   457
	  show ?thesis
paulson@15481
   458
	    apply (simp only: nat_to_bv_helper.simps [of n])
skalberg@14494
   459
	    apply simp
skalberg@14494
   460
	    done
skalberg@14494
   461
	next
skalberg@14494
   462
	  assume n2n: "n div 2 < n"
skalberg@14494
   463
	  assume [simp]: "0 < n"
skalberg@14494
   464
	  hence n20: "0 \<le> n div 2"
skalberg@14494
   465
	    by arith
skalberg@14494
   466
	  from ind [of "n div 2"] and n2n n20
skalberg@14494
   467
	  have ind': "\<forall>l. nat_to_bv_helper (n div 2) l = nat_to_bv_helper (n div 2) [] @ l"
skalberg@14494
   468
	    by blast
skalberg@14494
   469
	  show ?thesis
paulson@15481
   470
	    apply (simp only: nat_to_bv_helper.simps [of n])
berghofe@15325
   471
	    apply (cases "n=0")
berghofe@15325
   472
	    apply simp
berghofe@15325
   473
	    apply (simp only: if_False)
skalberg@14494
   474
	    apply simp
skalberg@14494
   475
	    apply (subst spec [OF ind',of "\<zero>#l"])
skalberg@14494
   476
	    apply (subst spec [OF ind',of "\<one>#l"])
skalberg@14494
   477
	    apply (subst spec [OF ind',of "[\<one>]"])
skalberg@14494
   478
	    apply (subst spec [OF ind',of "[\<zero>]"])
skalberg@14494
   479
	    apply simp
skalberg@14494
   480
	    done
skalberg@14494
   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 -
berghofe@15325
   514
	have "(2::nat) ^ (length xs + length l2) = 2 ^ length xs * 2 ^ length l2"
skalberg@14494
   515
	  by (induct "length xs",simp_all)
skalberg@14494
   516
	hence "bitval x * 2 ^ (length xs + length l2) + bv_to_nat xs * 2 ^ length l2 =
skalberg@14494
   517
	  bitval x * 2 ^ length xs * 2 ^ length l2 + bv_to_nat xs * 2 ^ length l2"
skalberg@14494
   518
	  by simp
skalberg@14494
   519
	also have "... = (bitval x * 2 ^ length xs + bv_to_nat xs) * 2 ^ length l2"
skalberg@14494
   520
	  by (simp add: ring_distrib)
skalberg@14494
   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
skalberg@14494
   556
	assume "n mod 2 = 0"
berghofe@15325
   557
	with mod_div_equality [of n 2]
skalberg@14494
   558
	show "n div 2 * 2 = n"
skalberg@14494
   559
	  by simp
skalberg@14494
   560
      next
berghofe@15325
   561
	assume "n mod 2 = Suc 0"
berghofe@15325
   562
	with mod_div_equality [of n 2]
berghofe@15325
   563
	show "Suc (n div 2 * 2) = n"
skalberg@14494
   564
	  by simp
skalberg@14494
   565
      qed
skalberg@14494
   566
  qed
skalberg@14494
   567
qed
skalberg@14494
   568
skalberg@14494
   569
lemma [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@14494
   572
lemma [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)
skalberg@14494
   718
	assume [simp]: "xs = []"
skalberg@14494
   719
	show ?thesis
skalberg@14494
   720
	  by (simp add: nat_to_bv_non0)
skalberg@14494
   721
      next
skalberg@14494
   722
	fix y ys
skalberg@14494
   723
	assume [simp]: "xs = y # ys"
skalberg@14494
   724
	show ?thesis
skalberg@14494
   725
	  apply simp
skalberg@14494
   726
	  apply (subst bv_to_nat_dist_append)
skalberg@14494
   727
	  apply simp
skalberg@14494
   728
	proof -
skalberg@14494
   729
	  have "nat_to_bv (2 * 2 ^ length ys + (bv_to_nat (rev ys) * 2 + bitval y)) =
skalberg@14494
   730
	    nat_to_bv (2 * (2 ^ length ys + bv_to_nat (rev ys)) + bitval y)"
skalberg@14494
   731
	    by (simp add: add_ac mult_ac)
skalberg@14494
   732
	  also have "... = nat_to_bv (2 * (bv_to_nat (\<one>#rev ys)) + bitval y)"
skalberg@14494
   733
	    by simp
skalberg@14494
   734
	  also have "... = norm_unsigned (\<one>#rev ys) @ [y]"
skalberg@14494
   735
	  proof -
skalberg@14494
   736
	    from ind
skalberg@14494
   737
	    have "nat_to_bv (2 ^ length (rev ys) + bv_to_nat (rev ys)) = \<one> # rev ys"
skalberg@14494
   738
	      by auto
skalberg@14494
   739
	    hence [simp]: "nat_to_bv (2 ^ length ys + bv_to_nat (rev ys)) = \<one> # rev ys"
skalberg@14494
   740
	      by simp
skalberg@14494
   741
	    show ?thesis
skalberg@14494
   742
	      apply (subst nat_helper1)
skalberg@14494
   743
	      apply simp_all
skalberg@14494
   744
	      done
skalberg@14494
   745
	  qed
skalberg@14494
   746
	  also have "... = (\<one>#rev ys) @ [y]"
skalberg@14494
   747
	    by simp
skalberg@14494
   748
	  also have "... = \<one> # rev ys @ [y]"
skalberg@14494
   749
	    by simp
skalberg@14494
   750
	  finally show "nat_to_bv (2 * 2 ^ length ys + (bv_to_nat (rev ys) * 2 + bitval y)) = \<one> # rev ys @ [y]"
skalberg@14494
   751
	    .
skalberg@14494
   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 [simp]: "bv_to_nat (norm_unsigned xs) = bv_to_nat xs"
skalberg@14494
   775
  by (rule bit_list_induct [of _ w],simp_all)
skalberg@14494
   776
skalberg@14494
   777
lemma bv_to_nat_qinj:
skalberg@14494
   778
  assumes one: "bv_to_nat xs = bv_to_nat ys"
skalberg@14494
   779
  and     len: "length xs = length ys"
skalberg@14494
   780
  shows        "xs = ys"
skalberg@14494
   781
proof -
skalberg@14494
   782
  from one
skalberg@14494
   783
  have "nat_to_bv (bv_to_nat xs) = nat_to_bv (bv_to_nat ys)"
skalberg@14494
   784
    by simp
skalberg@14494
   785
  hence xsys: "norm_unsigned xs = norm_unsigned ys"
skalberg@14494
   786
    by simp
skalberg@14494
   787
  have "xs = bv_extend (length xs) \<zero> (norm_unsigned xs)"
skalberg@14494
   788
    by (simp add: bv_extend_norm_unsigned)
skalberg@14494
   789
  also have "... = bv_extend (length ys) \<zero> (norm_unsigned ys)"
skalberg@14494
   790
    by (simp add: xsys len)
skalberg@14494
   791
  also have "... = ys"
skalberg@14494
   792
    by (simp add: bv_extend_norm_unsigned)
skalberg@14494
   793
  finally show ?thesis .
skalberg@14494
   794
qed
skalberg@14494
   795
skalberg@14494
   796
lemma norm_unsigned_nat_to_bv [simp]:
berghofe@15325
   797
  "norm_unsigned (nat_to_bv n) = nat_to_bv n"
skalberg@14494
   798
proof -
skalberg@14494
   799
  have "norm_unsigned (nat_to_bv n) = nat_to_bv (bv_to_nat (norm_unsigned (nat_to_bv n)))"
skalberg@14494
   800
    by (subst nat_bv_nat,simp)
skalberg@14494
   801
  also have "... = nat_to_bv n"
skalberg@14494
   802
    by simp
skalberg@14494
   803
  finally show ?thesis .
skalberg@14494
   804
qed
skalberg@14494
   805
skalberg@14494
   806
lemma length_nat_to_bv_upper_limit:
skalberg@14494
   807
  assumes nk: "n \<le> 2 ^ k - 1"
skalberg@14494
   808
  shows       "length (nat_to_bv n) \<le> k"
berghofe@15325
   809
proof (cases "n = 0")
berghofe@15325
   810
  case True
skalberg@14494
   811
  thus ?thesis
skalberg@14494
   812
    by (simp add: nat_to_bv_def nat_to_bv_helper.simps)
skalberg@14494
   813
next
berghofe@15325
   814
  case False
berghofe@15325
   815
  hence n0: "0 < n" by simp
skalberg@14494
   816
  show ?thesis
skalberg@14494
   817
  proof (rule ccontr)
skalberg@14494
   818
    assume "~ length (nat_to_bv n) \<le> k"
skalberg@14494
   819
    hence "k < length (nat_to_bv n)"
skalberg@14494
   820
      by simp
skalberg@14494
   821
    hence "k \<le> length (nat_to_bv n) - 1"
skalberg@14494
   822
      by arith
berghofe@15325
   823
    hence "(2::nat) ^ k \<le> 2 ^ (length (nat_to_bv n) - 1)"
skalberg@14494
   824
      by simp
skalberg@14494
   825
    also have "... = 2 ^ (length (norm_unsigned (nat_to_bv n)) - 1)"
berghofe@15325
   826
      by simp
skalberg@14494
   827
    also have "... \<le> bv_to_nat (nat_to_bv n)"
berghofe@15325
   828
      by (rule bv_to_nat_lower_limit,simp add: n0)
skalberg@14494
   829
    also have "... = n"
berghofe@15325
   830
      by simp
skalberg@14494
   831
    finally have "2 ^ k \<le> n" .
skalberg@14494
   832
    with n0
skalberg@14494
   833
    have "2 ^ k - 1 < n"
skalberg@14494
   834
      by arith
skalberg@14494
   835
    with nk
skalberg@14494
   836
    show False
skalberg@14494
   837
      by simp
skalberg@14494
   838
  qed
skalberg@14494
   839
qed
skalberg@14494
   840
skalberg@14494
   841
lemma length_nat_to_bv_lower_limit:
skalberg@14494
   842
  assumes nk: "2 ^ k \<le> n"
skalberg@14494
   843
  shows       "k < length (nat_to_bv n)"
skalberg@14494
   844
proof (rule ccontr)
skalberg@14494
   845
  assume "~ k < length (nat_to_bv n)"
skalberg@14494
   846
  hence lnk: "length (nat_to_bv n) \<le> k"
skalberg@14494
   847
    by simp
skalberg@14494
   848
  have "n = bv_to_nat (nat_to_bv n)"
skalberg@14494
   849
    by simp
skalberg@14494
   850
  also have "... < 2 ^ length (nat_to_bv n)"
skalberg@14494
   851
    by (rule bv_to_nat_upper_range)
skalberg@14494
   852
  also from lnk have "... \<le> 2 ^ k"
skalberg@14494
   853
    by simp
skalberg@14494
   854
  finally have "n < 2 ^ k" .
skalberg@14494
   855
  with nk
skalberg@14494
   856
  show False
skalberg@14494
   857
    by simp
skalberg@14494
   858
qed
skalberg@14494
   859
wenzelm@14589
   860
subsection {* Unsigned Arithmetic Operations *}
skalberg@14494
   861
skalberg@14494
   862
constdefs
skalberg@14494
   863
  bv_add :: "[bit list, bit list ] => bit list"
skalberg@14494
   864
  "bv_add w1 w2 == nat_to_bv (bv_to_nat w1 + bv_to_nat w2)"
skalberg@14494
   865
skalberg@14494
   866
lemma [simp]: "bv_add (norm_unsigned w1) w2 = bv_add w1 w2"
skalberg@14494
   867
  by (simp add: bv_add_def)
skalberg@14494
   868
skalberg@14494
   869
lemma [simp]: "bv_add w1 (norm_unsigned w2) = bv_add w1 w2"
skalberg@14494
   870
  by (simp add: bv_add_def)
skalberg@14494
   871
skalberg@14494
   872
lemma [simp]: "norm_unsigned (bv_add w1 w2) = bv_add w1 w2"
berghofe@15325
   873
  by (simp add: bv_add_def)
skalberg@14494
   874
skalberg@14494
   875
lemma bv_add_length: "length (bv_add w1 w2) \<le> Suc (max (length w1) (length w2))"
skalberg@14494
   876
proof (unfold bv_add_def,rule length_nat_to_bv_upper_limit)
skalberg@14494
   877
  from bv_to_nat_upper_range [of w1] and bv_to_nat_upper_range [of w2]
skalberg@14494
   878
  have "bv_to_nat w1 + bv_to_nat w2 \<le> (2 ^ length w1 - 1) + (2 ^ length w2 - 1)"
skalberg@14494
   879
    by arith
skalberg@14494
   880
  also have "... \<le> max (2 ^ length w1 - 1) (2 ^ length w2 - 1) + max (2 ^ length w1 - 1) (2 ^ length w2 - 1)"
skalberg@14494
   881
    by (rule add_mono,safe intro!: le_maxI1 le_maxI2)
skalberg@14494
   882
  also have "... = 2 * max (2 ^ length w1 - 1) (2 ^ length w2 - 1)"
skalberg@14494
   883
    by simp
skalberg@14494
   884
  also have "... \<le> 2 ^ Suc (max (length w1) (length w2)) - 2"
skalberg@14494
   885
  proof (cases "length w1 \<le> length w2")
berghofe@15325
   886
    assume w1w2: "length w1 \<le> length w2"
berghofe@15325
   887
    hence "(2::nat) ^ length w1 \<le> 2 ^ length w2"
skalberg@14494
   888
      by simp
berghofe@15325
   889
    hence "(2::nat) ^ length w1 - 1 \<le> 2 ^ length w2 - 1"
skalberg@14494
   890
      by arith
berghofe@15325
   891
    with w1w2 show ?thesis
berghofe@15325
   892
      by (simp add: diff_mult_distrib2 split: split_max)
skalberg@14494
   893
  next
skalberg@14494
   894
    assume [simp]: "~ (length w1 \<le> length w2)"
berghofe@15325
   895
    have "~ ((2::nat) ^ length w1 - 1 \<le> 2 ^ length w2 - 1)"
skalberg@14494
   896
    proof
berghofe@15325
   897
      assume "(2::nat) ^ length w1 - 1 \<le> 2 ^ length w2 - 1"
berghofe@15325
   898
      hence "((2::nat) ^ length w1 - 1) + 1 \<le> (2 ^ length w2 - 1) + 1"
skalberg@14494
   899
	by (rule add_right_mono)
berghofe@15325
   900
      hence "(2::nat) ^ length w1 \<le> 2 ^ length w2"
skalberg@14494
   901
	by simp
skalberg@14494
   902
      hence "length w1 \<le> length w2"
skalberg@14494
   903
	by simp
skalberg@14494
   904
      thus False
skalberg@14494
   905
	by simp
skalberg@14494
   906
    qed
skalberg@14494
   907
    thus ?thesis
berghofe@15325
   908
      by (simp add: diff_mult_distrib2 split: split_max)
skalberg@14494
   909
  qed
skalberg@14494
   910
  finally show "bv_to_nat w1 + bv_to_nat w2 \<le> 2 ^ Suc (max (length w1) (length w2)) - 1"
skalberg@14494
   911
    by arith
skalberg@14494
   912
qed
skalberg@14494
   913
skalberg@14494
   914
constdefs
skalberg@14494
   915
  bv_mult :: "[bit list, bit list ] => bit list"
skalberg@14494
   916
  "bv_mult w1 w2 == nat_to_bv (bv_to_nat w1 * bv_to_nat w2)"
skalberg@14494
   917
skalberg@14494
   918
lemma [simp]: "bv_mult (norm_unsigned w1) w2 = bv_mult w1 w2"
skalberg@14494
   919
  by (simp add: bv_mult_def)
skalberg@14494
   920
skalberg@14494
   921
lemma [simp]: "bv_mult w1 (norm_unsigned w2) = bv_mult w1 w2"
skalberg@14494
   922
  by (simp add: bv_mult_def)
skalberg@14494
   923
skalberg@14494
   924
lemma [simp]: "norm_unsigned (bv_mult w1 w2) = bv_mult w1 w2"
berghofe@15325
   925
  by (simp add: bv_mult_def)
skalberg@14494
   926
skalberg@14494
   927
lemma bv_mult_length: "length (bv_mult w1 w2) \<le> length w1 + length w2"
skalberg@14494
   928
proof (unfold bv_mult_def,rule length_nat_to_bv_upper_limit)
skalberg@14494
   929
  from bv_to_nat_upper_range [of w1] and bv_to_nat_upper_range [of w2]
skalberg@14494
   930
  have h: "bv_to_nat w1 \<le> 2 ^ length w1 - 1 \<and> bv_to_nat w2 \<le> 2 ^ length w2 - 1"
skalberg@14494
   931
    by arith
skalberg@14494
   932
  have "bv_to_nat w1 * bv_to_nat w2 \<le> (2 ^ length w1 - 1) * (2 ^ length w2 - 1)"
skalberg@14494
   933
    apply (cut_tac h)
skalberg@14494
   934
    apply (rule mult_mono)
skalberg@14494
   935
    apply auto
skalberg@14494
   936
    done
skalberg@14494
   937
  also have "... < 2 ^ length w1 * 2 ^ length w2"
skalberg@14494
   938
    by (rule mult_strict_mono,auto)
skalberg@14494
   939
  also have "... = 2 ^ (length w1 + length w2)"
skalberg@14494
   940
    by (simp add: power_add)
skalberg@14494
   941
  finally show "bv_to_nat w1 * bv_to_nat w2 \<le> 2 ^ (length w1 + length w2) - 1"
skalberg@14494
   942
    by arith
skalberg@14494
   943
qed
skalberg@14494
   944
wenzelm@14589
   945
subsection {* Signed Vectors *}
skalberg@14494
   946
skalberg@14494
   947
consts
skalberg@14494
   948
  norm_signed :: "bit list => bit list"
skalberg@14494
   949
skalberg@14494
   950
primrec
skalberg@14494
   951
  norm_signed_Nil: "norm_signed [] = []"
skalberg@14494
   952
  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
   953
skalberg@14494
   954
lemma [simp]: "norm_signed [\<zero>] = []"
skalberg@14494
   955
  by simp
skalberg@14494
   956
skalberg@14494
   957
lemma [simp]: "norm_signed [\<one>] = [\<one>]"
skalberg@14494
   958
  by simp
skalberg@14494
   959
skalberg@14494
   960
lemma [simp]: "norm_signed (\<zero>#\<one>#xs) = \<zero>#\<one>#xs"
skalberg@14494
   961
  by simp
skalberg@14494
   962
skalberg@14494
   963
lemma [simp]: "norm_signed (\<zero>#\<zero>#xs) = norm_signed (\<zero>#xs)"
skalberg@14494
   964
  by simp
skalberg@14494
   965
skalberg@14494
   966
lemma [simp]: "norm_signed (\<one>#\<zero>#xs) = \<one>#\<zero>#xs"
skalberg@14494
   967
  by simp
skalberg@14494
   968
skalberg@14494
   969
lemma [simp]: "norm_signed (\<one>#\<one>#xs) = norm_signed (\<one>#xs)"
skalberg@14494
   970
  by simp
skalberg@14494
   971
skalberg@14494
   972
lemmas [simp del] = norm_signed_Cons
skalberg@14494
   973
skalberg@14494
   974
constdefs
skalberg@14494
   975
  int_to_bv :: "int => bit list"
skalberg@14494
   976
  "int_to_bv n == if 0 \<le> n
berghofe@15325
   977
                 then norm_signed (\<zero>#nat_to_bv (nat n))
berghofe@15325
   978
                 else norm_signed (bv_not (\<zero>#nat_to_bv (nat (-n- 1))))"
skalberg@14494
   979
berghofe@15325
   980
lemma int_to_bv_ge0 [simp]: "0 \<le> n ==> int_to_bv n = norm_signed (\<zero> # nat_to_bv (nat n))"
skalberg@14494
   981
  by (simp add: int_to_bv_def)
skalberg@14494
   982
berghofe@15325
   983
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
   984
  by (simp add: int_to_bv_def)
skalberg@14494
   985
skalberg@14494
   986
lemma [simp]: "norm_signed (norm_signed w) = norm_signed w"
skalberg@14494
   987
proof (rule bit_list_induct [of _ w],simp_all)
skalberg@14494
   988
  fix xs
skalberg@14494
   989
  assume "norm_signed (norm_signed xs) = norm_signed xs"
skalberg@14494
   990
  show "norm_signed (norm_signed (\<zero>#xs)) = norm_signed (\<zero>#xs)"
skalberg@14494
   991
  proof (rule bit_list_cases [of xs],simp_all)
skalberg@14494
   992
    fix ys
skalberg@14494
   993
    assume [symmetric,simp]: "xs = \<zero>#ys"
skalberg@14494
   994
    show "norm_signed (norm_signed (\<zero>#ys)) = norm_signed (\<zero>#ys)"
skalberg@14494
   995
      by simp
skalberg@14494
   996
  qed
skalberg@14494
   997
next
skalberg@14494
   998
  fix xs
skalberg@14494
   999
  assume "norm_signed (norm_signed xs) = norm_signed xs"
skalberg@14494
  1000
  show "norm_signed (norm_signed (\<one>#xs)) = norm_signed (\<one>#xs)"
skalberg@14494
  1001
  proof (rule bit_list_cases [of xs],simp_all)
skalberg@14494
  1002
    fix ys
skalberg@14494
  1003
    assume [symmetric,simp]: "xs = \<one>#ys"
skalberg@14494
  1004
    show "norm_signed (norm_signed (\<one>#ys)) = norm_signed (\<one>#ys)"
skalberg@14494
  1005
      by simp
skalberg@14494
  1006
  qed
skalberg@14494
  1007
qed
skalberg@14494
  1008
skalberg@14494
  1009
constdefs
skalberg@14494
  1010
  bv_to_int :: "bit list => int"
berghofe@15325
  1011
  "bv_to_int w == case bv_msb w of \<zero> => int (bv_to_nat w) | \<one> => - int (bv_to_nat (bv_not w) + 1)"
skalberg@14494
  1012
skalberg@14494
  1013
lemma [simp]: "bv_to_int [] = 0"
skalberg@14494
  1014
  by (simp add: bv_to_int_def)
skalberg@14494
  1015
berghofe@15325
  1016
lemma [simp]: "bv_to_int (\<zero>#bs) = int (bv_to_nat bs)"
skalberg@14494
  1017
  by (simp add: bv_to_int_def)
skalberg@14494
  1018
berghofe@15325
  1019
lemma [simp]: "bv_to_int (\<one>#bs) = - int (bv_to_nat (bv_not bs) + 1)"
skalberg@14494
  1020
  by (simp add: bv_to_int_def)
skalberg@14494
  1021
skalberg@14494
  1022
lemma [simp]: "bv_to_int (norm_signed w) = bv_to_int w"
skalberg@14494
  1023
proof (rule bit_list_induct [of _ w],simp_all)
skalberg@14494
  1024
  fix xs
skalberg@14494
  1025
  assume ind: "bv_to_int (norm_signed xs) = bv_to_int xs"
berghofe@15325
  1026
  show "bv_to_int (norm_signed (\<zero>#xs)) = int (bv_to_nat xs)"
skalberg@14494
  1027
  proof (rule bit_list_cases [of xs],simp_all)
skalberg@14494
  1028
    fix ys
skalberg@14494
  1029
    assume [simp]: "xs = \<zero>#ys"
skalberg@14494
  1030
    from ind
berghofe@15325
  1031
    show "bv_to_int (norm_signed (\<zero>#ys)) = int (bv_to_nat ys)"
skalberg@14494
  1032
      by simp
skalberg@14494
  1033
  qed
skalberg@14494
  1034
next
skalberg@14494
  1035
  fix xs
skalberg@14494
  1036
  assume ind: "bv_to_int (norm_signed xs) = bv_to_int xs"
berghofe@15325
  1037
  show "bv_to_int (norm_signed (\<one>#xs)) = -1 - int (bv_to_nat (bv_not xs))"
skalberg@14494
  1038
  proof (rule bit_list_cases [of xs],simp_all)
skalberg@14494
  1039
    fix ys
skalberg@14494
  1040
    assume [simp]: "xs = \<one>#ys"
skalberg@14494
  1041
    from ind
berghofe@15325
  1042
    show "bv_to_int (norm_signed (\<one>#ys)) = -1 - int (bv_to_nat (bv_not ys))"
skalberg@14494
  1043
      by simp
skalberg@14494
  1044
  qed
skalberg@14494
  1045
qed
skalberg@14494
  1046
skalberg@14494
  1047
lemma bv_to_int_upper_range: "bv_to_int w < 2 ^ (length w - 1)"
skalberg@14494
  1048
proof (rule bit_list_cases [of w],simp_all)
skalberg@14494
  1049
  fix bs
berghofe@15325
  1050
  from bv_to_nat_upper_range
berghofe@15325
  1051
  show "int (bv_to_nat bs) < 2 ^ length bs"
berghofe@15325
  1052
    by (simp add: int_nat_two_exp)
skalberg@14494
  1053
next
skalberg@14494
  1054
  fix bs
berghofe@15325
  1055
  have "-1 - int (bv_to_nat (bv_not bs)) \<le> 0"
berghofe@15325
  1056
    by simp
skalberg@14494
  1057
  also have "... < 2 ^ length bs"
skalberg@14494
  1058
    by (induct bs,simp_all)
berghofe@15325
  1059
  finally show "-1 - int (bv_to_nat (bv_not bs)) < 2 ^ length bs"
skalberg@14494
  1060
    .
skalberg@14494
  1061
qed
skalberg@14494
  1062
skalberg@14494
  1063
lemma bv_to_int_lower_range: "- (2 ^ (length w - 1)) \<le> bv_to_int w"
skalberg@14494
  1064
proof (rule bit_list_cases [of w],simp_all)
skalberg@14494
  1065
  fix bs :: "bit list"
skalberg@14494
  1066
  have "- (2 ^ length bs) \<le> (0::int)"
skalberg@14494
  1067
    by (induct bs,simp_all)
berghofe@15325
  1068
  also have "... \<le> int (bv_to_nat bs)"
skalberg@14494
  1069
    by simp
berghofe@15325
  1070
  finally show "- (2 ^ length bs) \<le> int (bv_to_nat bs)"
skalberg@14494
  1071
    .
skalberg@14494
  1072
next
skalberg@14494
  1073
  fix bs
skalberg@14494
  1074
  from bv_to_nat_upper_range [of "bv_not bs"]
berghofe@15325
  1075
  show "- (2 ^ length bs) \<le> -1 - int (bv_to_nat (bv_not bs))"
berghofe@15325
  1076
    by (simp add: int_nat_two_exp)
skalberg@14494
  1077
qed
skalberg@14494
  1078
skalberg@14494
  1079
lemma int_bv_int [simp]: "int_to_bv (bv_to_int w) = norm_signed w"
skalberg@14494
  1080
proof (rule bit_list_cases [of w],simp)
skalberg@14494
  1081
  fix xs
skalberg@14494
  1082
  assume [simp]: "w = \<zero>#xs"
skalberg@14494
  1083
  show ?thesis
skalberg@14494
  1084
    apply simp
skalberg@14494
  1085
    apply (subst norm_signed_Cons [of "\<zero>" "xs"])
skalberg@14494
  1086
    apply simp
skalberg@14494
  1087
    using norm_unsigned_result [of xs]
skalberg@14494
  1088
    apply safe
skalberg@14494
  1089
    apply (rule bit_list_cases [of "norm_unsigned xs"])
skalberg@14494
  1090
    apply simp_all
skalberg@14494
  1091
    done
skalberg@14494
  1092
next
skalberg@14494
  1093
  fix xs
skalberg@14494
  1094
  assume [simp]: "w = \<one>#xs"
skalberg@14494
  1095
  show ?thesis
berghofe@15325
  1096
    apply (simp del: int_to_bv_lt0)
skalberg@14494
  1097
    apply (rule bit_list_induct [of _ xs])
skalberg@14494
  1098
    apply simp
skalberg@14494
  1099
    apply (subst int_to_bv_lt0)
berghofe@15325
  1100
    apply (subgoal_tac "- int (bv_to_nat (bv_not (\<zero> # bs))) + -1 < 0 + 0")
skalberg@14494
  1101
    apply simp
skalberg@14494
  1102
    apply (rule add_le_less_mono)
skalberg@14494
  1103
    apply simp
skalberg@14494
  1104
    apply simp
skalberg@14494
  1105
    apply (simp del: bv_to_nat1 bv_to_nat_helper)
skalberg@14494
  1106
    apply simp
skalberg@14494
  1107
    done
skalberg@14494
  1108
qed
skalberg@14494
  1109
skalberg@14494
  1110
lemma bv_int_bv [simp]: "bv_to_int (int_to_bv i) = i"
skalberg@14494
  1111
  by (cases "0 \<le> i",simp_all)
skalberg@14494
  1112
skalberg@14494
  1113
lemma bv_msb_norm [simp]: "bv_msb (norm_signed w) = bv_msb w"
skalberg@14494
  1114
  by (rule bit_list_cases [of w],simp_all add: norm_signed_Cons)
skalberg@14494
  1115
skalberg@14494
  1116
lemma norm_signed_length: "length (norm_signed w) \<le> length w"
skalberg@14494
  1117
  apply (cases w,simp_all)
skalberg@14494
  1118
  apply (subst norm_signed_Cons)
skalberg@14494
  1119
  apply (case_tac "a",simp_all)
skalberg@14494
  1120
  apply (rule rem_initial_length)
skalberg@14494
  1121
  done
skalberg@14494
  1122
skalberg@14494
  1123
lemma norm_signed_equal: "length (norm_signed w) = length w ==> norm_signed w = w"
skalberg@14494
  1124
proof (rule bit_list_cases [of w],simp_all)
skalberg@14494
  1125
  fix xs
skalberg@14494
  1126
  assume "length (norm_signed (\<zero>#xs)) = Suc (length xs)"
skalberg@14494
  1127
  thus "norm_signed (\<zero>#xs) = \<zero>#xs"
skalberg@14494
  1128
    apply (simp add: norm_signed_Cons)
skalberg@14494
  1129
    apply safe
skalberg@14494
  1130
    apply simp_all
skalberg@14494
  1131
    apply (rule norm_unsigned_equal)
skalberg@14494
  1132
    apply assumption
skalberg@14494
  1133
    done
skalberg@14494
  1134
next
skalberg@14494
  1135
  fix xs
skalberg@14494
  1136
  assume "length (norm_signed (\<one>#xs)) = Suc (length xs)"
skalberg@14494
  1137
  thus "norm_signed (\<one>#xs) = \<one>#xs"
skalberg@14494
  1138
    apply (simp add: norm_signed_Cons)
skalberg@14494
  1139
    apply (rule rem_initial_equal)
skalberg@14494
  1140
    apply assumption
skalberg@14494
  1141
    done
skalberg@14494
  1142
qed
skalberg@14494
  1143
skalberg@14494
  1144
lemma bv_extend_norm_signed: "bv_msb w = b ==> bv_extend (length w) b (norm_signed w) = w"
skalberg@14494
  1145
proof (rule bit_list_cases [of w],simp_all)
skalberg@14494
  1146
  fix xs
skalberg@14494
  1147
  show "bv_extend (Suc (length xs)) \<zero> (norm_signed (\<zero>#xs)) = \<zero>#xs"
skalberg@14494
  1148
  proof (simp add: norm_signed_list_def,auto)
skalberg@14494
  1149
    assume "norm_unsigned xs = []"
skalberg@14494
  1150
    hence xx: "rem_initial \<zero> xs = []"
skalberg@14494
  1151
      by (simp add: norm_unsigned_def)
skalberg@14494
  1152
    have "bv_extend (Suc (length xs)) \<zero> (\<zero>#rem_initial \<zero> xs) = \<zero>#xs"
skalberg@14494
  1153
      apply (simp add: bv_extend_def replicate_app_Cons_same)
skalberg@14494
  1154
      apply (fold bv_extend_def)
skalberg@14494
  1155
      apply (rule bv_extend_rem_initial)
skalberg@14494
  1156
      done
skalberg@14494
  1157
    thus "bv_extend (Suc (length xs)) \<zero> [\<zero>] = \<zero>#xs"
skalberg@14494
  1158
      by (simp add: xx)
skalberg@14494
  1159
  next
skalberg@14494
  1160
    show "bv_extend (Suc (length xs)) \<zero> (\<zero>#norm_unsigned xs) = \<zero>#xs"
skalberg@14494
  1161
      apply (simp add: norm_unsigned_def)
skalberg@14494
  1162
      apply (simp add: bv_extend_def replicate_app_Cons_same)
skalberg@14494
  1163
      apply (fold bv_extend_def)
skalberg@14494
  1164
      apply (rule bv_extend_rem_initial)
skalberg@14494
  1165
      done
skalberg@14494
  1166
  qed
skalberg@14494
  1167
next
skalberg@14494
  1168
  fix xs
skalberg@14494
  1169
  show "bv_extend (Suc (length xs)) \<one> (norm_signed (\<one>#xs)) = \<one>#xs"
skalberg@14494
  1170
    apply (simp add: norm_signed_Cons)
skalberg@14494
  1171
    apply (simp add: bv_extend_def replicate_app_Cons_same)
skalberg@14494
  1172
    apply (fold bv_extend_def)
skalberg@14494
  1173
    apply (rule bv_extend_rem_initial)
skalberg@14494
  1174
    done
skalberg@14494
  1175
qed
skalberg@14494
  1176
skalberg@14494
  1177
lemma bv_to_int_qinj:
skalberg@14494
  1178
  assumes one: "bv_to_int xs = bv_to_int ys"
skalberg@14494
  1179
  and     len: "length xs = length ys"
skalberg@14494
  1180
  shows        "xs = ys"
skalberg@14494
  1181
proof -
skalberg@14494
  1182
  from one
skalberg@14494
  1183
  have "int_to_bv (bv_to_int xs) = int_to_bv (bv_to_int ys)"
skalberg@14494
  1184
    by simp
skalberg@14494
  1185
  hence xsys: "norm_signed xs = norm_signed ys"
skalberg@14494
  1186
    by simp
skalberg@14494
  1187
  hence xsys': "bv_msb xs = bv_msb ys"
skalberg@14494
  1188
  proof -
skalberg@14494
  1189
    have "bv_msb xs = bv_msb (norm_signed xs)"
skalberg@14494
  1190
      by simp
skalberg@14494
  1191
    also have "... = bv_msb (norm_signed ys)"
skalberg@14494
  1192
      by (simp add: xsys)
skalberg@14494
  1193
    also have "... = bv_msb ys"
skalberg@14494
  1194
      by simp
skalberg@14494
  1195
    finally show ?thesis .
skalberg@14494
  1196
  qed
skalberg@14494
  1197
  have "xs = bv_extend (length xs) (bv_msb xs) (norm_signed xs)"
skalberg@14494
  1198
    by (simp add: bv_extend_norm_signed)
skalberg@14494
  1199
  also have "... = bv_extend (length ys) (bv_msb ys) (norm_signed ys)"
skalberg@14494
  1200
    by (simp add: xsys xsys' len)
skalberg@14494
  1201
  also have "... = ys"
skalberg@14494
  1202
    by (simp add: bv_extend_norm_signed)
skalberg@14494
  1203
  finally show ?thesis .
skalberg@14494
  1204
qed
skalberg@14494
  1205
skalberg@14494
  1206
lemma [simp]: "norm_signed (int_to_bv w) = int_to_bv w"
skalberg@14494
  1207
  by (simp add: int_to_bv_def)
skalberg@14494
  1208
skalberg@14494
  1209
lemma bv_to_int_msb0: "0 \<le> bv_to_int w1 ==> bv_msb w1 = \<zero>"
berghofe@15325
  1210
  by (rule bit_list_cases,simp_all)
skalberg@14494
  1211
skalberg@14494
  1212
lemma bv_to_int_msb1: "bv_to_int w1 < 0 ==> bv_msb w1 = \<one>"
berghofe@15325
  1213
  by (rule bit_list_cases,simp_all)
skalberg@14494
  1214
skalberg@14494
  1215
lemma bv_to_int_lower_limit_gt0:
skalberg@14494
  1216
  assumes w0: "0 < bv_to_int w"
skalberg@14494
  1217
  shows       "2 ^ (length (norm_signed w) - 2) \<le> bv_to_int w"
skalberg@14494
  1218
proof -
skalberg@14494
  1219
  from w0
skalberg@14494
  1220
  have "0 \<le> bv_to_int w"
skalberg@14494
  1221
    by simp
skalberg@14494
  1222
  hence [simp]: "bv_msb w = \<zero>"
skalberg@14494
  1223
    by (rule bv_to_int_msb0)
skalberg@14494
  1224
  have "2 ^ (length (norm_signed w) - 2) \<le> bv_to_int (norm_signed w)"
skalberg@14494
  1225
  proof (rule bit_list_cases [of w])
skalberg@14494
  1226
    assume "w = []"
skalberg@14494
  1227
    with w0
skalberg@14494
  1228
    show ?thesis
skalberg@14494
  1229
      by simp
skalberg@14494
  1230
  next
skalberg@14494
  1231
    fix w'
skalberg@14494
  1232
    assume weq: "w = \<zero> # w'"
skalberg@14494
  1233
    thus ?thesis
skalberg@14494
  1234
    proof (simp add: norm_signed_Cons,safe)
skalberg@14494
  1235
      assume "norm_unsigned w' = []"
skalberg@14494
  1236
      with weq and w0
skalberg@14494
  1237
      show False
skalberg@14494
  1238
	by (simp add: norm_empty_bv_to_nat_zero)
skalberg@14494
  1239
    next
skalberg@14494
  1240
      assume w'0: "norm_unsigned w' \<noteq> []"
skalberg@14494
  1241
      have "0 < bv_to_nat w'"
skalberg@14494
  1242
      proof (rule ccontr)
skalberg@14494
  1243
	assume "~ (0 < bv_to_nat w')"
berghofe@15325
  1244
	hence "bv_to_nat w' = 0"
skalberg@14494
  1245
	  by arith
skalberg@14494
  1246
	hence "norm_unsigned w' = []"
skalberg@14494
  1247
	  by (simp add: bv_to_nat_zero_imp_empty)
skalberg@14494
  1248
	with w'0
skalberg@14494
  1249
	show False
skalberg@14494
  1250
	  by simp
skalberg@14494
  1251
      qed
skalberg@14494
  1252
      with bv_to_nat_lower_limit [of w']
berghofe@15325
  1253
      show "2 ^ (length (norm_unsigned w') - Suc 0) \<le> int (bv_to_nat w')"
berghofe@15325
  1254
	by (simp add: int_nat_two_exp)
skalberg@14494
  1255
    qed
skalberg@14494
  1256
  next
skalberg@14494
  1257
    fix w'
skalberg@14494
  1258
    assume "w = \<one> # w'"
skalberg@14494
  1259
    from w0
skalberg@14494
  1260
    have "bv_msb w = \<zero>"
skalberg@14494
  1261
      by simp
skalberg@14494
  1262
    with prems
skalberg@14494
  1263
    show ?thesis
skalberg@14494
  1264
      by simp
skalberg@14494
  1265
  qed
skalberg@14494
  1266
  also have "...  = bv_to_int w"
skalberg@14494
  1267
    by simp
skalberg@14494
  1268
  finally show ?thesis .
skalberg@14494
  1269
qed
skalberg@14494
  1270
skalberg@14494
  1271
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
  1272
  apply (rule bit_list_cases [of w],simp_all)
skalberg@14494
  1273
  apply (case_tac "bs",simp_all)
skalberg@14494
  1274
  apply (case_tac "a",simp_all)
skalberg@14494
  1275
  apply (simp add: norm_signed_Cons)
skalberg@14494
  1276
  apply safe
skalberg@14494
  1277
  apply simp
skalberg@14494
  1278
proof -
skalberg@14494
  1279
  fix l
skalberg@14494
  1280
  assume msb: "\<zero> = bv_msb (norm_unsigned l)"
skalberg@14494
  1281
  assume "norm_unsigned l \<noteq> []"
skalberg@14494
  1282
  with norm_unsigned_result [of l]
skalberg@14494
  1283
  have "bv_msb (norm_unsigned l) = \<one>"
skalberg@14494
  1284
    by simp
skalberg@14494
  1285
  with msb
skalberg@14494
  1286
  show False
skalberg@14494
  1287
    by simp
skalberg@14494
  1288
next
skalberg@14494
  1289
  fix xs
skalberg@14494
  1290
  assume p: "\<one> = bv_msb (tl (norm_signed (\<one> # xs)))"
skalberg@14494
  1291
  have "\<one> \<noteq> bv_msb (tl (norm_signed (\<one> # xs)))"
skalberg@14494
  1292
    by (rule bit_list_induct [of _ xs],simp_all)
skalberg@14494
  1293
  with p
skalberg@14494
  1294
  show False
skalberg@14494
  1295
    by simp
skalberg@14494
  1296
qed
skalberg@14494
  1297
skalberg@14494
  1298
lemma bv_to_int_upper_limit_lem1:
skalberg@14494
  1299
  assumes w0: "bv_to_int w < -1"
skalberg@14494
  1300
  shows       "bv_to_int w < - (2 ^ (length (norm_signed w) - 2))"
skalberg@14494
  1301
proof -
skalberg@14494
  1302
  from w0
skalberg@14494
  1303
  have "bv_to_int w < 0"
skalberg@14494
  1304
    by simp
skalberg@14494
  1305
  hence msbw [simp]: "bv_msb w = \<one>"
skalberg@14494
  1306
    by (rule bv_to_int_msb1)
skalberg@14494
  1307
  have "bv_to_int w = bv_to_int (norm_signed w)"
skalberg@14494
  1308
    by simp
skalberg@14494
  1309
  also from norm_signed_result [of w]
skalberg@14494
  1310
  have "... < - (2 ^ (length (norm_signed w) - 2))"
skalberg@14494
  1311
  proof (safe)
skalberg@14494
  1312
    assume "norm_signed w = []"
skalberg@14494
  1313
    hence "bv_to_int (norm_signed w) = 0"
skalberg@14494
  1314
      by simp
skalberg@14494
  1315
    with w0
skalberg@14494
  1316
    show ?thesis
skalberg@14494
  1317
      by simp
skalberg@14494
  1318
  next
skalberg@14494
  1319
    assume "norm_signed w = [\<one>]"
skalberg@14494
  1320
    hence "bv_to_int (norm_signed w) = -1"
skalberg@14494
  1321
      by simp
skalberg@14494
  1322
    with w0
skalberg@14494
  1323
    show ?thesis
skalberg@14494
  1324
      by simp
skalberg@14494
  1325
  next
skalberg@14494
  1326
    assume "bv_msb (norm_signed w) \<noteq> bv_msb (tl (norm_signed w))"
skalberg@14494
  1327
    hence msb_tl: "\<one> \<noteq> bv_msb (tl (norm_signed w))"
skalberg@14494
  1328
      by simp
skalberg@14494
  1329
    show "bv_to_int (norm_signed w) < - (2 ^ (length (norm_signed w) - 2))"
skalberg@14494
  1330
    proof (rule bit_list_cases [of "norm_signed w"])
skalberg@14494
  1331
      assume "norm_signed w = []"
skalberg@14494
  1332
      hence "bv_to_int (norm_signed w) = 0"
skalberg@14494
  1333
	by simp
skalberg@14494
  1334
      with w0
skalberg@14494
  1335
      show ?thesis
skalberg@14494
  1336
	by simp
skalberg@14494
  1337
    next
skalberg@14494
  1338
      fix w'
skalberg@14494
  1339
      assume nw: "norm_signed w = \<zero> # w'"
skalberg@14494
  1340
      from msbw
skalberg@14494
  1341
      have "bv_msb (norm_signed w) = \<one>"
skalberg@14494
  1342
	by simp
skalberg@14494
  1343
      with nw
skalberg@14494
  1344
      show ?thesis
skalberg@14494
  1345
	by simp
skalberg@14494
  1346
    next
skalberg@14494
  1347
      fix w'
skalberg@14494
  1348
      assume weq: "norm_signed w = \<one> # w'"
skalberg@14494
  1349
      show ?thesis
skalberg@14494
  1350
      proof (rule bit_list_cases [of w'])
skalberg@14494
  1351
	assume w'eq: "w' = []"
skalberg@14494
  1352
	from w0
skalberg@14494
  1353
	have "bv_to_int (norm_signed w) < -1"
skalberg@14494
  1354
	  by simp
skalberg@14494
  1355
	with w'eq and weq
skalberg@14494
  1356
	show ?thesis
skalberg@14494
  1357
	  by simp
skalberg@14494
  1358
      next
skalberg@14494
  1359
	fix w''
skalberg@14494
  1360
	assume w'eq: "w' = \<zero> # w''"
skalberg@14494
  1361
	show ?thesis
skalberg@14494
  1362
	  apply (simp add: weq w'eq)
berghofe@15325
  1363
	  apply (subgoal_tac "- int (bv_to_nat (bv_not w'')) + -1 < 0 + 0")
berghofe@15325
  1364
	  apply (simp add: int_nat_two_exp)
skalberg@14494
  1365
	  apply (rule add_le_less_mono)
skalberg@14494
  1366
	  apply simp_all
skalberg@14494
  1367
	  done
skalberg@14494
  1368
      next
skalberg@14494
  1369
	fix w''
skalberg@14494
  1370
	assume w'eq: "w' = \<one> # w''"
skalberg@14494
  1371
	with weq and msb_tl
skalberg@14494
  1372
	show ?thesis
skalberg@14494
  1373
	  by simp
skalberg@14494
  1374
      qed
skalberg@14494
  1375
    qed
skalberg@14494
  1376
  qed
skalberg@14494
  1377
  finally show ?thesis .
skalberg@14494
  1378
qed
skalberg@14494
  1379
skalberg@14494
  1380
lemma length_int_to_bv_upper_limit_gt0:
skalberg@14494
  1381
  assumes w0: "0 < i"
skalberg@14494
  1382
  and     wk: "i \<le> 2 ^ (k - 1) - 1"
skalberg@14494
  1383
  shows       "length (int_to_bv i) \<le> k"
skalberg@14494
  1384
proof (rule ccontr)
skalberg@14494
  1385
  from w0 wk
skalberg@14494
  1386
  have k1: "1 < k"
skalberg@14494
  1387
    by (cases "k - 1",simp_all,arith)
skalberg@14494
  1388
  assume "~ length (int_to_bv i) \<le> k"
skalberg@14494
  1389
  hence "k < length (int_to_bv i)"
skalberg@14494
  1390
    by simp
skalberg@14494
  1391
  hence "k \<le> length (int_to_bv i) - 1"
skalberg@14494
  1392
    by arith
skalberg@14494
  1393
  hence a: "k - 1 \<le> length (int_to_bv i) - 2"
skalberg@14494
  1394
    by arith
paulson@15067
  1395
  hence "(2::int) ^ (k - 1) \<le> 2 ^ (length (int_to_bv i) - 2)" by simp
skalberg@14494
  1396
  also have "... \<le> i"
skalberg@14494
  1397
  proof -
skalberg@14494
  1398
    have "2 ^ (length (norm_signed (int_to_bv i)) - 2) \<le> bv_to_int (int_to_bv i)"
skalberg@14494
  1399
    proof (rule bv_to_int_lower_limit_gt0)
skalberg@14494
  1400
      from w0
skalberg@14494
  1401
      show "0 < bv_to_int (int_to_bv i)"
skalberg@14494
  1402
	by simp
skalberg@14494
  1403
    qed
skalberg@14494
  1404
    thus ?thesis
skalberg@14494
  1405
      by simp
skalberg@14494
  1406
  qed
skalberg@14494
  1407
  finally have "2 ^ (k - 1) \<le> i" .
skalberg@14494
  1408
  with wk
skalberg@14494
  1409
  show False
skalberg@14494
  1410
    by simp
skalberg@14494
  1411
qed
skalberg@14494
  1412
skalberg@14494
  1413
lemma pos_length_pos:
skalberg@14494
  1414
  assumes i0: "0 < bv_to_int w"
skalberg@14494
  1415
  shows       "0 < length w"
skalberg@14494
  1416
proof -
skalberg@14494
  1417
  from norm_signed_result [of w]
skalberg@14494
  1418
  have "0 < length (norm_signed w)"
skalberg@14494
  1419
  proof (auto)
skalberg@14494
  1420
    assume ii: "norm_signed w = []"
skalberg@14494
  1421
    have "bv_to_int (norm_signed w) = 0"
skalberg@14494
  1422
      by (subst ii,simp)
skalberg@14494
  1423
    hence "bv_to_int w = 0"
skalberg@14494
  1424
      by simp
skalberg@14494
  1425
    with i0
skalberg@14494
  1426
    show False
skalberg@14494
  1427
      by simp
skalberg@14494
  1428
  next
skalberg@14494
  1429
    assume ii: "norm_signed w = []"
skalberg@14494
  1430
    assume jj: "bv_msb w \<noteq> \<zero>"
skalberg@14494
  1431
    have "\<zero> = bv_msb (norm_signed w)"
skalberg@14494
  1432
      by (subst ii,simp)
skalberg@14494
  1433
    also have "... \<noteq> \<zero>"
skalberg@14494
  1434
      by (simp add: jj)
skalberg@14494
  1435
    finally show False by simp
skalberg@14494
  1436
  qed
skalberg@14494
  1437
  also have "... \<le> length w"
skalberg@14494
  1438
    by (rule norm_signed_length)
skalberg@14494
  1439
  finally show ?thesis
skalberg@14494
  1440
    .
skalberg@14494
  1441
qed
skalberg@14494
  1442
skalberg@14494
  1443
lemma neg_length_pos:
skalberg@14494
  1444
  assumes i0: "bv_to_int w < -1"
skalberg@14494
  1445
  shows       "0 < length w"
skalberg@14494
  1446
proof -
skalberg@14494
  1447
  from norm_signed_result [of w]
skalberg@14494
  1448
  have "0 < length (norm_signed w)"
skalberg@14494
  1449
  proof (auto)
skalberg@14494
  1450
    assume ii: "norm_signed w = []"
skalberg@14494
  1451
    have "bv_to_int (norm_signed w) = 0"
skalberg@14494
  1452
      by (subst ii,simp)
skalberg@14494
  1453
    hence "bv_to_int w = 0"
skalberg@14494
  1454
      by simp
skalberg@14494
  1455
    with i0
skalberg@14494
  1456
    show False
skalberg@14494
  1457
      by simp
skalberg@14494
  1458
  next
skalberg@14494
  1459
    assume ii: "norm_signed w = []"
skalberg@14494
  1460
    assume jj: "bv_msb w \<noteq> \<zero>"
skalberg@14494
  1461
    have "\<zero> = bv_msb (norm_signed w)"
skalberg@14494
  1462
      by (subst ii,simp)
skalberg@14494
  1463
    also have "... \<noteq> \<zero>"
skalberg@14494
  1464
      by (simp add: jj)
skalberg@14494
  1465
    finally show False by simp
skalberg@14494
  1466
  qed
skalberg@14494
  1467
  also have "... \<le> length w"
skalberg@14494
  1468
    by (rule norm_signed_length)
skalberg@14494
  1469
  finally show ?thesis
skalberg@14494
  1470
    .
skalberg@14494
  1471
qed
skalberg@14494
  1472
skalberg@14494
  1473
lemma length_int_to_bv_lower_limit_gt0:
skalberg@14494
  1474
  assumes wk: "2 ^ (k - 1) \<le> i"
skalberg@14494
  1475
  shows       "k < length (int_to_bv i)"
skalberg@14494
  1476
proof (rule ccontr)
skalberg@14494
  1477
  have "0 < (2::int) ^ (k - 1)"
paulson@15067
  1478
    by (rule zero_less_power,simp)
skalberg@14494
  1479
  also have "... \<le> i"
skalberg@14494
  1480
    by (rule wk)
skalberg@14494
  1481
  finally have i0: "0 < i"
skalberg@14494
  1482
    .
skalberg@14494
  1483
  have lii0: "0 < length (int_to_bv i)"
skalberg@14494
  1484
    apply (rule pos_length_pos)
skalberg@14494
  1485
    apply (simp,rule i0)
skalberg@14494
  1486
    done
skalberg@14494
  1487
  assume "~ k < length (int_to_bv i)"
skalberg@14494
  1488
  hence "length (int_to_bv i) \<le> k"
skalberg@14494
  1489
    by simp
skalberg@14494
  1490
  with lii0
skalberg@14494
  1491
  have a: "length (int_to_bv i) - 1 \<le> k - 1"
skalberg@14494
  1492
    by arith
skalberg@14494
  1493
  have "i < 2 ^ (length (int_to_bv i) - 1)"
skalberg@14494
  1494
  proof -
skalberg@14494
  1495
    have "i = bv_to_int (int_to_bv i)"
skalberg@14494
  1496
      by simp
skalberg@14494
  1497
    also have "... < 2 ^ (length (int_to_bv i) - 1)"
skalberg@14494
  1498
      by (rule bv_to_int_upper_range)
skalberg@14494
  1499
    finally show ?thesis .
skalberg@14494
  1500
  qed
paulson@15067
  1501
  also have "(2::int) ^ (length (int_to_bv i) - 1) \<le> 2 ^ (k - 1)" using a
paulson@15067
  1502
         by simp
skalberg@14494
  1503
  finally have "i < 2 ^ (k - 1)" .
skalberg@14494
  1504
  with wk
skalberg@14494
  1505
  show False
skalberg@14494
  1506
    by simp
skalberg@14494
  1507
qed
skalberg@14494
  1508
skalberg@14494
  1509
lemma length_int_to_bv_upper_limit_lem1:
skalberg@14494
  1510
  assumes w1: "i < -1"
skalberg@14494
  1511
  and     wk: "- (2 ^ (k - 1)) \<le> i"
skalberg@14494
  1512
  shows       "length (int_to_bv i) \<le> k"
skalberg@14494
  1513
proof (rule ccontr)
skalberg@14494
  1514
  from w1 wk
skalberg@14494
  1515
  have k1: "1 < k"
skalberg@14494
  1516
    by (cases "k - 1",simp_all,arith)
skalberg@14494
  1517
  assume "~ length (int_to_bv i) \<le> k"
skalberg@14494
  1518
  hence "k < length (int_to_bv i)"
skalberg@14494
  1519
    by simp
skalberg@14494
  1520
  hence "k \<le> length (int_to_bv i) - 1"
skalberg@14494
  1521
    by arith
skalberg@14494
  1522
  hence a: "k - 1 \<le> length (int_to_bv i) - 2"
skalberg@14494
  1523
    by arith
skalberg@14494
  1524
  have "i < - (2 ^ (length (int_to_bv i) - 2))"
skalberg@14494
  1525
  proof -
skalberg@14494
  1526
    have "i = bv_to_int (int_to_bv i)"
skalberg@14494
  1527
      by simp
skalberg@14494
  1528
    also have "... < - (2 ^ (length (norm_signed (int_to_bv i)) - 2))"
skalberg@14494
  1529
      by (rule bv_to_int_upper_limit_lem1,simp,rule w1)
skalberg@14494
  1530
    finally show ?thesis by simp
skalberg@14494
  1531
  qed
skalberg@14494
  1532
  also have "... \<le> -(2 ^ (k - 1))"
skalberg@14494
  1533
  proof -
paulson@15067
  1534
    have "(2::int) ^ (k - 1) \<le> 2 ^ (length (int_to_bv i) - 2)" using a
paulson@15067
  1535
      by simp
skalberg@14494
  1536
    thus ?thesis
skalberg@14494
  1537
      by simp
skalberg@14494
  1538
  qed
skalberg@14494
  1539
  finally have "i < -(2 ^ (k - 1))" .
skalberg@14494
  1540
  with wk
skalberg@14494
  1541
  show False
skalberg@14494
  1542
    by simp
skalberg@14494
  1543
qed
skalberg@14494
  1544
skalberg@14494
  1545
lemma length_int_to_bv_lower_limit_lem1:
skalberg@14494
  1546
  assumes wk: "i < -(2 ^ (k - 1))"
skalberg@14494
  1547
  shows       "k < length (int_to_bv i)"
skalberg@14494
  1548
proof (rule ccontr)
skalberg@14494
  1549
  from wk
skalberg@14494
  1550
  have "i \<le> -(2 ^ (k - 1)) - 1"
skalberg@14494
  1551
    by simp
skalberg@14494
  1552
  also have "... < -1"
skalberg@14494
  1553
  proof -
skalberg@14494
  1554
    have "0 < (2::int) ^ (k - 1)"
paulson@15067
  1555
      by (rule zero_less_power,simp)
skalberg@14494
  1556
    hence "-((2::int) ^ (k - 1)) < 0"
skalberg@14494
  1557
      by simp
skalberg@14494
  1558
    thus ?thesis by simp
skalberg@14494
  1559
  qed
skalberg@14494
  1560
  finally have i1: "i < -1" .
skalberg@14494
  1561
  have lii0: "0 < length (int_to_bv i)"
skalberg@14494
  1562
    apply (rule neg_length_pos)
skalberg@14494
  1563
    apply (simp,rule i1)
skalberg@14494
  1564
    done
skalberg@14494
  1565
  assume "~ k < length (int_to_bv i)"
skalberg@14494
  1566
  hence "length (int_to_bv i) \<le> k"
skalberg@14494
  1567
    by simp
skalberg@14494
  1568
  with lii0
skalberg@14494
  1569
  have a: "length (int_to_bv i) - 1 \<le> k - 1"
skalberg@14494
  1570
    by arith
paulson@15067
  1571
  hence "(2::int) ^ (length (int_to_bv i) - 1) \<le> 2 ^ (k - 1)" by simp
skalberg@14494
  1572
  hence "-((2::int) ^ (k - 1)) \<le> - (2 ^ (length (int_to_bv i) - 1))"
skalberg@14494
  1573
    by simp
skalberg@14494
  1574
  also have "... \<le> i"
skalberg@14494
  1575
  proof -
skalberg@14494
  1576
    have "- (2 ^ (length (int_to_bv i) - 1)) \<le> bv_to_int (int_to_bv i)"
skalberg@14494
  1577
      by (rule bv_to_int_lower_range)
skalberg@14494
  1578
    also have "... = i"
skalberg@14494
  1579
      by simp
skalberg@14494
  1580
    finally show ?thesis .
skalberg@14494
  1581
  qed
skalberg@14494
  1582
  finally have "-(2 ^ (k - 1)) \<le> i" .
skalberg@14494
  1583
  with wk
skalberg@14494
  1584
  show False
skalberg@14494
  1585
    by simp
skalberg@14494
  1586
qed
skalberg@14494
  1587
wenzelm@14589
  1588
subsection {* Signed Arithmetic Operations *}
skalberg@14494
  1589
wenzelm@14589
  1590
subsubsection {* Conversion from unsigned to signed *}
skalberg@14494
  1591
skalberg@14494
  1592
constdefs
skalberg@14494
  1593
  utos :: "bit list => bit list"
skalberg@14494
  1594
  "utos w == norm_signed (\<zero> # w)"
skalberg@14494
  1595
skalberg@14494
  1596
lemma [simp]: "utos (norm_unsigned w) = utos w"
skalberg@14494
  1597
  by (simp add: utos_def norm_signed_Cons)
skalberg@14494
  1598
skalberg@14494
  1599
lemma [simp]: "norm_signed (utos w) = utos w"
skalberg@14494
  1600
  by (simp add: utos_def)
skalberg@14494
  1601
skalberg@14494
  1602
lemma utos_length: "length (utos w) \<le> Suc (length w)"
skalberg@14494
  1603
  by (simp add: utos_def norm_signed_Cons)
skalberg@14494
  1604
berghofe@15325
  1605
lemma bv_to_int_utos: "bv_to_int (utos w) = int (bv_to_nat w)"
skalberg@14494
  1606
proof (simp add: utos_def norm_signed_Cons,safe)
skalberg@14494
  1607
  assume "norm_unsigned w = []"
skalberg@14494
  1608
  hence "bv_to_nat (norm_unsigned w) = 0"
skalberg@14494
  1609
    by simp
skalberg@14494
  1610
  thus "bv_to_nat w = 0"
skalberg@14494
  1611
    by simp
skalberg@14494
  1612
qed
skalberg@14494
  1613
wenzelm@14589
  1614
subsubsection {* Unary minus *}
skalberg@14494
  1615
skalberg@14494
  1616
constdefs
skalberg@14494
  1617
  bv_uminus :: "bit list => bit list"
skalberg@14494
  1618
  "bv_uminus w == int_to_bv (- bv_to_int w)"
skalberg@14494
  1619
skalberg@14494
  1620
lemma [simp]: "bv_uminus (norm_signed w) = bv_uminus w"
skalberg@14494
  1621
  by (simp add: bv_uminus_def)
skalberg@14494
  1622
skalberg@14494
  1623
lemma [simp]: "norm_signed (bv_uminus w) = bv_uminus w"
skalberg@14494
  1624
  by (simp add: bv_uminus_def)
skalberg@14494
  1625
skalberg@14494
  1626
lemma bv_uminus_length: "length (bv_uminus w) \<le> Suc (length w)"
skalberg@14494
  1627
proof -
skalberg@14494
  1628
  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
  1629
    by arith
skalberg@14494
  1630
  thus ?thesis
skalberg@14494
  1631
  proof safe
skalberg@14494
  1632
    assume p: "1 < - bv_to_int w"
skalberg@14494
  1633
    have lw: "0 < length w"
skalberg@14494
  1634
      apply (rule neg_length_pos)
skalberg@14494
  1635
      using p
skalberg@14494
  1636
      apply simp
skalberg@14494
  1637
      done
skalberg@14494
  1638
    show ?thesis
skalberg@14494
  1639
    proof (simp add: bv_uminus_def,rule length_int_to_bv_upper_limit_gt0,simp_all)
skalberg@14494
  1640
      from prems
skalberg@14494
  1641
      show "bv_to_int w < 0"
skalberg@14494
  1642
	by simp
skalberg@14494
  1643
    next
skalberg@14494
  1644
      have "-(2^(length w - 1)) \<le> bv_to_int w"
skalberg@14494
  1645
	by (rule bv_to_int_lower_range)
skalberg@14494
  1646
      hence "- bv_to_int w \<le> 2^(length w - 1)"
skalberg@14494
  1647
	by simp
skalberg@14494
  1648
      also from lw have "... < 2 ^ length w"
skalberg@14494
  1649
	by simp
skalberg@14494
  1650
      finally show "- bv_to_int w < 2 ^ length w"
skalberg@14494
  1651
	by simp
skalberg@14494
  1652
    qed
skalberg@14494
  1653
  next
skalberg@14494
  1654
    assume p: "- bv_to_int w = 1"
skalberg@14494
  1655
    hence lw: "0 < length w"
skalberg@14494
  1656
      by (cases w,simp_all)
skalberg@14494
  1657
    from p
skalberg@14494
  1658
    show ?thesis
skalberg@14494
  1659
      apply (simp add: bv_uminus_def)
skalberg@14494
  1660
      using lw
skalberg@14494
  1661
      apply (simp (no_asm) add: nat_to_bv_non0)
skalberg@14494
  1662
      done
skalberg@14494
  1663
  next
skalberg@14494
  1664
    assume "- bv_to_int w = 0"
skalberg@14494
  1665
    thus ?thesis
skalberg@14494
  1666
      by (simp add: bv_uminus_def)
skalberg@14494
  1667
  next
skalberg@14494
  1668
    assume p: "- bv_to_int w = -1"
skalberg@14494
  1669
    thus ?thesis
skalberg@14494
  1670
      by (simp add: bv_uminus_def)
skalberg@14494
  1671
  next
skalberg@14494
  1672
    assume p: "- bv_to_int w < -1"
skalberg@14494
  1673
    show ?thesis
skalberg@14494
  1674
      apply (simp add: bv_uminus_def)
skalberg@14494
  1675
      apply (rule length_int_to_bv_upper_limit_lem1)
skalberg@14494
  1676
      apply (rule p)
skalberg@14494
  1677
      apply simp
skalberg@14494
  1678
    proof -
skalberg@14494
  1679
      have "bv_to_int w < 2 ^ (length w - 1)"
skalberg@14494
  1680
	by (rule bv_to_int_upper_range)
paulson@15067
  1681
      also have "... \<le> 2 ^ length w" by simp
skalberg@14494
  1682
      finally show "bv_to_int w \<le> 2 ^ length w"
skalberg@14494
  1683
	by simp
skalberg@14494
  1684
    qed
skalberg@14494
  1685
  qed
skalberg@14494
  1686
qed
skalberg@14494
  1687
skalberg@14494
  1688
lemma bv_uminus_length_utos: "length (bv_uminus (utos w)) \<le> Suc (length w)"
skalberg@14494
  1689
proof -
skalberg@14494
  1690
  have "-bv_to_int (utos w) = 0 \<or> -bv_to_int (utos w) = -1 \<or> -bv_to_int (utos w) < -1"
skalberg@14494
  1691
    apply (simp add: bv_to_int_utos)
skalberg@14494
  1692
    by arith
skalberg@14494
  1693
  thus ?thesis
skalberg@14494
  1694
  proof safe
skalberg@14494
  1695
    assume "-bv_to_int (utos w) = 0"
skalberg@14494
  1696
    thus ?thesis
skalberg@14494
  1697
      by (simp add: bv_uminus_def)
skalberg@14494
  1698
  next
skalberg@14494
  1699
    assume "-bv_to_int (utos w) = -1"
skalberg@14494
  1700
    thus ?thesis
skalberg@14494
  1701
      by (simp add: bv_uminus_def)
skalberg@14494
  1702
  next
skalberg@14494
  1703
    assume p: "-bv_to_int (utos w) < -1"
skalberg@14494
  1704
    show ?thesis
skalberg@14494
  1705
      apply (simp add: bv_uminus_def)
skalberg@14494
  1706
      apply (rule length_int_to_bv_upper_limit_lem1)
skalberg@14494
  1707
      apply (rule p)
skalberg@14494
  1708
      apply (simp add: bv_to_int_utos)
skalberg@14494
  1709
      using bv_to_nat_upper_range [of w]
berghofe@15325
  1710
      apply (simp add: int_nat_two_exp)
skalberg@14494
  1711
      done
skalberg@14494
  1712
  qed
skalberg@14494
  1713
qed
skalberg@14494
  1714
skalberg@14494
  1715
constdefs
skalberg@14494
  1716
  bv_sadd :: "[bit list, bit list ] => bit list"
skalberg@14494
  1717
  "bv_sadd w1 w2 == int_to_bv (bv_to_int w1 + bv_to_int w2)"
skalberg@14494
  1718
skalberg@14494
  1719
lemma [simp]: "bv_sadd (norm_signed w1) w2 = bv_sadd w1 w2"
skalberg@14494
  1720
  by (simp add: bv_sadd_def)
skalberg@14494
  1721
skalberg@14494
  1722
lemma [simp]: "bv_sadd w1 (norm_signed w2) = bv_sadd w1 w2"
skalberg@14494
  1723
  by (simp add: bv_sadd_def)
skalberg@14494
  1724
skalberg@14494
  1725
lemma [simp]: "norm_signed (bv_sadd w1 w2) = bv_sadd w1 w2"
skalberg@14494
  1726
  by (simp add: bv_sadd_def)
skalberg@14494
  1727
skalberg@14494
  1728
lemma adder_helper:
skalberg@14494
  1729
  assumes lw: "0 < max (length w1) (length w2)"
skalberg@14494
  1730
  shows   "((2::int) ^ (length w1 - 1)) + (2 ^ (length w2 - 1)) \<le> 2 ^ max (length w1) (length w2)"
skalberg@14494
  1731
proof -
skalberg@14494
  1732
  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
  1733
    apply (cases "length w1 \<le> length w2")
skalberg@14494
  1734
    apply (auto simp add: max_def)
skalberg@14494
  1735
    apply arith
skalberg@14494
  1736
    apply arith
skalberg@14494
  1737
    done
skalberg@14494
  1738
  also have "... = 2 ^ max (length w1) (length w2)"
skalberg@14494
  1739
  proof -
skalberg@14494
  1740
    from lw
skalberg@14494
  1741
    show ?thesis
skalberg@14494
  1742
      apply simp
skalberg@14494
  1743
      apply (subst power_Suc [symmetric])
skalberg@14494
  1744
      apply (simp del: power.simps)
skalberg@14494
  1745
      done
skalberg@14494
  1746
  qed
skalberg@14494
  1747
  finally show ?thesis .
skalberg@14494
  1748
qed
skalberg@14494
  1749
skalberg@14494
  1750
lemma bv_sadd_length: "length (bv_sadd w1 w2) \<le> Suc (max (length w1) (length w2))"
skalberg@14494
  1751
proof -
skalberg@14494
  1752
  let ?Q = "bv_to_int w1 + bv_to_int w2"
skalberg@14494
  1753
skalberg@14494
  1754
  have helper: "?Q \<noteq> 0 ==> 0 < max (length w1) (length w2)"
skalberg@14494
  1755
  proof -
skalberg@14494
  1756
    assume p: "?Q \<noteq> 0"
skalberg@14494
  1757
    show "0 < max (length w1) (length w2)"
skalberg@14494
  1758
    proof (simp add: less_max_iff_disj,rule)
skalberg@14494
  1759
      assume [simp]: "w1 = []"
skalberg@14494
  1760
      show "w2 \<noteq> []"
skalberg@14494
  1761
      proof (rule ccontr,simp)
skalberg@14494
  1762
	assume [simp]: "w2 = []"
skalberg@14494
  1763
	from p
skalberg@14494
  1764
	show False
skalberg@14494
  1765
	  by simp
skalberg@14494
  1766
      qed
skalberg@14494
  1767
    qed
skalberg@14494
  1768
  qed
skalberg@14494
  1769
skalberg@14494
  1770
  have "0 < ?Q \<or> ?Q = 0 \<or> ?Q = -1 \<or> ?Q < -1"
skalberg@14494
  1771
    by arith
skalberg@14494
  1772
  thus ?thesis
skalberg@14494
  1773
  proof safe
skalberg@14494
  1774
    assume "?Q = 0"
skalberg@14494
  1775
    thus ?thesis
skalberg@14494
  1776
      by (simp add: bv_sadd_def)
skalberg@14494
  1777
  next
skalberg@14494
  1778
    assume "?Q = -1"
skalberg@14494
  1779
    thus ?thesis
skalberg@14494
  1780
      by (simp add: bv_sadd_def)
skalberg@14494
  1781
  next
skalberg@14494
  1782
    assume p: "0 < ?Q"
skalberg@14494
  1783
    show ?thesis
skalberg@14494
  1784
      apply (simp add: bv_sadd_def)
skalberg@14494
  1785
      apply (rule length_int_to_bv_upper_limit_gt0)
skalberg@14494
  1786
      apply (rule p)
skalberg@14494
  1787
    proof simp
skalberg@14494
  1788
      from bv_to_int_upper_range [of w2]
skalberg@14494
  1789
      have "bv_to_int w2 \<le> 2 ^ (length w2 - 1)"
skalberg@14494
  1790
	by simp
skalberg@14494
  1791
      with bv_to_int_upper_range [of w1]
skalberg@14494
  1792
      have "bv_to_int w1 + bv_to_int w2 < (2 ^ (length w1 - 1)) + (2 ^ (length w2 - 1))"
skalberg@14494
  1793
	by (rule zadd_zless_mono)
skalberg@14494
  1794
      also have "... \<le> 2 ^ max (length w1) (length w2)"
skalberg@14494
  1795
	apply (rule adder_helper)
skalberg@14494
  1796
	apply (rule helper)
skalberg@14494
  1797
	using p
skalberg@14494
  1798
	apply simp
skalberg@14494
  1799
	done
skalberg@14494
  1800
      finally show "?Q < 2 ^ max (length w1) (length w2)"
skalberg@14494
  1801
	.
skalberg@14494
  1802
    qed
skalberg@14494
  1803
  next
skalberg@14494
  1804
    assume p: "?Q < -1"
skalberg@14494
  1805
    show ?thesis
skalberg@14494
  1806
      apply (simp add: bv_sadd_def)
skalberg@14494
  1807
      apply (rule length_int_to_bv_upper_limit_lem1,simp_all)
skalberg@14494
  1808
      apply (rule p)
skalberg@14494
  1809
    proof -
skalberg@14494
  1810
      have "(2 ^ (length w1 - 1)) + 2 ^ (length w2 - 1) \<le> (2::int) ^ max (length w1) (length w2)"
skalberg@14494
  1811
	apply (rule adder_helper)
skalberg@14494
  1812
	apply (rule helper)
skalberg@14494
  1813
	using p
skalberg@14494
  1814
	apply simp
skalberg@14494
  1815
	done
skalberg@14494
  1816
      hence "-((2::int) ^ max (length w1) (length w2)) \<le> - (2 ^ (length w1 - 1)) + -(2 ^ (length w2 - 1))"
skalberg@14494
  1817
	by simp
skalberg@14494
  1818
      also have "- (2 ^ (length w1 - 1)) + -(2 ^ (length w2 - 1)) \<le> ?Q"
skalberg@14494
  1819
	apply (rule add_mono)
skalberg@14494
  1820
	apply (rule bv_to_int_lower_range [of w1])
skalberg@14494
  1821
	apply (rule bv_to_int_lower_range [of w2])
skalberg@14494
  1822
	done
skalberg@14494
  1823
      finally show "- (2^max (length w1) (length w2)) \<le> ?Q" .
skalberg@14494
  1824
    qed
skalberg@14494
  1825
  qed
skalberg@14494
  1826
qed
skalberg@14494
  1827
skalberg@14494
  1828
constdefs
skalberg@14494
  1829
  bv_sub :: "[bit list, bit list] => bit list"
skalberg@14494
  1830
  "bv_sub w1 w2 == bv_sadd w1 (bv_uminus w2)"
skalberg@14494
  1831
skalberg@14494
  1832
lemma [simp]: "bv_sub (norm_signed w1) w2 = bv_sub w1 w2"
skalberg@14494
  1833
  by (simp add: bv_sub_def)
skalberg@14494
  1834
skalberg@14494
  1835
lemma [simp]: "bv_sub w1 (norm_signed w2) = bv_sub w1 w2"
skalberg@14494
  1836
  by (simp add: bv_sub_def)
skalberg@14494
  1837
skalberg@14494
  1838
lemma [simp]: "norm_signed (bv_sub w1 w2) = bv_sub w1 w2"
skalberg@14494
  1839
  by (simp add: bv_sub_def)
skalberg@14494
  1840
skalberg@14494
  1841
lemma bv_sub_length: "length (bv_sub w1 w2) \<le> Suc (max (length w1) (length w2))"
skalberg@14494
  1842
proof (cases "bv_to_int w2 = 0")
skalberg@14494
  1843
  assume p: "bv_to_int w2 = 0"
skalberg@14494
  1844
  show ?thesis
skalberg@14494
  1845
  proof (simp add: bv_sub_def bv_sadd_def bv_uminus_def p)
skalberg@14494
  1846
    have "length (norm_signed w1) \<le> length w1"
skalberg@14494
  1847
      by (rule norm_signed_length)
skalberg@14494
  1848
    also have "... \<le> max (length w1) (length w2)"
skalberg@14494
  1849
      by (rule le_maxI1)
skalberg@14494
  1850
    also have "... \<le> Suc (max (length w1) (length w2))"
skalberg@14494
  1851
      by arith
skalberg@14494
  1852
    finally show "length (norm_signed w1) \<le> Suc (max (length w1) (length w2))"
skalberg@14494
  1853
      .
skalberg@14494
  1854
  qed
skalberg@14494
  1855
next
skalberg@14494
  1856
  assume "bv_to_int w2 \<noteq> 0"
skalberg@14494
  1857
  hence "0 < length w2"
skalberg@14494
  1858
    by (cases w2,simp_all)
skalberg@14494
  1859
  hence lmw: "0 < max (length w1) (length w2)"
skalberg@14494
  1860
    by arith
skalberg@14494
  1861
skalberg@14494
  1862
  let ?Q = "bv_to_int w1 - bv_to_int w2"
skalberg@14494
  1863
skalberg@14494
  1864
  have "0 < ?Q \<or> ?Q = 0 \<or> ?Q = -1 \<or> ?Q < -1"
skalberg@14494
  1865
    by arith
skalberg@14494
  1866
  thus ?thesis
skalberg@14494
  1867
  proof safe
skalberg@14494
  1868
    assume "?Q = 0"
skalberg@14494
  1869
    thus ?thesis
skalberg@14494
  1870
      by (simp add: bv_sub_def bv_sadd_def bv_uminus_def)
skalberg@14494
  1871
  next
skalberg@14494
  1872
    assume "?Q = -1"
skalberg@14494
  1873
    thus ?thesis
skalberg@14494
  1874
      by (simp add: bv_sub_def bv_sadd_def bv_uminus_def)
skalberg@14494
  1875
  next
skalberg@14494
  1876
    assume p: "0 < ?Q"
skalberg@14494
  1877
    show ?thesis
skalberg@14494
  1878
      apply (simp add: bv_sub_def bv_sadd_def bv_uminus_def)
skalberg@14494
  1879
      apply (rule length_int_to_bv_upper_limit_gt0)
skalberg@14494
  1880
      apply (rule p)
skalberg@14494
  1881
    proof simp
skalberg@14494
  1882
      from bv_to_int_lower_range [of w2]
skalberg@14494
  1883
      have v2: "- bv_to_int w2 \<le> 2 ^ (length w2 - 1)"
skalberg@14494
  1884
	by simp
skalberg@14494
  1885
      have "bv_to_int w1 + - bv_to_int w2 < (2 ^ (length w1 - 1)) + (2 ^ (length w2 - 1))"
skalberg@14494
  1886
	apply (rule zadd_zless_mono)
skalberg@14494
  1887
	apply (rule bv_to_int_upper_range [of w1])
skalberg@14494
  1888
	apply (rule v2)
skalberg@14494
  1889
	done
skalberg@14494
  1890
      also have "... \<le> 2 ^ max (length w1) (length w2)"
skalberg@14494
  1891
	apply (rule adder_helper)
skalberg@14494
  1892
	apply (rule lmw)
skalberg@14494
  1893
	done
skalberg@14494
  1894
      finally show "?Q < 2 ^ max (length w1) (length w2)"
skalberg@14494
  1895
	by simp
skalberg@14494
  1896
    qed
skalberg@14494
  1897
  next
skalberg@14494
  1898
    assume p: "?Q < -1"
skalberg@14494
  1899
    show ?thesis
skalberg@14494
  1900
      apply (simp add: bv_sub_def bv_sadd_def bv_uminus_def)
skalberg@14494
  1901
      apply (rule length_int_to_bv_upper_limit_lem1)
skalberg@14494
  1902
      apply (rule p)
skalberg@14494
  1903
    proof simp
skalberg@14494
  1904
      have "(2 ^ (length w1 - 1)) + 2 ^ (length w2 - 1) \<le> (2::int) ^ max (length w1) (length w2)"
skalberg@14494
  1905
	apply (rule adder_helper)
skalberg@14494
  1906
	apply (rule lmw)
skalberg@14494
  1907
	done
skalberg@14494
  1908
      hence "-((2::int) ^ max (length w1) (length w2)) \<le> - (2 ^ (length w1 - 1)) + -(2 ^ (length w2 - 1))"
skalberg@14494
  1909
	by simp
skalberg@14494
  1910
      also have "- (2 ^ (length w1 - 1)) + -(2 ^ (length w2 - 1)) \<le> bv_to_int w1 + -bv_to_int w2"
skalberg@14494
  1911
	apply (rule add_mono)
skalberg@14494
  1912
	apply (rule bv_to_int_lower_range [of w1])
skalberg@14494
  1913
	using bv_to_int_upper_range [of w2]
skalberg@14494
  1914
	apply simp
skalberg@14494
  1915
	done
skalberg@14494
  1916
      finally show "- (2^max (length w1) (length w2)) \<le> ?Q"
skalberg@14494
  1917
	by simp
skalberg@14494
  1918
    qed
skalberg@14494
  1919
  qed
skalberg@14494
  1920
qed
skalberg@14494
  1921
skalberg@14494
  1922
constdefs
skalberg@14494
  1923
  bv_smult :: "[bit list, bit list] => bit list"
skalberg@14494
  1924
  "bv_smult w1 w2 == int_to_bv (bv_to_int w1 * bv_to_int w2)"
skalberg@14494
  1925
skalberg@14494
  1926
lemma [simp]: "bv_smult (norm_signed w1) w2 = bv_smult w1 w2"
skalberg@14494
  1927
  by (simp add: bv_smult_def)
skalberg@14494
  1928
skalberg@14494
  1929
lemma [simp]: "bv_smult w1 (norm_signed w2) = bv_smult w1 w2"
skalberg@14494
  1930
  by (simp add: bv_smult_def)
skalberg@14494
  1931
skalberg@14494
  1932
lemma [simp]: "norm_signed (bv_smult w1 w2) = bv_smult w1 w2"
skalberg@14494
  1933
  by (simp add: bv_smult_def)
skalberg@14494
  1934
skalberg@14494
  1935
lemma bv_smult_length: "length (bv_smult w1 w2) \<le> length w1 + length w2"
skalberg@14494
  1936
proof -
skalberg@14494
  1937
  let ?Q = "bv_to_int w1 * bv_to_int w2"
skalberg@14494
  1938
skalberg@14494
  1939
  have lmw: "?Q \<noteq> 0 ==> 0 < length w1 \<and> 0 < length w2"
skalberg@14494
  1940
    by auto
skalberg@14494
  1941
skalberg@14494
  1942
  have "0 < ?Q \<or> ?Q = 0 \<or> ?Q = -1 \<or> ?Q < -1"
skalberg@14494
  1943
    by arith
skalberg@14494
  1944
  thus ?thesis
skalberg@14494
  1945
  proof (safe dest!: iffD1 [OF mult_eq_0_iff])
skalberg@14494
  1946
    assume "bv_to_int w1 = 0"
skalberg@14494
  1947
    thus ?thesis
skalberg@14494
  1948
      by (simp add: bv_smult_def)
skalberg@14494
  1949
  next
skalberg@14494
  1950
    assume "bv_to_int w2 = 0"
skalberg@14494
  1951
    thus ?thesis
skalberg@14494
  1952
      by (simp add: bv_smult_def)
skalberg@14494
  1953
  next
skalberg@14494
  1954
    assume p: "?Q = -1"
skalberg@14494
  1955
    show ?thesis
skalberg@14494
  1956
      apply (simp add: bv_smult_def p)
skalberg@14494
  1957
      apply (cut_tac lmw)
skalberg@14494
  1958
      apply arith
skalberg@14494
  1959
      using p
skalberg@14494
  1960
      apply simp
skalberg@14494
  1961
      done
skalberg@14494
  1962
  next
skalberg@14494
  1963
    assume p: "0 < ?Q"
skalberg@14494
  1964
    thus ?thesis
skalberg@14494
  1965
    proof (simp add: zero_less_mult_iff,safe)
skalberg@14494
  1966
      assume bi1: "0 < bv_to_int w1"
skalberg@14494
  1967
      assume bi2: "0 < bv_to_int w2"
skalberg@14494
  1968
      show ?thesis
skalberg@14494
  1969
	apply (simp add: bv_smult_def)
skalberg@14494
  1970
	apply (rule length_int_to_bv_upper_limit_gt0)
skalberg@14494
  1971
	apply (rule p)
skalberg@14494
  1972
      proof simp
skalberg@14494
  1973
	have "?Q < 2 ^ (length w1 - 1) * 2 ^ (length w2 - 1)"
skalberg@14494
  1974
	  apply (rule mult_strict_mono)
skalberg@14494
  1975
	  apply (rule bv_to_int_upper_range)
skalberg@14494
  1976
	  apply (rule bv_to_int_upper_range)
paulson@15067
  1977
	  apply (rule zero_less_power)
skalberg@14494
  1978
	  apply simp
skalberg@14494
  1979
	  using bi2
skalberg@14494
  1980
	  apply simp
skalberg@14494
  1981
	  done
skalberg@14494
  1982
	also have "... \<le> 2 ^ (length w1 + length w2 - Suc 0)"
skalberg@14494
  1983
	  apply simp
skalberg@14494
  1984
	  apply (subst zpower_zadd_distrib [symmetric])
skalberg@14494
  1985
	  apply simp
skalberg@14494
  1986
	  apply arith
skalberg@14494
  1987
	  done
skalberg@14494
  1988
	finally show "?Q < 2 ^ (length w1 + length w2 - Suc 0)"
skalberg@14494
  1989
	  .
skalberg@14494
  1990
      qed
skalberg@14494
  1991
    next
skalberg@14494
  1992
      assume bi1: "bv_to_int w1 < 0"
skalberg@14494
  1993
      assume bi2: "bv_to_int w2 < 0"
skalberg@14494
  1994
      show ?thesis
skalberg@14494
  1995
	apply (simp add: bv_smult_def)
skalberg@14494
  1996
	apply (rule length_int_to_bv_upper_limit_gt0)
skalberg@14494
  1997
	apply (rule p)
skalberg@14494
  1998
      proof simp
skalberg@14494
  1999
	have "-bv_to_int w1 * -bv_to_int w2 \<le> 2 ^ (length w1 - 1) * 2 ^(length w2 - 1)"
skalberg@14494
  2000
	  apply (rule mult_mono)
skalberg@14494
  2001
	  using bv_to_int_lower_range [of w1]
skalberg@14494
  2002
	  apply simp
skalberg@14494
  2003
	  using bv_to_int_lower_range [of w2]
skalberg@14494
  2004
	  apply simp
paulson@15067
  2005
	  apply (rule zero_le_power,simp)
skalberg@14494
  2006
	  using bi2
skalberg@14494
  2007
	  apply simp
skalberg@14494
  2008
	  done
skalberg@14494
  2009
	hence "?Q \<le> 2 ^ (length w1 - 1) * 2 ^(length w2 - 1)"
skalberg@14494
  2010
	  by simp
skalberg@14494
  2011
	also have "... < 2 ^ (length w1 + length w2 - Suc 0)"
skalberg@14494
  2012
	  apply simp
skalberg@14494
  2013
	  apply (subst zpower_zadd_distrib [symmetric])
skalberg@14494
  2014
	  apply simp
skalberg@14494
  2015
	  apply (cut_tac lmw)
skalberg@14494
  2016
	  apply arith
skalberg@14494
  2017
	  apply (cut_tac p)
skalberg@14494
  2018
	  apply arith
skalberg@14494
  2019
	  done
skalberg@14494
  2020
	finally show "?Q < 2 ^ (length w1 + length w2 - Suc 0)" .
skalberg@14494
  2021
      qed
skalberg@14494
  2022
    qed
skalberg@14494
  2023
  next
skalberg@14494
  2024
    assume p: "?Q < -1"
skalberg@14494
  2025
    show ?thesis
skalberg@14494
  2026
      apply (subst bv_smult_def)
skalberg@14494
  2027
      apply (rule length_int_to_bv_upper_limit_lem1)
skalberg@14494
  2028
      apply (rule p)
skalberg@14494
  2029
    proof simp
skalberg@14494
  2030
      have "(2::int) ^ (length w1 - 1) * 2 ^(length w2 - 1) \<le> 2 ^ (length w1 + length w2 - Suc 0)"
skalberg@14494
  2031
	apply simp
skalberg@14494
  2032
	apply (subst zpower_zadd_distrib [symmetric])
skalberg@14494
  2033
	apply simp
skalberg@14494
  2034
	apply (cut_tac lmw)
skalberg@14494
  2035
	apply arith
skalberg@14494
  2036
	apply (cut_tac p)
skalberg@14494
  2037
	apply arith
skalberg@14494
  2038
	done
skalberg@14494
  2039
      hence "-((2::int) ^ (length w1 + length w2 - Suc 0)) \<le> -(2^(length w1 - 1) * 2 ^ (length w2 - 1))"
skalberg@14494
  2040
	by simp
skalberg@14494
  2041
      also have "... \<le> ?Q"
skalberg@14494
  2042
      proof -
skalberg@14494
  2043
	from p
skalberg@14494
  2044
	have q: "bv_to_int w1 * bv_to_int w2 < 0"
skalberg@14494
  2045
	  by simp
skalberg@14494
  2046
	thus ?thesis
skalberg@14494
  2047
	proof (simp add: mult_less_0_iff,safe)
skalberg@14494
  2048
	  assume bi1: "0 < bv_to_int w1"
skalberg@14494
  2049
	  assume bi2: "bv_to_int w2 < 0"
skalberg@14494
  2050
	  have "-bv_to_int w2 * bv_to_int w1 \<le> ((2::int)^(length w2 - 1)) * (2 ^ (length w1 - 1))"
skalberg@14494
  2051
	    apply (rule mult_mono)
skalberg@14494
  2052
	    using bv_to_int_lower_range [of w2]
skalberg@14494
  2053
	    apply simp
skalberg@14494
  2054
	    using bv_to_int_upper_range [of w1]
skalberg@14494
  2055
	    apply simp
paulson@15067
  2056
	    apply (rule zero_le_power,simp)
skalberg@14494
  2057
	    using bi1
skalberg@14494
  2058
	    apply simp
skalberg@14494
  2059
	    done
skalberg@14494
  2060
	  hence "-?Q \<le> ((2::int)^(length w1 - 1)) * (2 ^ (length w2 - 1))"
skalberg@14494
  2061
	    by (simp add: zmult_ac)
skalberg@14494
  2062
	  thus "-(((2::int)^(length w1 - Suc 0)) * (2 ^ (length w2 - Suc 0))) \<le> ?Q"
skalberg@14494
  2063
	    by simp
skalberg@14494
  2064
	next
skalberg@14494
  2065
	  assume bi1: "bv_to_int w1 < 0"
skalberg@14494
  2066
	  assume bi2: "0 < bv_to_int w2"
skalberg@14494
  2067
	  have "-bv_to_int w1 * bv_to_int w2 \<le> ((2::int)^(length w1 - 1)) * (2 ^ (length w2 - 1))"
skalberg@14494
  2068
	    apply (rule mult_mono)
skalberg@14494
  2069
	    using bv_to_int_lower_range [of w1]
skalberg@14494
  2070
	    apply simp
skalberg@14494
  2071
	    using bv_to_int_upper_range [of w2]
skalberg@14494
  2072
	    apply simp
paulson@15067
  2073
	    apply (rule zero_le_power,simp)
skalberg@14494
  2074
	    using bi2
skalberg@14494
  2075
	    apply simp
skalberg@14494
  2076
	    done
skalberg@14494
  2077
	  hence "-?Q \<le> ((2::int)^(length w1 - 1)) * (2 ^ (length w2 - 1))"
skalberg@14494
  2078
	    by (simp add: zmult_ac)
skalberg@14494
  2079
	  thus "-(((2::int)^(length w1 - Suc 0)) * (2 ^ (length w2 - Suc 0))) \<le> ?Q"
skalberg@14494
  2080
	    by simp
skalberg@14494
  2081
	qed
skalberg@14494
  2082
      qed
skalberg@14494
  2083
      finally show "-(2 ^ (length w1 + length w2 - Suc 0)) \<le> ?Q"
skalberg@14494
  2084
	.
skalberg@14494
  2085
    qed
skalberg@14494
  2086
  qed
skalberg@14494
  2087
qed
skalberg@14494
  2088
skalberg@14494
  2089
lemma bv_msb_one: "bv_msb w = \<one> ==> 0 < bv_to_nat w"
berghofe@15325
  2090
  by (cases w,simp_all)
skalberg@14494
  2091
skalberg@14494
  2092
lemma bv_smult_length_utos: "length (bv_smult (utos w1) w2) \<le> length w1 + length w2"
skalberg@14494
  2093
proof -
skalberg@14494
  2094
  let ?Q = "bv_to_int (utos w1) * bv_to_int w2"
skalberg@14494
  2095
skalberg@14494
  2096
  have lmw: "?Q \<noteq> 0 ==> 0 < length (utos w1) \<and> 0 < length w2"
skalberg@14494
  2097
    by auto
skalberg@14494
  2098
skalberg@14494
  2099
  have "0 < ?Q \<or> ?Q = 0 \<or> ?Q = -1 \<or> ?Q < -1"
skalberg@14494
  2100
    by arith
skalberg@14494
  2101
  thus ?thesis
skalberg@14494
  2102
  proof (safe dest!: iffD1 [OF mult_eq_0_iff])
skalberg@14494
  2103
    assume "bv_to_int (utos w1) = 0"
skalberg@14494
  2104
    thus ?thesis
skalberg@14494
  2105
      by (simp add: bv_smult_def)
skalberg@14494
  2106
  next
skalberg@14494
  2107
    assume "bv_to_int w2 = 0"
skalberg@14494
  2108
    thus ?thesis
skalberg@14494
  2109
      by (simp add: bv_smult_def)
skalberg@14494
  2110
  next
skalberg@14494
  2111
    assume p: "0 < ?Q"
skalberg@14494
  2112
    thus ?thesis
skalberg@14494
  2113
    proof (simp add: zero_less_mult_iff,safe)
skalberg@14494
  2114
      assume biw2: "0 < bv_to_int w2"
skalberg@14494
  2115
      show ?thesis
skalberg@14494
  2116
	apply (simp add: bv_smult_def)
skalberg@14494
  2117
	apply (rule length_int_to_bv_upper_limit_gt0)
skalberg@14494
  2118
	apply (rule p)
skalberg@14494
  2119
      proof simp
skalberg@14494
  2120
	have "?Q < 2 ^ length w1 * 2 ^ (length w2 - 1)"
skalberg@14494
  2121
	  apply (rule mult_strict_mono)
berghofe@15325
  2122
	  apply (simp add: bv_to_int_utos int_nat_two_exp)
skalberg@14494
  2123
	  apply (rule bv_to_nat_upper_range)
skalberg@14494
  2124
	  apply (rule bv_to_int_upper_range)
paulson@15067
  2125
	  apply (rule zero_less_power,simp)
skalberg@14494
  2126
	  using biw2
skalberg@14494
  2127
	  apply simp
skalberg@14494
  2128
	  done
skalberg@14494
  2129
	also have "... \<le> 2 ^ (length w1 + length w2 - Suc 0)"
skalberg@14494
  2130
 	  apply simp
skalberg@14494
  2131
	  apply (subst zpower_zadd_distrib [symmetric])
skalberg@14494
  2132
	  apply simp
skalberg@14494
  2133
	  apply (cut_tac lmw)
skalberg@14494
  2134
	  apply arith
skalberg@14494
  2135
	  using p
skalberg@14494
  2136
	  apply auto
skalberg@14494
  2137
	  done
skalberg@14494
  2138
	finally show "?Q < 2 ^ (length w1 + length w2 - Suc 0)"
skalberg@14494
  2139
	  .
skalberg@14494
  2140
      qed
skalberg@14494
  2141
    next
skalberg@14494
  2142
      assume "bv_to_int (utos w1) < 0"
skalberg@14494
  2143
      thus ?thesis
berghofe@15325
  2144
	by (simp add: bv_to_int_utos)
skalberg@14494
  2145
    qed
skalberg@14494
  2146
  next
skalberg@14494
  2147
    assume p: "?Q = -1"
skalberg@14494
  2148
    thus ?thesis
skalberg@14494
  2149
      apply (simp add: bv_smult_def)
skalberg@14494
  2150
      apply (cut_tac lmw)
skalberg@14494
  2151
      apply arith
skalberg@14494
  2152
      apply simp
skalberg@14494
  2153
      done
skalberg@14494
  2154
  next
skalberg@14494
  2155
    assume p: "?Q < -1"
skalberg@14494
  2156
    show ?thesis
skalberg@14494
  2157
      apply (subst bv_smult_def)
skalberg@14494
  2158
      apply (rule length_int_to_bv_upper_limit_lem1)
skalberg@14494
  2159
      apply (rule p)
skalberg@14494
  2160
    proof simp
skalberg@14494
  2161
      have "(2::int) ^ length w1 * 2 ^(length w2 - 1) \<le> 2 ^ (length w1 + length w2 - Suc 0)"
skalberg@14494
  2162
	apply simp
skalberg@14494
  2163
	apply (subst zpower_zadd_distrib [symmetric])
skalberg@14494
  2164
	apply simp
skalberg@14494
  2165
	apply (cut_tac lmw)
skalberg@14494
  2166
	apply arith
skalberg@14494
  2167
	apply (cut_tac p)
skalberg@14494
  2168
	apply arith
skalberg@14494
  2169
	done
skalberg@14494
  2170
      hence "-((2::int) ^ (length w1 + length w2 - Suc 0)) \<le> -(2^ length w1 * 2 ^ (length w2 - 1))"
skalberg@14494
  2171
	by simp
skalberg@14494
  2172
      also have "... \<le> ?Q"
skalberg@14494
  2173
      proof -
skalberg@14494
  2174
	from p
skalberg@14494
  2175
	have q: "bv_to_int (utos w1) * bv_to_int w2 < 0"
skalberg@14494
  2176
	  by simp
skalberg@14494
  2177
	thus ?thesis
skalberg@14494
  2178
	proof (simp add: mult_less_0_iff,safe)
skalberg@14494
  2179
	  assume bi1: "0 < bv_to_int (utos w1)"
skalberg@14494
  2180
	  assume bi2: "bv_to_int w2 < 0"
skalberg@14494
  2181
	  have "-bv_to_int w2 * bv_to_int (utos w1) \<le> ((2::int)^(length w2 - 1)) * (2 ^ length w1)"
skalberg@14494
  2182
	    apply (rule mult_mono)
skalberg@14494
  2183
	    using bv_to_int_lower_range [of w2]
skalberg@14494
  2184
	    apply simp
skalberg@14494
  2185
	    apply (simp add: bv_to_int_utos)
skalberg@14494
  2186
	    using bv_to_nat_upper_range [of w1]
berghofe@15325
  2187
	    apply (simp add: int_nat_two_exp)
paulson@15067
  2188
	    apply (rule zero_le_power,simp)
skalberg@14494
  2189
	    using bi1
skalberg@14494
  2190
	    apply simp
skalberg@14494
  2191
	    done
skalberg@14494
  2192
	  hence "-?Q \<le> ((2::int)^length w1) * (2 ^ (length w2 - 1))"
skalberg@14494
  2193
	    by (simp add: zmult_ac)
skalberg@14494
  2194
	  thus "-(((2::int)^length w1) * (2 ^ (length w2 - Suc 0))) \<le> ?Q"
skalberg@14494
  2195
	    by simp
skalberg@14494
  2196
	next
skalberg@14494
  2197
	  assume bi1: "bv_to_int (utos w1) < 0"
skalberg@14494
  2198
	  thus "-(((2::int)^length w1) * (2 ^ (length w2 - Suc 0))) \<le> ?Q"
berghofe@15325
  2199
	    by (simp add: bv_to_int_utos)
skalberg@14494
  2200
	qed
skalberg@14494
  2201
      qed
skalberg@14494
  2202
      finally show "-(2 ^ (length w1 + length w2 - Suc 0)) \<le> ?Q"
skalberg@14494
  2203
	.
skalberg@14494
  2204
    qed
skalberg@14494
  2205
  qed
skalberg@14494
  2206
qed
skalberg@14494
  2207
skalberg@14494
  2208
lemma bv_smult_sym: "bv_smult w1 w2 = bv_smult w2 w1"
skalberg@14494
  2209
  by (simp add: bv_smult_def zmult_ac)
skalberg@14494
  2210
wenzelm@14589
  2211
subsection {* Structural operations *}
skalberg@14494
  2212
skalberg@14494
  2213
constdefs
skalberg@14494
  2214
  bv_select :: "[bit list,nat] => bit"
skalberg@14494
  2215
  "bv_select w i == w ! (length w - 1 - i)"
skalberg@14494
  2216
  bv_chop :: "[bit list,nat] => bit list * bit list"
skalberg@14494
  2217
  "bv_chop w i == let len = length w in (take (len - i) w,drop (len - i) w)"
skalberg@14494
  2218
  bv_slice :: "[bit list,nat*nat] => bit list"
skalberg@14494
  2219
  "bv_slice w == \<lambda>(b,e). fst (bv_chop (snd (bv_chop w (b+1))) e)"
skalberg@14494
  2220
skalberg@14494
  2221
lemma bv_select_rev:
skalberg@14494
  2222
  assumes notnull: "n < length w"
skalberg@14494
  2223
  shows            "bv_select w n = rev w ! n"
skalberg@14494
  2224
proof -
skalberg@14494
  2225
  have "\<forall>n. n < length w --> bv_select w n = rev w ! n"
skalberg@14494
  2226
  proof (rule length_induct [of _ w],auto simp add: bv_select_def)
skalberg@14494
  2227
    fix xs :: "bit list"
skalberg@14494
  2228
    fix n
skalberg@14494
  2229
    assume ind: "\<forall>ys::bit list. length ys < length xs --> (\<forall>n. n < length ys --> ys ! (length ys - Suc n) = rev ys ! n)"
skalberg@14494
  2230
    assume notx: "n < length xs"
skalberg@14494
  2231
    show "xs ! (length xs - Suc n) = rev xs ! n"
skalberg@14494
  2232
    proof (cases xs)
skalberg@14494
  2233
      assume "xs = []"
skalberg@14494
  2234
      with notx
skalberg@14494
  2235
      show ?thesis
skalberg@14494
  2236
	by simp
skalberg@14494
  2237
    next
skalberg@14494
  2238
      fix y ys
skalberg@14494
  2239
      assume [simp]: "xs = y # ys"
skalberg@14494
  2240
      show ?thesis
skalberg@14494
  2241
      proof (auto simp add: nth_append)
skalberg@14494
  2242
	assume noty: "n < length ys"
skalberg@14494
  2243
	from spec [OF ind,of ys]
skalberg@14494
  2244
	have "\<forall>n. n < length ys --> ys ! (length ys - Suc n) = rev ys ! n"
skalberg@14494
  2245
	  by simp
skalberg@14494
  2246
	hence "n < length ys --> ys ! (length ys - Suc n) = rev ys ! n"
skalberg@14494
  2247
	  ..
skalberg@14494
  2248
	hence "ys ! (length ys - Suc n) = rev ys ! n"
skalberg@14494
  2249
	  ..
skalberg@14494
  2250
	thus "(y # ys) ! (length ys - n) = rev ys ! n"
skalberg@14494
  2251
	  by (simp add: nth_Cons' noty not_less_iff_le [symmetric])
skalberg@14494
  2252
      next
skalberg@14494
  2253
	assume "~ n < length ys"
skalberg@14494
  2254
	hence x: "length ys \<le> n"
skalberg@14494
  2255
	  by simp
skalberg@14494
  2256
	from notx
skalberg@14494
  2257
	have "n < Suc (length ys)"
skalberg@14494
  2258
	  by simp
skalberg@14494
  2259
	hence "n \<le> length ys"
skalberg@14494
  2260
	  by simp
skalberg@14494
  2261
	with x
skalberg@14494
  2262
	have "length ys = n"
skalberg@14494
  2263
	  by simp
skalberg@14494
  2264
	thus "y = [y] ! (n - length ys)"
skalberg@14494
  2265
	  by simp
skalberg@14494
  2266
      qed
skalberg@14494
  2267
    qed
skalberg@14494
  2268
  qed
skalberg@14494
  2269
  hence "n < length w --> bv_select w n = rev w ! n"
skalberg@14494
  2270
    ..
skalberg@14494
  2271
  thus ?thesis
skalberg@14494
  2272
    ..
skalberg@14494
  2273
qed
skalberg@14494
  2274
skalberg@14494
  2275
lemma bv_chop_append: "bv_chop (w1 @ w2) (length w2) = (w1,w2)"
skalberg@14494
  2276
  by (simp add: bv_chop_def Let_def)
skalberg@14494
  2277
skalberg@14494
  2278
lemma append_bv_chop_id: "fst (bv_chop w l) @ snd (bv_chop w l) = w"
skalberg@14494
  2279
  by (simp add: bv_chop_def Let_def)
skalberg@14494
  2280
skalberg@14494
  2281
lemma bv_chop_length_fst [simp]: "length (fst (bv_chop w i)) = length w - i"
skalberg@14494
  2282
  by (simp add: bv_chop_def Let_def,arith)
skalberg@14494
  2283
skalberg@14494
  2284
lemma bv_chop_length_snd [simp]: "length (snd (bv_chop w i)) = min i (length w)"
skalberg@14494
  2285
  by (simp add: bv_chop_def Let_def,arith)
skalberg@14494
  2286
skalberg@14494
  2287
lemma bv_slice_length [simp]: "[| j \<le> i; i < length w |] ==> length (bv_slice w (i,j)) = i - j + 1"
skalberg@14494
  2288
  by (auto simp add: bv_slice_def,arith)
skalberg@14494
  2289
skalberg@14494
  2290
constdefs
berghofe@15325
  2291
  length_nat :: "nat => nat"
skalberg@14494
  2292
  "length_nat x == LEAST n. x < 2 ^ n"
skalberg@14494
  2293
skalberg@14494
  2294
lemma length_nat: "length (nat_to_bv n) = length_nat n"
skalberg@14494
  2295
  apply (simp add: length_nat_def)
skalberg@14494
  2296
  apply (rule Least_equality [symmetric])
skalberg@14494
  2297
  prefer 2
skalberg@14494
  2298
  apply (rule length_nat_to_bv_upper_limit)
skalberg@14494
  2299
  apply arith
skalberg@14494
  2300
  apply (rule ccontr)
skalberg@14494
  2301
proof -
skalberg@14494
  2302
  assume "~ n < 2 ^ length (nat_to_bv n)"
skalberg@14494
  2303
  hence "2 ^ length (nat_to_bv n) \<le> n"
skalberg@14494
  2304
    by simp
skalberg@14494
  2305
  hence "length (nat_to_bv n) < length (nat_to_bv n)"
skalberg@14494
  2306
    by (rule length_nat_to_bv_lower_limit)
skalberg@14494
  2307
  thus False
skalberg@14494
  2308
    by simp
skalberg@14494
  2309
qed
skalberg@14494
  2310
skalberg@14494
  2311
lemma length_nat_0 [simp]: "length_nat 0 = 0"
skalberg@14494
  2312
  by (simp add: length_nat_def Least_equality)
skalberg@14494
  2313
skalberg@14494
  2314
lemma length_nat_non0:
skalberg@14494
  2315
  assumes n0: "0 < n"
skalberg@14494
  2316
  shows       "length_nat n = Suc (length_nat (n div 2))"
skalberg@14494
  2317
  apply (simp add: length_nat [symmetric])
skalberg@14494
  2318
  apply (subst nat_to_bv_non0 [of n])
skalberg@14494
  2319
  apply (simp_all add: n0)
skalberg@14494
  2320
  done
skalberg@14494
  2321
skalberg@14494
  2322
constdefs
skalberg@14494
  2323
  length_int :: "int => nat"
berghofe@15325
  2324
  "length_int x == if 0 < x then Suc (length_nat (nat x)) else if x = 0 then 0 else Suc (length_nat (nat (-x - 1)))"
skalberg@14494
  2325
skalberg@14494
  2326
lemma length_int: "length (int_to_bv i) = length_int i"
skalberg@14494
  2327
proof (cases "0 < i")
skalberg@14494
  2328
  assume i0: "0 < i"
berghofe@15325
  2329
  hence "length (int_to_bv i) = length (norm_signed (\<zero> # norm_unsigned (nat_to_bv (nat i))))"
skalberg@14494
  2330
    by simp
berghofe@15325
  2331
  also from norm_unsigned_result [of "nat_to_bv (nat i)"]
berghofe@15325
  2332
  have "... = Suc (length_nat (nat i))"
skalberg@14494
  2333
    apply safe
berghofe@15325
  2334
    apply (simp del: norm_unsigned_nat_to_bv)
skalberg@14494
  2335
    apply (drule norm_empty_bv_to_nat_zero)
skalberg@14494
  2336
    using prems
skalberg@14494
  2337
    apply simp
berghofe@15325
  2338
    apply arith
berghofe@15325
  2339
    apply (cases "norm_unsigned (nat_to_bv (nat i))")
berghofe@15325
  2340
    apply (drule norm_empty_bv_to_nat_zero [of "nat_to_bv (nat i)"])
skalberg@14494
  2341
    using prems
skalberg@14494
  2342
    apply simp
skalberg@14494
  2343
    apply simp
skalberg@14494
  2344
    using prems
skalberg@14494
  2345
    apply (simp add: length_nat [symmetric])
skalberg@14494
  2346
    done
skalberg@14494
  2347
  finally show ?thesis
skalberg@14494
  2348
    using i0
skalberg@14494
  2349
    by (simp add: length_int_def)
skalberg@14494
  2350
next
skalberg@14494
  2351
  assume "~ 0 < i"
skalberg@14494
  2352
  hence i0: "i \<le> 0"
skalberg@14494
  2353
    by simp
skalberg@14494
  2354
  show ?thesis
skalberg@14494
  2355
  proof (cases "i = 0")
skalberg@14494
  2356
    assume "i = 0"
skalberg@14494
  2357
    thus ?thesis
skalberg@14494
  2358
      by (simp add: length_int_def)
skalberg@14494
  2359
  next
skalberg@14494
  2360
    assume "i \<noteq> 0"
skalberg@14494
  2361
    with i0
skalberg@14494
  2362
    have i0: "i < 0"
skalberg@14494
  2363
      by simp
berghofe@15325
  2364
    hence "length (int_to_bv i) = length (norm_signed (\<one> # bv_not (norm_unsigned (nat_to_bv (nat (- i) - 1)