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