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