src/HOL/Word/BinBoolList.thy
author wenzelm
Wed Sep 17 21:27:14 2008 +0200 (2008-09-17)
changeset 28263 69eaa97e7e96
parent 28059 295a8fc92684
child 28298 3eb2855e5402
permissions -rw-r--r--
moved global ML bindings to global place;
kleing@24333
     1
(* 
kleing@24333
     2
  ID:     $Id$
kleing@24333
     3
  Author: Jeremy Dawson, NICTA
kleing@24333
     4
kleing@24333
     5
  contains theorems to do with integers, expressed using Pls, Min, BIT,
kleing@24333
     6
  theorems linking them to lists of booleans, and repeated splitting 
kleing@24333
     7
  and concatenation.
kleing@24333
     8
*) 
kleing@24333
     9
kleing@24333
    10
header "Bool lists and integers"
kleing@24333
    11
haftmann@26557
    12
theory BinBoolList
haftmann@26557
    13
imports BinOperations
haftmann@26557
    14
begin
kleing@24333
    15
huffman@24465
    16
subsection "Arithmetic in terms of bool lists"
huffman@24465
    17
haftmann@26557
    18
(* arithmetic operations in terms of the reversed bool list,
huffman@24465
    19
  assuming input list(s) the same length, and don't extend them *)
huffman@24465
    20
haftmann@26557
    21
primrec rbl_succ :: "bool list => bool list" where
huffman@24465
    22
  Nil: "rbl_succ Nil = Nil"
haftmann@26557
    23
  | Cons: "rbl_succ (x # xs) = (if x then False # rbl_succ xs else True # xs)"
huffman@24465
    24
haftmann@26557
    25
primrec rbl_pred :: "bool list => bool list" where
haftmann@26557
    26
  Nil: "rbl_pred Nil = Nil"
haftmann@26557
    27
  | Cons: "rbl_pred (x # xs) = (if x then False # xs else True # rbl_pred xs)"
huffman@24465
    28
haftmann@26557
    29
primrec rbl_add :: "bool list => bool list => bool list" where
haftmann@26557
    30
    (* result is length of first arg, second arg may be longer *)
haftmann@26557
    31
  Nil: "rbl_add Nil x = Nil"
haftmann@26557
    32
  | Cons: "rbl_add (y # ys) x = (let ws = rbl_add ys (tl x) in 
huffman@24465
    33
    (y ~= hd x) # (if hd x & y then rbl_succ ws else ws))"
huffman@24465
    34
haftmann@26557
    35
primrec rbl_mult :: "bool list => bool list => bool list" where
haftmann@26557
    36
    (* result is length of first arg, second arg may be longer *)
haftmann@26557
    37
  Nil: "rbl_mult Nil x = Nil"
haftmann@26557
    38
  | Cons: "rbl_mult (y # ys) x = (let ws = False # rbl_mult ys x in 
huffman@24465
    39
    if y then rbl_add ws x else ws)"
kleing@24333
    40
kleing@24333
    41
lemma butlast_power:
kleing@24333
    42
  "(butlast ^ n) bl = take (length bl - n) bl"
kleing@24333
    43
  by (induct n) (auto simp: butlast_take)
kleing@24333
    44
huffman@26086
    45
lemma bin_to_bl_aux_Pls_minus_simp [simp]:
haftmann@25919
    46
  "0 < n ==> bin_to_bl_aux n Int.Pls bl = 
haftmann@25919
    47
    bin_to_bl_aux (n - 1) Int.Pls (False # bl)"
kleing@24333
    48
  by (cases n) auto
kleing@24333
    49
huffman@26086
    50
lemma bin_to_bl_aux_Min_minus_simp [simp]:
haftmann@25919
    51
  "0 < n ==> bin_to_bl_aux n Int.Min bl = 
haftmann@25919
    52
    bin_to_bl_aux (n - 1) Int.Min (True # bl)"
kleing@24333
    53
  by (cases n) auto
kleing@24333
    54
huffman@26086
    55
lemma bin_to_bl_aux_Bit_minus_simp [simp]:
kleing@24333
    56
  "0 < n ==> bin_to_bl_aux n (w BIT b) bl = 
kleing@24333
    57
    bin_to_bl_aux (n - 1) w ((b = bit.B1) # bl)"
kleing@24333
    58
  by (cases n) auto
kleing@24333
    59
huffman@26086
    60
lemma bin_to_bl_aux_Bit0_minus_simp [simp]:
huffman@26086
    61
  "0 < n ==> bin_to_bl_aux n (Int.Bit0 w) bl = 
huffman@26086
    62
    bin_to_bl_aux (n - 1) w (False # bl)"
huffman@26086
    63
  by (cases n) auto
huffman@26086
    64
huffman@26086
    65
lemma bin_to_bl_aux_Bit1_minus_simp [simp]:
huffman@26086
    66
  "0 < n ==> bin_to_bl_aux n (Int.Bit1 w) bl = 
huffman@26086
    67
    bin_to_bl_aux (n - 1) w (True # bl)"
huffman@26086
    68
  by (cases n) auto
kleing@24333
    69
huffman@24465
    70
(** link between bin and bool list **)
huffman@24465
    71
haftmann@26557
    72
lemma bl_to_bin_aux_append: 
haftmann@26557
    73
  "bl_to_bin_aux (bs @ cs) w = bl_to_bin_aux cs (bl_to_bin_aux bs w)"
haftmann@26557
    74
  by (induct bs arbitrary: w) auto
huffman@24465
    75
haftmann@26557
    76
lemma bin_to_bl_aux_append: 
haftmann@26557
    77
  "bin_to_bl_aux n w bs @ cs = bin_to_bl_aux n w (bs @ cs)"
haftmann@26557
    78
  by (induct n arbitrary: w bs) auto
kleing@24333
    79
huffman@24465
    80
lemma bl_to_bin_append: 
haftmann@26557
    81
  "bl_to_bin (bs @ cs) = bl_to_bin_aux cs (bl_to_bin bs)"
huffman@24465
    82
  unfolding bl_to_bin_def by (rule bl_to_bin_aux_append)
huffman@24465
    83
kleing@24333
    84
lemma bin_to_bl_aux_alt: 
kleing@24333
    85
  "bin_to_bl_aux n w bs = bin_to_bl n w @ bs" 
kleing@24333
    86
  unfolding bin_to_bl_def by (simp add : bin_to_bl_aux_append)
kleing@24333
    87
huffman@24465
    88
lemma bin_to_bl_0: "bin_to_bl 0 bs = []"
kleing@24333
    89
  unfolding bin_to_bl_def by auto
kleing@24333
    90
haftmann@26557
    91
lemma size_bin_to_bl_aux: 
haftmann@26557
    92
  "size (bin_to_bl_aux n w bs) = n + length bs"
haftmann@26557
    93
  by (induct n arbitrary: w bs) auto
kleing@24333
    94
huffman@24465
    95
lemma size_bin_to_bl: "size (bin_to_bl n w) = n" 
kleing@24333
    96
  unfolding bin_to_bl_def by (simp add : size_bin_to_bl_aux)
kleing@24333
    97
haftmann@26557
    98
lemma bin_bl_bin': 
haftmann@26557
    99
  "bl_to_bin (bin_to_bl_aux n w bs) = 
haftmann@26557
   100
    bl_to_bin_aux bs (bintrunc n w)"
haftmann@26557
   101
  by (induct n arbitrary: w bs) (auto simp add : bl_to_bin_def)
huffman@24465
   102
huffman@24465
   103
lemma bin_bl_bin: "bl_to_bin (bin_to_bl n w) = bintrunc n w"
huffman@24465
   104
  unfolding bin_to_bl_def bin_bl_bin' by auto
huffman@24465
   105
haftmann@26557
   106
lemma bl_bin_bl':
haftmann@26557
   107
  "bin_to_bl (n + length bs) (bl_to_bin_aux bs w) = 
huffman@24465
   108
    bin_to_bl_aux n w bs"
haftmann@26557
   109
  apply (induct bs arbitrary: w n)
huffman@24465
   110
   apply auto
huffman@24465
   111
    apply (simp_all only : add_Suc [symmetric])
huffman@24465
   112
    apply (auto simp add : bin_to_bl_def)
huffman@24465
   113
  done
huffman@24465
   114
huffman@24465
   115
lemma bl_bin_bl: "bin_to_bl (length bs) (bl_to_bin bs) = bs"
huffman@24465
   116
  unfolding bl_to_bin_def
huffman@24465
   117
  apply (rule box_equals)
huffman@24465
   118
    apply (rule bl_bin_bl')
huffman@24465
   119
   prefer 2
huffman@24465
   120
   apply (rule bin_to_bl_aux.Z)
huffman@24465
   121
  apply simp
huffman@24465
   122
  done
huffman@24465
   123
  
huffman@24465
   124
declare 
huffman@24465
   125
  bin_to_bl_0 [simp] 
huffman@24465
   126
  size_bin_to_bl [simp] 
huffman@24465
   127
  bin_bl_bin [simp] 
huffman@24465
   128
  bl_bin_bl [simp]
huffman@24465
   129
huffman@24465
   130
lemma bl_to_bin_inj:
huffman@24465
   131
  "bl_to_bin bs = bl_to_bin cs ==> length bs = length cs ==> bs = cs"
huffman@24465
   132
  apply (rule_tac box_equals)
huffman@24465
   133
    defer
huffman@24465
   134
    apply (rule bl_bin_bl)
huffman@24465
   135
   apply (rule bl_bin_bl)
huffman@24465
   136
  apply simp
huffman@24465
   137
  done
huffman@24465
   138
huffman@24465
   139
lemma bl_to_bin_False: "bl_to_bin (False # bl) = bl_to_bin bl"
huffman@24465
   140
  unfolding bl_to_bin_def by auto
huffman@24465
   141
  
haftmann@25919
   142
lemma bl_to_bin_Nil: "bl_to_bin [] = Int.Pls"
huffman@24465
   143
  unfolding bl_to_bin_def by auto
huffman@24465
   144
haftmann@26557
   145
lemma bin_to_bl_Pls_aux: 
haftmann@26557
   146
  "bin_to_bl_aux n Int.Pls bl = replicate n False @ bl"
haftmann@26557
   147
  by (induct n arbitrary: bl) (auto simp: replicate_app_Cons_same)
kleing@24333
   148
haftmann@25919
   149
lemma bin_to_bl_Pls: "bin_to_bl n Int.Pls = replicate n False"
kleing@24333
   150
  unfolding bin_to_bl_def by (simp add : bin_to_bl_Pls_aux)
kleing@24333
   151
kleing@24333
   152
lemma bin_to_bl_Min_aux [rule_format] : 
haftmann@25919
   153
  "ALL bl. bin_to_bl_aux n Int.Min bl = replicate n True @ bl"
kleing@24333
   154
  by (induct n) (auto simp: replicate_app_Cons_same)
kleing@24333
   155
haftmann@25919
   156
lemma bin_to_bl_Min: "bin_to_bl n Int.Min = replicate n True"
kleing@24333
   157
  unfolding bin_to_bl_def by (simp add : bin_to_bl_Min_aux)
kleing@24333
   158
huffman@24465
   159
lemma bl_to_bin_rep_F: 
huffman@24465
   160
  "bl_to_bin (replicate n False @ bl) = bl_to_bin bl"
huffman@24465
   161
  apply (simp add: bin_to_bl_Pls_aux [symmetric] bin_bl_bin')
huffman@24465
   162
  apply (simp add: bl_to_bin_def)
huffman@24465
   163
  done
huffman@24465
   164
huffman@24465
   165
lemma bin_to_bl_trunc:
huffman@24465
   166
  "n <= m ==> bin_to_bl n (bintrunc m w) = bin_to_bl n w"
huffman@24465
   167
  by (auto intro: bl_to_bin_inj)
huffman@24465
   168
huffman@24465
   169
declare 
huffman@24465
   170
  bin_to_bl_trunc [simp] 
huffman@24465
   171
  bl_to_bin_False [simp] 
huffman@24465
   172
  bl_to_bin_Nil [simp]
huffman@24465
   173
kleing@24333
   174
lemma bin_to_bl_aux_bintr [rule_format] :
kleing@24333
   175
  "ALL m bin bl. bin_to_bl_aux n (bintrunc m bin) bl = 
kleing@24333
   176
    replicate (n - m) False @ bin_to_bl_aux (min n m) bin bl"
haftmann@27105
   177
  apply (induct n)
kleing@24333
   178
   apply clarsimp
kleing@24333
   179
  apply clarsimp
kleing@24333
   180
  apply (case_tac "m")
kleing@24333
   181
   apply (clarsimp simp: bin_to_bl_Pls_aux) 
kleing@24333
   182
   apply (erule thin_rl)
kleing@24333
   183
   apply (induct_tac n)   
kleing@24333
   184
    apply auto
kleing@24333
   185
  done
kleing@24333
   186
kleing@24333
   187
lemmas bin_to_bl_bintr = 
kleing@24333
   188
  bin_to_bl_aux_bintr [where bl = "[]", folded bin_to_bl_def]
kleing@24333
   189
haftmann@25919
   190
lemma bl_to_bin_rep_False: "bl_to_bin (replicate n False) = Int.Pls"
huffman@24465
   191
  by (induct n) auto
huffman@24465
   192
haftmann@26557
   193
lemma len_bin_to_bl_aux: 
haftmann@26557
   194
  "length (bin_to_bl_aux n w bs) = n + length bs"
haftmann@26557
   195
  by (induct n arbitrary: w bs) auto
kleing@24333
   196
kleing@24333
   197
lemma len_bin_to_bl [simp]: "length (bin_to_bl n w) = n"
kleing@24333
   198
  unfolding bin_to_bl_def len_bin_to_bl_aux by auto
kleing@24333
   199
  
haftmann@26557
   200
lemma sign_bl_bin': 
haftmann@26557
   201
  "bin_sign (bl_to_bin_aux bs w) = bin_sign w"
haftmann@26557
   202
  by (induct bs arbitrary: w) auto
kleing@24333
   203
  
haftmann@25919
   204
lemma sign_bl_bin: "bin_sign (bl_to_bin bs) = Int.Pls"
kleing@24333
   205
  unfolding bl_to_bin_def by (simp add : sign_bl_bin')
kleing@24333
   206
  
haftmann@26557
   207
lemma bl_sbin_sign_aux: 
haftmann@26557
   208
  "hd (bin_to_bl_aux (Suc n) w bs) = 
haftmann@25919
   209
    (bin_sign (sbintrunc n w) = Int.Min)"
haftmann@26557
   210
  apply (induct n arbitrary: w bs)
kleing@24333
   211
   apply clarsimp
haftmann@26557
   212
   apply (cases w rule: bin_exhaust)
kleing@24333
   213
   apply (simp split add : bit.split)
kleing@24333
   214
  apply clarsimp
kleing@24333
   215
  done
kleing@24333
   216
    
kleing@24333
   217
lemma bl_sbin_sign: 
haftmann@25919
   218
  "hd (bin_to_bl (Suc n) w) = (bin_sign (sbintrunc n w) = Int.Min)"
kleing@24333
   219
  unfolding bin_to_bl_def by (rule bl_sbin_sign_aux)
kleing@24333
   220
haftmann@26557
   221
lemma bin_nth_of_bl_aux [rule_format]: 
haftmann@26557
   222
  "\<forall>w. bin_nth (bl_to_bin_aux bl w) n = 
kleing@24333
   223
    (n < size bl & rev bl ! n | n >= length bl & bin_nth w (n - size bl))"
kleing@24333
   224
  apply (induct_tac bl)
kleing@24333
   225
   apply clarsimp
kleing@24333
   226
  apply clarsimp
kleing@24333
   227
  apply (cut_tac x=n and y="size list" in linorder_less_linear)
kleing@24333
   228
  apply (erule disjE, simp add: nth_append)+
haftmann@26557
   229
  apply auto
kleing@24333
   230
  done
kleing@24333
   231
kleing@24333
   232
lemma bin_nth_of_bl: "bin_nth (bl_to_bin bl) n = (n < length bl & rev bl ! n)";
kleing@24333
   233
  unfolding bl_to_bin_def by (simp add : bin_nth_of_bl_aux)
kleing@24333
   234
kleing@24333
   235
lemma bin_nth_bl [rule_format] : "ALL m w. n < m --> 
kleing@24333
   236
    bin_nth w n = nth (rev (bin_to_bl m w)) n"
kleing@24333
   237
  apply (induct n)
kleing@24333
   238
   apply clarsimp
kleing@24333
   239
   apply (case_tac m, clarsimp)
kleing@24333
   240
   apply (clarsimp simp: bin_to_bl_def)
kleing@24333
   241
   apply (simp add: bin_to_bl_aux_alt)
kleing@24333
   242
  apply clarsimp
kleing@24333
   243
  apply (case_tac m, clarsimp)
kleing@24333
   244
  apply (clarsimp simp: bin_to_bl_def)
kleing@24333
   245
  apply (simp add: bin_to_bl_aux_alt)
kleing@24333
   246
  done
kleing@24333
   247
huffman@24465
   248
lemma nth_rev [rule_format] : 
huffman@24465
   249
  "n < length xs --> rev xs ! n = xs ! (length xs - 1 - n)"
huffman@24465
   250
  apply (induct_tac "xs")
huffman@24465
   251
   apply simp
huffman@24465
   252
  apply (clarsimp simp add : nth_append nth.simps split add : nat.split)
huffman@24465
   253
  apply (rule_tac f = "%n. list ! n" in arg_cong) 
huffman@24465
   254
  apply arith
huffman@24465
   255
  done
huffman@24465
   256
wenzelm@25350
   257
lemmas nth_rev_alt = nth_rev [where xs = "rev ys", simplified, standard]
huffman@24465
   258
kleing@24333
   259
lemma nth_bin_to_bl_aux [rule_format] : 
kleing@24333
   260
  "ALL w n bl. n < m + length bl --> (bin_to_bl_aux m w bl) ! n = 
kleing@24333
   261
    (if n < m then bin_nth w (m - 1 - n) else bl ! (n - m))"
haftmann@27105
   262
  apply (induct m)
kleing@24333
   263
   apply clarsimp
kleing@24333
   264
  apply clarsimp
kleing@24333
   265
  apply (case_tac w rule: bin_exhaust)
kleing@24333
   266
  apply clarsimp
haftmann@27105
   267
  apply (case_tac "n - m")
kleing@24333
   268
   apply arith
kleing@24333
   269
  apply simp
kleing@24333
   270
  apply (rule_tac f = "%n. bl ! n" in arg_cong) 
kleing@24333
   271
  apply arith
kleing@24333
   272
  done
kleing@24333
   273
  
kleing@24333
   274
lemma nth_bin_to_bl: "n < m ==> (bin_to_bl m w) ! n = bin_nth w (m - Suc n)"
kleing@24333
   275
  unfolding bin_to_bl_def by (simp add : nth_bin_to_bl_aux)
kleing@24333
   276
haftmann@26557
   277
lemma bl_to_bin_lt2p_aux [rule_format]: 
haftmann@26557
   278
  "\<forall>w. bl_to_bin_aux bs w < (w + 1) * (2 ^ length bs)"
haftmann@26557
   279
  apply (induct bs)
kleing@24333
   280
   apply clarsimp
kleing@24333
   281
  apply clarsimp
kleing@24333
   282
  apply safe
haftmann@26557
   283
  apply (erule allE, erule xtr8 [rotated],
haftmann@26557
   284
         simp add: numeral_simps ring_simps cong add : number_of_False_cong)+
kleing@24333
   285
  done
kleing@24333
   286
kleing@24333
   287
lemma bl_to_bin_lt2p: "bl_to_bin bs < (2 ^ length bs)"
kleing@24333
   288
  apply (unfold bl_to_bin_def)
kleing@24333
   289
  apply (rule xtr1)
kleing@24333
   290
   prefer 2
kleing@24333
   291
   apply (rule bl_to_bin_lt2p_aux)
kleing@24333
   292
  apply simp
kleing@24333
   293
  done
kleing@24333
   294
kleing@24333
   295
lemma bl_to_bin_ge2p_aux [rule_format] : 
haftmann@26557
   296
  "\<forall>w. bl_to_bin_aux bs w >= w * (2 ^ length bs)"
kleing@24333
   297
  apply (induct bs)
kleing@24333
   298
   apply clarsimp
kleing@24333
   299
  apply clarsimp
kleing@24333
   300
  apply safe
haftmann@28059
   301
   apply (erule allE, erule preorder_class.order_trans [rotated],
huffman@26086
   302
          simp add: numeral_simps ring_simps cong add : number_of_False_cong)+
kleing@24333
   303
  done
kleing@24333
   304
kleing@24333
   305
lemma bl_to_bin_ge0: "bl_to_bin bs >= 0"
kleing@24333
   306
  apply (unfold bl_to_bin_def)
kleing@24333
   307
  apply (rule xtr4)
kleing@24333
   308
   apply (rule bl_to_bin_ge2p_aux)
kleing@24333
   309
  apply simp
kleing@24333
   310
  done
kleing@24333
   311
kleing@24333
   312
lemma butlast_rest_bin: 
kleing@24333
   313
  "butlast (bin_to_bl n w) = bin_to_bl (n - 1) (bin_rest w)"
kleing@24333
   314
  apply (unfold bin_to_bl_def)
kleing@24333
   315
  apply (cases w rule: bin_exhaust)
kleing@24333
   316
  apply (cases n, clarsimp)
kleing@24333
   317
  apply clarsimp
kleing@24333
   318
  apply (auto simp add: bin_to_bl_aux_alt)
kleing@24333
   319
  done
kleing@24333
   320
kleing@24333
   321
lemmas butlast_bin_rest = butlast_rest_bin
wenzelm@25350
   322
  [where w="bl_to_bin bl" and n="length bl", simplified, standard]
kleing@24333
   323
haftmann@26557
   324
lemma butlast_rest_bl2bin_aux:
haftmann@26557
   325
  "bl ~= [] \<Longrightarrow>
haftmann@26557
   326
    bl_to_bin_aux (butlast bl) w = bin_rest (bl_to_bin_aux bl w)"
haftmann@26557
   327
  by (induct bl arbitrary: w) auto
kleing@24333
   328
  
kleing@24333
   329
lemma butlast_rest_bl2bin: 
kleing@24333
   330
  "bl_to_bin (butlast bl) = bin_rest (bl_to_bin bl)"
kleing@24333
   331
  apply (unfold bl_to_bin_def)
kleing@24333
   332
  apply (cases bl)
kleing@24333
   333
   apply (auto simp add: butlast_rest_bl2bin_aux)
kleing@24333
   334
  done
kleing@24333
   335
haftmann@26557
   336
lemma trunc_bl2bin_aux [rule_format]: 
haftmann@26557
   337
  "ALL w. bintrunc m (bl_to_bin_aux bl w) = 
haftmann@26557
   338
    bl_to_bin_aux (drop (length bl - m) bl) (bintrunc (m - length bl) w)"
kleing@24333
   339
  apply (induct_tac bl)
kleing@24333
   340
   apply clarsimp
kleing@24333
   341
  apply clarsimp
kleing@24333
   342
  apply safe
kleing@24333
   343
   apply (case_tac "m - size list")
kleing@24333
   344
    apply (simp add : diff_is_0_eq [THEN iffD1, THEN Suc_diff_le])
kleing@24333
   345
   apply simp
haftmann@26557
   346
   apply (rule_tac f = "%nat. bl_to_bin_aux list (Int.Bit1 (bintrunc nat w))" 
kleing@24333
   347
                   in arg_cong) 
kleing@24333
   348
   apply simp
kleing@24333
   349
  apply (case_tac "m - size list")
kleing@24333
   350
   apply (simp add: diff_is_0_eq [THEN iffD1, THEN Suc_diff_le])
kleing@24333
   351
  apply simp
haftmann@26557
   352
  apply (rule_tac f = "%nat. bl_to_bin_aux list (Int.Bit0 (bintrunc nat w))" 
kleing@24333
   353
                  in arg_cong) 
kleing@24333
   354
  apply simp
kleing@24333
   355
  done
kleing@24333
   356
kleing@24333
   357
lemma trunc_bl2bin: 
kleing@24333
   358
  "bintrunc m (bl_to_bin bl) = bl_to_bin (drop (length bl - m) bl)"
kleing@24333
   359
  unfolding bl_to_bin_def by (simp add : trunc_bl2bin_aux)
kleing@24333
   360
  
kleing@24333
   361
lemmas trunc_bl2bin_len [simp] =
kleing@24333
   362
  trunc_bl2bin [of "length bl" bl, simplified, standard]  
kleing@24333
   363
kleing@24333
   364
lemma bl2bin_drop: 
kleing@24333
   365
  "bl_to_bin (drop k bl) = bintrunc (length bl - k) (bl_to_bin bl)"
kleing@24333
   366
  apply (rule trans)
kleing@24333
   367
   prefer 2
kleing@24333
   368
   apply (rule trunc_bl2bin [symmetric])
kleing@24333
   369
  apply (cases "k <= length bl")
kleing@24333
   370
   apply auto
kleing@24333
   371
  done
kleing@24333
   372
kleing@24333
   373
lemma nth_rest_power_bin [rule_format] :
kleing@24333
   374
  "ALL n. bin_nth ((bin_rest ^ k) w) n = bin_nth w (n + k)"
kleing@24333
   375
  apply (induct k, clarsimp)
kleing@24333
   376
  apply clarsimp
kleing@24333
   377
  apply (simp only: bin_nth.Suc [symmetric] add_Suc)
kleing@24333
   378
  done
kleing@24333
   379
kleing@24333
   380
lemma take_rest_power_bin:
kleing@24333
   381
  "m <= n ==> take m (bin_to_bl n w) = bin_to_bl m ((bin_rest ^ (n - m)) w)" 
kleing@24333
   382
  apply (rule nth_equalityI)
kleing@24333
   383
   apply simp
kleing@24333
   384
  apply (clarsimp simp add: nth_bin_to_bl nth_rest_power_bin)
kleing@24333
   385
  done
kleing@24333
   386
huffman@24465
   387
lemma hd_butlast: "size xs > 1 ==> hd (butlast xs) = hd xs"
huffman@24465
   388
  by (cases xs) auto
kleing@24333
   389
haftmann@26557
   390
lemma last_bin_last': 
haftmann@26557
   391
  "size xs > 0 \<Longrightarrow> last xs = (bin_last (bl_to_bin_aux xs w) = bit.B1)" 
haftmann@26557
   392
  by (induct xs arbitrary: w) auto
kleing@24333
   393
kleing@24333
   394
lemma last_bin_last: 
kleing@24333
   395
  "size xs > 0 ==> last xs = (bin_last (bl_to_bin xs) = bit.B1)" 
kleing@24333
   396
  unfolding bl_to_bin_def by (erule last_bin_last')
kleing@24333
   397
  
kleing@24333
   398
lemma bin_last_last: 
kleing@24333
   399
  "bin_last w = (if last (bin_to_bl (Suc n) w) then bit.B1 else bit.B0)" 
kleing@24333
   400
  apply (unfold bin_to_bl_def)
kleing@24333
   401
  apply simp
kleing@24333
   402
  apply (auto simp add: bin_to_bl_aux_alt)
kleing@24333
   403
  done
kleing@24333
   404
huffman@24465
   405
(** links between bit-wise operations and operations on bool lists **)
huffman@24465
   406
    
haftmann@26557
   407
lemma map2_Nil [simp]: "map2 f [] ys = []"
haftmann@26557
   408
  unfolding map2_def by auto
kleing@24333
   409
haftmann@26557
   410
lemma map2_Cons [simp]:
haftmann@26557
   411
  "map2 f (x # xs) (y # ys) = f x y # map2 f xs ys"
haftmann@26557
   412
  unfolding map2_def by auto
kleing@24333
   413
kleing@24333
   414
lemma bl_xor_aux_bin [rule_format] : "ALL v w bs cs. 
haftmann@26557
   415
    map2 (%x y. x ~= y) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = 
haftmann@26557
   416
    bin_to_bl_aux n (v XOR w) (map2 (%x y. x ~= y) bs cs)"
kleing@24333
   417
  apply (induct_tac n)
kleing@24333
   418
   apply safe
kleing@24333
   419
   apply simp
kleing@24333
   420
  apply (case_tac v rule: bin_exhaust)
kleing@24333
   421
  apply (case_tac w rule: bin_exhaust)
kleing@24333
   422
  apply clarsimp
kleing@24333
   423
  apply (case_tac b)
kleing@24333
   424
  apply (case_tac ba, safe, simp_all)+
kleing@24333
   425
  done
kleing@24333
   426
    
kleing@24333
   427
lemma bl_or_aux_bin [rule_format] : "ALL v w bs cs. 
haftmann@26557
   428
    map2 (op | ) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = 
haftmann@26557
   429
    bin_to_bl_aux n (v OR w) (map2 (op | ) bs cs)" 
kleing@24333
   430
  apply (induct_tac n)
kleing@24333
   431
   apply safe
kleing@24333
   432
   apply simp
kleing@24333
   433
  apply (case_tac v rule: bin_exhaust)
kleing@24333
   434
  apply (case_tac w rule: bin_exhaust)
kleing@24333
   435
  apply clarsimp
kleing@24333
   436
  apply (case_tac b)
kleing@24333
   437
  apply (case_tac ba, safe, simp_all)+
kleing@24333
   438
  done
kleing@24333
   439
    
kleing@24333
   440
lemma bl_and_aux_bin [rule_format] : "ALL v w bs cs. 
haftmann@26557
   441
    map2 (op & ) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = 
haftmann@26557
   442
    bin_to_bl_aux n (v AND w) (map2 (op & ) bs cs)" 
kleing@24333
   443
  apply (induct_tac n)
kleing@24333
   444
   apply safe
kleing@24333
   445
   apply simp
kleing@24333
   446
  apply (case_tac v rule: bin_exhaust)
kleing@24333
   447
  apply (case_tac w rule: bin_exhaust)
kleing@24333
   448
  apply clarsimp
kleing@24333
   449
  apply (case_tac b)
kleing@24333
   450
  apply (case_tac ba, safe, simp_all)+
kleing@24333
   451
  done
kleing@24333
   452
    
kleing@24333
   453
lemma bl_not_aux_bin [rule_format] : 
kleing@24333
   454
  "ALL w cs. map Not (bin_to_bl_aux n w cs) = 
huffman@24353
   455
    bin_to_bl_aux n (NOT w) (map Not cs)"
kleing@24333
   456
  apply (induct n)
kleing@24333
   457
   apply clarsimp
kleing@24333
   458
  apply clarsimp
kleing@24333
   459
  apply (case_tac w rule: bin_exhaust)
kleing@24333
   460
  apply (case_tac b)
kleing@24333
   461
   apply auto
kleing@24333
   462
  done
kleing@24333
   463
kleing@24333
   464
lemmas bl_not_bin = bl_not_aux_bin
kleing@24333
   465
  [where cs = "[]", unfolded bin_to_bl_def [symmetric] map.simps]
kleing@24333
   466
kleing@24333
   467
lemmas bl_and_bin = bl_and_aux_bin [where bs="[]" and cs="[]", 
haftmann@26557
   468
                                    unfolded map2_Nil, folded bin_to_bl_def]
kleing@24333
   469
kleing@24333
   470
lemmas bl_or_bin = bl_or_aux_bin [where bs="[]" and cs="[]", 
haftmann@26557
   471
                                  unfolded map2_Nil, folded bin_to_bl_def]
kleing@24333
   472
kleing@24333
   473
lemmas bl_xor_bin = bl_xor_aux_bin [where bs="[]" and cs="[]", 
haftmann@26557
   474
                                    unfolded map2_Nil, folded bin_to_bl_def]
kleing@24333
   475
kleing@24333
   476
lemma drop_bin2bl_aux [rule_format] : 
kleing@24333
   477
  "ALL m bin bs. drop m (bin_to_bl_aux n bin bs) = 
kleing@24333
   478
    bin_to_bl_aux (n - m) bin (drop (m - n) bs)"
kleing@24333
   479
  apply (induct n, clarsimp)
kleing@24333
   480
  apply clarsimp
kleing@24333
   481
  apply (case_tac bin rule: bin_exhaust)
kleing@24333
   482
  apply (case_tac "m <= n", simp)
kleing@24333
   483
  apply (case_tac "m - n", simp)
kleing@24333
   484
  apply simp
kleing@24333
   485
  apply (rule_tac f = "%nat. drop nat bs" in arg_cong) 
kleing@24333
   486
  apply simp
kleing@24333
   487
  done
kleing@24333
   488
kleing@24333
   489
lemma drop_bin2bl: "drop m (bin_to_bl n bin) = bin_to_bl (n - m) bin"
kleing@24333
   490
  unfolding bin_to_bl_def by (simp add : drop_bin2bl_aux)
kleing@24333
   491
kleing@24333
   492
lemma take_bin2bl_lem1 [rule_format] : 
kleing@24333
   493
  "ALL w bs. take m (bin_to_bl_aux m w bs) = bin_to_bl m w"
kleing@24333
   494
  apply (induct m, clarsimp)
kleing@24333
   495
  apply clarsimp
kleing@24333
   496
  apply (simp add: bin_to_bl_aux_alt)
kleing@24333
   497
  apply (simp add: bin_to_bl_def)
kleing@24333
   498
  apply (simp add: bin_to_bl_aux_alt)
kleing@24333
   499
  done
kleing@24333
   500
kleing@24333
   501
lemma take_bin2bl_lem [rule_format] : 
kleing@24333
   502
  "ALL w bs. take m (bin_to_bl_aux (m + n) w bs) = 
kleing@24333
   503
    take m (bin_to_bl (m + n) w)"
kleing@24333
   504
  apply (induct n)
kleing@24333
   505
   apply clarify
kleing@24333
   506
   apply (simp_all (no_asm) add: bin_to_bl_def take_bin2bl_lem1)
kleing@24333
   507
  apply simp
kleing@24333
   508
  done
kleing@24333
   509
kleing@24333
   510
lemma bin_split_take [rule_format] : 
kleing@24333
   511
  "ALL b c. bin_split n c = (a, b) --> 
kleing@24333
   512
    bin_to_bl m a = take m (bin_to_bl (m + n) c)"
kleing@24333
   513
  apply (induct n)
kleing@24333
   514
   apply clarsimp
kleing@24333
   515
  apply (clarsimp simp: Let_def split: ls_splits)
kleing@24333
   516
  apply (simp add: bin_to_bl_def)
kleing@24333
   517
  apply (simp add: take_bin2bl_lem)
kleing@24333
   518
  done
kleing@24333
   519
kleing@24333
   520
lemma bin_split_take1: 
kleing@24333
   521
  "k = m + n ==> bin_split n c = (a, b) ==> 
kleing@24333
   522
    bin_to_bl m a = take m (bin_to_bl k c)"
kleing@24333
   523
  by (auto elim: bin_split_take)
kleing@24333
   524
  
kleing@24333
   525
lemma nth_takefill [rule_format] : "ALL m l. m < n --> 
kleing@24333
   526
    takefill fill n l ! m = (if m < length l then l ! m else fill)"
kleing@24333
   527
  apply (induct n, clarsimp)
kleing@24333
   528
  apply clarsimp
kleing@24333
   529
  apply (case_tac m)
kleing@24333
   530
   apply (simp split: list.split)
kleing@24333
   531
  apply clarsimp
kleing@24333
   532
  apply (erule allE)+
kleing@24333
   533
  apply (erule (1) impE)
kleing@24333
   534
  apply (simp split: list.split)
kleing@24333
   535
  done
kleing@24333
   536
kleing@24333
   537
lemma takefill_alt [rule_format] :
kleing@24333
   538
  "ALL l. takefill fill n l = take n l @ replicate (n - length l) fill"
kleing@24333
   539
  by (induct n) (auto split: list.split)
kleing@24333
   540
kleing@24333
   541
lemma takefill_replicate [simp]:
kleing@24333
   542
  "takefill fill n (replicate m fill) = replicate n fill"
kleing@24333
   543
  by (simp add : takefill_alt replicate_add [symmetric])
kleing@24333
   544
kleing@24333
   545
lemma takefill_le' [rule_format] :
kleing@24333
   546
  "ALL l n. n = m + k --> takefill x m (takefill x n l) = takefill x m l"
kleing@24333
   547
  by (induct m) (auto split: list.split)
kleing@24333
   548
kleing@24333
   549
lemma length_takefill [simp]: "length (takefill fill n l) = n"
kleing@24333
   550
  by (simp add : takefill_alt)
kleing@24333
   551
kleing@24333
   552
lemma take_takefill':
kleing@24333
   553
  "!!w n.  n = k + m ==> take k (takefill fill n w) = takefill fill k w"
kleing@24333
   554
  by (induct k) (auto split add : list.split) 
kleing@24333
   555
kleing@24333
   556
lemma drop_takefill:
kleing@24333
   557
  "!!w. drop k (takefill fill (m + k) w) = takefill fill m (drop k w)"
kleing@24333
   558
  by (induct k) (auto split add : list.split) 
kleing@24333
   559
kleing@24333
   560
lemma takefill_le [simp]:
kleing@24333
   561
  "m \<le> n \<Longrightarrow> takefill x m (takefill x n l) = takefill x m l"
kleing@24333
   562
  by (auto simp: le_iff_add takefill_le')
kleing@24333
   563
kleing@24333
   564
lemma take_takefill [simp]:
kleing@24333
   565
  "m \<le> n \<Longrightarrow> take m (takefill fill n w) = takefill fill m w"
kleing@24333
   566
  by (auto simp: le_iff_add take_takefill')
kleing@24333
   567
 
kleing@24333
   568
lemma takefill_append:
kleing@24333
   569
  "takefill fill (m + length xs) (xs @ w) = xs @ (takefill fill m w)"
kleing@24333
   570
  by (induct xs) auto
kleing@24333
   571
kleing@24333
   572
lemma takefill_same': 
kleing@24333
   573
  "l = length xs ==> takefill fill l xs = xs"
kleing@24333
   574
  by clarify (induct xs, auto)
kleing@24333
   575
 
kleing@24333
   576
lemmas takefill_same [simp] = takefill_same' [OF refl]
kleing@24333
   577
kleing@24333
   578
lemma takefill_bintrunc:
kleing@24333
   579
  "takefill False n bl = rev (bin_to_bl n (bl_to_bin (rev bl)))"
kleing@24333
   580
  apply (rule nth_equalityI)
kleing@24333
   581
   apply simp
kleing@24333
   582
  apply (clarsimp simp: nth_takefill nth_rev nth_bin_to_bl bin_nth_of_bl)
kleing@24333
   583
  done
kleing@24333
   584
kleing@24333
   585
lemma bl_bin_bl_rtf:
kleing@24333
   586
  "bin_to_bl n (bl_to_bin bl) = rev (takefill False n (rev bl))"
kleing@24333
   587
  by (simp add : takefill_bintrunc)
kleing@24333
   588
  
kleing@24333
   589
lemmas bl_bin_bl_rep_drop = 
kleing@24333
   590
  bl_bin_bl_rtf [simplified takefill_alt,
kleing@24333
   591
                 simplified, simplified rev_take, simplified]
kleing@24333
   592
kleing@24333
   593
lemma tf_rev:
kleing@24333
   594
  "n + k = m + length bl ==> takefill x m (rev (takefill y n bl)) = 
kleing@24333
   595
    rev (takefill y m (rev (takefill x k (rev bl))))"
kleing@24333
   596
  apply (rule nth_equalityI)
kleing@24333
   597
   apply (auto simp add: nth_takefill nth_rev)
kleing@24333
   598
  apply (rule_tac f = "%n. bl ! n" in arg_cong) 
kleing@24333
   599
  apply arith 
kleing@24333
   600
  done
kleing@24333
   601
kleing@24333
   602
lemma takefill_minus:
kleing@24333
   603
  "0 < n ==> takefill fill (Suc (n - 1)) w = takefill fill n w"
kleing@24333
   604
  by auto
kleing@24333
   605
kleing@24333
   606
lemmas takefill_Suc_cases = 
kleing@24333
   607
  list.cases [THEN takefill.Suc [THEN trans], standard]
kleing@24333
   608
kleing@24333
   609
lemmas takefill_Suc_Nil = takefill_Suc_cases (1)
kleing@24333
   610
lemmas takefill_Suc_Cons = takefill_Suc_cases (2)
kleing@24333
   611
kleing@24333
   612
lemmas takefill_minus_simps = takefill_Suc_cases [THEN [2] 
kleing@24333
   613
  takefill_minus [symmetric, THEN trans], standard]
kleing@24333
   614
kleing@24333
   615
lemmas takefill_pred_simps [simp] =
kleing@24333
   616
  takefill_minus_simps [where n="number_of bin", simplified nobm1, standard]
kleing@24333
   617
kleing@24333
   618
(* links with function bl_to_bin *)
kleing@24333
   619
kleing@24333
   620
lemma bl_to_bin_aux_cat: 
haftmann@26557
   621
  "!!nv v. bl_to_bin_aux bs (bin_cat w nv v) = 
haftmann@26557
   622
    bin_cat w (nv + length bs) (bl_to_bin_aux bs v)"
kleing@24333
   623
  apply (induct bs)
kleing@24333
   624
   apply simp
kleing@24333
   625
  apply (simp add: bin_cat_Suc_Bit [symmetric] del: bin_cat.simps)
kleing@24333
   626
  done
kleing@24333
   627
kleing@24333
   628
lemma bin_to_bl_aux_cat: 
kleing@24333
   629
  "!!w bs. bin_to_bl_aux (nv + nw) (bin_cat v nw w) bs = 
kleing@24333
   630
    bin_to_bl_aux nv v (bin_to_bl_aux nw w bs)"
kleing@24333
   631
  by (induct nw) auto 
kleing@24333
   632
kleing@24333
   633
lemmas bl_to_bin_aux_alt = 
haftmann@25919
   634
  bl_to_bin_aux_cat [where nv = "0" and v = "Int.Pls", 
kleing@24333
   635
    simplified bl_to_bin_def [symmetric], simplified]
kleing@24333
   636
kleing@24333
   637
lemmas bin_to_bl_cat =
kleing@24333
   638
  bin_to_bl_aux_cat [where bs = "[]", folded bin_to_bl_def]
kleing@24333
   639
kleing@24333
   640
lemmas bl_to_bin_aux_app_cat = 
kleing@24333
   641
  trans [OF bl_to_bin_aux_append bl_to_bin_aux_alt]
kleing@24333
   642
kleing@24333
   643
lemmas bin_to_bl_aux_cat_app =
kleing@24333
   644
  trans [OF bin_to_bl_aux_cat bin_to_bl_aux_alt]
kleing@24333
   645
kleing@24333
   646
lemmas bl_to_bin_app_cat = bl_to_bin_aux_app_cat
haftmann@25919
   647
  [where w = "Int.Pls", folded bl_to_bin_def]
kleing@24333
   648
kleing@24333
   649
lemmas bin_to_bl_cat_app = bin_to_bl_aux_cat_app
kleing@24333
   650
  [where bs = "[]", folded bin_to_bl_def]
kleing@24333
   651
kleing@24333
   652
(* bl_to_bin_app_cat_alt and bl_to_bin_app_cat are easily interderivable *)
kleing@24333
   653
lemma bl_to_bin_app_cat_alt: 
kleing@24333
   654
  "bin_cat (bl_to_bin cs) n w = bl_to_bin (cs @ bin_to_bl n w)"
kleing@24333
   655
  by (simp add : bl_to_bin_app_cat)
kleing@24333
   656
kleing@24333
   657
lemma mask_lem: "(bl_to_bin (True # replicate n False)) = 
haftmann@25919
   658
    Int.succ (bl_to_bin (replicate n True))"
kleing@24333
   659
  apply (unfold bl_to_bin_def)
kleing@24333
   660
  apply (induct n)
kleing@24333
   661
   apply simp
kleing@24333
   662
  apply (simp only: Suc_eq_add_numeral_1 replicate_add
kleing@24333
   663
                    append_Cons [symmetric] bl_to_bin_aux_append)
kleing@24333
   664
  apply simp
kleing@24333
   665
  done
kleing@24333
   666
huffman@24465
   667
(* function bl_of_nth *)
kleing@24333
   668
lemma length_bl_of_nth [simp]: "length (bl_of_nth n f) = n"
kleing@24333
   669
  by (induct n)  auto
kleing@24333
   670
kleing@24333
   671
lemma nth_bl_of_nth [simp]:
kleing@24333
   672
  "m < n \<Longrightarrow> rev (bl_of_nth n f) ! m = f m"
kleing@24333
   673
  apply (induct n)
kleing@24333
   674
   apply simp
kleing@24333
   675
  apply (clarsimp simp add : nth_append)
kleing@24333
   676
  apply (rule_tac f = "f" in arg_cong) 
kleing@24333
   677
  apply simp
kleing@24333
   678
  done
kleing@24333
   679
kleing@24333
   680
lemma bl_of_nth_inj: 
kleing@24333
   681
  "(!!k. k < n ==> f k = g k) ==> bl_of_nth n f = bl_of_nth n g"
kleing@24333
   682
  by (induct n)  auto
kleing@24333
   683
kleing@24333
   684
lemma bl_of_nth_nth_le [rule_format] : "ALL xs. 
kleing@24333
   685
    length xs >= n --> bl_of_nth n (nth (rev xs)) = drop (length xs - n) xs";
kleing@24333
   686
  apply (induct n, clarsimp)
kleing@24333
   687
  apply clarsimp
kleing@24333
   688
  apply (rule trans [OF _ hd_Cons_tl])
kleing@24333
   689
   apply (frule Suc_le_lessD)
kleing@24333
   690
   apply (simp add: nth_rev trans [OF drop_Suc drop_tl, symmetric])
kleing@24333
   691
   apply (subst hd_drop_conv_nth)
kleing@24333
   692
     apply force
kleing@24333
   693
    apply simp_all
kleing@24333
   694
  apply (rule_tac f = "%n. drop n xs" in arg_cong) 
kleing@24333
   695
  apply simp
kleing@24333
   696
  done
kleing@24333
   697
kleing@24333
   698
lemmas bl_of_nth_nth [simp] = order_refl [THEN bl_of_nth_nth_le, simplified]
kleing@24333
   699
kleing@24333
   700
lemma size_rbl_pred: "length (rbl_pred bl) = length bl"
kleing@24333
   701
  by (induct bl) auto
kleing@24333
   702
kleing@24333
   703
lemma size_rbl_succ: "length (rbl_succ bl) = length bl"
kleing@24333
   704
  by (induct bl) auto
kleing@24333
   705
kleing@24333
   706
lemma size_rbl_add:
kleing@24333
   707
  "!!cl. length (rbl_add bl cl) = length bl"
kleing@24333
   708
  by (induct bl) (auto simp: Let_def size_rbl_succ)
kleing@24333
   709
kleing@24333
   710
lemma size_rbl_mult: 
kleing@24333
   711
  "!!cl. length (rbl_mult bl cl) = length bl"
kleing@24333
   712
  by (induct bl) (auto simp add : Let_def size_rbl_add)
kleing@24333
   713
kleing@24333
   714
lemmas rbl_sizes [simp] = 
kleing@24333
   715
  size_rbl_pred size_rbl_succ size_rbl_add size_rbl_mult
kleing@24333
   716
kleing@24333
   717
lemmas rbl_Nils =
kleing@24333
   718
  rbl_pred.Nil rbl_succ.Nil rbl_add.Nil rbl_mult.Nil
kleing@24333
   719
kleing@24333
   720
lemma rbl_pred: 
haftmann@25919
   721
  "!!bin. rbl_pred (rev (bin_to_bl n bin)) = rev (bin_to_bl n (Int.pred bin))"
kleing@24333
   722
  apply (induct n, simp)
kleing@24333
   723
  apply (unfold bin_to_bl_def)
kleing@24333
   724
  apply clarsimp
kleing@24333
   725
  apply (case_tac bin rule: bin_exhaust)
kleing@24333
   726
  apply (case_tac b)
kleing@24333
   727
   apply (clarsimp simp: bin_to_bl_aux_alt)+
kleing@24333
   728
  done
kleing@24333
   729
kleing@24333
   730
lemma rbl_succ: 
haftmann@25919
   731
  "!!bin. rbl_succ (rev (bin_to_bl n bin)) = rev (bin_to_bl n (Int.succ bin))"
kleing@24333
   732
  apply (induct n, simp)
kleing@24333
   733
  apply (unfold bin_to_bl_def)
kleing@24333
   734
  apply clarsimp
kleing@24333
   735
  apply (case_tac bin rule: bin_exhaust)
kleing@24333
   736
  apply (case_tac b)
kleing@24333
   737
   apply (clarsimp simp: bin_to_bl_aux_alt)+
kleing@24333
   738
  done
kleing@24333
   739
kleing@24333
   740
lemma rbl_add: 
kleing@24333
   741
  "!!bina binb. rbl_add (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb)) = 
kleing@24333
   742
    rev (bin_to_bl n (bina + binb))"
kleing@24333
   743
  apply (induct n, simp)
kleing@24333
   744
  apply (unfold bin_to_bl_def)
kleing@24333
   745
  apply clarsimp
kleing@24333
   746
  apply (case_tac bina rule: bin_exhaust)
kleing@24333
   747
  apply (case_tac binb rule: bin_exhaust)
kleing@24333
   748
  apply (case_tac b)
kleing@24333
   749
   apply (case_tac [!] "ba")
kleing@24333
   750
     apply (auto simp: rbl_succ succ_def bin_to_bl_aux_alt Let_def add_ac)
kleing@24333
   751
  done
kleing@24333
   752
kleing@24333
   753
lemma rbl_add_app2: 
kleing@24333
   754
  "!!blb. length blb >= length bla ==> 
kleing@24333
   755
    rbl_add bla (blb @ blc) = rbl_add bla blb"
kleing@24333
   756
  apply (induct bla, simp)
kleing@24333
   757
  apply clarsimp
kleing@24333
   758
  apply (case_tac blb, clarsimp)
kleing@24333
   759
  apply (clarsimp simp: Let_def)
kleing@24333
   760
  done
kleing@24333
   761
kleing@24333
   762
lemma rbl_add_take2: 
kleing@24333
   763
  "!!blb. length blb >= length bla ==> 
kleing@24333
   764
    rbl_add bla (take (length bla) blb) = rbl_add bla blb"
kleing@24333
   765
  apply (induct bla, simp)
kleing@24333
   766
  apply clarsimp
kleing@24333
   767
  apply (case_tac blb, clarsimp)
kleing@24333
   768
  apply (clarsimp simp: Let_def)
kleing@24333
   769
  done
kleing@24333
   770
kleing@24333
   771
lemma rbl_add_long: 
kleing@24333
   772
  "m >= n ==> rbl_add (rev (bin_to_bl n bina)) (rev (bin_to_bl m binb)) = 
kleing@24333
   773
    rev (bin_to_bl n (bina + binb))"
kleing@24333
   774
  apply (rule box_equals [OF _ rbl_add_take2 rbl_add])
kleing@24333
   775
   apply (rule_tac f = "rbl_add (rev (bin_to_bl n bina))" in arg_cong) 
kleing@24333
   776
   apply (rule rev_swap [THEN iffD1])
kleing@24333
   777
   apply (simp add: rev_take drop_bin2bl)
kleing@24333
   778
  apply simp
kleing@24333
   779
  done
kleing@24333
   780
kleing@24333
   781
lemma rbl_mult_app2:
kleing@24333
   782
  "!!blb. length blb >= length bla ==> 
kleing@24333
   783
    rbl_mult bla (blb @ blc) = rbl_mult bla blb"
kleing@24333
   784
  apply (induct bla, simp)
kleing@24333
   785
  apply clarsimp
kleing@24333
   786
  apply (case_tac blb, clarsimp)
kleing@24333
   787
  apply (clarsimp simp: Let_def rbl_add_app2)
kleing@24333
   788
  done
kleing@24333
   789
kleing@24333
   790
lemma rbl_mult_take2: 
kleing@24333
   791
  "length blb >= length bla ==> 
kleing@24333
   792
    rbl_mult bla (take (length bla) blb) = rbl_mult bla blb"
kleing@24333
   793
  apply (rule trans)
kleing@24333
   794
   apply (rule rbl_mult_app2 [symmetric])
kleing@24333
   795
   apply simp
kleing@24333
   796
  apply (rule_tac f = "rbl_mult bla" in arg_cong) 
kleing@24333
   797
  apply (rule append_take_drop_id)
kleing@24333
   798
  done
kleing@24333
   799
    
kleing@24333
   800
lemma rbl_mult_gt1: 
kleing@24333
   801
  "m >= length bl ==> rbl_mult bl (rev (bin_to_bl m binb)) = 
kleing@24333
   802
    rbl_mult bl (rev (bin_to_bl (length bl) binb))"
kleing@24333
   803
  apply (rule trans)
kleing@24333
   804
   apply (rule rbl_mult_take2 [symmetric])
kleing@24333
   805
   apply simp_all
kleing@24333
   806
  apply (rule_tac f = "rbl_mult bl" in arg_cong) 
kleing@24333
   807
  apply (rule rev_swap [THEN iffD1])
kleing@24333
   808
  apply (simp add: rev_take drop_bin2bl)
kleing@24333
   809
  done
kleing@24333
   810
    
kleing@24333
   811
lemma rbl_mult_gt: 
kleing@24333
   812
  "m > n ==> rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl m binb)) = 
kleing@24333
   813
    rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb))"
kleing@24333
   814
  by (auto intro: trans [OF rbl_mult_gt1])
kleing@24333
   815
  
kleing@24333
   816
lemmas rbl_mult_Suc = lessI [THEN rbl_mult_gt]
kleing@24333
   817
kleing@24333
   818
lemma rbbl_Cons: 
kleing@24333
   819
  "b # rev (bin_to_bl n x) = rev (bin_to_bl (Suc n) (x BIT If b bit.B1 bit.B0))"
kleing@24333
   820
  apply (unfold bin_to_bl_def)
kleing@24333
   821
  apply simp
kleing@24333
   822
  apply (simp add: bin_to_bl_aux_alt)
kleing@24333
   823
  done
kleing@24333
   824
  
kleing@24333
   825
lemma rbl_mult: "!!bina binb. 
kleing@24333
   826
    rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb)) = 
kleing@24333
   827
    rev (bin_to_bl n (bina * binb))"
kleing@24333
   828
  apply (induct n)
kleing@24333
   829
   apply simp
kleing@24333
   830
  apply (unfold bin_to_bl_def)
kleing@24333
   831
  apply clarsimp
kleing@24333
   832
  apply (case_tac bina rule: bin_exhaust)
kleing@24333
   833
  apply (case_tac binb rule: bin_exhaust)
kleing@24333
   834
  apply (case_tac b)
kleing@24333
   835
   apply (case_tac [!] "ba")
kleing@24333
   836
     apply (auto simp: bin_to_bl_aux_alt Let_def)
kleing@24333
   837
     apply (auto simp: rbbl_Cons rbl_mult_Suc rbl_add)
kleing@24333
   838
  done
kleing@24333
   839
kleing@24333
   840
lemma rbl_add_split: 
kleing@24333
   841
  "P (rbl_add (y # ys) (x # xs)) = 
kleing@24333
   842
    (ALL ws. length ws = length ys --> ws = rbl_add ys xs --> 
wenzelm@26008
   843
    (y --> ((x --> P (False # rbl_succ ws)) & (~ x -->  P (True # ws)))) &
kleing@24333
   844
    (~ y --> P (x # ws)))"
kleing@24333
   845
  apply (auto simp add: Let_def)
kleing@24333
   846
   apply (case_tac [!] "y")
kleing@24333
   847
     apply auto
kleing@24333
   848
  done
kleing@24333
   849
kleing@24333
   850
lemma rbl_mult_split: 
kleing@24333
   851
  "P (rbl_mult (y # ys) xs) = 
kleing@24333
   852
    (ALL ws. length ws = Suc (length ys) --> ws = False # rbl_mult ys xs --> 
kleing@24333
   853
    (y --> P (rbl_add ws xs)) & (~ y -->  P ws))"
kleing@24333
   854
  by (clarsimp simp add : Let_def)
kleing@24333
   855
  
kleing@24333
   856
lemma and_len: "xs = ys ==> xs = ys & length xs = length ys"
kleing@24333
   857
  by auto
kleing@24333
   858
kleing@24333
   859
lemma size_if: "size (if p then xs else ys) = (if p then size xs else size ys)"
kleing@24333
   860
  by auto
kleing@24333
   861
kleing@24333
   862
lemma tl_if: "tl (if p then xs else ys) = (if p then tl xs else tl ys)"
kleing@24333
   863
  by auto
kleing@24333
   864
kleing@24333
   865
lemma hd_if: "hd (if p then xs else ys) = (if p then hd xs else hd ys)"
kleing@24333
   866
  by auto
kleing@24333
   867
huffman@24465
   868
lemma if_Not_x: "(if p then ~ x else x) = (p = (~ x))"
huffman@24465
   869
  by auto
huffman@24465
   870
huffman@24465
   871
lemma if_x_Not: "(if p then x else ~ x) = (p = x)"
huffman@24465
   872
  by auto
huffman@24465
   873
kleing@24333
   874
lemma if_same_and: "(If p x y & If p u v) = (if p then x & u else y & v)"
kleing@24333
   875
  by auto
kleing@24333
   876
kleing@24333
   877
lemma if_same_eq: "(If p x y  = (If p u v)) = (if p then x = (u) else y = (v))"
kleing@24333
   878
  by auto
kleing@24333
   879
kleing@24333
   880
lemma if_same_eq_not:
kleing@24333
   881
  "(If p x y  = (~ If p u v)) = (if p then x = (~u) else y = (~v))"
kleing@24333
   882
  by auto
kleing@24333
   883
kleing@24333
   884
(* note - if_Cons can cause blowup in the size, if p is complex,
kleing@24333
   885
  so make a simproc *)
kleing@24333
   886
lemma if_Cons: "(if p then x # xs else y # ys) = If p x y # If p xs ys"
kleing@24333
   887
  by auto
kleing@24333
   888
kleing@24333
   889
lemma if_single:
kleing@24333
   890
  "(if xc then [xab] else [an]) = [if xc then xab else an]"
kleing@24333
   891
  by auto
kleing@24333
   892
huffman@24465
   893
lemma if_bool_simps:
huffman@24465
   894
  "If p True y = (p | y) & If p False y = (~p & y) & 
huffman@24465
   895
    If p y True = (p --> y) & If p y False = (p & y)"
huffman@24465
   896
  by auto
huffman@24465
   897
huffman@24465
   898
lemmas if_simps = if_x_Not if_Not_x if_cancel if_True if_False if_bool_simps
huffman@24465
   899
wenzelm@25350
   900
lemmas seqr = eq_reflection [where x = "size w", standard]
kleing@24333
   901
kleing@24333
   902
lemmas tl_Nil = tl.simps (1)
kleing@24333
   903
lemmas tl_Cons = tl.simps (2)
kleing@24333
   904
kleing@24333
   905
huffman@24350
   906
subsection "Repeated splitting or concatenation"
kleing@24333
   907
kleing@24333
   908
lemma sclem:
kleing@24333
   909
  "size (concat (map (bin_to_bl n) xs)) = length xs * n"
kleing@24333
   910
  by (induct xs) auto
kleing@24333
   911
kleing@24333
   912
lemma bin_cat_foldl_lem [rule_format] :
kleing@24333
   913
  "ALL x. foldl (%u. bin_cat u n) x xs = 
kleing@24333
   914
    bin_cat x (size xs * n) (foldl (%u. bin_cat u n) y xs)"
kleing@24333
   915
  apply (induct xs)
kleing@24333
   916
   apply simp
kleing@24333
   917
  apply clarify
kleing@24333
   918
  apply (simp (no_asm))
kleing@24333
   919
  apply (frule asm_rl)
kleing@24333
   920
  apply (drule spec)
kleing@24333
   921
  apply (erule trans)
kleing@24333
   922
  apply (drule_tac x = "bin_cat y n a" in spec) 
kleing@24333
   923
  apply (simp add : bin_cat_assoc_sym min_def)
kleing@24333
   924
  done
kleing@24333
   925
kleing@24333
   926
lemma bin_rcat_bl:
kleing@24333
   927
  "(bin_rcat n wl) = bl_to_bin (concat (map (bin_to_bl n) wl))"
kleing@24333
   928
  apply (unfold bin_rcat_def)
kleing@24333
   929
  apply (rule sym)
kleing@24333
   930
  apply (induct wl)
kleing@24333
   931
   apply (auto simp add : bl_to_bin_append)
kleing@24333
   932
  apply (simp add : bl_to_bin_aux_alt sclem)
kleing@24333
   933
  apply (simp add : bin_cat_foldl_lem [symmetric])
kleing@24333
   934
  done
kleing@24333
   935
kleing@24333
   936
lemmas bin_rsplit_aux_simps = bin_rsplit_aux.simps bin_rsplitl_aux.simps
kleing@24333
   937
lemmas rsplit_aux_simps = bin_rsplit_aux_simps
kleing@24333
   938
wenzelm@25350
   939
lemmas th_if_simp1 = split_if [where P = "op = l",
kleing@24333
   940
  THEN iffD1, THEN conjunct1, THEN mp, standard]
wenzelm@25350
   941
lemmas th_if_simp2 = split_if [where P = "op = l",
kleing@24333
   942
  THEN iffD1, THEN conjunct2, THEN mp, standard]
kleing@24333
   943
kleing@24333
   944
lemmas rsplit_aux_simp1s = rsplit_aux_simps [THEN th_if_simp1]
kleing@24333
   945
kleing@24333
   946
lemmas rsplit_aux_simp2ls = rsplit_aux_simps [THEN th_if_simp2]
kleing@24333
   947
(* these safe to [simp add] as require calculating m - n *)
kleing@24333
   948
lemmas bin_rsplit_aux_simp2s [simp] = rsplit_aux_simp2ls [unfolded Let_def]
kleing@24333
   949
lemmas rbscl = bin_rsplit_aux_simp2s (2)
kleing@24333
   950
kleing@24333
   951
lemmas rsplit_aux_0_simps [simp] = 
kleing@24333
   952
  rsplit_aux_simp1s [OF disjI1] rsplit_aux_simp1s [OF disjI2]
kleing@24333
   953
kleing@24333
   954
lemma bin_rsplit_aux_append:
haftmann@26557
   955
  "bin_rsplit_aux n m c (bs @ cs) = bin_rsplit_aux n m c bs @ cs"
haftmann@26557
   956
  apply (induct n m c bs rule: bin_rsplit_aux.induct)
kleing@24333
   957
  apply (subst bin_rsplit_aux.simps)
kleing@24333
   958
  apply (subst bin_rsplit_aux.simps)
kleing@24333
   959
  apply (clarsimp split: ls_splits)
haftmann@26557
   960
  apply auto
kleing@24333
   961
  done
kleing@24333
   962
kleing@24333
   963
lemma bin_rsplitl_aux_append:
haftmann@26557
   964
  "bin_rsplitl_aux n m c (bs @ cs) = bin_rsplitl_aux n m c bs @ cs"
haftmann@26557
   965
  apply (induct n m c bs rule: bin_rsplitl_aux.induct)
kleing@24333
   966
  apply (subst bin_rsplitl_aux.simps)
kleing@24333
   967
  apply (subst bin_rsplitl_aux.simps)
kleing@24333
   968
  apply (clarsimp split: ls_splits)
haftmann@26557
   969
  apply auto
kleing@24333
   970
  done
kleing@24333
   971
kleing@24333
   972
lemmas rsplit_aux_apps [where bs = "[]"] =
kleing@24333
   973
  bin_rsplit_aux_append bin_rsplitl_aux_append
kleing@24333
   974
kleing@24333
   975
lemmas rsplit_def_auxs = bin_rsplit_def bin_rsplitl_def
kleing@24333
   976
kleing@24333
   977
lemmas rsplit_aux_alts = rsplit_aux_apps 
kleing@24333
   978
  [unfolded append_Nil rsplit_def_auxs [symmetric]]
kleing@24333
   979
kleing@24333
   980
lemma bin_split_minus: "0 < n ==> bin_split (Suc (n - 1)) w = bin_split n w"
kleing@24333
   981
  by auto
kleing@24333
   982
kleing@24333
   983
lemmas bin_split_minus_simp =
kleing@24333
   984
  bin_split.Suc [THEN [2] bin_split_minus [symmetric, THEN trans], standard]
kleing@24333
   985
kleing@24333
   986
lemma bin_split_pred_simp [simp]: 
kleing@24333
   987
  "(0::nat) < number_of bin \<Longrightarrow>
kleing@24333
   988
  bin_split (number_of bin) w =
haftmann@25919
   989
  (let (w1, w2) = bin_split (number_of (Int.pred bin)) (bin_rest w)
kleing@24333
   990
   in (w1, w2 BIT bin_last w))" 
kleing@24333
   991
  by (simp only: nobm1 bin_split_minus_simp)
kleing@24333
   992
huffman@24465
   993
declare bin_split_pred_simp [simp]
huffman@24465
   994
kleing@24333
   995
lemma bin_rsplit_aux_simp_alt:
haftmann@26557
   996
  "bin_rsplit_aux n m c bs =
kleing@24333
   997
   (if m = 0 \<or> n = 0 
kleing@24333
   998
   then bs
kleing@24333
   999
   else let (a, b) = bin_split n c in bin_rsplit n (m - n, a) @ b # bs)"
haftmann@26557
  1000
  unfolding bin_rsplit_aux.simps [of n m c bs]
haftmann@26557
  1001
  apply simp
haftmann@26557
  1002
  apply (subst rsplit_aux_alts)
haftmann@26557
  1003
  apply (simp add: bin_rsplit_def)
kleing@24333
  1004
  done
kleing@24333
  1005
kleing@24333
  1006
lemmas bin_rsplit_simp_alt = 
haftmann@26557
  1007
  trans [OF bin_rsplit_def
kleing@24333
  1008
            bin_rsplit_aux_simp_alt, standard]
kleing@24333
  1009
kleing@24333
  1010
lemmas bthrs = bin_rsplit_simp_alt [THEN [2] trans]
kleing@24333
  1011
kleing@24333
  1012
lemma bin_rsplit_size_sign' [rule_format] : 
kleing@24333
  1013
  "n > 0 ==> (ALL nw w. rev sw = bin_rsplit n (nw, w) --> 
kleing@24333
  1014
    (ALL v: set sw. bintrunc n v = v))"
kleing@24333
  1015
  apply (induct sw)
kleing@24333
  1016
   apply clarsimp
kleing@24333
  1017
  apply clarsimp
kleing@24333
  1018
  apply (drule bthrs)
kleing@24333
  1019
  apply (simp (no_asm_use) add: Let_def split: ls_splits)
kleing@24333
  1020
  apply clarify
kleing@24333
  1021
  apply (erule impE, rule exI, erule exI)
kleing@24333
  1022
  apply (drule split_bintrunc)
kleing@24333
  1023
  apply simp
kleing@24333
  1024
  done
kleing@24333
  1025
kleing@24333
  1026
lemmas bin_rsplit_size_sign = bin_rsplit_size_sign' [OF asm_rl 
kleing@24333
  1027
  rev_rev_ident [THEN trans] set_rev [THEN equalityD2 [THEN subsetD]],
kleing@24333
  1028
  standard]
kleing@24333
  1029
kleing@24333
  1030
lemma bin_nth_rsplit [rule_format] :
kleing@24333
  1031
  "n > 0 ==> m < n ==> (ALL w k nw. rev sw = bin_rsplit n (nw, w) --> 
kleing@24333
  1032
       k < size sw --> bin_nth (sw ! k) m = bin_nth w (k * n + m))"
kleing@24333
  1033
  apply (induct sw)
kleing@24333
  1034
   apply clarsimp
kleing@24333
  1035
  apply clarsimp
kleing@24333
  1036
  apply (drule bthrs)
kleing@24333
  1037
  apply (simp (no_asm_use) add: Let_def split: ls_splits)
kleing@24333
  1038
  apply clarify
kleing@24333
  1039
  apply (erule allE, erule impE, erule exI)
kleing@24333
  1040
  apply (case_tac k)
kleing@24333
  1041
   apply clarsimp   
kleing@24333
  1042
   prefer 2
kleing@24333
  1043
   apply clarsimp
kleing@24333
  1044
   apply (erule allE)
kleing@24333
  1045
   apply (erule (1) impE)
kleing@24333
  1046
   apply (drule bin_nth_split, erule conjE, erule allE,
kleing@24333
  1047
          erule trans, simp add : add_ac)+
kleing@24333
  1048
  done
kleing@24333
  1049
kleing@24333
  1050
lemma bin_rsplit_all:
kleing@24333
  1051
  "0 < nw ==> nw <= n ==> bin_rsplit n (nw, w) = [bintrunc n w]"
kleing@24333
  1052
  unfolding bin_rsplit_def
kleing@24333
  1053
  by (clarsimp dest!: split_bintrunc simp: rsplit_aux_simp2ls split: ls_splits)
kleing@24333
  1054
kleing@24333
  1055
lemma bin_rsplit_l [rule_format] :
kleing@24333
  1056
  "ALL bin. bin_rsplitl n (m, bin) = bin_rsplit n (m, bintrunc m bin)"
kleing@24333
  1057
  apply (rule_tac a = "m" in wf_less_than [THEN wf_induct])
kleing@24333
  1058
  apply (simp (no_asm) add : bin_rsplitl_def bin_rsplit_def)
kleing@24333
  1059
  apply (rule allI)
kleing@24333
  1060
  apply (subst bin_rsplitl_aux.simps)
kleing@24333
  1061
  apply (subst bin_rsplit_aux.simps)
haftmann@26557
  1062
  apply (clarsimp simp: Let_def split: ls_splits)
kleing@24333
  1063
  apply (drule bin_split_trunc)
kleing@24333
  1064
  apply (drule sym [THEN trans], assumption)
haftmann@26557
  1065
  apply (subst rsplit_aux_alts(1))
haftmann@26557
  1066
  apply (subst rsplit_aux_alts(2))
haftmann@26557
  1067
  apply clarsimp
haftmann@26557
  1068
  unfolding bin_rsplit_def bin_rsplitl_def
haftmann@26557
  1069
  apply simp
kleing@24333
  1070
  done
haftmann@26557
  1071
kleing@24333
  1072
lemma bin_rsplit_rcat [rule_format] :
kleing@24333
  1073
  "n > 0 --> bin_rsplit n (n * size ws, bin_rcat n ws) = map (bintrunc n) ws"
kleing@24333
  1074
  apply (unfold bin_rsplit_def bin_rcat_def)
kleing@24333
  1075
  apply (rule_tac xs = "ws" in rev_induct)
kleing@24333
  1076
   apply clarsimp
kleing@24333
  1077
  apply clarsimp
haftmann@26557
  1078
  apply (subst rsplit_aux_alts)
haftmann@26557
  1079
  unfolding bin_split_cat
haftmann@26557
  1080
  apply simp
kleing@24333
  1081
  done
kleing@24333
  1082
kleing@24333
  1083
lemma bin_rsplit_aux_len_le [rule_format] :
haftmann@26557
  1084
  "\<forall>ws m. n \<noteq> 0 \<longrightarrow> ws = bin_rsplit_aux n nw w bs \<longrightarrow>
haftmann@26557
  1085
    length ws \<le> m \<longleftrightarrow> nw + length bs * n \<le> m * n"
haftmann@26557
  1086
  apply (induct n nw w bs rule: bin_rsplit_aux.induct)
kleing@24333
  1087
  apply (subst bin_rsplit_aux.simps)
haftmann@26557
  1088
  apply (simp add: lrlem Let_def split: ls_splits)
kleing@24333
  1089
  done
kleing@24333
  1090
kleing@24333
  1091
lemma bin_rsplit_len_le: 
nipkow@25134
  1092
  "n \<noteq> 0 --> ws = bin_rsplit n (nw, w) --> (length ws <= m) = (nw <= m * n)"
kleing@24333
  1093
  unfolding bin_rsplit_def by (clarsimp simp add : bin_rsplit_aux_len_le)
kleing@24333
  1094
 
kleing@24333
  1095
lemma bin_rsplit_aux_len [rule_format] :
haftmann@26557
  1096
  "n\<noteq>0 --> length (bin_rsplit_aux n nw w cs) = 
kleing@24333
  1097
    (nw + n - 1) div n + length cs"
haftmann@26557
  1098
  apply (induct n nw w cs rule: bin_rsplit_aux.induct)
kleing@24333
  1099
  apply (subst bin_rsplit_aux.simps)
kleing@24333
  1100
  apply (clarsimp simp: Let_def split: ls_splits)
kleing@24333
  1101
  apply (erule thin_rl)
haftmann@27651
  1102
  apply (case_tac m)
haftmann@27651
  1103
  apply simp
kleing@24333
  1104
  apply (case_tac "m <= n")
haftmann@27677
  1105
  apply auto
kleing@24333
  1106
  done
kleing@24333
  1107
kleing@24333
  1108
lemma bin_rsplit_len: 
nipkow@25134
  1109
  "n\<noteq>0 ==> length (bin_rsplit n (nw, w)) = (nw + n - 1) div n"
kleing@24333
  1110
  unfolding bin_rsplit_def by (clarsimp simp add : bin_rsplit_aux_len)
kleing@24333
  1111
haftmann@26557
  1112
lemma bin_rsplit_aux_len_indep:
haftmann@26557
  1113
  "n \<noteq> 0 \<Longrightarrow> length bs = length cs \<Longrightarrow>
haftmann@26557
  1114
    length (bin_rsplit_aux n nw v bs) =
haftmann@26557
  1115
    length (bin_rsplit_aux n nw w cs)"
haftmann@26557
  1116
proof (induct n nw w cs arbitrary: v bs rule: bin_rsplit_aux.induct)
haftmann@26557
  1117
  case (1 n m w cs v bs) show ?case
haftmann@26557
  1118
  proof (cases "m = 0")
haftmann@26557
  1119
    case True then show ?thesis by simp
haftmann@26557
  1120
  next
haftmann@26557
  1121
    case False
haftmann@26557
  1122
    from "1.hyps" `m \<noteq> 0` `n \<noteq> 0` have hyp: "\<And>v bs. length bs = Suc (length cs) \<Longrightarrow>
haftmann@26557
  1123
      length (bin_rsplit_aux n (m - n) v bs) =
haftmann@26557
  1124
      length (bin_rsplit_aux n (m - n) (fst (bin_split n w)) (snd (bin_split n w) # cs))"
haftmann@26557
  1125
    by auto
haftmann@26557
  1126
    show ?thesis using `length bs = length cs` `n \<noteq> 0`
haftmann@26557
  1127
      by (auto simp add: bin_rsplit_aux_simp_alt Let_def bin_rsplit_len
haftmann@26557
  1128
        split: ls_splits)
haftmann@26557
  1129
  qed
haftmann@26557
  1130
qed
kleing@24333
  1131
kleing@24333
  1132
lemma bin_rsplit_len_indep: 
nipkow@25134
  1133
  "n\<noteq>0 ==> length (bin_rsplit n (nw, v)) = length (bin_rsplit n (nw, w))"
kleing@24333
  1134
  apply (unfold bin_rsplit_def)
haftmann@26557
  1135
  apply (simp (no_asm))
kleing@24333
  1136
  apply (erule bin_rsplit_aux_len_indep)
kleing@24333
  1137
  apply (rule refl)
kleing@24333
  1138
  done
kleing@24333
  1139
kleing@24333
  1140
end
kleing@24333
  1141