src/HOL/Word/Bits_Int.thy
author haftmann
Thu, 02 Jul 2020 12:10:58 +0000
changeset 71989 bad75618fb82
parent 71986 76193dd4aec8
child 71991 8bff286878bf
permissions -rw-r--r--
extraction of equations x = t from premises beneath meta-all
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     1
(*  Title:      HOL/Word/Bits_Int.thy
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     2
    Author:     Jeremy Dawson and Gerwin Klein, NICTA
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     3
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     4
Definitions and basic theorems for bit-wise logical operations
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     5
for integers expressed using Pls, Min, BIT,
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     6
and converting them to and from lists of bools.
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     7
*)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     8
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
     9
section \<open>Bitwise Operations on integers\<close>
24350
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
    10
54854
3324a0078636 prefer "Bits" as theory name for abstract bit operations, similar to "Orderings", "Lattices", "Groups" etc.
haftmann
parents: 54848
diff changeset
    11
theory Bits_Int
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
    12
  imports Misc_Auxiliary Bits
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    13
begin
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    14
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    15
subsection \<open>Implicit bit representation of \<^typ>\<open>int\<close>\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    16
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    17
abbreviation (input) bin_last :: "int \<Rightarrow> bool"
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    18
  where "bin_last \<equiv> odd"
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    19
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    20
lemma bin_last_def:
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    21
  "bin_last w \<longleftrightarrow> w mod 2 = 1"
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    22
  by (fact odd_iff_mod_2_eq_one)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    23
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
    24
abbreviation (input) bin_rest :: "int \<Rightarrow> int"
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
    25
  where "bin_rest w \<equiv> w div 2"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    26
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    27
lemma BitM_inc: "Num.BitM (Num.inc w) = Num.Bit1 w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    28
  by (induct w) simp_all
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    29
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    30
lemma bin_last_numeral_simps [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    31
  "\<not> bin_last 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    32
  "bin_last 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    33
  "bin_last (- 1)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    34
  "bin_last Numeral1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    35
  "\<not> bin_last (numeral (Num.Bit0 w))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    36
  "bin_last (numeral (Num.Bit1 w))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    37
  "\<not> bin_last (- numeral (Num.Bit0 w))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    38
  "bin_last (- numeral (Num.Bit1 w))"
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
    39
  by simp_all
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    40
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    41
lemma bin_rest_numeral_simps [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    42
  "bin_rest 0 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    43
  "bin_rest 1 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    44
  "bin_rest (- 1) = - 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    45
  "bin_rest Numeral1 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    46
  "bin_rest (numeral (Num.Bit0 w)) = numeral w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    47
  "bin_rest (numeral (Num.Bit1 w)) = numeral w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    48
  "bin_rest (- numeral (Num.Bit0 w)) = - numeral w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    49
  "bin_rest (- numeral (Num.Bit1 w)) = - numeral (w + Num.One)"
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
    50
  by simp_all
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    51
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    52
lemma bin_rl_eqI: "\<lbrakk>bin_rest x = bin_rest y; bin_last x = bin_last y\<rbrakk> \<Longrightarrow> x = y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
    53
  by (auto elim: oddE)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    54
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    55
lemma [simp]: 
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    56
  shows bin_rest_lt0: "bin_rest i < 0 \<longleftrightarrow> i < 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    57
  and  bin_rest_ge_0: "bin_rest i \<ge> 0 \<longleftrightarrow> i \<ge> 0"
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
    58
  by auto
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    59
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    60
lemma bin_rest_gt_0 [simp]: "bin_rest x > 0 \<longleftrightarrow> x > 1"
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
    61
  by auto
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    62
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    63
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    64
subsection \<open>Explicit bit representation of \<^typ>\<open>int\<close>\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    65
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    66
primrec bl_to_bin_aux :: "bool list \<Rightarrow> int \<Rightarrow> int"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    67
  where
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    68
    Nil: "bl_to_bin_aux [] w = w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
    69
  | Cons: "bl_to_bin_aux (b # bs) w = bl_to_bin_aux bs (of_bool b + 2 * w)"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    70
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    71
definition bl_to_bin :: "bool list \<Rightarrow> int"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    72
  where "bl_to_bin bs = bl_to_bin_aux bs 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    73
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    74
primrec bin_to_bl_aux :: "nat \<Rightarrow> int \<Rightarrow> bool list \<Rightarrow> bool list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    75
  where
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    76
    Z: "bin_to_bl_aux 0 w bl = bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    77
  | Suc: "bin_to_bl_aux (Suc n) w bl = bin_to_bl_aux n (bin_rest w) ((bin_last w) # bl)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    78
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    79
definition bin_to_bl :: "nat \<Rightarrow> int \<Rightarrow> bool list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    80
  where "bin_to_bl n w = bin_to_bl_aux n w []"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    81
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    82
lemma bin_to_bl_aux_zero_minus_simp [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    83
  "0 < n \<Longrightarrow> bin_to_bl_aux n 0 bl = bin_to_bl_aux (n - 1) 0 (False # bl)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    84
  by (cases n) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    85
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    86
lemma bin_to_bl_aux_minus1_minus_simp [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    87
  "0 < n \<Longrightarrow> bin_to_bl_aux n (- 1) bl = bin_to_bl_aux (n - 1) (- 1) (True # bl)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    88
  by (cases n) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    89
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    90
lemma bin_to_bl_aux_one_minus_simp [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    91
  "0 < n \<Longrightarrow> bin_to_bl_aux n 1 bl = bin_to_bl_aux (n - 1) 0 (True # bl)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    92
  by (cases n) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    93
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    94
lemma bin_to_bl_aux_Bit0_minus_simp [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    95
  "0 < n \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    96
    bin_to_bl_aux n (numeral (Num.Bit0 w)) bl = bin_to_bl_aux (n - 1) (numeral w) (False # bl)"
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
    97
  by (cases n) simp_all
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    98
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    99
lemma bin_to_bl_aux_Bit1_minus_simp [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   100
  "0 < n \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   101
    bin_to_bl_aux n (numeral (Num.Bit1 w)) bl = bin_to_bl_aux (n - 1) (numeral w) (True # bl)"
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
   102
  by (cases n) simp_all
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   103
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   104
lemma bl_to_bin_aux_append: "bl_to_bin_aux (bs @ cs) w = bl_to_bin_aux cs (bl_to_bin_aux bs w)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   105
  by (induct bs arbitrary: w) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   106
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   107
lemma bin_to_bl_aux_append: "bin_to_bl_aux n w bs @ cs = bin_to_bl_aux n w (bs @ cs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   108
  by (induct n arbitrary: w bs) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   109
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   110
lemma bl_to_bin_append: "bl_to_bin (bs @ cs) = bl_to_bin_aux cs (bl_to_bin bs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   111
  unfolding bl_to_bin_def by (rule bl_to_bin_aux_append)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   112
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   113
lemma bin_to_bl_aux_alt: "bin_to_bl_aux n w bs = bin_to_bl n w @ bs"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   114
  by (simp add: bin_to_bl_def bin_to_bl_aux_append)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   115
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   116
lemma bin_to_bl_0 [simp]: "bin_to_bl 0 bs = []"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   117
  by (auto simp: bin_to_bl_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   118
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   119
lemma size_bin_to_bl_aux: "length (bin_to_bl_aux n w bs) = n + length bs"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   120
  by (induct n arbitrary: w bs) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   121
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   122
lemma size_bin_to_bl [simp]: "length (bin_to_bl n w) = n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   123
  by (simp add: bin_to_bl_def size_bin_to_bl_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   124
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   125
lemma bl_bin_bl': "bin_to_bl (n + length bs) (bl_to_bin_aux bs w) = bin_to_bl_aux n w bs"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   126
  apply (induct bs arbitrary: w n)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   127
   apply auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   128
    apply (simp_all only: add_Suc [symmetric])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   129
    apply (auto simp add: bin_to_bl_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   130
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   131
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   132
lemma bl_bin_bl [simp]: "bin_to_bl (length bs) (bl_to_bin bs) = bs"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   133
  unfolding bl_to_bin_def
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   134
  apply (rule box_equals)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   135
    apply (rule bl_bin_bl')
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   136
   prefer 2
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   137
   apply (rule bin_to_bl_aux.Z)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   138
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   139
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   140
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   141
lemma bl_to_bin_inj: "bl_to_bin bs = bl_to_bin cs \<Longrightarrow> length bs = length cs \<Longrightarrow> bs = cs"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   142
  apply (rule_tac box_equals)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   143
    defer
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   144
    apply (rule bl_bin_bl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   145
   apply (rule bl_bin_bl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   146
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   147
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   148
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   149
lemma bl_to_bin_False [simp]: "bl_to_bin (False # bl) = bl_to_bin bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   150
  by (auto simp: bl_to_bin_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   151
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   152
lemma bl_to_bin_Nil [simp]: "bl_to_bin [] = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   153
  by (auto simp: bl_to_bin_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   154
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   155
lemma bin_to_bl_zero_aux: "bin_to_bl_aux n 0 bl = replicate n False @ bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   156
  by (induct n arbitrary: bl) (auto simp: replicate_app_Cons_same)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   157
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   158
lemma bin_to_bl_zero: "bin_to_bl n 0 = replicate n False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   159
  by (simp add: bin_to_bl_def bin_to_bl_zero_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   160
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   161
lemma bin_to_bl_minus1_aux: "bin_to_bl_aux n (- 1) bl = replicate n True @ bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   162
  by (induct n arbitrary: bl) (auto simp: replicate_app_Cons_same)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   163
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   164
lemma bin_to_bl_minus1: "bin_to_bl n (- 1) = replicate n True"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   165
  by (simp add: bin_to_bl_def bin_to_bl_minus1_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   166
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   167
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   168
subsection \<open>Bit projection\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   169
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   170
abbreviation (input) bin_nth :: \<open>int \<Rightarrow> nat \<Rightarrow> bool\<close>
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   171
  where \<open>bin_nth \<equiv> bit\<close>
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   172
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   173
lemma bin_nth_eq_iff: "bin_nth x = bin_nth y \<longleftrightarrow> x = y"
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   174
  by (simp add: bit_eq_iff fun_eq_iff)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   175
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   176
lemma bin_eqI:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   177
  "x = y" if "\<And>n. bin_nth x n \<longleftrightarrow> bin_nth y n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   178
  using that bin_nth_eq_iff [of x y] by (simp add: fun_eq_iff)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   179
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   180
lemma bin_eq_iff: "x = y \<longleftrightarrow> (\<forall>n. bin_nth x n = bin_nth y n)"
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   181
  by (fact bit_eq_iff)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   182
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   183
lemma bin_nth_zero [simp]: "\<not> bin_nth 0 n"
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   184
  by simp
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   185
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   186
lemma bin_nth_1 [simp]: "bin_nth 1 n \<longleftrightarrow> n = 0"
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   187
  by (cases n) (simp_all add: bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   188
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   189
lemma bin_nth_minus1 [simp]: "bin_nth (- 1) n"
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   190
  by (induction n) (simp_all add: bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   191
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   192
lemma bin_nth_numeral: "bin_rest x = y \<Longrightarrow> bin_nth x (numeral n) = bin_nth y (pred_numeral n)"
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   193
  by (simp add: numeral_eq_Suc bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   194
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   195
lemmas bin_nth_numeral_simps [simp] =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   196
  bin_nth_numeral [OF bin_rest_numeral_simps(2)]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   197
  bin_nth_numeral [OF bin_rest_numeral_simps(5)]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   198
  bin_nth_numeral [OF bin_rest_numeral_simps(6)]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   199
  bin_nth_numeral [OF bin_rest_numeral_simps(7)]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   200
  bin_nth_numeral [OF bin_rest_numeral_simps(8)]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   201
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   202
lemmas bin_nth_simps =
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   203
  bit_0 bit_Suc bin_nth_zero bin_nth_minus1
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   204
  bin_nth_numeral_simps
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   205
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   206
lemma nth_2p_bin: "bin_nth (2 ^ n) m = (m = n)" \<comment> \<open>for use when simplifying with \<open>bin_nth_Bit\<close>\<close>
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   207
  by (auto simp add: bit_exp_iff)
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   208
  
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   209
lemma nth_rest_power_bin: "bin_nth ((bin_rest ^^ k) w) n = bin_nth w (n + k)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   210
  apply (induct k arbitrary: n)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   211
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   212
  apply clarsimp
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   213
  apply (simp only: bit_Suc [symmetric] add_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   214
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   215
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   216
lemma bin_nth_numeral_unfold:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   217
  "bin_nth (numeral (num.Bit0 x)) n \<longleftrightarrow> n > 0 \<and> bin_nth (numeral x) (n - 1)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   218
  "bin_nth (numeral (num.Bit1 x)) n \<longleftrightarrow> (n > 0 \<longrightarrow> bin_nth (numeral x) (n - 1))"
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
   219
  by (cases n; simp)+
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   220
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   221
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   222
subsection \<open>Truncating\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   223
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   224
definition bin_sign :: "int \<Rightarrow> int"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   225
  where "bin_sign k = (if k \<ge> 0 then 0 else - 1)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   226
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   227
lemma bin_sign_simps [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   228
  "bin_sign 0 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   229
  "bin_sign 1 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   230
  "bin_sign (- 1) = - 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   231
  "bin_sign (numeral k) = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   232
  "bin_sign (- numeral k) = -1"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   233
  by (simp_all add: bin_sign_def)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   234
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   235
lemma bin_sign_rest [simp]: "bin_sign (bin_rest w) = bin_sign w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   236
  by (simp add: bin_sign_def)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   237
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   238
abbreviation (input) bintrunc :: "nat \<Rightarrow> int \<Rightarrow> int"
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   239
  where \<open>bintrunc \<equiv> take_bit\<close>
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   240
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   241
lemma bintrunc_mod2p: "bintrunc n w = w mod 2 ^ n"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   242
  by (fact take_bit_eq_mod)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   243
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   244
primrec sbintrunc :: "nat \<Rightarrow> int \<Rightarrow> int"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   245
  where
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   246
    Z : "sbintrunc 0 bin = (if odd bin then - 1 else 0)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   247
  | Suc : "sbintrunc (Suc n) bin = of_bool (odd bin) + 2 * sbintrunc n (bin div 2)"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   248
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   249
lemma sbintrunc_mod2p: "sbintrunc n w = (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   250
proof (induction n arbitrary: w)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   251
  case 0
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   252
  then show ?case
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
   253
    by (auto simp add: odd_iff_mod_2_eq_one)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   254
next
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   255
  case (Suc n)
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   256
  from Suc [of \<open>w div 2\<close>]
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   257
  show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   258
    using even_succ_mod_exp [of \<open>(b * 2 + 2 * 2 ^ n)\<close> \<open>Suc (Suc n)\<close> for b :: int]
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   259
    by (auto elim!: evenE oddE simp add: mult_mod_right ac_simps)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   260
qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   261
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   262
lemma sign_bintr: "bin_sign (bintrunc n w) = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   263
  by (simp add: bintrunc_mod2p bin_sign_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   264
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   265
lemma bintrunc_n_0 [simp]: "bintrunc n 0 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   266
  by (simp add: bintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   267
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   268
lemma sbintrunc_n_0 [simp]: "sbintrunc n 0 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   269
  by (simp add: sbintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   270
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   271
lemma sbintrunc_n_minus1 [simp]: "sbintrunc n (- 1) = -1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   272
  by (induct n) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   273
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   274
lemma bintrunc_Suc_numeral:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   275
  "bintrunc (Suc n) 1 = 1"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   276
  "bintrunc (Suc n) (- 1) = 1 + 2 * bintrunc n (- 1)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   277
  "bintrunc (Suc n) (numeral (Num.Bit0 w)) = 2 * bintrunc n (numeral w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   278
  "bintrunc (Suc n) (numeral (Num.Bit1 w)) = 1 + 2 * bintrunc n (numeral w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   279
  "bintrunc (Suc n) (- numeral (Num.Bit0 w)) = 2 * bintrunc n (- numeral w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   280
  "bintrunc (Suc n) (- numeral (Num.Bit1 w)) = 1 + 2 * bintrunc n (- numeral (w + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   281
  by (simp_all add: take_bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   282
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   283
lemma sbintrunc_0_numeral [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   284
  "sbintrunc 0 1 = -1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   285
  "sbintrunc 0 (numeral (Num.Bit0 w)) = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   286
  "sbintrunc 0 (numeral (Num.Bit1 w)) = -1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   287
  "sbintrunc 0 (- numeral (Num.Bit0 w)) = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   288
  "sbintrunc 0 (- numeral (Num.Bit1 w)) = -1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   289
  by simp_all
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   290
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   291
lemma sbintrunc_Suc_numeral:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   292
  "sbintrunc (Suc n) 1 = 1"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   293
  "sbintrunc (Suc n) (numeral (Num.Bit0 w)) = 2 * sbintrunc n (numeral w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   294
  "sbintrunc (Suc n) (numeral (Num.Bit1 w)) = 1 + 2 * sbintrunc n (numeral w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   295
  "sbintrunc (Suc n) (- numeral (Num.Bit0 w)) = 2 * sbintrunc n (- numeral w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   296
  "sbintrunc (Suc n) (- numeral (Num.Bit1 w)) = 1 + 2 * sbintrunc n (- numeral (w + Num.One))"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   297
  by simp_all
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   298
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   299
lemma bin_sign_lem: "(bin_sign (sbintrunc n bin) = -1) = bin_nth bin n"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   300
  apply (rule sym)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   301
  apply (induct n arbitrary: bin)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   302
   apply (simp_all add: bit_Suc bin_sign_def)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   303
  done
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   304
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   305
lemma nth_bintr: "bin_nth (bintrunc m w) n \<longleftrightarrow> n < m \<and> bin_nth w n"
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   306
  by (fact bit_take_bit_iff)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   307
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   308
lemma nth_sbintr: "bin_nth (sbintrunc m w) n = (if n < m then bin_nth w n else bin_nth w m)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   309
  apply (induct n arbitrary: w m)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   310
   apply (case_tac m)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   311
    apply simp_all
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   312
  apply (case_tac m)
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   313
   apply (simp_all add: bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   314
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   315
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   316
lemma bin_nth_Bit0:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   317
  "bin_nth (numeral (Num.Bit0 w)) n \<longleftrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   318
    (\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   319
  using bit_double_iff [of \<open>numeral w :: int\<close> n]
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   320
  by (auto intro: exI [of _ \<open>n - 1\<close>])
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   321
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   322
lemma bin_nth_Bit1:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   323
  "bin_nth (numeral (Num.Bit1 w)) n \<longleftrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   324
    n = 0 \<or> (\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   325
  using even_bit_succ_iff [of \<open>2 * numeral w :: int\<close> n]
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   326
    bit_double_iff [of \<open>numeral w :: int\<close> n]
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   327
  by auto
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   328
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   329
lemma bintrunc_bintrunc_l: "n \<le> m \<Longrightarrow> bintrunc m (bintrunc n w) = bintrunc n w"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   330
  by (simp add: min.absorb2)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   331
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   332
lemma sbintrunc_sbintrunc_l: "n \<le> m \<Longrightarrow> sbintrunc m (sbintrunc n w) = sbintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   333
  by (rule bin_eqI) (auto simp: nth_sbintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   334
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   335
lemma bintrunc_bintrunc_ge: "n \<le> m \<Longrightarrow> bintrunc n (bintrunc m w) = bintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   336
  by (rule bin_eqI) (auto simp: nth_bintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   337
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   338
lemma bintrunc_bintrunc_min [simp]: "bintrunc m (bintrunc n w) = bintrunc (min m n) w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   339
  by (rule bin_eqI) (auto simp: nth_bintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   340
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   341
lemma sbintrunc_sbintrunc_min [simp]: "sbintrunc m (sbintrunc n w) = sbintrunc (min m n) w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   342
  by (rule bin_eqI) (auto simp: nth_sbintr min.absorb1 min.absorb2)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   343
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   344
lemmas sbintrunc_Suc_Pls =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   345
  sbintrunc.Suc [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   346
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   347
lemmas sbintrunc_Suc_Min =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   348
  sbintrunc.Suc [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   349
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   350
lemmas sbintrunc_Sucs = sbintrunc_Suc_Pls sbintrunc_Suc_Min
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   351
  sbintrunc_Suc_numeral
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   352
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   353
lemmas sbintrunc_Pls =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   354
  sbintrunc.Z [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   355
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   356
lemmas sbintrunc_Min =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   357
  sbintrunc.Z [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   358
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   359
lemmas sbintrunc_0_simps =
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   360
  sbintrunc_Pls sbintrunc_Min
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   361
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   362
lemmas sbintrunc_simps = sbintrunc_0_simps sbintrunc_Sucs
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   363
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   364
lemma bintrunc_minus: "0 < n \<Longrightarrow> bintrunc (Suc (n - 1)) w = bintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   365
  by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   366
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   367
lemma sbintrunc_minus: "0 < n \<Longrightarrow> sbintrunc (Suc (n - 1)) w = sbintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   368
  by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   369
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   370
lemmas sbintrunc_minus_simps =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   371
  sbintrunc_Sucs [THEN [2] sbintrunc_minus [symmetric, THEN trans]]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   372
71984
10a8d943a8d8 more explicit proofs
haftmann
parents: 71957
diff changeset
   373
lemma sbintrunc_BIT_I:
10a8d943a8d8 more explicit proofs
haftmann
parents: 71957
diff changeset
   374
  \<open>0 < n \<Longrightarrow>
10a8d943a8d8 more explicit proofs
haftmann
parents: 71957
diff changeset
   375
  sbintrunc (n - 1) 0 = y \<Longrightarrow>
10a8d943a8d8 more explicit proofs
haftmann
parents: 71957
diff changeset
   376
  sbintrunc n 0 = 2 * y\<close>
10a8d943a8d8 more explicit proofs
haftmann
parents: 71957
diff changeset
   377
  by simp
10a8d943a8d8 more explicit proofs
haftmann
parents: 71957
diff changeset
   378
10a8d943a8d8 more explicit proofs
haftmann
parents: 71957
diff changeset
   379
lemma sbintrunc_Suc_Is:
10a8d943a8d8 more explicit proofs
haftmann
parents: 71957
diff changeset
   380
  \<open>sbintrunc n (- 1) = y \<Longrightarrow>
10a8d943a8d8 more explicit proofs
haftmann
parents: 71957
diff changeset
   381
  sbintrunc (Suc n) (- 1) = 1 + 2 * y\<close>
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   382
  by auto
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   383
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   384
lemma sbintrunc_Suc_lem: "sbintrunc (Suc n) x = y \<Longrightarrow> m = Suc n \<Longrightarrow> sbintrunc m x = y"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   385
  by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   386
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   387
lemmas sbintrunc_Suc_Ialts =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   388
  sbintrunc_Suc_Is [THEN sbintrunc_Suc_lem]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   389
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   390
lemma sbintrunc_bintrunc_lt: "m > n \<Longrightarrow> sbintrunc n (bintrunc m w) = sbintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   391
  by (rule bin_eqI) (auto simp: nth_sbintr nth_bintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   392
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   393
lemma bintrunc_sbintrunc_le: "m \<le> Suc n \<Longrightarrow> bintrunc m (sbintrunc n w) = bintrunc m w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   394
  apply (rule bin_eqI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   395
  using le_Suc_eq less_Suc_eq_le apply (auto simp: nth_sbintr nth_bintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   396
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   397
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   398
lemmas bintrunc_sbintrunc [simp] = order_refl [THEN bintrunc_sbintrunc_le]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   399
lemmas sbintrunc_bintrunc [simp] = lessI [THEN sbintrunc_bintrunc_lt]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   400
lemmas bintrunc_bintrunc [simp] = order_refl [THEN bintrunc_bintrunc_l]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   401
lemmas sbintrunc_sbintrunc [simp] = order_refl [THEN sbintrunc_sbintrunc_l]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   402
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   403
lemma bintrunc_sbintrunc' [simp]: "0 < n \<Longrightarrow> bintrunc n (sbintrunc (n - 1) w) = bintrunc n w"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   404
  by (cases n) simp_all
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   405
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   406
lemma sbintrunc_bintrunc' [simp]: "0 < n \<Longrightarrow> sbintrunc (n - 1) (bintrunc n w) = sbintrunc (n - 1) w"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   407
  by (cases n) simp_all
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   408
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   409
lemma bin_sbin_eq_iff: "bintrunc (Suc n) x = bintrunc (Suc n) y \<longleftrightarrow> sbintrunc n x = sbintrunc n y"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   410
  apply (rule iffI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   411
   apply (rule box_equals [OF _ sbintrunc_bintrunc sbintrunc_bintrunc])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   412
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   413
  apply (rule box_equals [OF _ bintrunc_sbintrunc bintrunc_sbintrunc])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   414
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   415
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   416
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   417
lemma bin_sbin_eq_iff':
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   418
  "0 < n \<Longrightarrow> bintrunc n x = bintrunc n y \<longleftrightarrow> sbintrunc (n - 1) x = sbintrunc (n - 1) y"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   419
  by (cases n) (simp_all add: bin_sbin_eq_iff)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   420
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   421
lemmas bintrunc_sbintruncS0 [simp] = bintrunc_sbintrunc' [unfolded One_nat_def]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   422
lemmas sbintrunc_bintruncS0 [simp] = sbintrunc_bintrunc' [unfolded One_nat_def]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   423
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   424
lemmas bintrunc_bintrunc_l' = le_add1 [THEN bintrunc_bintrunc_l]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   425
lemmas sbintrunc_sbintrunc_l' = le_add1 [THEN sbintrunc_sbintrunc_l]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   426
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   427
(* although bintrunc_minus_simps, if added to default simpset,
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   428
  tends to get applied where it's not wanted in developing the theories,
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   429
  we get a version for when the word length is given literally *)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   430
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   431
lemmas nat_non0_gr =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   432
  trans [OF iszero_def [THEN Not_eq_iff [THEN iffD2]] refl]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   433
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   434
lemma bintrunc_numeral:
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   435
  "bintrunc (numeral k) x = of_bool (odd x) + 2 * bintrunc (pred_numeral k) (x div 2)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   436
  by (simp add: numeral_eq_Suc take_bit_Suc mod_2_eq_odd)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   437
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   438
lemma sbintrunc_numeral:
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   439
  "sbintrunc (numeral k) x = of_bool (odd x) + 2 * sbintrunc (pred_numeral k) (x div 2)"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   440
  by (simp add: numeral_eq_Suc)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   441
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   442
lemma bintrunc_numeral_simps [simp]:
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   443
  "bintrunc (numeral k) (numeral (Num.Bit0 w)) =
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   444
    2 * bintrunc (pred_numeral k) (numeral w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   445
  "bintrunc (numeral k) (numeral (Num.Bit1 w)) =
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   446
    1 + 2 * bintrunc (pred_numeral k) (numeral w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   447
  "bintrunc (numeral k) (- numeral (Num.Bit0 w)) =
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   448
    2 * bintrunc (pred_numeral k) (- numeral w)"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   449
  "bintrunc (numeral k) (- numeral (Num.Bit1 w)) =
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   450
    1 + 2 * bintrunc (pred_numeral k) (- numeral (w + Num.One))"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   451
  "bintrunc (numeral k) 1 = 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   452
  by (simp_all add: bintrunc_numeral)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   453
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   454
lemma sbintrunc_numeral_simps [simp]:
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   455
  "sbintrunc (numeral k) (numeral (Num.Bit0 w)) =
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   456
    2 * sbintrunc (pred_numeral k) (numeral w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   457
  "sbintrunc (numeral k) (numeral (Num.Bit1 w)) =
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   458
    1 + 2 * sbintrunc (pred_numeral k) (numeral w)"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   459
  "sbintrunc (numeral k) (- numeral (Num.Bit0 w)) =
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   460
    2 * sbintrunc (pred_numeral k) (- numeral w)"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   461
  "sbintrunc (numeral k) (- numeral (Num.Bit1 w)) =
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   462
    1 + 2 * sbintrunc (pred_numeral k) (- numeral (w + Num.One))"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   463
  "sbintrunc (numeral k) 1 = 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   464
  by (simp_all add: sbintrunc_numeral)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   465
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   466
lemma no_bintr_alt1: "bintrunc n = (\<lambda>w. w mod 2 ^ n :: int)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   467
  by (rule ext) (rule bintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   468
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   469
lemma range_bintrunc: "range (bintrunc n) = {i. 0 \<le> i \<and> i < 2 ^ n}"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   470
  apply (unfold no_bintr_alt1)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   471
  apply (auto simp add: image_iff)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   472
  apply (rule exI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   473
  apply (rule sym)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   474
  using int_mod_lem [symmetric, of "2 ^ n"]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   475
  apply auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   476
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   477
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   478
lemma no_sbintr_alt2: "sbintrunc n = (\<lambda>w. (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   479
  by (rule ext) (simp add : sbintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   480
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   481
lemma range_sbintrunc: "range (sbintrunc n) = {i. - (2 ^ n) \<le> i \<and> i < 2 ^ n}"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   482
  apply (unfold no_sbintr_alt2)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   483
  apply (auto simp add: image_iff eq_diff_eq)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   484
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   485
  apply (rule exI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   486
  apply (auto intro: int_mod_lem [THEN iffD1, symmetric])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   487
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   488
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   489
lemma sb_inc_lem: "a + 2^k < 0 \<Longrightarrow> a + 2^k + 2^(Suc k) \<le> (a + 2^k) mod 2^(Suc k)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   490
  for a :: int
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   491
  using int_mod_ge' [where n = "2 ^ (Suc k)" and b = "a + 2 ^ k"]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   492
  by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   493
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   494
lemma sb_inc_lem': "a < - (2^k) \<Longrightarrow> a + 2^k + 2^(Suc k) \<le> (a + 2^k) mod 2^(Suc k)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   495
  for a :: int
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   496
  by (rule sb_inc_lem) simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   497
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   498
lemma sbintrunc_inc: "x < - (2^n) \<Longrightarrow> x + 2^(Suc n) \<le> sbintrunc n x"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   499
  unfolding no_sbintr_alt2 by (drule sb_inc_lem') simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   500
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   501
lemma sb_dec_lem: "0 \<le> - (2 ^ k) + a \<Longrightarrow> (a + 2 ^ k) mod (2 * 2 ^ k) \<le> - (2 ^ k) + a"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   502
  for a :: int
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   503
  using int_mod_le'[where n = "2 ^ (Suc k)" and b = "a + 2 ^ k"] by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   504
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   505
lemma sb_dec_lem': "2 ^ k \<le> a \<Longrightarrow> (a + 2 ^ k) mod (2 * 2 ^ k) \<le> - (2 ^ k) + a"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   506
  for a :: int
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   507
  by (rule sb_dec_lem) simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   508
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   509
lemma sbintrunc_dec: "x \<ge> (2 ^ n) \<Longrightarrow> x - 2 ^ (Suc n) >= sbintrunc n x"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   510
  unfolding no_sbintr_alt2 by (drule sb_dec_lem') simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   511
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   512
lemma bintr_ge0: "0 \<le> bintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   513
  by (simp add: bintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   514
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   515
lemma bintr_lt2p: "bintrunc n w < 2 ^ n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   516
  by (simp add: bintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   517
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   518
lemma bintr_Min: "bintrunc n (- 1) = 2 ^ n - 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   519
  by (simp add: bintrunc_mod2p m1mod2k)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   520
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   521
lemma sbintr_ge: "- (2 ^ n) \<le> sbintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   522
  by (simp add: sbintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   523
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   524
lemma sbintr_lt: "sbintrunc n w < 2 ^ n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   525
  by (simp add: sbintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   526
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   527
lemma sign_Pls_ge_0: "bin_sign bin = 0 \<longleftrightarrow> bin \<ge> 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   528
  for bin :: int
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   529
  by (simp add: bin_sign_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   530
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   531
lemma sign_Min_lt_0: "bin_sign bin = -1 \<longleftrightarrow> bin < 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   532
  for bin :: int
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   533
  by (simp add: bin_sign_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   534
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   535
lemma bin_rest_trunc: "bin_rest (bintrunc n bin) = bintrunc (n - 1) (bin_rest bin)"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   536
  by (simp add: take_bit_rec [of n bin])
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   537
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   538
lemma bin_rest_power_trunc:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   539
  "(bin_rest ^^ k) (bintrunc n bin) = bintrunc (n - k) ((bin_rest ^^ k) bin)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   540
  by (induct k) (auto simp: bin_rest_trunc)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   541
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   542
lemma bin_rest_trunc_i: "bintrunc n (bin_rest bin) = bin_rest (bintrunc (Suc n) bin)"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   543
  by (auto simp add: take_bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   544
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   545
lemma bin_rest_strunc: "bin_rest (sbintrunc (Suc n) bin) = sbintrunc n (bin_rest bin)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   546
  by (induct n arbitrary: bin) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   547
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   548
lemma bintrunc_rest [simp]: "bintrunc n (bin_rest (bintrunc n bin)) = bin_rest (bintrunc n bin)"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   549
  by (induct n arbitrary: bin) (simp_all add: take_bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   550
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   551
lemma sbintrunc_rest [simp]: "sbintrunc n (bin_rest (sbintrunc n bin)) = bin_rest (sbintrunc n bin)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   552
  by (induct n arbitrary: bin) simp_all
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   553
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   554
lemma bintrunc_rest': "bintrunc n \<circ> bin_rest \<circ> bintrunc n = bin_rest \<circ> bintrunc n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   555
  by (rule ext) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   556
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   557
lemma sbintrunc_rest': "sbintrunc n \<circ> bin_rest \<circ> sbintrunc n = bin_rest \<circ> sbintrunc n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   558
  by (rule ext) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   559
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   560
lemma rco_lem: "f \<circ> g \<circ> f = g \<circ> f \<Longrightarrow> f \<circ> (g \<circ> f) ^^ n = g ^^ n \<circ> f"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   561
  apply (rule ext)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   562
  apply (induct_tac n)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   563
   apply (simp_all (no_asm))
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   564
  apply (drule fun_cong)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   565
  apply (unfold o_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   566
  apply (erule trans)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   567
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   568
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   569
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   570
lemmas rco_bintr = bintrunc_rest'
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   571
  [THEN rco_lem [THEN fun_cong], unfolded o_def]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   572
lemmas rco_sbintr = sbintrunc_rest'
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   573
  [THEN rco_lem [THEN fun_cong], unfolded o_def]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   574
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   575
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   576
subsection \<open>Splitting and concatenation\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   577
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   578
definition bin_split :: \<open>nat \<Rightarrow> int \<Rightarrow> int \<times> int\<close>
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   579
  where [simp]: \<open>bin_split n k = (drop_bit n k, take_bit n k)\<close>
71943
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
   580
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   581
lemma [code]:
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   582
  "bin_split (Suc n) w = (let (w1, w2) = bin_split n (w div 2) in (w1, of_bool (odd w) + 2 * w2))"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   583
  "bin_split 0 w = (w, 0)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   584
  by (simp_all add: drop_bit_Suc take_bit_Suc mod_2_eq_odd)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   585
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   586
primrec bin_cat :: "int \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   587
  where
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   588
    Z: "bin_cat w 0 v = w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   589
  | Suc: "bin_cat w (Suc n) v = of_bool (odd v) + 2 * bin_cat w n (v div 2)"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   590
71943
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
   591
lemma bin_cat_eq_push_bit_add_take_bit:
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
   592
  \<open>bin_cat k n l = push_bit n k + take_bit n l\<close>
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
   593
  by (induction n arbitrary: k l)
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   594
    (simp_all add: take_bit_Suc push_bit_double mod_2_eq_odd)
71943
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
   595
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   596
lemma bin_sign_cat: "bin_sign (bin_cat x n y) = bin_sign x"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   597
proof -
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   598
  have \<open>0 \<le> x\<close> if \<open>0 \<le> x * 2 ^ n + y mod 2 ^ n\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   599
  proof -
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   600
    from that have \<open>x \<noteq> - 1\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   601
      using int_mod_le' [of \<open>y mod 2 ^ n\<close> \<open>2 ^ n\<close>] by auto
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   602
    have *: \<open>- 1 \<le> (- (y mod 2 ^ n)) div 2 ^ n\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   603
      by (simp add: zdiv_zminus1_eq_if)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   604
    from that have \<open>- (y mod 2 ^ n) \<le> x * 2 ^ n\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   605
      by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   606
    then have \<open>(- (y mod 2 ^ n)) div 2 ^ n \<le> (x * 2 ^ n) div 2 ^ n\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   607
      using zdiv_mono1 zero_less_numeral zero_less_power by blast
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   608
    with * have \<open>- 1 \<le> x * 2 ^ n div 2 ^ n\<close> by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   609
    with \<open>x \<noteq> - 1\<close> show ?thesis
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   610
      by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   611
  qed
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   612
  then show ?thesis
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   613
    by (simp add: bin_sign_def not_le not_less bin_cat_eq_push_bit_add_take_bit push_bit_eq_mult take_bit_eq_mod)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   614
qed
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   615
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   616
lemma bin_cat_assoc: "bin_cat (bin_cat x m y) n z = bin_cat x (m + n) (bin_cat y n z)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   617
  by (induct n arbitrary: z) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   618
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   619
lemma bin_cat_assoc_sym: "bin_cat x m (bin_cat y n z) = bin_cat (bin_cat x (m - n) y) (min m n) z"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   620
  apply (induct n arbitrary: z m)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   621
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   622
  apply (case_tac m, auto)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   623
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   624
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   625
definition bin_rcat :: "nat \<Rightarrow> int list \<Rightarrow> int"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   626
  where "bin_rcat n = foldl (\<lambda>u v. bin_cat u n v) 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   627
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   628
fun bin_rsplit_aux :: "nat \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   629
  where "bin_rsplit_aux n m c bs =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   630
    (if m = 0 \<or> n = 0 then bs
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   631
     else
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   632
      let (a, b) = bin_split n c
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   633
      in bin_rsplit_aux n (m - n) a (b # bs))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   634
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   635
definition bin_rsplit :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   636
  where "bin_rsplit n w = bin_rsplit_aux n (fst w) (snd w) []"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   637
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   638
fun bin_rsplitl_aux :: "nat \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   639
  where "bin_rsplitl_aux n m c bs =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   640
    (if m = 0 \<or> n = 0 then bs
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   641
     else
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   642
      let (a, b) = bin_split (min m n) c
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   643
      in bin_rsplitl_aux n (m - n) a (b # bs))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   644
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   645
definition bin_rsplitl :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   646
  where "bin_rsplitl n w = bin_rsplitl_aux n (fst w) (snd w) []"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   647
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   648
declare bin_rsplit_aux.simps [simp del]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   649
declare bin_rsplitl_aux.simps [simp del]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   650
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   651
lemma bin_nth_cat:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   652
  "bin_nth (bin_cat x k y) n =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   653
    (if n < k then bin_nth y n else bin_nth x (n - k))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   654
  apply (induct k arbitrary: n y)
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   655
   apply simp
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   656
  apply (case_tac n)
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   657
   apply (simp_all add: bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   658
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   659
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   660
lemma bin_nth_drop_bit_iff:
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   661
  \<open>bin_nth (drop_bit n c) k \<longleftrightarrow> bin_nth c (n + k)\<close>
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   662
  by (simp add: bit_drop_bit_eq)
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   663
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   664
lemma bin_nth_take_bit_iff:
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   665
  \<open>bin_nth (take_bit n c) k \<longleftrightarrow> k < n \<and> bin_nth c k\<close>
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   666
  by (fact bit_take_bit_iff)
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   667
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   668
lemma bin_nth_split:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   669
  "bin_split n c = (a, b) \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   670
    (\<forall>k. bin_nth a k = bin_nth c (n + k)) \<and>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   671
    (\<forall>k. bin_nth b k = (k < n \<and> bin_nth c k))"
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   672
  by (auto simp add: bin_nth_drop_bit_iff bin_nth_take_bit_iff)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   673
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   674
lemma bin_cat_zero [simp]: "bin_cat 0 n w = bintrunc n w"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   675
  by (simp add: bin_cat_eq_push_bit_add_take_bit)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   676
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   677
lemma bintr_cat1: "bintrunc (k + n) (bin_cat a n b) = bin_cat (bintrunc k a) n b"
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   678
  by (metis bin_cat_assoc bin_cat_zero)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   679
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   680
lemma bintr_cat: "bintrunc m (bin_cat a n b) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   681
    bin_cat (bintrunc (m - n) a) n (bintrunc (min m n) b)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   682
  by (rule bin_eqI) (auto simp: bin_nth_cat nth_bintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   683
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   684
lemma bintr_cat_same [simp]: "bintrunc n (bin_cat a n b) = bintrunc n b"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   685
  by (auto simp add : bintr_cat)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   686
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   687
lemma cat_bintr [simp]: "bin_cat a n (bintrunc n b) = bin_cat a n b"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   688
  by (simp add: bin_cat_eq_push_bit_add_take_bit)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   689
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   690
lemma split_bintrunc: "bin_split n c = (a, b) \<Longrightarrow> b = bintrunc n c"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   691
  by simp
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   692
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   693
lemma bin_cat_split: "bin_split n w = (u, v) \<Longrightarrow> w = bin_cat u n v"
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   694
  by (auto simp add: bin_cat_eq_push_bit_add_take_bit bits_ident)
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   695
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   696
lemma drop_bit_bin_cat_eq:
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   697
  \<open>drop_bit n (bin_cat v n w) = v\<close>
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   698
  by (induct n arbitrary: w)
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   699
    (simp_all add: drop_bit_Suc)
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   700
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   701
lemma take_bit_bin_cat_eq:
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   702
  \<open>take_bit n (bin_cat v n w) = take_bit n w\<close>
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   703
  by (induct n arbitrary: w)
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   704
    (simp_all add: take_bit_Suc mod_2_eq_odd)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   705
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   706
lemma bin_split_cat: "bin_split n (bin_cat v n w) = (v, bintrunc n w)"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   707
  by (simp add: drop_bit_bin_cat_eq take_bit_bin_cat_eq)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   708
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   709
lemma bin_split_zero [simp]: "bin_split n 0 = (0, 0)"
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   710
  by simp
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   711
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   712
lemma bin_split_minus1 [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   713
  "bin_split n (- 1) = (- 1, bintrunc n (- 1))"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   714
  by simp
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   715
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   716
lemma bin_split_trunc:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   717
  "bin_split (min m n) c = (a, b) \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   718
    bin_split n (bintrunc m c) = (bintrunc (m - n) a, b)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   719
  apply (induct n arbitrary: m b c, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   720
  apply (simp add: bin_rest_trunc Let_def split: prod.split_asm)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   721
  apply (case_tac m)
71946
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
   722
   apply (auto simp: Let_def drop_bit_Suc take_bit_Suc mod_2_eq_odd split: prod.split_asm)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   723
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   724
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   725
lemma bin_split_trunc1:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   726
  "bin_split n c = (a, b) \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   727
    bin_split n (bintrunc m c) = (bintrunc (m - n) a, bintrunc m b)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   728
  apply (induct n arbitrary: m b c, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   729
  apply (simp add: bin_rest_trunc Let_def split: prod.split_asm)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   730
  apply (case_tac m)
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   731
   apply (auto simp: Let_def drop_bit_Suc take_bit_Suc mod_2_eq_odd split: prod.split_asm)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   732
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   733
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   734
lemma bin_cat_num: "bin_cat a n b = a * 2 ^ n + bintrunc n b"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   735
  by (simp add: bin_cat_eq_push_bit_add_take_bit push_bit_eq_mult)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   736
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   737
lemma bin_split_num: "bin_split n b = (b div 2 ^ n, b mod 2 ^ n)"
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   738
  by (simp add: drop_bit_eq_div take_bit_eq_mod)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   739
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   740
lemmas bin_rsplit_aux_simps = bin_rsplit_aux.simps bin_rsplitl_aux.simps
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   741
lemmas rsplit_aux_simps = bin_rsplit_aux_simps
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   742
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   743
lemmas th_if_simp1 = if_split [where P = "(=) l", THEN iffD1, THEN conjunct1, THEN mp] for l
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   744
lemmas th_if_simp2 = if_split [where P = "(=) l", THEN iffD1, THEN conjunct2, THEN mp] for l
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   745
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   746
lemmas rsplit_aux_simp1s = rsplit_aux_simps [THEN th_if_simp1]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   747
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   748
lemmas rsplit_aux_simp2ls = rsplit_aux_simps [THEN th_if_simp2]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   749
\<comment> \<open>these safe to \<open>[simp add]\<close> as require calculating \<open>m - n\<close>\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   750
lemmas bin_rsplit_aux_simp2s [simp] = rsplit_aux_simp2ls [unfolded Let_def]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   751
lemmas rbscl = bin_rsplit_aux_simp2s (2)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   752
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   753
lemmas rsplit_aux_0_simps [simp] =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   754
  rsplit_aux_simp1s [OF disjI1] rsplit_aux_simp1s [OF disjI2]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   755
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   756
lemma bin_rsplit_aux_append: "bin_rsplit_aux n m c (bs @ cs) = bin_rsplit_aux n m c bs @ cs"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   757
  apply (induct n m c bs rule: bin_rsplit_aux.induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   758
  apply (subst bin_rsplit_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   759
  apply (subst bin_rsplit_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   760
  apply (clarsimp split: prod.split)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   761
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   762
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   763
lemma bin_rsplitl_aux_append: "bin_rsplitl_aux n m c (bs @ cs) = bin_rsplitl_aux n m c bs @ cs"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   764
  apply (induct n m c bs rule: bin_rsplitl_aux.induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   765
  apply (subst bin_rsplitl_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   766
  apply (subst bin_rsplitl_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   767
  apply (clarsimp split: prod.split)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   768
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   769
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   770
lemmas rsplit_aux_apps [where bs = "[]"] =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   771
  bin_rsplit_aux_append bin_rsplitl_aux_append
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   772
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   773
lemmas rsplit_def_auxs = bin_rsplit_def bin_rsplitl_def
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   774
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   775
lemmas rsplit_aux_alts = rsplit_aux_apps
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   776
  [unfolded append_Nil rsplit_def_auxs [symmetric]]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   777
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   778
lemma bin_split_minus: "0 < n \<Longrightarrow> bin_split (Suc (n - 1)) w = bin_split n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   779
  by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   780
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   781
lemma bin_split_pred_simp [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   782
  "(0::nat) < numeral bin \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   783
    bin_split (numeral bin) w =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   784
      (let (w1, w2) = bin_split (numeral bin - 1) (bin_rest w)
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   785
       in (w1, of_bool (odd w) + 2 * w2))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   786
  by (simp add: take_bit_rec drop_bit_rec mod_2_eq_odd)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   787
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   788
lemma bin_rsplit_aux_simp_alt:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   789
  "bin_rsplit_aux n m c bs =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   790
    (if m = 0 \<or> n = 0 then bs
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   791
     else let (a, b) = bin_split n c in bin_rsplit n (m - n, a) @ b # bs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   792
  apply (simp add: bin_rsplit_aux.simps [of n m c bs])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   793
  apply (subst rsplit_aux_alts)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   794
  apply (simp add: bin_rsplit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   795
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   796
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   797
lemmas bin_rsplit_simp_alt =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   798
  trans [OF bin_rsplit_def bin_rsplit_aux_simp_alt]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   799
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   800
lemmas bthrs = bin_rsplit_simp_alt [THEN [2] trans]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   801
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   802
lemma bin_rsplit_size_sign' [rule_format]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   803
  "n > 0 \<Longrightarrow> rev sw = bin_rsplit n (nw, w) \<Longrightarrow> \<forall>v\<in>set sw. bintrunc n v = v"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   804
  apply (induct sw arbitrary: nw w)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   805
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   806
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   807
  apply (drule bthrs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   808
  apply (simp (no_asm_use) add: Let_def split: prod.split_asm if_split_asm)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   809
  apply clarify
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   810
  apply simp
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   811
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   812
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   813
lemmas bin_rsplit_size_sign = bin_rsplit_size_sign' [OF asm_rl
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   814
  rev_rev_ident [THEN trans] set_rev [THEN equalityD2 [THEN subsetD]]]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   815
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   816
lemma bin_nth_rsplit [rule_format] :
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   817
  "n > 0 \<Longrightarrow> m < n \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   818
    \<forall>w k nw.
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   819
      rev sw = bin_rsplit n (nw, w) \<longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   820
      k < size sw \<longrightarrow> bin_nth (sw ! k) m = bin_nth w (k * n + m)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   821
  apply (induct sw)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   822
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   823
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   824
  apply (drule bthrs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   825
  apply (simp (no_asm_use) add: Let_def split: prod.split_asm if_split_asm)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   826
  apply (erule allE, erule impE, erule exI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   827
  apply (case_tac k)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   828
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   829
   prefer 2
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   830
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   831
   apply (erule allE)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   832
   apply (erule (1) impE)
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   833
   apply (simp add: bit_drop_bit_eq ac_simps)
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   834
  apply (simp add: bit_take_bit_iff ac_simps)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   835
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   836
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   837
lemma bin_rsplit_all: "0 < nw \<Longrightarrow> nw \<le> n \<Longrightarrow> bin_rsplit n (nw, w) = [bintrunc n w]"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   838
  by (auto simp: bin_rsplit_def rsplit_aux_simp2ls split: prod.split dest!: split_bintrunc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   839
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   840
lemma bin_rsplit_l [rule_format]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   841
  "\<forall>bin. bin_rsplitl n (m, bin) = bin_rsplit n (m, bintrunc m bin)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   842
  apply (rule_tac a = "m" in wf_less_than [THEN wf_induct])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   843
  apply (simp (no_asm) add: bin_rsplitl_def bin_rsplit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   844
  apply (rule allI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   845
  apply (subst bin_rsplitl_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   846
  apply (subst bin_rsplit_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   847
  apply (clarsimp simp: Let_def split: prod.split)
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   848
  apply (simp add: ac_simps)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   849
  apply (subst rsplit_aux_alts(1))
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   850
  apply (subst rsplit_aux_alts(2))
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   851
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   852
  unfolding bin_rsplit_def bin_rsplitl_def
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   853
  apply (simp add: drop_bit_take_bit)
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   854
  apply (case_tac \<open>x < n\<close>)
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   855
  apply (simp_all add: not_less min_def)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   856
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   857
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   858
lemma bin_rsplit_rcat [rule_format]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   859
  "n > 0 \<longrightarrow> bin_rsplit n (n * size ws, bin_rcat n ws) = map (bintrunc n) ws"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   860
  apply (unfold bin_rsplit_def bin_rcat_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   861
  apply (rule_tac xs = ws in rev_induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   862
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   863
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   864
  apply (subst rsplit_aux_alts)
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   865
  apply (simp add: drop_bit_bin_cat_eq take_bit_bin_cat_eq)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   866
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   867
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   868
lemma bin_rsplit_aux_len_le [rule_format] :
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   869
  "\<forall>ws m. n \<noteq> 0 \<longrightarrow> ws = bin_rsplit_aux n nw w bs \<longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   870
    length ws \<le> m \<longleftrightarrow> nw + length bs * n \<le> m * n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   871
proof -
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   872
  have *: R
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   873
    if d: "i \<le> j \<or> m < j'"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   874
    and R1: "i * k \<le> j * k \<Longrightarrow> R"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   875
    and R2: "Suc m * k' \<le> j' * k' \<Longrightarrow> R"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   876
    for i j j' k k' m :: nat and R
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   877
    using d
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   878
    apply safe
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   879
    apply (rule R1, erule mult_le_mono1)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   880
    apply (rule R2, erule Suc_le_eq [THEN iffD2 [THEN mult_le_mono1]])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   881
    done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   882
  have **: "0 < sc \<Longrightarrow> sc - n + (n + lb * n) \<le> m * n \<longleftrightarrow> sc + lb * n \<le> m * n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   883
    for sc m n lb :: nat
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   884
    apply safe
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   885
     apply arith
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   886
    apply (case_tac "sc \<ge> n")
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   887
     apply arith
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   888
    apply (insert linorder_le_less_linear [of m lb])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   889
    apply (erule_tac k=n and k'=n in *)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   890
     apply arith
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   891
    apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   892
    done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   893
  show ?thesis
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   894
    apply (induct n nw w bs rule: bin_rsplit_aux.induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   895
    apply (subst bin_rsplit_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   896
    apply (simp add: ** Let_def split: prod.split)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   897
    done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   898
qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   899
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   900
lemma bin_rsplit_len_le: "n \<noteq> 0 \<longrightarrow> ws = bin_rsplit n (nw, w) \<longrightarrow> length ws \<le> m \<longleftrightarrow> nw \<le> m * n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   901
  by (auto simp: bin_rsplit_def bin_rsplit_aux_len_le)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   902
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   903
lemma bin_rsplit_aux_len:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   904
  "n \<noteq> 0 \<Longrightarrow> length (bin_rsplit_aux n nw w cs) = (nw + n - 1) div n + length cs"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   905
  apply (induct n nw w cs rule: bin_rsplit_aux.induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   906
  apply (subst bin_rsplit_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   907
  apply (clarsimp simp: Let_def split: prod.split)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   908
  apply (erule thin_rl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   909
  apply (case_tac m)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   910
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   911
  apply (case_tac "m \<le> n")
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   912
   apply (auto simp add: div_add_self2)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   913
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   914
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   915
lemma bin_rsplit_len: "n \<noteq> 0 \<Longrightarrow> length (bin_rsplit n (nw, w)) = (nw + n - 1) div n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   916
  by (auto simp: bin_rsplit_def bin_rsplit_aux_len)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   917
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   918
lemma bin_rsplit_aux_len_indep:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   919
  "n \<noteq> 0 \<Longrightarrow> length bs = length cs \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   920
    length (bin_rsplit_aux n nw v bs) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   921
    length (bin_rsplit_aux n nw w cs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   922
proof (induct n nw w cs arbitrary: v bs rule: bin_rsplit_aux.induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   923
  case (1 n m w cs v bs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   924
  show ?case
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   925
  proof (cases "m = 0")
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   926
    case True
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   927
    with \<open>length bs = length cs\<close> show ?thesis by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   928
  next
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   929
    case False
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   930
    from "1.hyps" [of \<open>bin_split n w\<close> \<open>drop_bit n w\<close> \<open>take_bit n w\<close>] \<open>m \<noteq> 0\<close> \<open>n \<noteq> 0\<close>
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   931
    have hyp: "\<And>v bs. length bs = Suc (length cs) \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   932
      length (bin_rsplit_aux n (m - n) v bs) =
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   933
      length (bin_rsplit_aux n (m - n) (drop_bit n w) (take_bit n w # cs))"
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   934
      using bin_rsplit_aux_len by fastforce 
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   935
    from \<open>length bs = length cs\<close> \<open>n \<noteq> 0\<close> show ?thesis
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   936
      by (auto simp add: bin_rsplit_aux_simp_alt Let_def bin_rsplit_len split: prod.split)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   937
  qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   938
qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   939
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   940
lemma bin_rsplit_len_indep:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   941
  "n \<noteq> 0 \<Longrightarrow> length (bin_rsplit n (nw, v)) = length (bin_rsplit n (nw, w))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   942
  apply (unfold bin_rsplit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   943
  apply (simp (no_asm))
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   944
  apply (erule bin_rsplit_aux_len_indep)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   945
  apply (rule refl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   946
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   947
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   948
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
   949
subsection \<open>Logical operations\<close>
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   950
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   951
primrec bin_sc :: "nat \<Rightarrow> bool \<Rightarrow> int \<Rightarrow> int"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   952
  where
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   953
    Z: "bin_sc 0 b w = of_bool b + 2 * bin_rest w"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   954
  | Suc: "bin_sc (Suc n) b w = of_bool (odd w) + 2 * bin_sc n b (w div 2)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   955
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   956
lemma bin_nth_sc [simp]: "bit (bin_sc n b w) n \<longleftrightarrow> b"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   957
  by (induction n arbitrary: w) (simp_all add: bit_Suc)
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   958
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   959
lemma bin_sc_sc_same [simp]: "bin_sc n c (bin_sc n b w) = bin_sc n c w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   960
  by (induction n arbitrary: w) (simp_all add: bit_Suc)
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   961
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   962
lemma bin_sc_sc_diff: "m \<noteq> n \<Longrightarrow> bin_sc m c (bin_sc n b w) = bin_sc n b (bin_sc m c w)"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   963
  apply (induct n arbitrary: w m)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   964
   apply (case_tac [!] m)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   965
     apply auto
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   966
  done
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   967
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   968
lemma bin_nth_sc_gen: "bin_nth (bin_sc n b w) m = (if m = n then b else bin_nth w m)"
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   969
  apply (induct n arbitrary: w m)
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   970
   apply (case_tac m; simp add: bit_Suc)
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   971
  apply (case_tac m; simp add: bit_Suc)
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   972
  done
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   973
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   974
lemma bin_sc_eq:
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   975
  \<open>bin_sc n False = unset_bit n\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   976
  \<open>bin_sc n True = Bit_Operations.set_bit n\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   977
  by (simp_all add: fun_eq_iff bit_eq_iff)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   978
    (simp_all add: bin_nth_sc_gen bit_set_bit_iff bit_unset_bit_iff)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   979
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   980
lemma bin_sc_nth [simp]: "bin_sc n (bin_nth w n) w = w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   981
  by (rule bit_eqI) (simp add: bin_nth_sc_gen)
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   982
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   983
lemma bin_sign_sc [simp]: "bin_sign (bin_sc n b w) = bin_sign w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   984
proof (induction n arbitrary: w)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   985
  case 0
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   986
  then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   987
    by (auto simp add: bin_sign_def) (use bin_rest_ge_0 in fastforce)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   988
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   989
  case (Suc n)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   990
  from Suc [of \<open>w div 2\<close>]
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   991
  show ?case by (auto simp add: bin_sign_def split: if_splits)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   992
qed
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   993
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   994
lemma bin_sc_bintr [simp]:
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   995
  "bintrunc m (bin_sc n x (bintrunc m w)) = bintrunc m (bin_sc n x w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   996
  apply (cases x)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   997
   apply (simp_all add: bin_sc_eq bit_eq_iff)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   998
   apply (auto simp add: bit_take_bit_iff bit_set_bit_iff bit_unset_bit_iff)
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   999
  done
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1000
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1001
lemma bin_clr_le: "bin_sc n False w \<le> w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1002
  by (simp add: bin_sc_eq unset_bit_less_eq)
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1003
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1004
lemma bin_set_ge: "bin_sc n True w \<ge> w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1005
  by (simp add: bin_sc_eq set_bit_greater_eq)
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1006
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1007
lemma bintr_bin_clr_le: "bintrunc n (bin_sc m False w) \<le> bintrunc n w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1008
  by (simp add: bin_sc_eq take_bit_unset_bit_eq unset_bit_less_eq)
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1009
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1010
lemma bintr_bin_set_ge: "bintrunc n (bin_sc m True w) \<ge> bintrunc n w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1011
  by (simp add: bin_sc_eq take_bit_set_bit_eq set_bit_greater_eq)
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1012
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1013
lemma bin_sc_FP [simp]: "bin_sc n False 0 = 0"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1014
  by (induct n) auto
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1015
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1016
lemma bin_sc_TM [simp]: "bin_sc n True (- 1) = - 1"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1017
  by (induct n) auto
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1018
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1019
lemmas bin_sc_simps = bin_sc.Z bin_sc.Suc bin_sc_TM bin_sc_FP
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1020
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1021
lemma bin_sc_minus: "0 < n \<Longrightarrow> bin_sc (Suc (n - 1)) b w = bin_sc n b w"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1022
  by auto
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1023
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1024
lemmas bin_sc_Suc_minus =
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1025
  trans [OF bin_sc_minus [symmetric] bin_sc.Suc]
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1026
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1027
lemma bin_sc_numeral [simp]:
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1028
  "bin_sc (numeral k) b w =
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1029
    of_bool (odd w) + 2 * bin_sc (pred_numeral k) b (w div 2)"
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1030
  by (simp add: numeral_eq_Suc)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1031
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1032
instantiation int :: bit_operations
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
  1033
begin
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
  1034
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1035
definition [iff]: "i !! n \<longleftrightarrow> bin_nth i n"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1036
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1037
definition "lsb i = i !! 0" for i :: int
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1038
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1039
definition "set_bit i n b = bin_sc n b i"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1040
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1041
definition "shiftl x n = x * 2 ^ n" for x :: int
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1042
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1043
definition "shiftr x n = x div 2 ^ n" for x :: int
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1044
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1045
definition "msb x \<longleftrightarrow> x < 0" for x :: int
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1046
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
  1047
instance ..
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
  1048
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
  1049
end
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
  1050
71943
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
  1051
lemma shiftl_eq_push_bit:
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
  1052
  \<open>k << n = push_bit n k\<close> for k :: int
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
  1053
  by (simp add: shiftl_int_def push_bit_eq_mult)
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
  1054
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
  1055
lemma shiftr_eq_drop_bit:
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
  1056
  \<open>k >> n = drop_bit n k\<close> for k :: int
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
  1057
  by (simp add: shiftr_int_def drop_bit_eq_div)
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
  1058
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1059
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1060
subsubsection \<open>Basic simplification rules\<close>
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1061
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1062
lemmas int_not_def = not_int_def
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1063
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1064
lemma int_not_simps [simp]:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1065
  "NOT (0::int) = -1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1066
  "NOT (1::int) = -2"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1067
  "NOT (- 1::int) = 0"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1068
  "NOT (numeral w::int) = - numeral (w + Num.One)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1069
  "NOT (- numeral (Num.Bit0 w)::int) = numeral (Num.BitM w)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1070
  "NOT (- numeral (Num.Bit1 w)::int) = numeral (Num.Bit0 w)"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1071
  by (simp_all add: not_int_def)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1072
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1073
lemma int_not_not: "NOT (NOT x) = x"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1074
  for x :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1075
  by (fact bit.double_compl)
46017
c5a1002161c3 simplify definition of OR for type int;
huffman
parents: 46016
diff changeset
  1076
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1077
lemma int_and_0 [simp]: "0 AND x = 0"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1078
  for x :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1079
  by (fact bit.conj_zero_left)
46019
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
  1080
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1081
lemma int_and_m1 [simp]: "-1 AND x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1082
  for x :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1083
  by (fact bit.conj_one_left)
46019
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
  1084
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1085
lemma int_or_zero [simp]: "0 OR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1086
  for x :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1087
  by (fact bit.disj_zero_left)
46018
0bb66de5a0bf simplify definition of XOR for type int;
huffman
parents: 46017
diff changeset
  1088
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1089
lemma int_or_minus1 [simp]: "-1 OR x = -1"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1090
  for x :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1091
  by (fact bit.disj_one_left)
46017
c5a1002161c3 simplify definition of OR for type int;
huffman
parents: 46016
diff changeset
  1092
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1093
lemma int_xor_zero [simp]: "0 XOR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1094
  for x :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1095
  by (fact bit.xor_zero_left)
46018
0bb66de5a0bf simplify definition of XOR for type int;
huffman
parents: 46017
diff changeset
  1096
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1097
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1098
subsubsection \<open>Binary destructors\<close>
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1099
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1100
lemma bin_rest_NOT [simp]: "bin_rest (NOT x) = NOT (bin_rest x)"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1101
  by (fact not_int_div_2)
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1102
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1103
lemma bin_last_NOT [simp]: "bin_last (NOT x) \<longleftrightarrow> \<not> bin_last x"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1104
  by simp
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1105
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1106
lemma bin_rest_AND [simp]: "bin_rest (x AND y) = bin_rest x AND bin_rest y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1107
  by (subst and_int_rec) auto
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1108
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1109
lemma bin_last_AND [simp]: "bin_last (x AND y) \<longleftrightarrow> bin_last x \<and> bin_last y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1110
  by (subst and_int_rec) auto
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1111
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1112
lemma bin_rest_OR [simp]: "bin_rest (x OR y) = bin_rest x OR bin_rest y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1113
  by (subst or_int_rec) auto
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1114
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1115
lemma bin_last_OR [simp]: "bin_last (x OR y) \<longleftrightarrow> bin_last x \<or> bin_last y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1116
  by (subst or_int_rec) auto
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1117
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1118
lemma bin_rest_XOR [simp]: "bin_rest (x XOR y) = bin_rest x XOR bin_rest y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1119
  by (subst xor_int_rec) auto
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1120
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1121
lemma bin_last_XOR [simp]: "bin_last (x XOR y) \<longleftrightarrow> (bin_last x \<or> bin_last y) \<and> \<not> (bin_last x \<and> bin_last y)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1122
  by (subst xor_int_rec) auto
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1123
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1124
lemma bin_nth_ops:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1125
  "\<And>x y. bin_nth (x AND y) n \<longleftrightarrow> bin_nth x n \<and> bin_nth y n"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1126
  "\<And>x y. bin_nth (x OR y) n \<longleftrightarrow> bin_nth x n \<or> bin_nth y n"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1127
  "\<And>x y. bin_nth (x XOR y) n \<longleftrightarrow> bin_nth x n \<noteq> bin_nth y n"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1128
  "\<And>x. bin_nth (NOT x) n \<longleftrightarrow> \<not> bin_nth x n"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1129
  by (simp_all add: bit_and_iff bit_or_iff bit_xor_iff bit_not_iff)
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1130
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1131
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1132
subsubsection \<open>Derived properties\<close>
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1133
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1134
lemma int_xor_minus1 [simp]: "-1 XOR x = NOT x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1135
  for x :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1136
  by (fact bit.xor_one_left)
46018
0bb66de5a0bf simplify definition of XOR for type int;
huffman
parents: 46017
diff changeset
  1137
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1138
lemma int_xor_extra_simps [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1139
  "w XOR 0 = w"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1140
  "w XOR -1 = NOT w"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1141
  for w :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1142
  by simp_all
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1143
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1144
lemma int_or_extra_simps [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1145
  "w OR 0 = w"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1146
  "w OR -1 = -1"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1147
  for w :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1148
  by simp_all
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1149
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
  1150
lemma int_and_extra_simps [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1151
  "w AND 0 = 0"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1152
  "w AND -1 = w"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1153
  for w :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1154
  by simp_all
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1155
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1156
text \<open>Commutativity of the above.\<close>
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1157
lemma bin_ops_comm:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1158
  fixes x y :: int
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1159
  shows int_and_comm: "x AND y = y AND x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1160
    and int_or_comm:  "x OR y = y OR x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1161
    and int_xor_comm: "x XOR y = y XOR x"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1162
  by (simp_all add: ac_simps)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1163
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1164
lemma bin_ops_same [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1165
  "x AND x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1166
  "x OR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1167
  "x XOR x = 0"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1168
  for x :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1169
  by simp_all
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1170
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1171
lemmas bin_log_esimps =
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1172
  int_and_extra_simps  int_or_extra_simps  int_xor_extra_simps
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1173
  int_and_0 int_and_m1 int_or_zero int_or_minus1 int_xor_zero int_xor_minus1
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1174
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1175
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1176
subsubsection \<open>Basic properties of logical (bit-wise) operations\<close>
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1177
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1178
lemma bbw_ao_absorb: "x AND (y OR x) = x \<and> x OR (y AND x) = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1179
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1180
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1181
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1182
lemma bbw_ao_absorbs_other:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1183
  "x AND (x OR y) = x \<and> (y AND x) OR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1184
  "(y OR x) AND x = x \<and> x OR (x AND y) = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1185
  "(x OR y) AND x = x \<and> (x AND y) OR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1186
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1187
  by (auto simp add: bin_eq_iff bin_nth_ops)
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
  1188
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1189
lemmas bbw_ao_absorbs [simp] = bbw_ao_absorb bbw_ao_absorbs_other
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1190
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1191
lemma int_xor_not: "(NOT x) XOR y = NOT (x XOR y) \<and> x XOR (NOT y) = NOT (x XOR y)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1192
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1193
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1194
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1195
lemma int_and_assoc: "(x AND y) AND z = x AND (y AND z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1196
  for x y z :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1197
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1198
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1199
lemma int_or_assoc: "(x OR y) OR z = x OR (y OR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1200
  for x y z :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1201
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1202
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1203
lemma int_xor_assoc: "(x XOR y) XOR z = x XOR (y XOR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1204
  for x y z :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1205
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1206
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1207
lemmas bbw_assocs = int_and_assoc int_or_assoc int_xor_assoc
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1208
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1209
(* BH: Why are these declared as simp rules??? *)
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1210
lemma bbw_lcs [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1211
  "y AND (x AND z) = x AND (y AND z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1212
  "y OR (x OR z) = x OR (y OR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1213
  "y XOR (x XOR z) = x XOR (y XOR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1214
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1215
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1216
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1217
lemma bbw_not_dist:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1218
  "NOT (x OR y) = (NOT x) AND (NOT y)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1219
  "NOT (x AND y) = (NOT x) OR (NOT y)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1220
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1221
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1222
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1223
lemma bbw_oa_dist: "(x AND y) OR z = (x OR z) AND (y OR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1224
  for x y z :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1225
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1226
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1227
lemma bbw_ao_dist: "(x OR y) AND z = (x AND z) OR (y AND z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1228
  for x y z :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1229
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1230
24367
3e29eafabe16 AC rules for bitwise logical operators no longer declared simp
huffman
parents: 24366
diff changeset
  1231
(*
3e29eafabe16 AC rules for bitwise logical operators no longer declared simp
huffman
parents: 24366
diff changeset
  1232
Why were these declared simp???
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1233
declare bin_ops_comm [simp] bbw_assocs [simp]
24367
3e29eafabe16 AC rules for bitwise logical operators no longer declared simp
huffman
parents: 24366
diff changeset
  1234
*)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1235
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1236
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1237
subsubsection \<open>Simplification with numerals\<close>
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1238
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1239
text \<open>Cases for \<open>0\<close> and \<open>-1\<close> are already covered by other simp rules.\<close>
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1240
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1241
lemma bin_rest_neg_numeral_BitM [simp]:
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1242
  "bin_rest (- numeral (Num.BitM w)) = - numeral w"
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
  1243
  by simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1244
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1245
lemma bin_last_neg_numeral_BitM [simp]:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1246
  "bin_last (- numeral (Num.BitM w))"
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1247
  by simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1248
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1249
(* FIXME: The rule sets below are very large (24 rules for each
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1250
  operator). Is there a simpler way to do this? *)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1251
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1252
lemma int_and_numerals [simp]:
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1253
  "numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = (2 :: int) * (numeral x AND numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1254
  "numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = (2 :: int) * (numeral x AND numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1255
  "numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = (2 :: int) * (numeral x AND numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1256
  "numeral (Num.Bit1 x) AND numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x AND numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1257
  "numeral (Num.Bit0 x) AND - numeral (Num.Bit0 y) = (2 :: int) * (numeral x AND - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1258
  "numeral (Num.Bit0 x) AND - numeral (Num.Bit1 y) = (2 :: int) * (numeral x AND - numeral (y + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1259
  "numeral (Num.Bit1 x) AND - numeral (Num.Bit0 y) = (2 :: int) * (numeral x AND - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1260
  "numeral (Num.Bit1 x) AND - numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x AND - numeral (y + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1261
  "- numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = (2 :: int) * (- numeral x AND numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1262
  "- numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = (2 :: int) * (- numeral x AND numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1263
  "- numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = (2 :: int) * (- numeral (x + Num.One) AND numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1264
  "- numeral (Num.Bit1 x) AND numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral (x + Num.One) AND numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1265
  "- numeral (Num.Bit0 x) AND - numeral (Num.Bit0 y) = (2 :: int) * (- numeral x AND - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1266
  "- numeral (Num.Bit0 x) AND - numeral (Num.Bit1 y) = (2 :: int) * (- numeral x AND - numeral (y + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1267
  "- numeral (Num.Bit1 x) AND - numeral (Num.Bit0 y) = (2 :: int) * (- numeral (x + Num.One) AND - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1268
  "- numeral (Num.Bit1 x) AND - numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral (x + Num.One) AND - numeral (y + Num.One))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1269
  "(1::int) AND numeral (Num.Bit0 y) = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1270
  "(1::int) AND numeral (Num.Bit1 y) = 1"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1271
  "(1::int) AND - numeral (Num.Bit0 y) = 0"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1272
  "(1::int) AND - numeral (Num.Bit1 y) = 1"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1273
  "numeral (Num.Bit0 x) AND (1::int) = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1274
  "numeral (Num.Bit1 x) AND (1::int) = 1"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1275
  "- numeral (Num.Bit0 x) AND (1::int) = 0"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1276
  "- numeral (Num.Bit1 x) AND (1::int) = 1"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1277
  by (rule bin_rl_eqI; simp)+
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1278
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1279
lemma int_or_numerals [simp]:
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1280
  "numeral (Num.Bit0 x) OR numeral (Num.Bit0 y) = (2 :: int) * (numeral x OR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1281
  "numeral (Num.Bit0 x) OR numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x OR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1282
  "numeral (Num.Bit1 x) OR numeral (Num.Bit0 y) = 1 + (2 :: int) * (numeral x OR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1283
  "numeral (Num.Bit1 x) OR numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x OR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1284
  "numeral (Num.Bit0 x) OR - numeral (Num.Bit0 y) = (2 :: int) * (numeral x OR - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1285
  "numeral (Num.Bit0 x) OR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x OR - numeral (y + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1286
  "numeral (Num.Bit1 x) OR - numeral (Num.Bit0 y) = 1 + (2 :: int) * (numeral x OR - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1287
  "numeral (Num.Bit1 x) OR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x OR - numeral (y + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1288
  "- numeral (Num.Bit0 x) OR numeral (Num.Bit0 y) = (2 :: int) * (- numeral x OR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1289
  "- numeral (Num.Bit0 x) OR numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral x OR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1290
  "- numeral (Num.Bit1 x) OR numeral (Num.Bit0 y) = 1 + (2 :: int) * (- numeral (x + Num.One) OR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1291
  "- numeral (Num.Bit1 x) OR numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral (x + Num.One) OR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1292
  "- numeral (Num.Bit0 x) OR - numeral (Num.Bit0 y) = (2 :: int) * (- numeral x OR - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1293
  "- numeral (Num.Bit0 x) OR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral x OR - numeral (y + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1294
  "- numeral (Num.Bit1 x) OR - numeral (Num.Bit0 y) = 1 + (2 :: int) * (- numeral (x + Num.One) OR - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1295
  "- numeral (Num.Bit1 x) OR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral (x + Num.One) OR - numeral (y + Num.One))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1296
  "(1::int) OR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1297
  "(1::int) OR numeral (Num.Bit1 y) = numeral (Num.Bit1 y)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1298
  "(1::int) OR - numeral (Num.Bit0 y) = - numeral (Num.BitM y)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1299
  "(1::int) OR - numeral (Num.Bit1 y) = - numeral (Num.Bit1 y)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1300
  "numeral (Num.Bit0 x) OR (1::int) = numeral (Num.Bit1 x)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1301
  "numeral (Num.Bit1 x) OR (1::int) = numeral (Num.Bit1 x)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1302
  "- numeral (Num.Bit0 x) OR (1::int) = - numeral (Num.BitM x)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1303
  "- numeral (Num.Bit1 x) OR (1::int) = - numeral (Num.Bit1 x)"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1304
  by (rule bin_rl_eqI; simp)+
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1305
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1306
lemma int_xor_numerals [simp]:
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1307
  "numeral (Num.Bit0 x) XOR numeral (Num.Bit0 y) = (2 :: int) * (numeral x XOR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1308
  "numeral (Num.Bit0 x) XOR numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x XOR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1309
  "numeral (Num.Bit1 x) XOR numeral (Num.Bit0 y) = 1 + (2 :: int) * (numeral x XOR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1310
  "numeral (Num.Bit1 x) XOR numeral (Num.Bit1 y) = (2 :: int) * (numeral x XOR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1311
  "numeral (Num.Bit0 x) XOR - numeral (Num.Bit0 y) = (2 :: int) * (numeral x XOR - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1312
  "numeral (Num.Bit0 x) XOR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x XOR - numeral (y + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1313
  "numeral (Num.Bit1 x) XOR - numeral (Num.Bit0 y) = 1 + (2 :: int) * (numeral x XOR - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1314
  "numeral (Num.Bit1 x) XOR - numeral (Num.Bit1 y) = (2 :: int) * (numeral x XOR - numeral (y + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1315
  "- numeral (Num.Bit0 x) XOR numeral (Num.Bit0 y) = (2 :: int) * (- numeral x XOR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1316
  "- numeral (Num.Bit0 x) XOR numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral x XOR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1317
  "- numeral (Num.Bit1 x) XOR numeral (Num.Bit0 y) = 1 + (2 :: int) * (- numeral (x + Num.One) XOR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1318
  "- numeral (Num.Bit1 x) XOR numeral (Num.Bit1 y) = (2 :: int) * (- numeral (x + Num.One) XOR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1319
  "- numeral (Num.Bit0 x) XOR - numeral (Num.Bit0 y) = (2 :: int) * (- numeral x XOR - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1320
  "- numeral (Num.Bit0 x) XOR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral x XOR - numeral (y + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1321
  "- numeral (Num.Bit1 x) XOR - numeral (Num.Bit0 y) = 1 + (2 :: int) * (- numeral (x + Num.One) XOR - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1322
  "- numeral (Num.Bit1 x) XOR - numeral (Num.Bit1 y) = (2 :: int) * (- numeral (x + Num.One) XOR - numeral (y + Num.One))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1323
  "(1::int) XOR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1324
  "(1::int) XOR numeral (Num.Bit1 y) = numeral (Num.Bit0 y)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1325
  "(1::int) XOR - numeral (Num.Bit0 y) = - numeral (Num.BitM y)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1326
  "(1::int) XOR - numeral (Num.Bit1 y) = - numeral (Num.Bit0 (y + Num.One))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1327
  "numeral (Num.Bit0 x) XOR (1::int) = numeral (Num.Bit1 x)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1328
  "numeral (Num.Bit1 x) XOR (1::int) = numeral (Num.Bit0 x)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1329
  "- numeral (Num.Bit0 x) XOR (1::int) = - numeral (Num.BitM x)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1330
  "- numeral (Num.Bit1 x) XOR (1::int) = - numeral (Num.Bit0 (x + Num.One))"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1331
  by (rule bin_rl_eqI; simp)+
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1332
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1333
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1334
subsubsection \<open>Interactions with arithmetic\<close>
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1335
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1336
lemma plus_and_or: "(x AND y) + (x OR y) = x + y" for x y :: int
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1337
proof (induction x arbitrary: y rule: int_bit_induct)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1338
  case zero
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1339
  then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1340
    by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1341
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1342
  case minus
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1343
  then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1344
    by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1345
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1346
  case (even x)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1347
  from even.IH [of \<open>y div 2\<close>]
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1348
  show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1349
    by (auto simp add: and_int_rec [of _ y] or_int_rec [of _ y] elim: oddE)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1350
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1351
  case (odd x)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1352
  from odd.IH [of \<open>y div 2\<close>]
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1353
  show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1354
    by (auto simp add: and_int_rec [of _ y] or_int_rec [of _ y] elim: oddE)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1355
qed
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1356
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1357
lemma le_int_or: "bin_sign y = 0 \<Longrightarrow> x \<le> x OR y"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1358
  for x y :: int
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1359
  by (simp add: bin_sign_def or_greater_eq split: if_splits)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1360
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1361
lemmas int_and_le =
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 47219
diff changeset
  1362
  xtrans(3) [OF bbw_ao_absorbs (2) [THEN conjunct2, symmetric] le_int_or]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1363
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1364
text \<open>Interaction between bit-wise and arithmetic: good example of \<open>bin_induction\<close>.\<close>
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1365
lemma bin_add_not: "x + NOT x = (-1::int)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1366
  by (simp add: not_int_def)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1367
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1368
lemma AND_mod: "x AND (2 ^ n - 1) = x mod 2 ^ n"
70169
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1369
  for x :: int
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1370
  by (simp flip: take_bit_eq_mod add: take_bit_eq_mask mask_eq_exp_minus_1)
70169
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1371
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1372
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1373
subsubsection \<open>Comparison\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1374
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1375
lemma AND_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1376
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1377
  assumes "0 \<le> x"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1378
  shows "0 \<le> x AND y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1379
  using assms by simp
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1380
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1381
lemma OR_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1382
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1383
  assumes "0 \<le> x" "0 \<le> y"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1384
  shows "0 \<le> x OR y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1385
  using assms by simp
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1386
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1387
lemma XOR_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1388
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1389
  assumes "0 \<le> x" "0 \<le> y"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1390
  shows "0 \<le> x XOR y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1391
  using assms by simp
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1392
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1393
lemma AND_upper1 [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1394
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1395
  assumes "0 \<le> x"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1396
  shows "x AND y \<le> x"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1397
  using assms by (induction x arbitrary: y rule: int_bit_induct)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1398
    (simp_all add: and_int_rec [of \<open>_ * 2\<close>] and_int_rec [of \<open>1 + _ * 2\<close>] add_increasing)
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1399
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1400
lemmas AND_upper1' [simp] = order_trans [OF AND_upper1] \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1401
lemmas AND_upper1'' [simp] = order_le_less_trans [OF AND_upper1] \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1402
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1403
lemma AND_upper2 [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1404
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1405
  assumes "0 \<le> y"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1406
  shows "x AND y \<le> y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1407
  using assms AND_upper1 [of y x] by (simp add: ac_simps)
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1408
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1409
lemmas AND_upper2' [simp] = order_trans [OF AND_upper2] \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1410
lemmas AND_upper2'' [simp] = order_le_less_trans [OF AND_upper2] \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1411
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1412
lemma OR_upper: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1413
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1414
  assumes "0 \<le> x" "x < 2 ^ n" "y < 2 ^ n"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1415
  shows "x OR y < 2 ^ n"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1416
using assms proof (induction x arbitrary: y n rule: int_bit_induct)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1417
  case zero
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1418
  then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1419
    by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1420
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1421
  case minus
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1422
  then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1423
    by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1424
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1425
  case (even x)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1426
  from even.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] even.prems even.hyps
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1427
  show ?case 
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1428
    by (cases n) (auto simp add: or_int_rec [of \<open>_ * 2\<close>] elim: oddE)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1429
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1430
  case (odd x)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1431
  from odd.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] odd.prems odd.hyps
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1432
  show ?case
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1433
    by (cases n) (auto simp add: or_int_rec [of \<open>1 + _ * 2\<close>], linarith)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1434
qed
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1435
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1436
lemma XOR_upper: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1437
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1438
  assumes "0 \<le> x" "x < 2 ^ n" "y < 2 ^ n"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1439
  shows "x XOR y < 2 ^ n"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1440
using assms proof (induction x arbitrary: y n rule: int_bit_induct)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1441
  case zero
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1442
  then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1443
    by simp
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1444
next
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1445
  case minus
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1446
  then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1447
    by simp
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1448
next
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1449
  case (even x)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1450
  from even.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] even.prems even.hyps
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1451
  show ?case 
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1452
    by (cases n) (auto simp add: xor_int_rec [of \<open>_ * 2\<close>] elim: oddE)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1453
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1454
  case (odd x)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1455
  from odd.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] odd.prems odd.hyps
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1456
  show ?case
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1457
    by (cases n) (auto simp add: xor_int_rec [of \<open>1 + _ * 2\<close>])
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1458
qed
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1459
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1460
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1461
subsubsection \<open>Truncating results of bit-wise operations\<close>
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1462
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1463
lemma bin_trunc_ao:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1464
  "bintrunc n x AND bintrunc n y = bintrunc n (x AND y)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1465
  "bintrunc n x OR bintrunc n y = bintrunc n (x OR y)"
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1466
  by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1467
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1468
lemma bin_trunc_xor: "bintrunc n (bintrunc n x XOR bintrunc n y) = bintrunc n (x XOR y)"
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1469
  by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1470
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1471
lemma bin_trunc_not: "bintrunc n (NOT (bintrunc n x)) = bintrunc n (NOT x)"
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1472
  by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1473
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1474
text \<open>Want theorems of the form of \<open>bin_trunc_xor\<close>.\<close>
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1475
lemma bintr_bintr_i: "x = bintrunc n y \<Longrightarrow> bintrunc n x = bintrunc n y"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1476
  by auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1477
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1478
lemmas bin_trunc_and = bin_trunc_ao(1) [THEN bintr_bintr_i]
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1479
lemmas bin_trunc_or = bin_trunc_ao(2) [THEN bintr_bintr_i]
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1480
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1481
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1482
subsubsection \<open>More lemmas\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1483
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1484
lemma not_int_cmp_0 [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1485
  fixes i :: int shows
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1486
  "0 < NOT i \<longleftrightarrow> i < -1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1487
  "0 \<le> NOT i \<longleftrightarrow> i < 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1488
  "NOT i < 0 \<longleftrightarrow> i \<ge> 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1489
  "NOT i \<le> 0 \<longleftrightarrow> i \<ge> -1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1490
by(simp_all add: int_not_def) arith+
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1491
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1492
lemma bbw_ao_dist2: "(x :: int) AND (y OR z) = x AND y OR x AND z"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1493
  by (fact bit.conj_disj_distrib)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1494
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1495
lemmas int_and_ac = bbw_lcs(1) int_and_comm int_and_assoc
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1496
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1497
lemma int_nand_same [simp]: fixes x :: int shows "x AND NOT x = 0"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1498
  by simp
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1499
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1500
lemma int_nand_same_middle: fixes x :: int shows "x AND y AND NOT x = 0"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1501
  by (simp add: bit_eq_iff bit_and_iff bit_not_iff)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1502
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1503
lemma and_xor_dist: fixes x :: int shows
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1504
  "x AND (y XOR z) = (x AND y) XOR (x AND z)"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1505
  by (fact bit.conj_xor_distrib)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1506
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1507
lemma int_and_lt0 [simp]:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1508
  \<open>x AND y < 0 \<longleftrightarrow> x < 0 \<and> y < 0\<close> for x y :: int
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1509
  by (fact and_negative_int_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1510
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1511
lemma int_and_ge0 [simp]: 
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1512
  \<open>x AND y \<ge> 0 \<longleftrightarrow> x \<ge> 0 \<or> y \<ge> 0\<close> for x y :: int
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1513
  by (fact and_nonnegative_int_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1514
  
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1515
lemma int_and_1: fixes x :: int shows "x AND 1 = x mod 2"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1516
  by (fact and_one_eq)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1517
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1518
lemma int_1_and: fixes x :: int shows "1 AND x = x mod 2"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1519
  by (fact one_and_eq)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1520
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1521
lemma int_or_lt0 [simp]: 
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1522
  \<open>x OR y < 0 \<longleftrightarrow> x < 0 \<or> y < 0\<close> for x y :: int
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1523
  by (fact or_negative_int_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1524
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1525
lemma int_or_ge0 [simp]:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1526
  \<open>x OR y \<ge> 0 \<longleftrightarrow> x \<ge> 0 \<and> y \<ge> 0\<close> for x y :: int
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1527
  by (fact or_nonnegative_int_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1528
  
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1529
lemma int_xor_lt0 [simp]:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1530
  \<open>x XOR y < 0 \<longleftrightarrow> (x < 0) \<noteq> (y < 0)\<close> for x y :: int
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1531
  by (fact xor_negative_int_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1532
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1533
lemma int_xor_ge0 [simp]:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1534
  \<open>x XOR y \<ge> 0 \<longleftrightarrow> (x \<ge> 0 \<longleftrightarrow> y \<ge> 0)\<close> for x y :: int
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1535
  by (fact xor_nonnegative_int_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1536
  
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1537
lemma even_conv_AND:
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1538
  \<open>even i \<longleftrightarrow> i AND 1 = 0\<close> for i :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1539
  by (simp add: and_one_eq mod2_eq_if)
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1540
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1541
lemma bin_last_conv_AND:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1542
  "bin_last i \<longleftrightarrow> i AND 1 \<noteq> 0"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1543
  by (simp add: and_one_eq mod2_eq_if)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1544
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1545
lemma bitval_bin_last:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1546
  "of_bool (bin_last i) = i AND 1"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1547
  by (simp add: and_one_eq mod2_eq_if)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1548
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1549
lemma bin_sign_and:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1550
  "bin_sign (i AND j) = - (bin_sign i * bin_sign j)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1551
by(simp add: bin_sign_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1552
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1553
lemma int_not_neg_numeral: "NOT (- numeral n) = (Num.sub n num.One :: int)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1554
by(simp add: int_not_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1555
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1556
lemma int_neg_numeral_pOne_conv_not: "- numeral (n + num.One) = (NOT (numeral n) :: int)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1557
by(simp add: int_not_def)
70169
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1558
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1559
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1560
subsection \<open>Setting and clearing bits\<close>
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1561
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1562
lemma bin_last_conv_lsb: "bin_last = lsb"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1563
by(clarsimp simp add: lsb_int_def fun_eq_iff)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1564
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1565
lemma int_lsb_numeral [simp]:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1566
  "lsb (0 :: int) = False"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1567
  "lsb (1 :: int) = True"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1568
  "lsb (Numeral1 :: int) = True"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1569
  "lsb (- 1 :: int) = True"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1570
  "lsb (- Numeral1 :: int) = True"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1571
  "lsb (numeral (num.Bit0 w) :: int) = False"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1572
  "lsb (numeral (num.Bit1 w) :: int) = True"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1573
  "lsb (- numeral (num.Bit0 w) :: int) = False"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1574
  "lsb (- numeral (num.Bit1 w) :: int) = True"
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1575
  by (simp_all add: lsb_int_def)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1576
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1577
lemma int_set_bit_0 [simp]: fixes x :: int shows
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1578
  "set_bit x 0 b = of_bool b + 2 * (x div 2)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1579
  by (auto simp add: set_bit_int_def intro: bin_rl_eqI)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1580
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1581
lemma int_set_bit_Suc: fixes x :: int shows
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1582
  "set_bit x (Suc n) b = of_bool (odd x) + 2 * set_bit (x div 2) n b"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1583
  by (auto simp add: set_bit_int_def intro: bin_rl_eqI)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1584
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1585
lemma bin_last_set_bit:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1586
  "bin_last (set_bit x n b) = (if n > 0 then bin_last x else b)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1587
  by (cases n) (simp_all add: int_set_bit_Suc)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1588
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1589
lemma bin_rest_set_bit: 
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1590
  "bin_rest (set_bit x n b) = (if n > 0 then set_bit (x div 2) (n - 1) b else x div 2)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1591
  by (cases n) (simp_all add: int_set_bit_Suc)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1592
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1593
lemma int_set_bit_numeral: fixes x :: int shows
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1594
  "set_bit x (numeral w) b = of_bool (odd x) + 2 * set_bit (x div 2) (pred_numeral w) b"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1595
  by (simp add: set_bit_int_def)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1596
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1597
lemmas int_set_bit_numerals [simp] =
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1598
  int_set_bit_numeral[where x="numeral w'"] 
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1599
  int_set_bit_numeral[where x="- numeral w'"]
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1600
  int_set_bit_numeral[where x="Numeral1"]
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1601
  int_set_bit_numeral[where x="1"]
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1602
  int_set_bit_numeral[where x="0"]
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1603
  int_set_bit_Suc[where x="numeral w'"]
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1604
  int_set_bit_Suc[where x="- numeral w'"]
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1605
  int_set_bit_Suc[where x="Numeral1"]
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1606
  int_set_bit_Suc[where x="1"]
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1607
  int_set_bit_Suc[where x="0"]
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1608
  for w'
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1609
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1610
lemma int_shiftl_BIT: fixes x :: int
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1611
  shows int_shiftl0 [simp]: "x << 0 = x"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1612
  and int_shiftl_Suc [simp]: "x << Suc n = 2 * (x << n)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1613
  by (auto simp add: shiftl_int_def)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1614
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1615
lemma int_0_shiftl [simp]: "0 << n = (0 :: int)"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1616
by(induct n) simp_all
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1617
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1618
lemma bin_last_shiftl: "bin_last (x << n) \<longleftrightarrow> n = 0 \<and> bin_last x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1619
by(cases n)(simp_all)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1620
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1621
lemma bin_rest_shiftl: "bin_rest (x << n) = (if n > 0 then x << (n - 1) else bin_rest x)"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1622
by(cases n)(simp_all)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1623
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1624
lemma bin_nth_shiftl [simp]: "bin_nth (x << n) m \<longleftrightarrow> n \<le> m \<and> bin_nth x (m - n)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1625
  by (simp add: bit_push_bit_iff_int shiftl_eq_push_bit)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1626
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1627
lemma bin_last_shiftr: "odd (x >> n) \<longleftrightarrow> x !! n" for x :: int
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1628
  by (simp add: shiftr_eq_drop_bit bit_iff_odd_drop_bit)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1629
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1630
lemma bin_rest_shiftr [simp]: "bin_rest (x >> n) = x >> Suc n"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1631
  by (simp add: bit_eq_iff shiftr_eq_drop_bit drop_bit_Suc bit_drop_bit_eq drop_bit_half)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1632
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1633
lemma bin_nth_shiftr [simp]: "bin_nth (x >> n) m = bin_nth x (n + m)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1634
  by (simp add: shiftr_eq_drop_bit bit_drop_bit_eq)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1635
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1636
lemma bin_nth_conv_AND:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1637
  fixes x :: int shows 
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1638
  "bin_nth x n \<longleftrightarrow> x AND (1 << n) \<noteq> 0"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1639
  by (simp add: bit_eq_iff)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1640
    (auto simp add: shiftl_eq_push_bit bit_and_iff bit_push_bit_iff bit_exp_iff)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1641
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1642
lemma int_shiftl_numeral [simp]: 
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1643
  "(numeral w :: int) << numeral w' = numeral (num.Bit0 w) << pred_numeral w'"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1644
  "(- numeral w :: int) << numeral w' = - numeral (num.Bit0 w) << pred_numeral w'"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1645
by(simp_all add: numeral_eq_Suc shiftl_int_def)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1646
  (metis add_One mult_inc semiring_norm(11) semiring_norm(13) semiring_norm(2) semiring_norm(6) semiring_norm(87))+
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1647
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
  1648
lemma int_shiftl_One_numeral [simp]:
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
  1649
  "(1 :: int) << numeral w = 2 << pred_numeral w"
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
  1650
  using int_shiftl_numeral [of Num.One w] by simp
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1651
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1652
lemma shiftl_ge_0 [simp]: fixes i :: int shows "i << n \<ge> 0 \<longleftrightarrow> i \<ge> 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1653
by(induct n) simp_all
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1654
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1655
lemma shiftl_lt_0 [simp]: fixes i :: int shows "i << n < 0 \<longleftrightarrow> i < 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1656
by (metis not_le shiftl_ge_0)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1657
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1658
lemma int_shiftl_test_bit: "(n << i :: int) !! m \<longleftrightarrow> m \<ge> i \<and> n !! (m - i)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1659
  by simp
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1660
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1661
lemma int_0shiftr [simp]: "(0 :: int) >> x = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1662
by(simp add: shiftr_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1663
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1664
lemma int_minus1_shiftr [simp]: "(-1 :: int) >> x = -1"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1665
by(simp add: shiftr_int_def div_eq_minus1)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1666
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1667
lemma int_shiftr_ge_0 [simp]: fixes i :: int shows "i >> n \<ge> 0 \<longleftrightarrow> i \<ge> 0"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1668
  by (simp add: shiftr_eq_drop_bit)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1669
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1670
lemma int_shiftr_lt_0 [simp]: fixes i :: int shows "i >> n < 0 \<longleftrightarrow> i < 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1671
by (metis int_shiftr_ge_0 not_less)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1672
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1673
lemma int_shiftr_numeral [simp]:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1674
  "(1 :: int) >> numeral w' = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1675
  "(numeral num.One :: int) >> numeral w' = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1676
  "(numeral (num.Bit0 w) :: int) >> numeral w' = numeral w >> pred_numeral w'"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1677
  "(numeral (num.Bit1 w) :: int) >> numeral w' = numeral w >> pred_numeral w'"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1678
  "(- numeral (num.Bit0 w) :: int) >> numeral w' = - numeral w >> pred_numeral w'"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1679
  "(- numeral (num.Bit1 w) :: int) >> numeral w' = - numeral (Num.inc w) >> pred_numeral w'"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1680
  by (simp_all add: shiftr_eq_drop_bit numeral_eq_Suc add_One drop_bit_Suc)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1681
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1682
lemma int_shiftr_numeral_Suc0 [simp]:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1683
  "(1 :: int) >> Suc 0 = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1684
  "(numeral num.One :: int) >> Suc 0 = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1685
  "(numeral (num.Bit0 w) :: int) >> Suc 0 = numeral w"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1686
  "(numeral (num.Bit1 w) :: int) >> Suc 0 = numeral w"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1687
  "(- numeral (num.Bit0 w) :: int) >> Suc 0 = - numeral w"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1688
  "(- numeral (num.Bit1 w) :: int) >> Suc 0 = - numeral (Num.inc w)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1689
  by (simp_all add: shiftr_eq_drop_bit drop_bit_Suc add_One)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1690
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1691
lemma bin_nth_minus_p2:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1692
  assumes sign: "bin_sign x = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1693
  and y: "y = 1 << n"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1694
  and m: "m < n"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1695
  and x: "x < y"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1696
  shows "bin_nth (x - y) m = bin_nth x m"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1697
proof -
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1698
  from sign y x have \<open>x \<ge> 0\<close> and \<open>y = 2 ^ n\<close> and \<open>x < 2 ^ n\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1699
    by (simp_all add: bin_sign_def shiftl_eq_push_bit push_bit_eq_mult split: if_splits)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1700
  from \<open>0 \<le> x\<close> \<open>x < 2 ^ n\<close> \<open>m < n\<close> have \<open>bit x m \<longleftrightarrow> bit (x - 2 ^ n) m\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1701
  proof (induction m arbitrary: x n)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1702
    case 0
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1703
    then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1704
      by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1705
  next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1706
    case (Suc m)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1707
    moreover define q where \<open>q = n - 1\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1708
    ultimately have n: \<open>n = Suc q\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1709
      by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1710
    have \<open>(x - 2 ^ Suc q) div 2 = x div 2 - 2 ^ q\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1711
      by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1712
    moreover from Suc.IH [of \<open>x div 2\<close> q] Suc.prems
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1713
    have \<open>bit (x div 2) m \<longleftrightarrow> bit (x div 2 - 2 ^ q) m\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1714
      by (simp add: n)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1715
    ultimately show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1716
      by (simp add: bit_Suc n)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1717
  qed
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1718
  with \<open>y = 2 ^ n\<close> show ?thesis
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1719
    by simp
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1720
qed
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1721
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1722
lemma bin_clr_conv_NAND:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1723
  "bin_sc n False i = i AND NOT (1 << n)"
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1724
  by (induct n arbitrary: i) (rule bin_rl_eqI; simp)+
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1725
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1726
lemma bin_set_conv_OR:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1727
  "bin_sc n True i = i OR (1 << n)"
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1728
  by (induct n arbitrary: i) (rule bin_rl_eqI; simp)+
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1729
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1730
lemma msb_conv_bin_sign: "msb x \<longleftrightarrow> bin_sign x = -1"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1731
by(simp add: bin_sign_def not_le msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1732
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1733
lemma msb_bin_rest [simp]: "msb (bin_rest x) = msb x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1734
by(simp add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1735
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1736
lemma int_msb_and [simp]: "msb ((x :: int) AND y) \<longleftrightarrow> msb x \<and> msb y"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1737
by(simp add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1738
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1739
lemma int_msb_or [simp]: "msb ((x :: int) OR y) \<longleftrightarrow> msb x \<or> msb y"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1740
by(simp add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1741
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1742
lemma int_msb_xor [simp]: "msb ((x :: int) XOR y) \<longleftrightarrow> msb x \<noteq> msb y"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1743
by(simp add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1744
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1745
lemma int_msb_not [simp]: "msb (NOT (x :: int)) \<longleftrightarrow> \<not> msb x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1746
by(simp add: msb_int_def not_less)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1747
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1748
lemma msb_shiftl [simp]: "msb ((x :: int) << n) \<longleftrightarrow> msb x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1749
by(simp add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1750
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1751
lemma msb_shiftr [simp]: "msb ((x :: int) >> r) \<longleftrightarrow> msb x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1752
by(simp add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1753
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1754
lemma msb_bin_sc [simp]: "msb (bin_sc n b x) \<longleftrightarrow> msb x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1755
by(simp add: msb_conv_bin_sign)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1756
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1757
lemma msb_set_bit [simp]: "msb (set_bit (x :: int) n b) \<longleftrightarrow> msb x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1758
by(simp add: msb_conv_bin_sign set_bit_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1759
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1760
lemma msb_0 [simp]: "msb (0 :: int) = False"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1761
by(simp add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1762
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1763
lemma msb_1 [simp]: "msb (1 :: int) = False"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1764
by(simp add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1765
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1766
lemma msb_numeral [simp]:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1767
  "msb (numeral n :: int) = False"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1768
  "msb (- numeral n :: int) = True"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1769
by(simp_all add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1770
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1771
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1772
subsection \<open>Semantic interpretation of \<^typ>\<open>bool list\<close> as \<^typ>\<open>int\<close>\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1773
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1774
lemma bin_bl_bin': "bl_to_bin (bin_to_bl_aux n w bs) = bl_to_bin_aux bs (bintrunc n w)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1775
  by (induct n arbitrary: w bs) (auto simp: bl_to_bin_def take_bit_Suc ac_simps mod_2_eq_odd)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1776
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1777
lemma bin_bl_bin [simp]: "bl_to_bin (bin_to_bl n w) = bintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1778
  by (auto simp: bin_to_bl_def bin_bl_bin')
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1779
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1780
lemma bl_to_bin_rep_F: "bl_to_bin (replicate n False @ bl) = bl_to_bin bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1781
  by (simp add: bin_to_bl_zero_aux [symmetric] bin_bl_bin') (simp add: bl_to_bin_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1782
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1783
lemma bin_to_bl_trunc [simp]: "n \<le> m \<Longrightarrow> bin_to_bl n (bintrunc m w) = bin_to_bl n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1784
  by (auto intro: bl_to_bin_inj)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1785
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1786
lemma bin_to_bl_aux_bintr:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1787
  "bin_to_bl_aux n (bintrunc m bin) bl =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1788
    replicate (n - m) False @ bin_to_bl_aux (min n m) bin bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1789
  apply (induct n arbitrary: m bin bl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1790
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1791
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1792
  apply (case_tac "m")
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1793
   apply (clarsimp simp: bin_to_bl_zero_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1794
   apply (erule thin_rl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1795
   apply (induct_tac n)
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
  1796
    apply (auto simp add: take_bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1797
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1798
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1799
lemma bin_to_bl_bintr:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1800
  "bin_to_bl n (bintrunc m bin) = replicate (n - m) False @ bin_to_bl (min n m) bin"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1801
  unfolding bin_to_bl_def by (rule bin_to_bl_aux_bintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1802
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1803
lemma bl_to_bin_rep_False: "bl_to_bin (replicate n False) = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1804
  by (induct n) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1805
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1806
lemma len_bin_to_bl_aux: "length (bin_to_bl_aux n w bs) = n + length bs"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1807
  by (fact size_bin_to_bl_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1808
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1809
lemma len_bin_to_bl: "length (bin_to_bl n w) = n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1810
  by (fact size_bin_to_bl) (* FIXME: duplicate *)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1811
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1812
lemma sign_bl_bin': "bin_sign (bl_to_bin_aux bs w) = bin_sign w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1813
  by (induction bs arbitrary: w) (simp_all add: bin_sign_def)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1814
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1815
lemma sign_bl_bin: "bin_sign (bl_to_bin bs) = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1816
  by (simp add: bl_to_bin_def sign_bl_bin')
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1817
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1818
lemma bl_sbin_sign_aux: "hd (bin_to_bl_aux (Suc n) w bs) = (bin_sign (sbintrunc n w) = -1)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1819
  by (induction n arbitrary: w bs) (simp_all add: bin_sign_def)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1820
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1821
lemma bl_sbin_sign: "hd (bin_to_bl (Suc n) w) = (bin_sign (sbintrunc n w) = -1)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1822
  unfolding bin_to_bl_def by (rule bl_sbin_sign_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1823
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1824
lemma bin_nth_of_bl_aux:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1825
  "bin_nth (bl_to_bin_aux bl w) n =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1826
    (n < size bl \<and> rev bl ! n \<or> n \<ge> length bl \<and> bin_nth w (n - size bl))"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1827
  apply (induction bl arbitrary: w)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1828
   apply simp_all
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1829
  apply safe
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1830
                      apply (simp_all add: not_le nth_append bit_double_iff even_bit_succ_iff split: if_splits)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1831
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1832
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1833
lemma bin_nth_of_bl: "bin_nth (bl_to_bin bl) n = (n < length bl \<and> rev bl ! n)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1834
  by (simp add: bl_to_bin_def bin_nth_of_bl_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1835
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1836
lemma bin_nth_bl: "n < m \<Longrightarrow> bin_nth w n = nth (rev (bin_to_bl m w)) n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1837
  apply (induct n arbitrary: m w)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1838
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1839
   apply (case_tac m, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1840
   apply (clarsimp simp: bin_to_bl_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1841
   apply (simp add: bin_to_bl_aux_alt)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1842
  apply (case_tac m, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1843
  apply (clarsimp simp: bin_to_bl_def)
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
  1844
  apply (simp add: bin_to_bl_aux_alt bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1845
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1846
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1847
lemma nth_bin_to_bl_aux:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1848
  "n < m + length bl \<Longrightarrow> (bin_to_bl_aux m w bl) ! n =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1849
    (if n < m then bin_nth w (m - 1 - n) else bl ! (n - m))"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1850
  apply (induction bl arbitrary: w)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1851
   apply simp_all
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1852
   apply (metis add.right_neutral bin_nth_bl bin_to_bl_def diff_Suc_less less_Suc_eq_0_disj less_imp_Suc_add list.size(3) nth_rev_alt size_bin_to_bl_aux)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1853
  apply (metis One_nat_def Suc_pred add_diff_cancel_left' add_diff_cancel_right' bin_to_bl_aux_alt bin_to_bl_def cancel_comm_monoid_add_class.diff_cancel diff_Suc_Suc diff_is_0_eq diff_zero le_add_diff_inverse le_eq_less_or_eq less_Suc_eq_0_disj less_antisym less_imp_Suc_add list.size(3) nat_less_le nth_append order_refl size_bin_to_bl_aux)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1854
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1855
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1856
lemma nth_bin_to_bl: "n < m \<Longrightarrow> (bin_to_bl m w) ! n = bin_nth w (m - Suc n)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1857
  by (simp add: bin_to_bl_def nth_bin_to_bl_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1858
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1859
lemma bl_to_bin_lt2p_aux: "bl_to_bin_aux bs w < (w + 1) * (2 ^ length bs)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1860
proof (induction bs arbitrary: w)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1861
  case Nil
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1862
  then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1863
    by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1864
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1865
  case (Cons b bs)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1866
  from Cons.IH [of \<open>1 + 2 * w\<close>] Cons.IH [of \<open>2 * w\<close>]
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1867
  show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1868
    apply (auto simp add: algebra_simps)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1869
    apply (subst mult_2 [of \<open>2 ^ length bs\<close>])
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1870
    apply (simp only: add.assoc)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1871
    apply (rule pos_add_strict)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1872
     apply simp_all
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1873
    done
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1874
qed
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1875
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1876
lemma bl_to_bin_lt2p_drop: "bl_to_bin bs < 2 ^ length (dropWhile Not bs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1877
proof (induct bs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1878
  case Nil
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1879
  then show ?case by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1880
next
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1881
  case (Cons b bs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1882
  with bl_to_bin_lt2p_aux[where w=1] show ?case
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1883
    by (simp add: bl_to_bin_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1884
qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1885
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1886
lemma bl_to_bin_lt2p: "bl_to_bin bs < 2 ^ length bs"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1887
  by (metis bin_bl_bin bintr_lt2p bl_bin_bl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1888
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1889
lemma bl_to_bin_ge2p_aux: "bl_to_bin_aux bs w \<ge> w * (2 ^ length bs)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1890
proof (induction bs arbitrary: w)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1891
  case Nil
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1892
  then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1893
    by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1894
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1895
  case (Cons b bs)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1896
  from Cons.IH [of \<open>1 + 2 * w\<close>] Cons.IH [of \<open>2 * w\<close>]
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1897
  show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1898
    apply (auto simp add: algebra_simps)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1899
    apply (rule add_le_imp_le_left [of \<open>2 ^ length bs\<close>])
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1900
    apply (rule add_increasing)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1901
    apply simp_all
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1902
    done
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1903
qed
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1904
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1905
lemma bl_to_bin_ge0: "bl_to_bin bs \<ge> 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1906
  apply (unfold bl_to_bin_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1907
  apply (rule xtrans(4))
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1908
   apply (rule bl_to_bin_ge2p_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1909
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1910
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1911
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1912
lemma butlast_rest_bin: "butlast (bin_to_bl n w) = bin_to_bl (n - 1) (bin_rest w)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1913
  apply (unfold bin_to_bl_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1914
  apply (cases n, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1915
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1916
  apply (auto simp add: bin_to_bl_aux_alt)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1917
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1918
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1919
lemma butlast_bin_rest: "butlast bl = bin_to_bl (length bl - Suc 0) (bin_rest (bl_to_bin bl))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1920
  using butlast_rest_bin [where w="bl_to_bin bl" and n="length bl"] by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1921
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1922
lemma butlast_rest_bl2bin_aux:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1923
  "bl \<noteq> [] \<Longrightarrow> bl_to_bin_aux (butlast bl) w = bin_rest (bl_to_bin_aux bl w)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1924
  by (induct bl arbitrary: w) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1925
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1926
lemma butlast_rest_bl2bin: "bl_to_bin (butlast bl) = bin_rest (bl_to_bin bl)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1927
  by (cases bl) (auto simp: bl_to_bin_def butlast_rest_bl2bin_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1928
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1929
lemma trunc_bl2bin_aux:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1930
  "bintrunc m (bl_to_bin_aux bl w) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1931
    bl_to_bin_aux (drop (length bl - m) bl) (bintrunc (m - length bl) w)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1932
proof (induct bl arbitrary: w)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1933
  case Nil
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1934
  show ?case by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1935
next
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1936
  case (Cons b bl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1937
  show ?case
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1938
  proof (cases "m - length bl")
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1939
    case 0
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1940
    then have "Suc (length bl) - m = Suc (length bl - m)" by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1941
    with Cons show ?thesis by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1942
  next
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1943
    case (Suc n)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1944
    then have "m - Suc (length bl) = n" by simp
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1945
    with Cons Suc show ?thesis by (simp add: take_bit_Suc ac_simps)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1946
  qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1947
qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1948
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1949
lemma trunc_bl2bin: "bintrunc m (bl_to_bin bl) = bl_to_bin (drop (length bl - m) bl)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1950
  by (simp add: bl_to_bin_def trunc_bl2bin_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1951
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1952
lemma trunc_bl2bin_len [simp]: "bintrunc (length bl) (bl_to_bin bl) = bl_to_bin bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1953
  by (simp add: trunc_bl2bin)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1954
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1955
lemma bl2bin_drop: "bl_to_bin (drop k bl) = bintrunc (length bl - k) (bl_to_bin bl)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1956
  apply (rule trans)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1957
   prefer 2
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1958
   apply (rule trunc_bl2bin [symmetric])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1959
  apply (cases "k \<le> length bl")
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1960
   apply auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1961
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1962
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1963
lemma take_rest_power_bin: "m \<le> n \<Longrightarrow> take m (bin_to_bl n w) = bin_to_bl m ((bin_rest ^^ (n - m)) w)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1964
  apply (rule nth_equalityI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1965
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1966
  apply (clarsimp simp add: nth_bin_to_bl nth_rest_power_bin)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1967
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1968
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1969
lemma last_bin_last': "size xs > 0 \<Longrightarrow> last xs \<longleftrightarrow> bin_last (bl_to_bin_aux xs w)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1970
  by (induct xs arbitrary: w) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1971
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1972
lemma last_bin_last: "size xs > 0 \<Longrightarrow> last xs \<longleftrightarrow> bin_last (bl_to_bin xs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1973
  unfolding bl_to_bin_def by (erule last_bin_last')
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1974
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1975
lemma bin_last_last: "bin_last w \<longleftrightarrow> last (bin_to_bl (Suc n) w)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1976
  by (simp add: bin_to_bl_def) (auto simp: bin_to_bl_aux_alt)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1977
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1978
lemma drop_bin2bl_aux:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1979
  "drop m (bin_to_bl_aux n bin bs) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1980
    bin_to_bl_aux (n - m) bin (drop (m - n) bs)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1981
  apply (induction n arbitrary: m bin bs)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1982
   apply auto
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1983
  apply (case_tac "m \<le> n")
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1984
   apply (auto simp add: not_le Suc_diff_le)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1985
  apply (case_tac "m - n")
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1986
   apply auto
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1987
  apply (use Suc_diff_Suc in fastforce)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1988
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1989
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1990
lemma drop_bin2bl: "drop m (bin_to_bl n bin) = bin_to_bl (n - m) bin"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1991
  by (simp add: bin_to_bl_def drop_bin2bl_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1992
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1993
lemma take_bin2bl_lem1: "take m (bin_to_bl_aux m w bs) = bin_to_bl m w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1994
  apply (induct m arbitrary: w bs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1995
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1996
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1997
  apply (simp add: bin_to_bl_aux_alt)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1998
  apply (simp add: bin_to_bl_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1999
  apply (simp add: bin_to_bl_aux_alt)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2000
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2001
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2002
lemma take_bin2bl_lem: "take m (bin_to_bl_aux (m + n) w bs) = take m (bin_to_bl (m + n) w)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2003
  by (induct n arbitrary: w bs) (simp_all (no_asm) add: bin_to_bl_def take_bin2bl_lem1, simp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2004
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2005
lemma bin_split_take: "bin_split n c = (a, b) \<Longrightarrow> bin_to_bl m a = take m (bin_to_bl (m + n) c)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2006
  apply (induct n arbitrary: b c)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2007
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2008
  apply (clarsimp simp: Let_def split: prod.split_asm)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2009
  apply (simp add: bin_to_bl_def)
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
  2010
  apply (simp add: take_bin2bl_lem drop_bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2011
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2012
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
  2013
lemma bin_to_bl_drop_bit:
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
  2014
  "k = m + n \<Longrightarrow> bin_to_bl m (drop_bit n c) = take m (bin_to_bl k c)"
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
  2015
  using bin_split_take by simp
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
  2016
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2017
lemma bin_split_take1:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2018
  "k = m + n \<Longrightarrow> bin_split n c = (a, b) \<Longrightarrow> bin_to_bl m a = take m (bin_to_bl k c)"
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
  2019
  using bin_split_take by simp
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2020
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2021
lemma takefill_bintrunc: "takefill False n bl = rev (bin_to_bl n (bl_to_bin (rev bl)))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2022
  apply (rule nth_equalityI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2023
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2024
  apply (clarsimp simp: nth_takefill nth_rev nth_bin_to_bl bin_nth_of_bl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2025
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2026
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2027
lemma bl_bin_bl_rtf: "bin_to_bl n (bl_to_bin bl) = rev (takefill False n (rev bl))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2028
  by (simp add: takefill_bintrunc)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2029
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2030
lemma bl_bin_bl_rep_drop:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2031
  "bin_to_bl n (bl_to_bin bl) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2032
    replicate (n - length bl) False @ drop (length bl - n) bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2033
  by (simp add: bl_bin_bl_rtf takefill_alt rev_take)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2034
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2035
lemma bl_to_bin_aux_cat:
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2036
  "bl_to_bin_aux bs (bin_cat w nv v) =
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2037
    bin_cat w (nv + length bs) (bl_to_bin_aux bs v)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2038
  by (rule bit_eqI)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2039
    (auto simp add: bin_nth_of_bl_aux bin_nth_cat algebra_simps)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2040
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2041
lemma bin_to_bl_aux_cat:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2042
  "\<And>w bs. bin_to_bl_aux (nv + nw) (bin_cat v nw w) bs =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2043
    bin_to_bl_aux nv v (bin_to_bl_aux nw w bs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2044
  by (induct nw) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2045
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2046
lemma bl_to_bin_aux_alt: "bl_to_bin_aux bs w = bin_cat w (length bs) (bl_to_bin bs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2047
  using bl_to_bin_aux_cat [where nv = "0" and v = "0"]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2048
  by (simp add: bl_to_bin_def [symmetric])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2049
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2050
lemma bin_to_bl_cat:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2051
  "bin_to_bl (nv + nw) (bin_cat v nw w) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2052
    bin_to_bl_aux nv v (bin_to_bl nw w)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2053
  by (simp add: bin_to_bl_def bin_to_bl_aux_cat)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2054
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2055
lemmas bl_to_bin_aux_app_cat =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2056
  trans [OF bl_to_bin_aux_append bl_to_bin_aux_alt]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2057
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2058
lemmas bin_to_bl_aux_cat_app =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2059
  trans [OF bin_to_bl_aux_cat bin_to_bl_aux_alt]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2060
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2061
lemma bl_to_bin_app_cat:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2062
  "bl_to_bin (bsa @ bs) = bin_cat (bl_to_bin bsa) (length bs) (bl_to_bin bs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2063
  by (simp only: bl_to_bin_aux_app_cat bl_to_bin_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2064
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2065
lemma bin_to_bl_cat_app:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2066
  "bin_to_bl (n + nw) (bin_cat w nw wa) = bin_to_bl n w @ bin_to_bl nw wa"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2067
  by (simp only: bin_to_bl_def bin_to_bl_aux_cat_app)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2068
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2069
text \<open>\<open>bl_to_bin_app_cat_alt\<close> and \<open>bl_to_bin_app_cat\<close> are easily interderivable.\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2070
lemma bl_to_bin_app_cat_alt: "bin_cat (bl_to_bin cs) n w = bl_to_bin (cs @ bin_to_bl n w)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2071
  by (simp add: bl_to_bin_app_cat)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2072
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2073
lemma mask_lem: "(bl_to_bin (True # replicate n False)) = bl_to_bin (replicate n True) + 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2074
  apply (unfold bl_to_bin_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2075
  apply (induct n)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2076
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2077
  apply (simp only: Suc_eq_plus1 replicate_add append_Cons [symmetric] bl_to_bin_aux_append)
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2078
  apply simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2079
  done
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2080
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2081
lemma bin_exhaust:
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2082
  "(\<And>x b. bin = of_bool b + 2 * x \<Longrightarrow> Q) \<Longrightarrow> Q" for bin :: int
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2083
  apply (cases \<open>even bin\<close>)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2084
   apply (auto elim!: evenE oddE)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2085
   apply fastforce
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2086
  apply fastforce
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2087
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2088
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2089
primrec rbl_succ :: "bool list \<Rightarrow> bool list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2090
  where
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2091
    Nil: "rbl_succ Nil = Nil"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2092
  | Cons: "rbl_succ (x # xs) = (if x then False # rbl_succ xs else True # xs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2093
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2094
primrec rbl_pred :: "bool list \<Rightarrow> bool list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2095
  where
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2096
    Nil: "rbl_pred Nil = Nil"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2097
  | Cons: "rbl_pred (x # xs) = (if x then False # xs else True # rbl_pred xs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2098
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2099
primrec rbl_add :: "bool list \<Rightarrow> bool list \<Rightarrow> bool list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2100
  where \<comment> \<open>result is length of first arg, second arg may be longer\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2101
    Nil: "rbl_add Nil x = Nil"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2102
  | Cons: "rbl_add (y # ys) x =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2103
      (let ws = rbl_add ys (tl x)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2104
       in (y \<noteq> hd x) # (if hd x \<and> y then rbl_succ ws else ws))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2105
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2106
primrec rbl_mult :: "bool list \<Rightarrow> bool list \<Rightarrow> bool list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2107
  where \<comment> \<open>result is length of first arg, second arg may be longer\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2108
    Nil: "rbl_mult Nil x = Nil"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2109
  | Cons: "rbl_mult (y # ys) x =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2110
      (let ws = False # rbl_mult ys x
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2111
       in if y then rbl_add ws x else ws)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2112
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2113
lemma size_rbl_pred: "length (rbl_pred bl) = length bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2114
  by (induct bl) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2115
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2116
lemma size_rbl_succ: "length (rbl_succ bl) = length bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2117
  by (induct bl) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2118
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2119
lemma size_rbl_add: "length (rbl_add bl cl) = length bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2120
  by (induct bl arbitrary: cl) (auto simp: Let_def size_rbl_succ)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2121
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2122
lemma size_rbl_mult: "length (rbl_mult bl cl) = length bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2123
  by (induct bl arbitrary: cl) (auto simp add: Let_def size_rbl_add)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2124
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2125
lemmas rbl_sizes [simp] =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2126
  size_rbl_pred size_rbl_succ size_rbl_add size_rbl_mult
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2127
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2128
lemmas rbl_Nils =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2129
  rbl_pred.Nil rbl_succ.Nil rbl_add.Nil rbl_mult.Nil
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2130
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2131
lemma rbl_add_app2: "length blb \<ge> length bla \<Longrightarrow> rbl_add bla (blb @ blc) = rbl_add bla blb"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2132
  apply (induct bla arbitrary: blb)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2133
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2134
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2135
  apply (case_tac blb, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2136
  apply (clarsimp simp: Let_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2137
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2138
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2139
lemma rbl_add_take2:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2140
  "length blb \<ge> length bla \<Longrightarrow> rbl_add bla (take (length bla) blb) = rbl_add bla blb"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2141
  apply (induct bla arbitrary: blb)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2142
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2143
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2144
  apply (case_tac blb, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2145
  apply (clarsimp simp: Let_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2146
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2147
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2148
lemma rbl_mult_app2: "length blb \<ge> length bla \<Longrightarrow> rbl_mult bla (blb @ blc) = rbl_mult bla blb"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2149
  apply (induct bla arbitrary: blb)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2150
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2151
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2152
  apply (case_tac blb, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2153
  apply (clarsimp simp: Let_def rbl_add_app2)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2154
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2155
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2156
lemma rbl_mult_take2:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2157
  "length blb \<ge> length bla \<Longrightarrow> rbl_mult bla (take (length bla) blb) = rbl_mult bla blb"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2158
  apply (rule trans)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2159
   apply (rule rbl_mult_app2 [symmetric])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2160
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2161
  apply (rule_tac f = "rbl_mult bla" in arg_cong)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2162
  apply (rule append_take_drop_id)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2163
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2164
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2165
lemma rbl_add_split:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2166
  "P (rbl_add (y # ys) (x # xs)) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2167
    (\<forall>ws. length ws = length ys \<longrightarrow> ws = rbl_add ys xs \<longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2168
      (y \<longrightarrow> ((x \<longrightarrow> P (False # rbl_succ ws)) \<and> (\<not> x \<longrightarrow> P (True # ws)))) \<and>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2169
      (\<not> y \<longrightarrow> P (x # ws)))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2170
  by (cases y) (auto simp: Let_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2171
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2172
lemma rbl_mult_split:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2173
  "P (rbl_mult (y # ys) xs) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2174
    (\<forall>ws. length ws = Suc (length ys) \<longrightarrow> ws = False # rbl_mult ys xs \<longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2175
      (y \<longrightarrow> P (rbl_add ws xs)) \<and> (\<not> y \<longrightarrow> P ws))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2176
  by (auto simp: Let_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2177
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2178
lemma rbl_pred: "rbl_pred (rev (bin_to_bl n bin)) = rev (bin_to_bl n (bin - 1))"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2179
proof (unfold bin_to_bl_def, induction n arbitrary: bin)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2180
  case 0
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2181
  then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2182
    by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2183
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2184
  case (Suc n)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2185
  obtain b k where \<open>bin = of_bool b + 2 * k\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2186
    using bin_exhaust by blast
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2187
  moreover have \<open>(2 * k - 1) div 2 = k - 1\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2188
    using even_succ_div_2 [of \<open>2 * (k - 1)\<close>] 
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2189
    by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2190
  ultimately show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2191
    using Suc [of \<open>bin div 2\<close>]
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2192
    by simp (simp add: bin_to_bl_aux_alt)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2193
qed
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2194
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2195
lemma rbl_succ: "rbl_succ (rev (bin_to_bl n bin)) = rev (bin_to_bl n (bin + 1))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2196
  apply (unfold bin_to_bl_def)
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2197
  apply (induction n arbitrary: bin)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2198
   apply simp_all
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2199
  apply (case_tac bin rule: bin_exhaust)
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2200
  apply simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2201
  apply (simp add: bin_to_bl_aux_alt ac_simps)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2202
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2203
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2204
lemma rbl_add:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2205
  "\<And>bina binb. rbl_add (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb)) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2206
    rev (bin_to_bl n (bina + binb))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2207
  apply (unfold bin_to_bl_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2208
  apply (induct n)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2209
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2210
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2211
  apply (case_tac bina rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2212
  apply (case_tac binb rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2213
  apply (case_tac b)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2214
   apply (case_tac [!] "ba")
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2215
     apply (auto simp: rbl_succ bin_to_bl_aux_alt Let_def ac_simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2216
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2217
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2218
lemma rbl_add_long:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2219
  "m \<ge> n \<Longrightarrow> rbl_add (rev (bin_to_bl n bina)) (rev (bin_to_bl m binb)) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2220
    rev (bin_to_bl n (bina + binb))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2221
  apply (rule box_equals [OF _ rbl_add_take2 rbl_add])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2222
   apply (rule_tac f = "rbl_add (rev (bin_to_bl n bina))" in arg_cong)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2223
   apply (rule rev_swap [THEN iffD1])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2224
   apply (simp add: rev_take drop_bin2bl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2225
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2226
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2227
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2228
lemma rbl_mult_gt1:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2229
  "m \<ge> length bl \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2230
    rbl_mult bl (rev (bin_to_bl m binb)) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2231
    rbl_mult bl (rev (bin_to_bl (length bl) binb))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2232
  apply (rule trans)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2233
   apply (rule rbl_mult_take2 [symmetric])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2234
   apply simp_all
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2235
  apply (rule_tac f = "rbl_mult bl" in arg_cong)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2236
  apply (rule rev_swap [THEN iffD1])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2237
  apply (simp add: rev_take drop_bin2bl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2238
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2239
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2240
lemma rbl_mult_gt:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2241
  "m > n \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2242
    rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl m binb)) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2243
    rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2244
  by (auto intro: trans [OF rbl_mult_gt1])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2245
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2246
lemmas rbl_mult_Suc = lessI [THEN rbl_mult_gt]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2247
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2248
lemma rbbl_Cons: "b # rev (bin_to_bl n x) = rev (bin_to_bl (Suc n) (of_bool b + 2 * x))"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2249
  by (simp add: bin_to_bl_def) (simp add: bin_to_bl_aux_alt)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2250
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2251
lemma rbl_mult:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2252
  "rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb)) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2253
    rev (bin_to_bl n (bina * binb))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2254
  apply (induct n arbitrary: bina binb)
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2255
   apply simp_all
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2256
  apply (unfold bin_to_bl_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2257
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2258
  apply (case_tac bina rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2259
  apply (case_tac binb rule: bin_exhaust)
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2260
  apply simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2261
  apply (simp add: bin_to_bl_aux_alt)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2262
  apply (simp add: rbbl_Cons rbl_mult_Suc rbl_add algebra_simps)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2263
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2264
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2265
lemma sclem: "size (concat (map (bin_to_bl n) xs)) = length xs * n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2266
  by (induct xs) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2267
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2268
lemma bin_cat_foldl_lem:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2269
  "foldl (\<lambda>u. bin_cat u n) x xs =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2270
    bin_cat x (size xs * n) (foldl (\<lambda>u. bin_cat u n) y xs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2271
  apply (induct xs arbitrary: x)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2272
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2273
  apply (simp (no_asm))
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2274
  apply (frule asm_rl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2275
  apply (drule meta_spec)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2276
  apply (erule trans)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2277
  apply (drule_tac x = "bin_cat y n a" in meta_spec)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2278
  apply (simp add: bin_cat_assoc_sym min.absorb2)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2279
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2280
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2281
lemma bin_rcat_bl: "bin_rcat n wl = bl_to_bin (concat (map (bin_to_bl n) wl))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2282
  apply (unfold bin_rcat_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2283
  apply (rule sym)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2284
  apply (induct wl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2285
   apply (auto simp add: bl_to_bin_append)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2286
  apply (simp add: bl_to_bin_aux_alt sclem)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2287
  apply (simp add: bin_cat_foldl_lem [symmetric])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2288
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2289
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2290
lemma bin_last_bl_to_bin: "bin_last (bl_to_bin bs) \<longleftrightarrow> bs \<noteq> [] \<and> last bs"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2291
by(cases "bs = []")(auto simp add: bl_to_bin_def last_bin_last'[where w=0])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2292
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2293
lemma bin_rest_bl_to_bin: "bin_rest (bl_to_bin bs) = bl_to_bin (butlast bs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2294
by(cases "bs = []")(simp_all add: bl_to_bin_def butlast_rest_bl2bin_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2295
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2296
lemma bl_xor_aux_bin:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2297
  "map2 (\<lambda>x y. x \<noteq> y) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2298
    bin_to_bl_aux n (v XOR w) (map2 (\<lambda>x y. x \<noteq> y) bs cs)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2299
  apply (induction n arbitrary: v w bs cs)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2300
   apply auto
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2301
  apply (case_tac v rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2302
  apply (case_tac w rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2303
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2304
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2305
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2306
lemma bl_or_aux_bin:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2307
  "map2 (\<or>) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2308
    bin_to_bl_aux n (v OR w) (map2 (\<or>) bs cs)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2309
  by (induct n arbitrary: v w bs cs) simp_all
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2310
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2311
lemma bl_and_aux_bin:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2312
  "map2 (\<and>) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2313
    bin_to_bl_aux n (v AND w) (map2 (\<and>) bs cs)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  2314
  by (induction n arbitrary: v w bs cs) simp_all
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2315
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2316
lemma bl_not_aux_bin: "map Not (bin_to_bl_aux n w cs) = bin_to_bl_aux n (NOT w) (map Not cs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2317
  by (induct n arbitrary: w cs) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2318
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2319
lemma bl_not_bin: "map Not (bin_to_bl n w) = bin_to_bl n (NOT w)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2320
  by (simp add: bin_to_bl_def bl_not_aux_bin)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2321
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2322
lemma bl_and_bin: "map2 (\<and>) (bin_to_bl n v) (bin_to_bl n w) = bin_to_bl n (v AND w)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2323
  by (simp add: bin_to_bl_def bl_and_aux_bin)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2324
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2325
lemma bl_or_bin: "map2 (\<or>) (bin_to_bl n v) (bin_to_bl n w) = bin_to_bl n (v OR w)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2326
  by (simp add: bin_to_bl_def bl_or_aux_bin)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2327
70193
49a65e3f04c9 consolidated map2 clones
haftmann
parents: 70192
diff changeset
  2328
lemma bl_xor_bin: "map2 (\<noteq>) (bin_to_bl n v) (bin_to_bl n w) = bin_to_bl n (v XOR w)"
49a65e3f04c9 consolidated map2 clones
haftmann
parents: 70192
diff changeset
  2329
  using bl_xor_aux_bin by (simp add: bin_to_bl_def)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2330
70169
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  2331
end