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