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