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