src/HOL/Word/Bits_Int.thy
author haftmann
Thu, 18 Jun 2020 09:07:29 +0000
changeset 71942 d2654b30f7bd
parent 71941 49af3d9a818c
child 71943 d3fb19847662
permissions -rw-r--r--
eliminated warnings
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     1
(*  Title:      HOL/Word/Bits_Int.thy
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     2
    Author:     Jeremy Dawson and Gerwin Klein, NICTA
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     3
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     4
Definitions and basic theorems for bit-wise logical operations
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     5
for integers expressed using Pls, Min, BIT,
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     6
and converting them to and from lists of bools.
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     7
*)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     8
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
     9
section \<open>Bitwise Operations on integers\<close>
24350
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
    10
54854
3324a0078636 prefer "Bits" as theory name for abstract bit operations, similar to "Orderings", "Lattices", "Groups" etc.
haftmann
parents: 54848
diff changeset
    11
theory Bits_Int
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    12
  imports Bits Misc_Auxiliary
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    13
begin
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    14
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    15
subsection \<open>Implicit bit representation of \<^typ>\<open>int\<close>\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    16
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    17
definition Bit :: "int \<Rightarrow> bool \<Rightarrow> int"  (infixl "BIT" 90)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    18
  where "k BIT b = (if b then 1 else 0) + k + k"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    19
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    20
lemma Bit_B0: "k BIT False = k + k"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    21
   by (simp add: Bit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    22
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    23
lemma Bit_B1: "k BIT True = k + k + 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    24
   by (simp add: Bit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    25
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    26
lemma Bit_B0_2t: "k BIT False = 2 * k"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    27
  by (rule trans, rule Bit_B0) simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    28
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    29
lemma Bit_B1_2t: "k BIT True = 2 * k + 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    30
  by (rule trans, rule Bit_B1) simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    31
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    32
lemma uminus_Bit_eq:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    33
  "- k BIT b = (- k - of_bool b) BIT b"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    34
  by (cases b) (simp_all add: Bit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    35
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    36
lemma power_BIT: "2 ^ Suc n - 1 = (2 ^ n - 1) BIT True"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    37
  by (simp add: Bit_B1)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    38
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    39
abbreviation (input) bin_last :: "int \<Rightarrow> bool"
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    40
  where "bin_last \<equiv> odd"
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    41
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    42
lemma bin_last_def:
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    43
  "bin_last w \<longleftrightarrow> w mod 2 = 1"
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    44
  by (fact odd_iff_mod_2_eq_one)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    45
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    46
definition bin_rest :: "int \<Rightarrow> int"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    47
  where "bin_rest w = w div 2"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    48
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    49
lemma bin_rl_simp [simp]: "bin_rest w BIT bin_last w = w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    50
  unfolding bin_rest_def bin_last_def Bit_def
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    51
  by (cases "w mod 2 = 0") (use div_mult_mod_eq [of w 2] in simp_all)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    52
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    53
lemma bin_rest_BIT [simp]: "bin_rest (x BIT b) = x"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    54
  unfolding bin_rest_def Bit_def
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    55
  by (cases b) simp_all
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    56
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    57
lemma even_BIT [simp]: "even (x BIT b) \<longleftrightarrow> \<not> b"
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    58
  by (simp add: Bit_def)
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    59
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    60
lemma bin_last_BIT [simp]: "bin_last (x BIT b) = b"
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    61
  by simp
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    62
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    63
lemma BIT_eq_iff [iff]: "u BIT b = v BIT c \<longleftrightarrow> u = v \<and> b = c"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    64
  by (auto simp: Bit_def) arith+
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    65
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    66
lemma BIT_bin_simps [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    67
  "numeral k BIT False = numeral (Num.Bit0 k)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    68
  "numeral k BIT True = numeral (Num.Bit1 k)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    69
  "(- numeral k) BIT False = - numeral (Num.Bit0 k)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    70
  "(- numeral k) BIT True = - numeral (Num.BitM k)"
71942
d2654b30f7bd eliminated warnings
haftmann
parents: 71941
diff changeset
    71
  by (simp_all only: Bit_B0 Bit_B1 numeral.simps numeral_BitM)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    72
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    73
lemma BIT_special_simps [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    74
  shows "0 BIT False = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    75
    and "0 BIT True = 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    76
    and "1 BIT False = 2"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    77
    and "1 BIT True = 3"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    78
    and "(- 1) BIT False = - 2"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    79
    and "(- 1) BIT True = - 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    80
  by (simp_all add: Bit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    81
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    82
lemma Bit_eq_0_iff: "w BIT b = 0 \<longleftrightarrow> w = 0 \<and> \<not> b"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    83
  by (auto simp: Bit_def) arith
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    84
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    85
lemma Bit_eq_m1_iff: "w BIT b = -1 \<longleftrightarrow> w = -1 \<and> b"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    86
  by (auto simp: Bit_def) arith
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    87
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    88
lemma BitM_inc: "Num.BitM (Num.inc w) = Num.Bit1 w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    89
  by (induct w) simp_all
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    90
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    91
lemma expand_BIT:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    92
  "numeral (Num.Bit0 w) = numeral w BIT False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    93
  "numeral (Num.Bit1 w) = numeral w BIT True"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    94
  "- numeral (Num.Bit0 w) = (- numeral w) BIT False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    95
  "- numeral (Num.Bit1 w) = (- numeral (w + Num.One)) BIT True"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    96
  by (simp_all add: add_One BitM_inc)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    97
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    98
lemma bin_last_numeral_simps [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    99
  "\<not> bin_last 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   100
  "bin_last 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   101
  "bin_last (- 1)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   102
  "bin_last Numeral1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   103
  "\<not> bin_last (numeral (Num.Bit0 w))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   104
  "bin_last (numeral (Num.Bit1 w))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   105
  "\<not> bin_last (- numeral (Num.Bit0 w))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   106
  "bin_last (- numeral (Num.Bit1 w))"
71756
3d1f72d25fc3 more complete rules on numerals
haftmann
parents: 71181
diff changeset
   107
  by (simp_all add: bin_last_def zmod_zminus1_eq_if)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   108
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   109
lemma bin_rest_numeral_simps [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   110
  "bin_rest 0 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   111
  "bin_rest 1 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   112
  "bin_rest (- 1) = - 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   113
  "bin_rest Numeral1 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   114
  "bin_rest (numeral (Num.Bit0 w)) = numeral w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   115
  "bin_rest (numeral (Num.Bit1 w)) = numeral w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   116
  "bin_rest (- numeral (Num.Bit0 w)) = - numeral w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   117
  "bin_rest (- numeral (Num.Bit1 w)) = - numeral (w + Num.One)"
71756
3d1f72d25fc3 more complete rules on numerals
haftmann
parents: 71181
diff changeset
   118
  by (simp_all add: bin_rest_def zdiv_zminus1_eq_if)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   119
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   120
lemma less_Bits: "v BIT b < w BIT c \<longleftrightarrow> v < w \<or> v \<le> w \<and> \<not> b \<and> c"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   121
  by (auto simp: Bit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   122
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   123
lemma le_Bits: "v BIT b \<le> w BIT c \<longleftrightarrow> v < w \<or> v \<le> w \<and> (\<not> b \<or> c)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   124
  by (auto simp: Bit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   125
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   126
lemma pred_BIT_simps [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   127
  "x BIT False - 1 = (x - 1) BIT True"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   128
  "x BIT True - 1 = x BIT False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   129
  by (simp_all add: Bit_B0_2t Bit_B1_2t)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   130
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   131
lemma succ_BIT_simps [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   132
  "x BIT False + 1 = x BIT True"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   133
  "x BIT True + 1 = (x + 1) BIT False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   134
  by (simp_all add: Bit_B0_2t Bit_B1_2t)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   135
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   136
lemma add_BIT_simps [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   137
  "x BIT False + y BIT False = (x + y) BIT False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   138
  "x BIT False + y BIT True = (x + y) BIT True"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   139
  "x BIT True + y BIT False = (x + y) BIT True"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   140
  "x BIT True + y BIT True = (x + y + 1) BIT False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   141
  by (simp_all add: Bit_B0_2t Bit_B1_2t)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   142
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   143
lemma mult_BIT_simps [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   144
  "x BIT False * y = (x * y) BIT False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   145
  "x * y BIT False = (x * y) BIT False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   146
  "x BIT True * y = (x * y) BIT False + y"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   147
  by (simp_all add: Bit_B0_2t Bit_B1_2t algebra_simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   148
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   149
lemma B_mod_2': "X = 2 \<Longrightarrow> (w BIT True) mod X = 1 \<and> (w BIT False) mod X = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   150
  by (simp add: Bit_B0 Bit_B1)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   151
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   152
lemma bin_ex_rl: "\<exists>w b. w BIT b = bin"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   153
  by (metis bin_rl_simp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   154
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   155
lemma bin_exhaust: "(\<And>x b. bin = x BIT b \<Longrightarrow> Q) \<Longrightarrow> Q"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   156
by (metis bin_ex_rl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   157
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   158
lemma bin_abs_lem: "bin = (w BIT b) \<Longrightarrow> bin \<noteq> -1 \<longrightarrow> bin \<noteq> 0 \<longrightarrow> nat \<bar>w\<bar> < nat \<bar>bin\<bar>"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   159
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   160
  apply (unfold Bit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   161
  apply (cases b)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   162
   apply (clarsimp, arith)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   163
  apply (clarsimp, arith)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   164
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   165
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   166
lemma bin_induct:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   167
  assumes PPls: "P 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   168
    and PMin: "P (- 1)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   169
    and PBit: "\<And>bin bit. P bin \<Longrightarrow> P (bin BIT bit)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   170
  shows "P bin"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   171
  apply (rule_tac P=P and a=bin and f1="nat \<circ> abs" in wf_measure [THEN wf_induct])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   172
  apply (simp add: measure_def inv_image_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   173
  apply (case_tac x rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   174
  apply (frule bin_abs_lem)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   175
  apply (auto simp add : PPls PMin PBit)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   176
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   177
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   178
lemma Bit_div2 [simp]: "(w BIT b) div 2 = w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   179
  unfolding bin_rest_def [symmetric] by (rule bin_rest_BIT)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   180
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   181
lemma bin_rl_eqI: "\<lbrakk>bin_rest x = bin_rest y; bin_last x = bin_last y\<rbrakk> \<Longrightarrow> x = y"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   182
  by (metis (mono_tags) BIT_eq_iff bin_ex_rl bin_last_BIT bin_rest_BIT)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   183
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   184
lemma twice_conv_BIT: "2 * x = x BIT False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   185
  by (rule bin_rl_eqI) (simp_all, simp_all add: bin_rest_def bin_last_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   186
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   187
lemma BIT_lt0 [simp]: "x BIT b < 0 \<longleftrightarrow> x < 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   188
by(cases b)(auto simp add: Bit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   189
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   190
lemma BIT_ge0 [simp]: "x BIT b \<ge> 0 \<longleftrightarrow> x \<ge> 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   191
by(cases b)(auto simp add: Bit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   192
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   193
lemma [simp]: 
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   194
  shows bin_rest_lt0: "bin_rest i < 0 \<longleftrightarrow> i < 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   195
  and  bin_rest_ge_0: "bin_rest i \<ge> 0 \<longleftrightarrow> i \<ge> 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   196
by(auto simp add: bin_rest_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   197
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   198
lemma bin_rest_gt_0 [simp]: "bin_rest x > 0 \<longleftrightarrow> x > 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   199
by(simp add: bin_rest_def add1_zle_eq pos_imp_zdiv_pos_iff) (metis add1_zle_eq one_add_one)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   200
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   201
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   202
subsection \<open>Explicit bit representation of \<^typ>\<open>int\<close>\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   203
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   204
primrec bl_to_bin_aux :: "bool list \<Rightarrow> int \<Rightarrow> int"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   205
  where
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   206
    Nil: "bl_to_bin_aux [] w = w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   207
  | Cons: "bl_to_bin_aux (b # bs) w = bl_to_bin_aux bs (w BIT b)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   208
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   209
definition bl_to_bin :: "bool list \<Rightarrow> int"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   210
  where "bl_to_bin bs = bl_to_bin_aux bs 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   211
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   212
primrec bin_to_bl_aux :: "nat \<Rightarrow> int \<Rightarrow> bool list \<Rightarrow> bool list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   213
  where
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   214
    Z: "bin_to_bl_aux 0 w bl = bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   215
  | 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
   216
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   217
definition bin_to_bl :: "nat \<Rightarrow> int \<Rightarrow> bool list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   218
  where "bin_to_bl n w = bin_to_bl_aux n w []"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   219
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   220
lemma bin_to_bl_aux_zero_minus_simp [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   221
  "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
   222
  by (cases n) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   223
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   224
lemma bin_to_bl_aux_minus1_minus_simp [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   225
  "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
   226
  by (cases n) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   227
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   228
lemma bin_to_bl_aux_one_minus_simp [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   229
  "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
   230
  by (cases n) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   231
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   232
lemma bin_to_bl_aux_Bit_minus_simp [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   233
  "0 < n \<Longrightarrow> bin_to_bl_aux n (w BIT b) bl = bin_to_bl_aux (n - 1) w (b # bl)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   234
  by (cases n) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   235
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   236
lemma bin_to_bl_aux_Bit0_minus_simp [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   237
  "0 < n \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   238
    bin_to_bl_aux n (numeral (Num.Bit0 w)) bl = bin_to_bl_aux (n - 1) (numeral w) (False # bl)"
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
   239
  by (cases n)
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
   240
    (simp_all only: bin_to_bl_aux.simps bin_last_numeral_simps, simp)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   241
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   242
lemma bin_to_bl_aux_Bit1_minus_simp [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   243
  "0 < n \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   244
    bin_to_bl_aux n (numeral (Num.Bit1 w)) bl = bin_to_bl_aux (n - 1) (numeral w) (True # bl)"
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
   245
  by (cases n)
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
   246
    (simp_all only: bin_to_bl_aux.simps bin_last_numeral_simps, simp)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   247
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   248
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
   249
  by (induct bs arbitrary: w) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   250
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   251
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
   252
  by (induct n arbitrary: w bs) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   253
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   254
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
   255
  unfolding bl_to_bin_def by (rule bl_to_bin_aux_append)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   256
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   257
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
   258
  by (simp add: bin_to_bl_def bin_to_bl_aux_append)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   259
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   260
lemma bin_to_bl_0 [simp]: "bin_to_bl 0 bs = []"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   261
  by (auto simp: bin_to_bl_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   262
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   263
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
   264
  by (induct n arbitrary: w bs) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   265
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   266
lemma size_bin_to_bl [simp]: "length (bin_to_bl n w) = n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   267
  by (simp add: bin_to_bl_def size_bin_to_bl_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   268
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   269
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
   270
  apply (induct bs arbitrary: w n)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   271
   apply auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   272
    apply (simp_all only: add_Suc [symmetric])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   273
    apply (auto simp add: bin_to_bl_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   274
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   275
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   276
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
   277
  unfolding bl_to_bin_def
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   278
  apply (rule box_equals)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   279
    apply (rule bl_bin_bl')
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   280
   prefer 2
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   281
   apply (rule bin_to_bl_aux.Z)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   282
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   283
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   284
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   285
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
   286
  apply (rule_tac box_equals)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   287
    defer
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   288
    apply (rule bl_bin_bl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   289
   apply (rule bl_bin_bl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   290
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   291
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   292
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   293
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
   294
  by (auto simp: bl_to_bin_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   295
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   296
lemma bl_to_bin_Nil [simp]: "bl_to_bin [] = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   297
  by (auto simp: bl_to_bin_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   298
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   299
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
   300
  by (induct n arbitrary: bl) (auto simp: replicate_app_Cons_same)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   301
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   302
lemma bin_to_bl_zero: "bin_to_bl n 0 = replicate n False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   303
  by (simp add: bin_to_bl_def bin_to_bl_zero_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   304
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   305
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
   306
  by (induct n arbitrary: bl) (auto simp: replicate_app_Cons_same)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   307
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   308
lemma bin_to_bl_minus1: "bin_to_bl n (- 1) = replicate n True"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   309
  by (simp add: bin_to_bl_def bin_to_bl_minus1_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   310
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   311
lemma bl_to_bin_BIT:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   312
  "bl_to_bin bs BIT b = bl_to_bin (bs @ [b])"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   313
  by (simp add: bl_to_bin_append)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   314
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   315
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   316
subsection \<open>Bit projection\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   317
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   318
primrec bin_nth :: "int \<Rightarrow> nat \<Rightarrow> bool"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   319
  where
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   320
    Z: "bin_nth w 0 \<longleftrightarrow> bin_last w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   321
  | Suc: "bin_nth w (Suc n) \<longleftrightarrow> bin_nth (bin_rest w) n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   322
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   323
lemma bin_nth_eq_mod:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   324
  "bin_nth w n \<longleftrightarrow> odd (w div 2 ^ n)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   325
  by (induction n arbitrary: w) (simp_all add: bin_last_def bin_rest_def odd_iff_mod_2_eq_one zdiv_zmult2_eq)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   326
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   327
lemma bin_nth_eq_iff: "bin_nth x = bin_nth y \<longleftrightarrow> x = y"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   328
proof -
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   329
  have bin_nth_lem [rule_format]: "\<forall>y. bin_nth x = bin_nth y \<longrightarrow> x = y"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   330
    apply (induct x rule: bin_induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   331
      apply safe
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   332
      apply (erule rev_mp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   333
      apply (induct_tac y rule: bin_induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   334
        apply safe
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   335
        apply (drule_tac x=0 in fun_cong, force)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   336
       apply (erule notE, rule ext, drule_tac x="Suc x" in fun_cong, force)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   337
      apply (drule_tac x=0 in fun_cong, force)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   338
     apply (erule rev_mp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   339
     apply (induct_tac y rule: bin_induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   340
       apply safe
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   341
       apply (drule_tac x=0 in fun_cong, force)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   342
      apply (erule notE, rule ext, drule_tac x="Suc x" in fun_cong, force)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   343
     apply (metis Bit_eq_m1_iff Z bin_last_BIT)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   344
    apply (case_tac y rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   345
    apply clarify
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   346
    apply (erule allE)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   347
    apply (erule impE)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   348
     prefer 2
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   349
     apply (erule conjI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   350
     apply (drule_tac x=0 in fun_cong, force)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   351
    apply (rule ext)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   352
    apply (drule_tac x="Suc x" for x in fun_cong, force)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   353
    done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   354
  show ?thesis
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   355
    by (auto elim: bin_nth_lem)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   356
qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   357
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   358
lemma bin_eqI:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   359
  "x = y" if "\<And>n. bin_nth x n \<longleftrightarrow> bin_nth y n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   360
  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
   361
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   362
lemma bin_eq_iff: "x = y \<longleftrightarrow> (\<forall>n. bin_nth x n = bin_nth y n)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   363
  using bin_nth_eq_iff by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   364
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   365
lemma bin_nth_zero [simp]: "\<not> bin_nth 0 n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   366
  by (induct n) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   367
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   368
lemma bin_nth_1 [simp]: "bin_nth 1 n \<longleftrightarrow> n = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   369
  by (cases n) simp_all
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   370
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   371
lemma bin_nth_minus1 [simp]: "bin_nth (- 1) n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   372
  by (induct n) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   373
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   374
lemma bin_nth_0_BIT: "bin_nth (w BIT b) 0 \<longleftrightarrow> b"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   375
  by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   376
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   377
lemma bin_nth_Suc_BIT: "bin_nth (w BIT b) (Suc n) = bin_nth w n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   378
  by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   379
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   380
lemma bin_nth_minus [simp]: "0 < n \<Longrightarrow> bin_nth (w BIT b) n = bin_nth w (n - 1)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   381
  by (cases n) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   382
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   383
lemma bin_nth_numeral: "bin_rest x = y \<Longrightarrow> bin_nth x (numeral n) = bin_nth y (pred_numeral n)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   384
  by (simp add: numeral_eq_Suc)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   385
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   386
lemmas bin_nth_numeral_simps [simp] =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   387
  bin_nth_numeral [OF bin_rest_numeral_simps(2)]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   388
  bin_nth_numeral [OF bin_rest_numeral_simps(5)]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   389
  bin_nth_numeral [OF bin_rest_numeral_simps(6)]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   390
  bin_nth_numeral [OF bin_rest_numeral_simps(7)]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   391
  bin_nth_numeral [OF bin_rest_numeral_simps(8)]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   392
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   393
lemmas bin_nth_simps =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   394
  bin_nth.Z bin_nth.Suc bin_nth_zero bin_nth_minus1
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   395
  bin_nth_numeral_simps
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   396
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   397
lemma nth_2p_bin: "bin_nth (2 ^ n) m = (m = n)" \<comment> \<open>for use when simplifying with \<open>bin_nth_Bit\<close>\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   398
  apply (induct n arbitrary: m)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   399
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   400
   apply safe
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   401
   apply (case_tac m)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   402
    apply (auto simp: Bit_B0_2t [symmetric])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   403
  done 
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   404
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   405
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
   406
  apply (induct k arbitrary: n)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   407
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   408
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   409
  apply (simp only: bin_nth.Suc [symmetric] add_Suc)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   410
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   411
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   412
lemma bin_nth_numeral_unfold:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   413
  "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
   414
  "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
   415
  by (cases n; simp)+
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   416
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   417
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   418
subsection \<open>Truncating\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   419
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   420
definition bin_sign :: "int \<Rightarrow> int"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   421
  where "bin_sign k = (if k \<ge> 0 then 0 else - 1)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   422
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   423
lemma bin_sign_simps [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   424
  "bin_sign 0 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   425
  "bin_sign 1 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   426
  "bin_sign (- 1) = - 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   427
  "bin_sign (numeral k) = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   428
  "bin_sign (- numeral k) = -1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   429
  "bin_sign (w BIT b) = bin_sign w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   430
  by (simp_all add: bin_sign_def Bit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   431
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   432
lemma bin_sign_rest [simp]: "bin_sign (bin_rest w) = bin_sign w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   433
  by (cases w rule: bin_exhaust) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   434
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   435
primrec bintrunc :: "nat \<Rightarrow> int \<Rightarrow> int"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   436
  where
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   437
    Z : "bintrunc 0 bin = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   438
  | Suc : "bintrunc (Suc n) bin = bintrunc n (bin_rest bin) BIT (bin_last bin)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   439
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   440
primrec sbintrunc :: "nat \<Rightarrow> int \<Rightarrow> int"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   441
  where
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   442
    Z : "sbintrunc 0 bin = (if bin_last bin then -1 else 0)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   443
  | Suc : "sbintrunc (Suc n) bin = sbintrunc n (bin_rest bin) BIT (bin_last bin)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   444
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   445
lemma bintrunc_mod2p: "bintrunc n w = w mod 2 ^ n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   446
  by (induct n arbitrary: w) (auto simp add: bin_last_def bin_rest_def Bit_def zmod_zmult2_eq)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   447
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   448
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
   449
proof (induction n arbitrary: w)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   450
  case 0
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   451
  then show ?case
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
   452
    by (auto simp add: odd_iff_mod_2_eq_one)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   453
next
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   454
  case (Suc n)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   455
  moreover have "((bin_rest w + 2 ^ n) mod (2 * 2 ^ n) - 2 ^ n) BIT bin_last w =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   456
    (w + 2 * 2 ^ n) mod (4 * 2 ^ n) - 2 * 2 ^ n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   457
  proof (cases w rule: parity_cases)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   458
    case even
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   459
    then show ?thesis
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
   460
      by (simp add: bin_rest_def Bit_B0_2t mult_mod_right)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   461
  next
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   462
    case odd
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   463
    then have "2 * (w div 2) = w - 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   464
      using minus_mod_eq_mult_div [of w 2] by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   465
    moreover have "(2 * 2 ^ n + w - 1) mod (2 * 2 * 2 ^ n) + 1 = (2 * 2 ^ n + w) mod (2 * 2 * 2 ^ n)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   466
      using odd emep1 [of "2 * 2 ^ n + w - 1" "2 * 2 * 2 ^ n"] by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   467
    ultimately show ?thesis 
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
   468
      using odd by (simp add: bin_rest_def Bit_B1_2t mult_mod_right) (simp add: algebra_simps)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   469
  qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   470
  ultimately show ?case
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   471
    by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   472
qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   473
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   474
lemma sign_bintr: "bin_sign (bintrunc n w) = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   475
  by (simp add: bintrunc_mod2p bin_sign_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   476
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   477
lemma bintrunc_n_0 [simp]: "bintrunc n 0 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   478
  by (simp add: bintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   479
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   480
lemma sbintrunc_n_0 [simp]: "sbintrunc n 0 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   481
  by (simp add: sbintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   482
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   483
lemma sbintrunc_n_minus1 [simp]: "sbintrunc n (- 1) = -1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   484
  by (induct n) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   485
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   486
lemma bintrunc_Suc_numeral:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   487
  "bintrunc (Suc n) 1 = 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   488
  "bintrunc (Suc n) (- 1) = bintrunc n (- 1) BIT True"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   489
  "bintrunc (Suc n) (numeral (Num.Bit0 w)) = bintrunc n (numeral w) BIT False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   490
  "bintrunc (Suc n) (numeral (Num.Bit1 w)) = bintrunc n (numeral w) BIT True"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   491
  "bintrunc (Suc n) (- numeral (Num.Bit0 w)) = bintrunc n (- numeral w) BIT False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   492
  "bintrunc (Suc n) (- numeral (Num.Bit1 w)) = bintrunc n (- numeral (w + Num.One)) BIT True"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   493
  by simp_all
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   494
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   495
lemma sbintrunc_0_numeral [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   496
  "sbintrunc 0 1 = -1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   497
  "sbintrunc 0 (numeral (Num.Bit0 w)) = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   498
  "sbintrunc 0 (numeral (Num.Bit1 w)) = -1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   499
  "sbintrunc 0 (- numeral (Num.Bit0 w)) = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   500
  "sbintrunc 0 (- numeral (Num.Bit1 w)) = -1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   501
  by simp_all
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   502
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   503
lemma sbintrunc_Suc_numeral:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   504
  "sbintrunc (Suc n) 1 = 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   505
  "sbintrunc (Suc n) (numeral (Num.Bit0 w)) = sbintrunc n (numeral w) BIT False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   506
  "sbintrunc (Suc n) (numeral (Num.Bit1 w)) = sbintrunc n (numeral w) BIT True"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   507
  "sbintrunc (Suc n) (- numeral (Num.Bit0 w)) = sbintrunc n (- numeral w) BIT False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   508
  "sbintrunc (Suc n) (- numeral (Num.Bit1 w)) = sbintrunc n (- numeral (w + Num.One)) BIT True"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   509
  by simp_all
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   510
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   511
lemma bin_sign_lem: "(bin_sign (sbintrunc n bin) = -1) = bin_nth bin n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   512
  apply (induct n arbitrary: bin)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   513
  apply (case_tac bin rule: bin_exhaust, case_tac b, auto)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   514
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   515
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   516
lemma nth_bintr: "bin_nth (bintrunc m w) n \<longleftrightarrow> n < m \<and> bin_nth w n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   517
  apply (induct n arbitrary: w m)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   518
   apply (case_tac m, auto)[1]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   519
  apply (case_tac m, auto)[1]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   520
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   521
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   522
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
   523
  apply (induct n arbitrary: w m)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   524
   apply (case_tac m)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   525
    apply simp_all
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   526
  apply (case_tac m)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   527
   apply simp_all
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   528
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   529
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   530
lemma bin_nth_Bit: "bin_nth (w BIT b) n \<longleftrightarrow> n = 0 \<and> b \<or> (\<exists>m. n = Suc m \<and> bin_nth w m)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   531
  by (cases n) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   532
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   533
lemma bin_nth_Bit0:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   534
  "bin_nth (numeral (Num.Bit0 w)) n \<longleftrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   535
    (\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   536
  using bin_nth_Bit [where w="numeral w" and b="False"] by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   537
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   538
lemma bin_nth_Bit1:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   539
  "bin_nth (numeral (Num.Bit1 w)) n \<longleftrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   540
    n = 0 \<or> (\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   541
  using bin_nth_Bit [where w="numeral w" and b="True"] by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   542
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   543
lemma bintrunc_bintrunc_l: "n \<le> m \<Longrightarrow> bintrunc m (bintrunc n w) = bintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   544
  by (rule bin_eqI) (auto simp: nth_bintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   545
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   546
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
   547
  by (rule bin_eqI) (auto simp: nth_sbintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   548
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   549
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
   550
  by (rule bin_eqI) (auto simp: nth_bintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   551
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   552
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
   553
  by (rule bin_eqI) (auto simp: nth_bintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   554
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   555
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
   556
  by (rule bin_eqI) (auto simp: nth_sbintr min.absorb1 min.absorb2)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   557
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   558
lemmas bintrunc_Pls =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   559
  bintrunc.Suc [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   560
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   561
lemmas bintrunc_Min [simp] =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   562
  bintrunc.Suc [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   563
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   564
lemmas bintrunc_BIT  [simp] =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   565
  bintrunc.Suc [where bin="w BIT b", simplified bin_last_BIT bin_rest_BIT] for w b
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   566
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   567
lemmas bintrunc_Sucs = bintrunc_Pls bintrunc_Min bintrunc_BIT
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   568
  bintrunc_Suc_numeral
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   569
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   570
lemmas sbintrunc_Suc_Pls =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   571
  sbintrunc.Suc [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   572
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   573
lemmas sbintrunc_Suc_Min =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   574
  sbintrunc.Suc [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   575
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   576
lemmas sbintrunc_Suc_BIT [simp] =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   577
  sbintrunc.Suc [where bin="w BIT b", simplified bin_last_BIT bin_rest_BIT] for w b
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   578
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   579
lemmas sbintrunc_Sucs = sbintrunc_Suc_Pls sbintrunc_Suc_Min sbintrunc_Suc_BIT
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   580
  sbintrunc_Suc_numeral
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   581
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   582
lemmas sbintrunc_Pls =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   583
  sbintrunc.Z [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   584
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   585
lemmas sbintrunc_Min =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   586
  sbintrunc.Z [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   587
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   588
lemmas sbintrunc_0_BIT_B0 [simp] =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   589
  sbintrunc.Z [where bin="w BIT False", simplified bin_last_numeral_simps bin_rest_numeral_simps]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   590
  for w
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   591
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   592
lemmas sbintrunc_0_BIT_B1 [simp] =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   593
  sbintrunc.Z [where bin="w BIT True", simplified bin_last_BIT bin_rest_numeral_simps]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   594
  for w
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   595
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   596
lemmas sbintrunc_0_simps =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   597
  sbintrunc_Pls sbintrunc_Min sbintrunc_0_BIT_B0 sbintrunc_0_BIT_B1
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   598
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   599
lemmas bintrunc_simps = bintrunc.Z bintrunc_Sucs
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   600
lemmas sbintrunc_simps = sbintrunc_0_simps sbintrunc_Sucs
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   601
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   602
lemma bintrunc_minus: "0 < n \<Longrightarrow> bintrunc (Suc (n - 1)) w = bintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   603
  by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   604
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   605
lemma sbintrunc_minus: "0 < n \<Longrightarrow> sbintrunc (Suc (n - 1)) w = sbintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   606
  by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   607
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   608
lemmas bintrunc_minus_simps =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   609
  bintrunc_Sucs [THEN [2] bintrunc_minus [symmetric, THEN trans]]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   610
lemmas sbintrunc_minus_simps =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   611
  sbintrunc_Sucs [THEN [2] sbintrunc_minus [symmetric, THEN trans]]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   612
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   613
lemmas thobini1 = arg_cong [where f = "\<lambda>w. w BIT b"] for b
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   614
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   615
lemmas bintrunc_BIT_I = trans [OF bintrunc_BIT thobini1]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   616
lemmas bintrunc_Min_I = trans [OF bintrunc_Min thobini1]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   617
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   618
lemmas bmsts = bintrunc_minus_simps(1-3) [THEN thobini1 [THEN [2] trans]]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   619
lemmas bintrunc_Pls_minus_I = bmsts(1)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   620
lemmas bintrunc_Min_minus_I = bmsts(2)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   621
lemmas bintrunc_BIT_minus_I = bmsts(3)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   622
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   623
lemma bintrunc_Suc_lem: "bintrunc (Suc n) x = y \<Longrightarrow> m = Suc n \<Longrightarrow> bintrunc m x = y"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   624
  by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   625
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   626
lemmas bintrunc_Suc_Ialts =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   627
  bintrunc_Min_I [THEN bintrunc_Suc_lem]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   628
  bintrunc_BIT_I [THEN bintrunc_Suc_lem]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   629
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   630
lemmas sbintrunc_BIT_I = trans [OF sbintrunc_Suc_BIT thobini1]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   631
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   632
lemmas sbintrunc_Suc_Is =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   633
  sbintrunc_Sucs(1-3) [THEN thobini1 [THEN [2] trans]]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   634
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   635
lemmas sbintrunc_Suc_minus_Is =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   636
  sbintrunc_minus_simps(1-3) [THEN thobini1 [THEN [2] trans]]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   637
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   638
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
   639
  by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   640
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   641
lemmas sbintrunc_Suc_Ialts =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   642
  sbintrunc_Suc_Is [THEN sbintrunc_Suc_lem]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   643
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   644
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
   645
  by (rule bin_eqI) (auto simp: nth_sbintr nth_bintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   646
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   647
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
   648
  apply (rule bin_eqI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   649
  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
   650
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   651
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   652
lemmas bintrunc_sbintrunc [simp] = order_refl [THEN bintrunc_sbintrunc_le]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   653
lemmas sbintrunc_bintrunc [simp] = lessI [THEN sbintrunc_bintrunc_lt]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   654
lemmas bintrunc_bintrunc [simp] = order_refl [THEN bintrunc_bintrunc_l]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   655
lemmas sbintrunc_sbintrunc [simp] = order_refl [THEN sbintrunc_sbintrunc_l]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   656
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   657
lemma bintrunc_sbintrunc' [simp]: "0 < n \<Longrightarrow> bintrunc n (sbintrunc (n - 1) w) = bintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   658
  by (cases n) (auto simp del: bintrunc.Suc)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   659
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   660
lemma sbintrunc_bintrunc' [simp]: "0 < n \<Longrightarrow> sbintrunc (n - 1) (bintrunc n w) = sbintrunc (n - 1) w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   661
  by (cases n) (auto simp del: bintrunc.Suc)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   662
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   663
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
   664
  apply (rule iffI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   665
   apply (rule box_equals [OF _ sbintrunc_bintrunc sbintrunc_bintrunc])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   666
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   667
  apply (rule box_equals [OF _ bintrunc_sbintrunc bintrunc_sbintrunc])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   668
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   669
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   670
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   671
lemma bin_sbin_eq_iff':
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   672
  "0 < n \<Longrightarrow> bintrunc n x = bintrunc n y \<longleftrightarrow> sbintrunc (n - 1) x = sbintrunc (n - 1) y"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   673
  by (cases n) (simp_all add: bin_sbin_eq_iff del: bintrunc.Suc)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   674
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   675
lemmas bintrunc_sbintruncS0 [simp] = bintrunc_sbintrunc' [unfolded One_nat_def]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   676
lemmas sbintrunc_bintruncS0 [simp] = sbintrunc_bintrunc' [unfolded One_nat_def]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   677
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   678
lemmas bintrunc_bintrunc_l' = le_add1 [THEN bintrunc_bintrunc_l]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   679
lemmas sbintrunc_sbintrunc_l' = le_add1 [THEN sbintrunc_sbintrunc_l]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   680
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   681
(* although bintrunc_minus_simps, if added to default simpset,
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   682
  tends to get applied where it's not wanted in developing the theories,
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   683
  we get a version for when the word length is given literally *)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   684
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   685
lemmas nat_non0_gr =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   686
  trans [OF iszero_def [THEN Not_eq_iff [THEN iffD2]] refl]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   687
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   688
lemma bintrunc_numeral:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   689
  "bintrunc (numeral k) x = bintrunc (pred_numeral k) (bin_rest x) BIT bin_last x"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   690
  by (simp add: numeral_eq_Suc)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   691
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   692
lemma sbintrunc_numeral:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   693
  "sbintrunc (numeral k) x = sbintrunc (pred_numeral k) (bin_rest x) BIT bin_last x"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   694
  by (simp add: numeral_eq_Suc)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   695
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   696
lemma bintrunc_numeral_simps [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   697
  "bintrunc (numeral k) (numeral (Num.Bit0 w)) = bintrunc (pred_numeral k) (numeral w) BIT False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   698
  "bintrunc (numeral k) (numeral (Num.Bit1 w)) = bintrunc (pred_numeral k) (numeral w) BIT True"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   699
  "bintrunc (numeral k) (- numeral (Num.Bit0 w)) = bintrunc (pred_numeral k) (- numeral w) BIT False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   700
  "bintrunc (numeral k) (- numeral (Num.Bit1 w)) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   701
    bintrunc (pred_numeral k) (- numeral (w + Num.One)) BIT True"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   702
  "bintrunc (numeral k) 1 = 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   703
  by (simp_all add: bintrunc_numeral)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   704
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   705
lemma sbintrunc_numeral_simps [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   706
  "sbintrunc (numeral k) (numeral (Num.Bit0 w)) = sbintrunc (pred_numeral k) (numeral w) BIT False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   707
  "sbintrunc (numeral k) (numeral (Num.Bit1 w)) = sbintrunc (pred_numeral k) (numeral w) BIT True"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   708
  "sbintrunc (numeral k) (- numeral (Num.Bit0 w)) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   709
    sbintrunc (pred_numeral k) (- numeral w) BIT False"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   710
  "sbintrunc (numeral k) (- numeral (Num.Bit1 w)) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   711
    sbintrunc (pred_numeral k) (- numeral (w + Num.One)) BIT True"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   712
  "sbintrunc (numeral k) 1 = 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   713
  by (simp_all add: sbintrunc_numeral)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   714
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   715
lemma no_bintr_alt1: "bintrunc n = (\<lambda>w. w mod 2 ^ n :: int)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   716
  by (rule ext) (rule bintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   717
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   718
lemma range_bintrunc: "range (bintrunc n) = {i. 0 \<le> i \<and> i < 2 ^ n}"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   719
  apply (unfold no_bintr_alt1)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   720
  apply (auto simp add: image_iff)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   721
  apply (rule exI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   722
  apply (rule sym)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   723
  using int_mod_lem [symmetric, of "2 ^ n"]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   724
  apply auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   725
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   726
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   727
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
   728
  by (rule ext) (simp add : sbintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   729
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   730
lemma range_sbintrunc: "range (sbintrunc n) = {i. - (2 ^ n) \<le> i \<and> i < 2 ^ n}"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   731
  apply (unfold no_sbintr_alt2)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   732
  apply (auto simp add: image_iff eq_diff_eq)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   733
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   734
  apply (rule exI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   735
  apply (auto intro: int_mod_lem [THEN iffD1, symmetric])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   736
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   737
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   738
lemma sb_inc_lem: "a + 2^k < 0 \<Longrightarrow> a + 2^k + 2^(Suc k) \<le> (a + 2^k) mod 2^(Suc k)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   739
  for a :: int
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   740
  using int_mod_ge' [where n = "2 ^ (Suc k)" and b = "a + 2 ^ k"]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   741
  by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   742
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   743
lemma sb_inc_lem': "a < - (2^k) \<Longrightarrow> a + 2^k + 2^(Suc k) \<le> (a + 2^k) mod 2^(Suc k)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   744
  for a :: int
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   745
  by (rule sb_inc_lem) simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   746
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   747
lemma sbintrunc_inc: "x < - (2^n) \<Longrightarrow> x + 2^(Suc n) \<le> sbintrunc n x"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   748
  unfolding no_sbintr_alt2 by (drule sb_inc_lem') simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   749
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   750
lemma sb_dec_lem: "0 \<le> - (2 ^ k) + a \<Longrightarrow> (a + 2 ^ k) mod (2 * 2 ^ k) \<le> - (2 ^ k) + a"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   751
  for a :: int
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   752
  using int_mod_le'[where n = "2 ^ (Suc k)" and b = "a + 2 ^ k"] by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   753
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   754
lemma sb_dec_lem': "2 ^ k \<le> a \<Longrightarrow> (a + 2 ^ k) mod (2 * 2 ^ k) \<le> - (2 ^ k) + a"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   755
  for a :: int
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   756
  by (rule sb_dec_lem) simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   757
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   758
lemma sbintrunc_dec: "x \<ge> (2 ^ n) \<Longrightarrow> x - 2 ^ (Suc n) >= sbintrunc n x"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   759
  unfolding no_sbintr_alt2 by (drule sb_dec_lem') simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   760
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   761
lemma bintr_ge0: "0 \<le> bintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   762
  by (simp add: bintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   763
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   764
lemma bintr_lt2p: "bintrunc n w < 2 ^ n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   765
  by (simp add: bintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   766
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   767
lemma bintr_Min: "bintrunc n (- 1) = 2 ^ n - 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   768
  by (simp add: bintrunc_mod2p m1mod2k)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   769
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   770
lemma sbintr_ge: "- (2 ^ n) \<le> sbintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   771
  by (simp add: sbintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   772
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   773
lemma sbintr_lt: "sbintrunc n w < 2 ^ n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   774
  by (simp add: sbintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   775
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   776
lemma sign_Pls_ge_0: "bin_sign bin = 0 \<longleftrightarrow> bin \<ge> 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   777
  for bin :: int
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   778
  by (simp add: bin_sign_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   779
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   780
lemma sign_Min_lt_0: "bin_sign bin = -1 \<longleftrightarrow> bin < 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   781
  for bin :: int
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   782
  by (simp add: bin_sign_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   783
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   784
lemma bin_rest_trunc: "bin_rest (bintrunc n bin) = bintrunc (n - 1) (bin_rest bin)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   785
  by (induct n arbitrary: bin) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   786
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   787
lemma bin_rest_power_trunc:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   788
  "(bin_rest ^^ k) (bintrunc n bin) = bintrunc (n - k) ((bin_rest ^^ k) bin)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   789
  by (induct k) (auto simp: bin_rest_trunc)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   790
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   791
lemma bin_rest_trunc_i: "bintrunc n (bin_rest bin) = bin_rest (bintrunc (Suc n) bin)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   792
  by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   793
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   794
lemma bin_rest_strunc: "bin_rest (sbintrunc (Suc n) bin) = sbintrunc n (bin_rest bin)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   795
  by (induct n arbitrary: bin) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   796
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   797
lemma bintrunc_rest [simp]: "bintrunc n (bin_rest (bintrunc n bin)) = bin_rest (bintrunc n bin)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   798
  apply (induct n arbitrary: bin)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   799
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   800
  apply (case_tac bin rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   801
  apply (auto simp: bintrunc_bintrunc_l)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   802
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   803
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   804
lemma sbintrunc_rest [simp]: "sbintrunc n (bin_rest (sbintrunc n bin)) = bin_rest (sbintrunc n bin)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   805
  apply (induct n arbitrary: bin)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   806
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   807
  apply (case_tac bin rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   808
  apply (auto simp: bintrunc_bintrunc_l split: bool.splits)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   809
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   810
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   811
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
   812
  by (rule ext) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   813
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   814
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
   815
  by (rule ext) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   816
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   817
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
   818
  apply (rule ext)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   819
  apply (induct_tac n)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   820
   apply (simp_all (no_asm))
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   821
  apply (drule fun_cong)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   822
  apply (unfold o_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   823
  apply (erule trans)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   824
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   825
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   826
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   827
lemmas rco_bintr = bintrunc_rest'
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   828
  [THEN rco_lem [THEN fun_cong], unfolded o_def]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   829
lemmas rco_sbintr = sbintrunc_rest'
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   830
  [THEN rco_lem [THEN fun_cong], unfolded o_def]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   831
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   832
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   833
subsection \<open>Splitting and concatenation\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   834
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   835
primrec bin_split :: "nat \<Rightarrow> int \<Rightarrow> int \<times> int"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   836
  where
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   837
    Z: "bin_split 0 w = (w, 0)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   838
  | Suc: "bin_split (Suc n) w =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   839
      (let (w1, w2) = bin_split n (bin_rest w)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   840
       in (w1, w2 BIT bin_last w))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   841
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   842
lemma [code]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   843
  "bin_split (Suc n) w = (let (w1, w2) = bin_split n (bin_rest w) in (w1, w2 BIT bin_last w))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   844
  "bin_split 0 w = (w, 0)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   845
  by simp_all
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   846
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   847
primrec bin_cat :: "int \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   848
  where
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   849
    Z: "bin_cat w 0 v = w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   850
  | Suc: "bin_cat w (Suc n) v = bin_cat w n (bin_rest v) BIT bin_last v"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   851
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   852
lemma bin_sign_cat: "bin_sign (bin_cat x n y) = bin_sign x"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   853
  by (induct n arbitrary: y) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   854
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   855
lemma bin_cat_Suc_Bit: "bin_cat w (Suc n) (v BIT b) = bin_cat w n v BIT b"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   856
  by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   857
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   858
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
   859
  by (induct n arbitrary: z) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   860
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   861
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
   862
  apply (induct n arbitrary: z m)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   863
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   864
  apply (case_tac m, auto)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   865
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   866
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   867
definition bin_rcat :: "nat \<Rightarrow> int list \<Rightarrow> int"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   868
  where "bin_rcat n = foldl (\<lambda>u v. bin_cat u n v) 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   869
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   870
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
   871
  where "bin_rsplit_aux n m c bs =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   872
    (if m = 0 \<or> n = 0 then bs
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   873
     else
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   874
      let (a, b) = bin_split n c
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   875
      in bin_rsplit_aux n (m - n) a (b # bs))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   876
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   877
definition bin_rsplit :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   878
  where "bin_rsplit n w = bin_rsplit_aux n (fst w) (snd w) []"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   879
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   880
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
   881
  where "bin_rsplitl_aux n m c bs =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   882
    (if m = 0 \<or> n = 0 then bs
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   883
     else
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   884
      let (a, b) = bin_split (min m n) c
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   885
      in bin_rsplitl_aux n (m - n) a (b # bs))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   886
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   887
definition bin_rsplitl :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   888
  where "bin_rsplitl n w = bin_rsplitl_aux n (fst w) (snd w) []"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   889
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   890
declare bin_rsplit_aux.simps [simp del]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   891
declare bin_rsplitl_aux.simps [simp del]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   892
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   893
lemma bin_nth_cat:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   894
  "bin_nth (bin_cat x k y) n =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   895
    (if n < k then bin_nth y n else bin_nth x (n - k))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   896
  apply (induct k arbitrary: n y)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   897
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   898
  apply (case_tac n, auto)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   899
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   900
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   901
lemma bin_nth_split:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   902
  "bin_split n c = (a, b) \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   903
    (\<forall>k. bin_nth a k = bin_nth c (n + k)) \<and>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   904
    (\<forall>k. bin_nth b k = (k < n \<and> bin_nth c k))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   905
  apply (induct n arbitrary: b c)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   906
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   907
  apply (clarsimp simp: Let_def split: prod.split_asm)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   908
  apply (case_tac k)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   909
  apply auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   910
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   911
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   912
lemma bin_cat_zero [simp]: "bin_cat 0 n w = bintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   913
  by (induct n arbitrary: w) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   914
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   915
lemma bintr_cat1: "bintrunc (k + n) (bin_cat a n b) = bin_cat (bintrunc k a) n b"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   916
  by (induct n arbitrary: b) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   917
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   918
lemma bintr_cat: "bintrunc m (bin_cat a n b) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   919
    bin_cat (bintrunc (m - n) a) n (bintrunc (min m n) b)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   920
  by (rule bin_eqI) (auto simp: bin_nth_cat nth_bintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   921
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   922
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
   923
  by (auto simp add : bintr_cat)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   924
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   925
lemma cat_bintr [simp]: "bin_cat a n (bintrunc n b) = bin_cat a n b"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   926
  by (induct n arbitrary: b) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   927
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   928
lemma split_bintrunc: "bin_split n c = (a, b) \<Longrightarrow> b = bintrunc n c"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   929
  by (induct n arbitrary: b c) (auto simp: Let_def split: prod.split_asm)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   930
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   931
lemma bin_cat_split: "bin_split n w = (u, v) \<Longrightarrow> w = bin_cat u n v"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   932
  by (induct n arbitrary: v w) (auto simp: Let_def split: prod.split_asm)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   933
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   934
lemma bin_split_cat: "bin_split n (bin_cat v n w) = (v, bintrunc n w)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   935
  by (induct n arbitrary: w) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   936
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   937
lemma bin_split_zero [simp]: "bin_split n 0 = (0, 0)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   938
  by (induct n) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   939
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   940
lemma bin_split_minus1 [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   941
  "bin_split n (- 1) = (- 1, bintrunc n (- 1))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   942
  by (induct n) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   943
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   944
lemma bin_split_trunc:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   945
  "bin_split (min m n) c = (a, b) \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   946
    bin_split n (bintrunc m c) = (bintrunc (m - n) a, b)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   947
  apply (induct n arbitrary: m b c, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   948
  apply (simp add: bin_rest_trunc Let_def split: prod.split_asm)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   949
  apply (case_tac m)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   950
   apply (auto simp: Let_def split: prod.split_asm)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   951
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   952
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   953
lemma bin_split_trunc1:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   954
  "bin_split n c = (a, b) \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   955
    bin_split n (bintrunc m c) = (bintrunc (m - n) a, bintrunc m b)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   956
  apply (induct n arbitrary: m b c, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   957
  apply (simp add: bin_rest_trunc Let_def split: prod.split_asm)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   958
  apply (case_tac m)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   959
   apply (auto simp: Let_def split: prod.split_asm)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   960
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   961
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   962
lemma bin_cat_num: "bin_cat a n b = a * 2 ^ n + bintrunc n b"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   963
  apply (induct n arbitrary: b)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   964
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   965
  apply (simp add: Bit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   966
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   967
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   968
lemma bin_split_num: "bin_split n b = (b div 2 ^ n, b mod 2 ^ n)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   969
  apply (induct n arbitrary: b)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   970
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   971
  apply (simp add: bin_rest_def zdiv_zmult2_eq)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   972
  apply (case_tac b rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   973
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   974
  apply (simp add: Bit_def mod_mult_mult1 pos_zmod_mult_2 add.commute)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   975
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   976
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   977
lemmas bin_rsplit_aux_simps = bin_rsplit_aux.simps bin_rsplitl_aux.simps
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   978
lemmas rsplit_aux_simps = bin_rsplit_aux_simps
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   979
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   980
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
   981
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
   982
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   983
lemmas rsplit_aux_simp1s = rsplit_aux_simps [THEN th_if_simp1]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   984
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   985
lemmas rsplit_aux_simp2ls = rsplit_aux_simps [THEN th_if_simp2]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   986
\<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
   987
lemmas bin_rsplit_aux_simp2s [simp] = rsplit_aux_simp2ls [unfolded Let_def]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   988
lemmas rbscl = bin_rsplit_aux_simp2s (2)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   989
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   990
lemmas rsplit_aux_0_simps [simp] =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   991
  rsplit_aux_simp1s [OF disjI1] rsplit_aux_simp1s [OF disjI2]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   992
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   993
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
   994
  apply (induct n m c bs rule: bin_rsplit_aux.induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   995
  apply (subst bin_rsplit_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   996
  apply (subst bin_rsplit_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   997
  apply (clarsimp split: prod.split)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   998
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   999
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1000
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
  1001
  apply (induct n m c bs rule: bin_rsplitl_aux.induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1002
  apply (subst bin_rsplitl_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1003
  apply (subst bin_rsplitl_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1004
  apply (clarsimp split: prod.split)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1005
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1006
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1007
lemmas rsplit_aux_apps [where bs = "[]"] =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1008
  bin_rsplit_aux_append bin_rsplitl_aux_append
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1009
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1010
lemmas rsplit_def_auxs = bin_rsplit_def bin_rsplitl_def
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1011
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1012
lemmas rsplit_aux_alts = rsplit_aux_apps
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1013
  [unfolded append_Nil rsplit_def_auxs [symmetric]]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1014
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1015
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
  1016
  by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1017
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1018
lemmas bin_split_minus_simp =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1019
  bin_split.Suc [THEN [2] bin_split_minus [symmetric, THEN trans]]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1020
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1021
lemma bin_split_pred_simp [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1022
  "(0::nat) < numeral bin \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1023
    bin_split (numeral bin) w =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1024
      (let (w1, w2) = bin_split (numeral bin - 1) (bin_rest w)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1025
       in (w1, w2 BIT bin_last w))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1026
  by (simp only: bin_split_minus_simp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1027
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1028
lemma bin_rsplit_aux_simp_alt:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1029
  "bin_rsplit_aux n m c bs =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1030
    (if m = 0 \<or> n = 0 then bs
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1031
     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
  1032
  apply (simp add: bin_rsplit_aux.simps [of n m c bs])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1033
  apply (subst rsplit_aux_alts)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1034
  apply (simp add: bin_rsplit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1035
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1036
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1037
lemmas bin_rsplit_simp_alt =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1038
  trans [OF bin_rsplit_def bin_rsplit_aux_simp_alt]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1039
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1040
lemmas bthrs = bin_rsplit_simp_alt [THEN [2] trans]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1041
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1042
lemma bin_rsplit_size_sign' [rule_format]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1043
  "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
  1044
  apply (induct sw arbitrary: nw w)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1045
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1046
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1047
  apply (drule bthrs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1048
  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
  1049
  apply clarify
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1050
  apply (drule split_bintrunc)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1051
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1052
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1053
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1054
lemmas bin_rsplit_size_sign = bin_rsplit_size_sign' [OF asm_rl
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1055
  rev_rev_ident [THEN trans] set_rev [THEN equalityD2 [THEN subsetD]]]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1056
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1057
lemma bin_nth_rsplit [rule_format] :
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1058
  "n > 0 \<Longrightarrow> m < n \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1059
    \<forall>w k nw.
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1060
      rev sw = bin_rsplit n (nw, w) \<longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1061
      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
  1062
  apply (induct sw)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1063
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1064
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1065
  apply (drule bthrs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1066
  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
  1067
  apply clarify
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1068
  apply (erule allE, erule impE, erule exI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1069
  apply (case_tac k)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1070
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1071
   prefer 2
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1072
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1073
   apply (erule allE)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1074
   apply (erule (1) impE)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1075
   apply (drule bin_nth_split, erule conjE, erule allE, erule trans, simp add: ac_simps)+
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1076
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1077
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1078
lemma bin_rsplit_all: "0 < nw \<Longrightarrow> nw \<le> n \<Longrightarrow> bin_rsplit n (nw, w) = [bintrunc n w]"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1079
  by (auto simp: bin_rsplit_def rsplit_aux_simp2ls split: prod.split dest!: split_bintrunc)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1080
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1081
lemma bin_rsplit_l [rule_format]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1082
  "\<forall>bin. bin_rsplitl n (m, bin) = bin_rsplit n (m, bintrunc m bin)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1083
  apply (rule_tac a = "m" in wf_less_than [THEN wf_induct])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1084
  apply (simp (no_asm) add: bin_rsplitl_def bin_rsplit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1085
  apply (rule allI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1086
  apply (subst bin_rsplitl_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1087
  apply (subst bin_rsplit_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1088
  apply (clarsimp simp: Let_def split: prod.split)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1089
  apply (drule bin_split_trunc)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1090
  apply (drule sym [THEN trans], assumption)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1091
  apply (subst rsplit_aux_alts(1))
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1092
  apply (subst rsplit_aux_alts(2))
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1093
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1094
  unfolding bin_rsplit_def bin_rsplitl_def
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1095
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1096
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1097
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1098
lemma bin_rsplit_rcat [rule_format]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1099
  "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
  1100
  apply (unfold bin_rsplit_def bin_rcat_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1101
  apply (rule_tac xs = ws in rev_induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1102
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1103
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1104
  apply (subst rsplit_aux_alts)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1105
  unfolding bin_split_cat
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1106
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1107
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1108
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1109
lemma bin_rsplit_aux_len_le [rule_format] :
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1110
  "\<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
  1111
    length ws \<le> m \<longleftrightarrow> nw + length bs * n \<le> m * n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1112
proof -
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1113
  have *: R
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1114
    if d: "i \<le> j \<or> m < j'"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1115
    and R1: "i * k \<le> j * k \<Longrightarrow> R"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1116
    and R2: "Suc m * k' \<le> j' * k' \<Longrightarrow> R"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1117
    for i j j' k k' m :: nat and R
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1118
    using d
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1119
    apply safe
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1120
    apply (rule R1, erule mult_le_mono1)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1121
    apply (rule R2, erule Suc_le_eq [THEN iffD2 [THEN mult_le_mono1]])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1122
    done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1123
  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
  1124
    for sc m n lb :: nat
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1125
    apply safe
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1126
     apply arith
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1127
    apply (case_tac "sc \<ge> n")
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1128
     apply arith
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1129
    apply (insert linorder_le_less_linear [of m lb])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1130
    apply (erule_tac k=n and k'=n in *)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1131
     apply arith
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1132
    apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1133
    done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1134
  show ?thesis
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1135
    apply (induct n nw w bs rule: bin_rsplit_aux.induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1136
    apply (subst bin_rsplit_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1137
    apply (simp add: ** Let_def split: prod.split)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1138
    done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1139
qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1140
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1141
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
  1142
  by (auto simp: bin_rsplit_def bin_rsplit_aux_len_le)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1143
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1144
lemma bin_rsplit_aux_len:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1145
  "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
  1146
  apply (induct n nw w cs rule: bin_rsplit_aux.induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1147
  apply (subst bin_rsplit_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1148
  apply (clarsimp simp: Let_def split: prod.split)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1149
  apply (erule thin_rl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1150
  apply (case_tac m)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1151
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1152
  apply (case_tac "m \<le> n")
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1153
   apply (auto simp add: div_add_self2)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1154
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1155
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1156
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
  1157
  by (auto simp: bin_rsplit_def bin_rsplit_aux_len)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1158
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1159
lemma bin_rsplit_aux_len_indep:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1160
  "n \<noteq> 0 \<Longrightarrow> length bs = length cs \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1161
    length (bin_rsplit_aux n nw v bs) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1162
    length (bin_rsplit_aux n nw w cs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1163
proof (induct n nw w cs arbitrary: v bs rule: bin_rsplit_aux.induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1164
  case (1 n m w cs v bs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1165
  show ?case
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1166
  proof (cases "m = 0")
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1167
    case True
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1168
    with \<open>length bs = length cs\<close> show ?thesis by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1169
  next
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1170
    case False
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1171
    from "1.hyps" \<open>m \<noteq> 0\<close> \<open>n \<noteq> 0\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1172
    have hyp: "\<And>v bs. length bs = Suc (length cs) \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1173
      length (bin_rsplit_aux n (m - n) v bs) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1174
      length (bin_rsplit_aux n (m - n) (fst (bin_split n w)) (snd (bin_split n w) # cs))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1175
      by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1176
    from \<open>length bs = length cs\<close> \<open>n \<noteq> 0\<close> show ?thesis
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1177
      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
  1178
  qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1179
qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1180
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1181
lemma bin_rsplit_len_indep:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1182
  "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
  1183
  apply (unfold bin_rsplit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1184
  apply (simp (no_asm))
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1185
  apply (erule bin_rsplit_aux_len_indep)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1186
  apply (rule refl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1187
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1188
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1189
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1190
subsection \<open>Logical operations\<close>
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
  1191
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1192
primrec bin_sc :: "nat \<Rightarrow> bool \<Rightarrow> int \<Rightarrow> int"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1193
  where
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1194
    Z: "bin_sc 0 b w = bin_rest w BIT b"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1195
  | Suc: "bin_sc (Suc n) b w = bin_sc n b (bin_rest w) BIT bin_last w"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1196
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1197
lemma bin_nth_sc [simp]: "bin_nth (bin_sc n b w) n \<longleftrightarrow> b"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1198
  by (induct n arbitrary: w) auto
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1199
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1200
lemma bin_sc_sc_same [simp]: "bin_sc n c (bin_sc n b w) = bin_sc n c w"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1201
  by (induct n arbitrary: w) auto
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1202
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1203
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
  1204
  apply (induct n arbitrary: w m)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1205
   apply (case_tac [!] m)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1206
     apply auto
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1207
  done
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1208
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1209
lemma bin_nth_sc_gen: "bin_nth (bin_sc n b w) m = (if m = n then b else bin_nth w m)"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1210
  by (induct n arbitrary: w m) (case_tac [!] m, auto)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1211
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1212
lemma bin_sc_nth [simp]: "bin_sc n (bin_nth w n) w = w"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1213
  by (induct n arbitrary: w) auto
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1214
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1215
lemma bin_sign_sc [simp]: "bin_sign (bin_sc n b w) = bin_sign w"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1216
  by (induct n arbitrary: w) auto
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1217
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1218
lemma bin_sc_bintr [simp]: "bintrunc m (bin_sc n x (bintrunc m (w))) = bintrunc m (bin_sc n x w)"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1219
  apply (induct n arbitrary: w m)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1220
   apply (case_tac [!] w rule: bin_exhaust)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1221
   apply (case_tac [!] m, auto)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1222
  done
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1223
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1224
lemma bin_clr_le: "bin_sc n False w \<le> w"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1225
  apply (induct n arbitrary: w)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1226
   apply (case_tac [!] w rule: bin_exhaust)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1227
   apply (auto simp: le_Bits)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1228
  done
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1229
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1230
lemma bin_set_ge: "bin_sc n True w \<ge> w"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1231
  apply (induct n arbitrary: w)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1232
   apply (case_tac [!] w rule: bin_exhaust)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1233
   apply (auto simp: le_Bits)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1234
  done
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1235
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1236
lemma bintr_bin_clr_le: "bintrunc n (bin_sc m False w) \<le> bintrunc n w"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1237
  apply (induct n arbitrary: w m)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1238
   apply simp
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1239
  apply (case_tac w rule: bin_exhaust)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1240
  apply (case_tac m)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1241
   apply (auto simp: le_Bits)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1242
  done
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1243
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1244
lemma bintr_bin_set_ge: "bintrunc n (bin_sc m True w) \<ge> bintrunc n w"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1245
  apply (induct n arbitrary: w m)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1246
   apply simp
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1247
  apply (case_tac w rule: bin_exhaust)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1248
  apply (case_tac m)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1249
   apply (auto simp: le_Bits)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1250
  done
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1251
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1252
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
  1253
  by (induct n) auto
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1254
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1255
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
  1256
  by (induct n) auto
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1257
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1258
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
  1259
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1260
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
  1261
  by auto
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1262
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1263
lemmas bin_sc_Suc_minus =
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1264
  trans [OF bin_sc_minus [symmetric] bin_sc.Suc]
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1265
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1266
lemma bin_sc_numeral [simp]:
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1267
  "bin_sc (numeral k) b w =
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1268
    bin_sc (pred_numeral k) b (bin_rest w) BIT bin_last w"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1269
  by (simp add: numeral_eq_Suc)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1270
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1271
instantiation int :: bit_operations
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
  1272
begin
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
  1273
71826
f424e164d752 modernized notation for bit operations
haftmann
parents: 71756
diff changeset
  1274
definition int_not_def: "NOT = (\<lambda>x::int. - x - 1)"
46019
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
  1275
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1276
function bitAND_int
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1277
  where "bitAND_int x y =
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1278
    (if x = 0 then 0 else if x = -1 then y
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1279
     else (bin_rest x AND bin_rest y) BIT (bin_last x \<and> bin_last y))"
46019
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
  1280
  by pat_completeness simp
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
  1281
46019
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
  1282
termination
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1283
  by (relation "measure (nat \<circ> abs \<circ> fst)", simp_all add: bin_rest_def)
46019
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
  1284
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
  1285
declare bitAND_int.simps [simp del]
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
  1286
71826
f424e164d752 modernized notation for bit operations
haftmann
parents: 71756
diff changeset
  1287
definition int_or_def: "(OR) = (\<lambda>x y::int. NOT (NOT x AND NOT y))"
f424e164d752 modernized notation for bit operations
haftmann
parents: 71756
diff changeset
  1288
f424e164d752 modernized notation for bit operations
haftmann
parents: 71756
diff changeset
  1289
definition int_xor_def: "(XOR) = (\<lambda>x y::int. (x AND NOT y) OR (NOT x AND y))"
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
  1290
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1291
definition [iff]: "i !! n \<longleftrightarrow> bin_nth i n"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1292
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1293
definition "lsb i = i !! 0" for i :: int
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1294
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1295
definition "set_bit i n b = bin_sc n b i"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1296
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1297
definition "shiftl x n = x * 2 ^ n" for x :: int
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1298
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1299
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
  1300
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1301
definition "msb x \<longleftrightarrow> x < 0" for x :: int
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1302
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
  1303
instance ..
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
  1304
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
  1305
end
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
  1306
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1307
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1308
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
  1309
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1310
lemma int_not_BIT [simp]: "NOT (w BIT b) = (NOT w) BIT (\<not> b)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1311
  by (cases b) (simp_all add: int_not_def Bit_def)
46016
c42e43287b5f simplify definition of NOT for type int
huffman
parents: 46001
diff changeset
  1312
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1313
lemma int_not_simps [simp]:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1314
  "NOT (0::int) = -1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1315
  "NOT (1::int) = -2"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1316
  "NOT (- 1::int) = 0"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1317
  "NOT (numeral w::int) = - numeral (w + Num.One)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1318
  "NOT (- numeral (Num.Bit0 w)::int) = numeral (Num.BitM w)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1319
  "NOT (- numeral (Num.Bit1 w)::int) = numeral (Num.Bit0 w)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1320
  unfolding int_not_def by simp_all
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1321
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1322
lemma int_not_not [simp]: "NOT (NOT x) = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1323
  for x :: int
46017
c5a1002161c3 simplify definition of OR for type int;
huffman
parents: 46016
diff changeset
  1324
  unfolding int_not_def by simp
c5a1002161c3 simplify definition of OR for type int;
huffman
parents: 46016
diff changeset
  1325
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1326
lemma int_and_0 [simp]: "0 AND x = 0"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1327
  for x :: int
46019
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
  1328
  by (simp add: bitAND_int.simps)
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
  1329
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1330
lemma int_and_m1 [simp]: "-1 AND x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1331
  for x :: int
46019
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
  1332
  by (simp add: bitAND_int.simps)
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
  1333
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1334
lemma int_and_Bits [simp]: "(x BIT b) AND (y BIT c) = (x AND y) BIT (b \<and> c)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1335
  by (subst bitAND_int.simps) (simp add: Bit_eq_0_iff Bit_eq_m1_iff)
46017
c5a1002161c3 simplify definition of OR for type int;
huffman
parents: 46016
diff changeset
  1336
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1337
lemma int_or_zero [simp]: "0 OR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1338
  for x :: int
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1339
  by (simp add: int_or_def)
46018
0bb66de5a0bf simplify definition of XOR for type int;
huffman
parents: 46017
diff changeset
  1340
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1341
lemma int_or_minus1 [simp]: "-1 OR x = -1"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1342
  for x :: int
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1343
  by (simp add: int_or_def)
46017
c5a1002161c3 simplify definition of OR for type int;
huffman
parents: 46016
diff changeset
  1344
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1345
lemma int_or_Bits [simp]: "(x BIT b) OR (y BIT c) = (x OR y) BIT (b \<or> c)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1346
  by (simp add: int_or_def)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1347
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1348
lemma int_xor_zero [simp]: "0 XOR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1349
  for x :: int
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1350
  by (simp add: int_xor_def)
46018
0bb66de5a0bf simplify definition of XOR for type int;
huffman
parents: 46017
diff changeset
  1351
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1352
lemma int_xor_Bits [simp]: "(x BIT b) XOR (y BIT c) = (x XOR y) BIT ((b \<or> c) \<and> \<not> (b \<and> c))"
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1353
  unfolding int_xor_def by auto
46018
0bb66de5a0bf simplify definition of XOR for type int;
huffman
parents: 46017
diff changeset
  1354
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1355
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1356
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
  1357
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1358
lemma bin_rest_NOT [simp]: "bin_rest (NOT x) = NOT (bin_rest x)"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1359
  by (cases x rule: bin_exhaust) simp
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1360
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1361
lemma bin_last_NOT [simp]: "bin_last (NOT x) \<longleftrightarrow> \<not> bin_last x"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1362
  by (cases x rule: bin_exhaust) simp
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1363
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1364
lemma bin_rest_AND [simp]: "bin_rest (x AND y) = bin_rest x AND bin_rest y"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1365
  by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1366
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1367
lemma bin_last_AND [simp]: "bin_last (x AND y) \<longleftrightarrow> bin_last x \<and> bin_last y"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1368
  by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1369
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1370
lemma bin_rest_OR [simp]: "bin_rest (x OR y) = bin_rest x OR bin_rest y"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1371
  by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1372
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1373
lemma bin_last_OR [simp]: "bin_last (x OR y) \<longleftrightarrow> bin_last x \<or> bin_last y"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1374
  by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1375
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1376
lemma bin_rest_XOR [simp]: "bin_rest (x XOR y) = bin_rest x XOR bin_rest y"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1377
  by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1378
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1379
lemma bin_last_XOR [simp]:
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1380
  "bin_last (x XOR y) \<longleftrightarrow> (bin_last x \<or> bin_last y) \<and> \<not> (bin_last x \<and> bin_last y)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1381
  by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1382
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1383
lemma bin_nth_ops:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1384
  "\<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
  1385
  "\<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
  1386
  "\<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
  1387
  "\<And>x. bin_nth (NOT x) n \<longleftrightarrow> \<not> bin_nth x n"
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1388
  by (induct n) auto
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1389
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1390
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1391
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
  1392
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1393
lemma int_xor_minus1 [simp]: "-1 XOR x = NOT x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1394
  for x :: int
46018
0bb66de5a0bf simplify definition of XOR for type int;
huffman
parents: 46017
diff changeset
  1395
  by (auto simp add: bin_eq_iff bin_nth_ops)
0bb66de5a0bf simplify definition of XOR for type int;
huffman
parents: 46017
diff changeset
  1396
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1397
lemma int_xor_extra_simps [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1398
  "w XOR 0 = w"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1399
  "w XOR -1 = NOT w"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1400
  for w :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1401
  by (auto simp add: bin_eq_iff bin_nth_ops)
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1402
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1403
lemma int_or_extra_simps [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1404
  "w OR 0 = w"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1405
  "w OR -1 = -1"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1406
  for w :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1407
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1408
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
  1409
lemma int_and_extra_simps [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1410
  "w AND 0 = 0"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1411
  "w AND -1 = w"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1412
  for w :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1413
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1414
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1415
text \<open>Commutativity of the above.\<close>
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1416
lemma bin_ops_comm:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1417
  fixes x y :: int
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1418
  shows int_and_comm: "x AND y = y AND x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1419
    and int_or_comm:  "x OR y = y OR x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1420
    and int_xor_comm: "x XOR y = y XOR x"
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1421
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1422
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1423
lemma bin_ops_same [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1424
  "x AND x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1425
  "x OR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1426
  "x XOR x = 0"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1427
  for x :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1428
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1429
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1430
lemmas bin_log_esimps =
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1431
  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
  1432
  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
  1433
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1434
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1435
subsubsection \<open>Basic properties of logical (bit-wise) operations\<close>
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1436
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1437
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
  1438
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1439
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1440
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1441
lemma bbw_ao_absorbs_other:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1442
  "x AND (x OR y) = x \<and> (y AND x) OR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1443
  "(y OR x) AND x = x \<and> x OR (x AND y) = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1444
  "(x OR y) AND x = x \<and> (x AND y) OR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1445
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1446
  by (auto simp add: bin_eq_iff bin_nth_ops)
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
  1447
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1448
lemmas bbw_ao_absorbs [simp] = bbw_ao_absorb bbw_ao_absorbs_other
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1449
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1450
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
  1451
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1452
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1453
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1454
lemma int_and_assoc: "(x AND y) AND z = x AND (y AND z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1455
  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
  1456
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1457
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1458
lemma int_or_assoc: "(x OR y) OR z = x OR (y OR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1459
  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
  1460
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1461
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1462
lemma int_xor_assoc: "(x XOR y) XOR z = x XOR (y XOR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1463
  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
  1464
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1465
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1466
lemmas bbw_assocs = int_and_assoc int_or_assoc int_xor_assoc
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1467
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1468
(* BH: Why are these declared as simp rules??? *)
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1469
lemma bbw_lcs [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1470
  "y AND (x AND z) = x AND (y AND z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1471
  "y OR (x OR z) = x OR (y OR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1472
  "y XOR (x XOR z) = x XOR (y XOR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1473
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1474
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1475
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1476
lemma bbw_not_dist:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1477
  "NOT (x OR y) = (NOT x) AND (NOT y)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1478
  "NOT (x AND y) = (NOT x) OR (NOT y)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1479
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1480
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1481
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1482
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
  1483
  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
  1484
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1485
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1486
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
  1487
  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
  1488
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1489
24367
3e29eafabe16 AC rules for bitwise logical operators no longer declared simp
huffman
parents: 24366
diff changeset
  1490
(*
3e29eafabe16 AC rules for bitwise logical operators no longer declared simp
huffman
parents: 24366
diff changeset
  1491
Why were these declared simp???
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1492
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
  1493
*)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1494
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1495
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1496
subsubsection \<open>Simplification with numerals\<close>
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1497
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1498
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
  1499
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1500
lemma bin_rest_neg_numeral_BitM [simp]:
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1501
  "bin_rest (- numeral (Num.BitM w)) = - numeral w"
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1502
  by (simp flip: BIT_bin_simps)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1503
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1504
lemma bin_last_neg_numeral_BitM [simp]:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1505
  "bin_last (- numeral (Num.BitM w))"
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1506
  by simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1507
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1508
(* FIXME: The rule sets below are very large (24 rules for each
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1509
  operator). Is there a simpler way to do this? *)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1510
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1511
lemma int_and_numerals [simp]:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1512
  "numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = (numeral x AND numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1513
  "numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = (numeral x AND numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1514
  "numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = (numeral x AND numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1515
  "numeral (Num.Bit1 x) AND numeral (Num.Bit1 y) = (numeral x AND numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1516
  "numeral (Num.Bit0 x) AND - numeral (Num.Bit0 y) = (numeral x AND - numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1517
  "numeral (Num.Bit0 x) AND - numeral (Num.Bit1 y) = (numeral x AND - numeral (y + Num.One)) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1518
  "numeral (Num.Bit1 x) AND - numeral (Num.Bit0 y) = (numeral x AND - numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1519
  "numeral (Num.Bit1 x) AND - numeral (Num.Bit1 y) = (numeral x AND - numeral (y + Num.One)) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1520
  "- numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = (- numeral x AND numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1521
  "- numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = (- numeral x AND numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1522
  "- numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = (- numeral (x + Num.One) AND numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1523
  "- numeral (Num.Bit1 x) AND numeral (Num.Bit1 y) = (- numeral (x + Num.One) AND numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1524
  "- numeral (Num.Bit0 x) AND - numeral (Num.Bit0 y) = (- numeral x AND - numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1525
  "- numeral (Num.Bit0 x) AND - numeral (Num.Bit1 y) = (- numeral x AND - numeral (y + Num.One)) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1526
  "- numeral (Num.Bit1 x) AND - numeral (Num.Bit0 y) = (- numeral (x + Num.One) AND - numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1527
  "- numeral (Num.Bit1 x) AND - numeral (Num.Bit1 y) = (- numeral (x + Num.One) AND - numeral (y + Num.One)) BIT True"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1528
  "(1::int) AND numeral (Num.Bit0 y) = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1529
  "(1::int) AND numeral (Num.Bit1 y) = 1"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1530
  "(1::int) AND - numeral (Num.Bit0 y) = 0"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1531
  "(1::int) AND - numeral (Num.Bit1 y) = 1"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1532
  "numeral (Num.Bit0 x) AND (1::int) = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1533
  "numeral (Num.Bit1 x) AND (1::int) = 1"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1534
  "- numeral (Num.Bit0 x) AND (1::int) = 0"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1535
  "- numeral (Num.Bit1 x) AND (1::int) = 1"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1536
  by (rule bin_rl_eqI; simp)+
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1537
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1538
lemma int_or_numerals [simp]:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1539
  "numeral (Num.Bit0 x) OR numeral (Num.Bit0 y) = (numeral x OR numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1540
  "numeral (Num.Bit0 x) OR numeral (Num.Bit1 y) = (numeral x OR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1541
  "numeral (Num.Bit1 x) OR numeral (Num.Bit0 y) = (numeral x OR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1542
  "numeral (Num.Bit1 x) OR numeral (Num.Bit1 y) = (numeral x OR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1543
  "numeral (Num.Bit0 x) OR - numeral (Num.Bit0 y) = (numeral x OR - numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1544
  "numeral (Num.Bit0 x) OR - numeral (Num.Bit1 y) = (numeral x OR - numeral (y + Num.One)) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1545
  "numeral (Num.Bit1 x) OR - numeral (Num.Bit0 y) = (numeral x OR - numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1546
  "numeral (Num.Bit1 x) OR - numeral (Num.Bit1 y) = (numeral x OR - numeral (y + Num.One)) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1547
  "- numeral (Num.Bit0 x) OR numeral (Num.Bit0 y) = (- numeral x OR numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1548
  "- numeral (Num.Bit0 x) OR numeral (Num.Bit1 y) = (- numeral x OR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1549
  "- numeral (Num.Bit1 x) OR numeral (Num.Bit0 y) = (- numeral (x + Num.One) OR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1550
  "- numeral (Num.Bit1 x) OR numeral (Num.Bit1 y) = (- numeral (x + Num.One) OR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1551
  "- numeral (Num.Bit0 x) OR - numeral (Num.Bit0 y) = (- numeral x OR - numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1552
  "- numeral (Num.Bit0 x) OR - numeral (Num.Bit1 y) = (- numeral x OR - numeral (y + Num.One)) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1553
  "- numeral (Num.Bit1 x) OR - numeral (Num.Bit0 y) = (- numeral (x + Num.One) OR - numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1554
  "- numeral (Num.Bit1 x) OR - numeral (Num.Bit1 y) = (- numeral (x + Num.One) OR - numeral (y + Num.One)) BIT True"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1555
  "(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
  1556
  "(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
  1557
  "(1::int) OR - numeral (Num.Bit0 y) = - numeral (Num.BitM y)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1558
  "(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
  1559
  "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
  1560
  "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
  1561
  "- numeral (Num.Bit0 x) OR (1::int) = - numeral (Num.BitM x)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1562
  "- numeral (Num.Bit1 x) OR (1::int) = - numeral (Num.Bit1 x)"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1563
  by (rule bin_rl_eqI; simp)+
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1564
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1565
lemma int_xor_numerals [simp]:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1566
  "numeral (Num.Bit0 x) XOR numeral (Num.Bit0 y) = (numeral x XOR numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1567
  "numeral (Num.Bit0 x) XOR numeral (Num.Bit1 y) = (numeral x XOR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1568
  "numeral (Num.Bit1 x) XOR numeral (Num.Bit0 y) = (numeral x XOR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1569
  "numeral (Num.Bit1 x) XOR numeral (Num.Bit1 y) = (numeral x XOR numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1570
  "numeral (Num.Bit0 x) XOR - numeral (Num.Bit0 y) = (numeral x XOR - numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1571
  "numeral (Num.Bit0 x) XOR - numeral (Num.Bit1 y) = (numeral x XOR - numeral (y + Num.One)) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1572
  "numeral (Num.Bit1 x) XOR - numeral (Num.Bit0 y) = (numeral x XOR - numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1573
  "numeral (Num.Bit1 x) XOR - numeral (Num.Bit1 y) = (numeral x XOR - numeral (y + Num.One)) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1574
  "- numeral (Num.Bit0 x) XOR numeral (Num.Bit0 y) = (- numeral x XOR numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1575
  "- numeral (Num.Bit0 x) XOR numeral (Num.Bit1 y) = (- numeral x XOR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1576
  "- numeral (Num.Bit1 x) XOR numeral (Num.Bit0 y) = (- numeral (x + Num.One) XOR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1577
  "- numeral (Num.Bit1 x) XOR numeral (Num.Bit1 y) = (- numeral (x + Num.One) XOR numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1578
  "- numeral (Num.Bit0 x) XOR - numeral (Num.Bit0 y) = (- numeral x XOR - numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1579
  "- numeral (Num.Bit0 x) XOR - numeral (Num.Bit1 y) = (- numeral x XOR - numeral (y + Num.One)) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1580
  "- numeral (Num.Bit1 x) XOR - numeral (Num.Bit0 y) = (- numeral (x + Num.One) XOR - numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1581
  "- numeral (Num.Bit1 x) XOR - numeral (Num.Bit1 y) = (- numeral (x + Num.One) XOR - numeral (y + Num.One)) BIT False"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1582
  "(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
  1583
  "(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
  1584
  "(1::int) XOR - numeral (Num.Bit0 y) = - numeral (Num.BitM y)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1585
  "(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
  1586
  "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
  1587
  "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
  1588
  "- numeral (Num.Bit0 x) XOR (1::int) = - numeral (Num.BitM x)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1589
  "- numeral (Num.Bit1 x) XOR (1::int) = - numeral (Num.Bit0 (x + Num.One))"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1590
  by (rule bin_rl_eqI; simp)+
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1591
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1592
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1593
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
  1594
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1595
lemma plus_and_or [rule_format]: "\<forall>y::int. (x AND y) + (x OR y) = x + y"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1596
  apply (induct x rule: bin_induct)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1597
    apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1598
   apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1599
  apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1600
  apply (case_tac y rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1601
  apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1602
  apply (unfold Bit_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1603
  apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1604
  apply (erule_tac x = "x" in allE)
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1605
  apply simp
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1606
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1607
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1608
lemma le_int_or: "bin_sign y = 0 \<Longrightarrow> x \<le> x OR y"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1609
  for x y :: int
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
  1610
  apply (induct y arbitrary: x rule: bin_induct)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1611
    apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1612
   apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1613
  apply (case_tac x rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1614
  apply (case_tac b)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1615
   apply (case_tac [!] bit)
46604
9f9e85264e4d make uses of bin_sign respect int/bin distinction
huffman
parents: 46023
diff changeset
  1616
     apply (auto simp: le_Bits)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1617
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1618
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1619
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
  1620
  xtrans(3) [OF bbw_ao_absorbs (2) [THEN conjunct2, symmetric] le_int_or]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1621
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1622
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
  1623
lemma bin_add_not: "x + NOT x = (-1::int)"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1624
  apply (induct x rule: bin_induct)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1625
    apply clarsimp
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1626
   apply clarsimp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1627
  apply (case_tac bit, auto)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1628
  done
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1629
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 70193
diff changeset
  1630
lemma mod_BIT:
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 70193
diff changeset
  1631
  "bin BIT bit mod 2 ^ Suc n = (bin mod 2 ^ n) BIT bit" for bit
70169
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1632
proof -
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1633
  have "2 * (bin mod 2 ^ n) + 1 = (2 * bin mod 2 ^ Suc n) + 1"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1634
    by (simp add: mod_mult_mult1)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1635
  also have "\<dots> = ((2 * bin mod 2 ^ Suc n) + 1) mod 2 ^ Suc n"
70170
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
  1636
    by (simp add: ac_simps pos_zmod_mult_2)
70169
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1637
  also have "\<dots> = (2 * bin + 1) mod 2 ^ Suc n"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1638
    by (simp only: mod_simps)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1639
  finally show ?thesis
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1640
    by (auto simp add: Bit_def)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1641
qed
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1642
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1643
lemma AND_mod: "x AND 2 ^ n - 1 = x mod 2 ^ n"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1644
  for x :: int
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1645
proof (induct x arbitrary: n rule: bin_induct)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1646
  case 1
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1647
  then show ?case
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1648
    by simp
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1649
next
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1650
  case 2
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1651
  then show ?case
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1652
    by (simp, simp add: m1mod2k)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1653
next
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1654
  case (3 bin bit)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1655
  show ?case
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1656
  proof (cases n)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1657
    case 0
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1658
    then show ?thesis by simp
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1659
  next
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1660
    case (Suc m)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1661
    with 3 show ?thesis
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1662
      by (simp only: power_BIT mod_BIT int_and_Bits) simp
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1663
  qed
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1664
qed
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1665
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1666
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1667
subsubsection \<open>Comparison\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1668
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1669
lemma AND_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1670
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1671
  assumes "0 \<le> x"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1672
  shows "0 \<le> x AND y"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1673
  using assms
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1674
proof (induct x arbitrary: y rule: bin_induct)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1675
  case 1
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1676
  then show ?case by simp
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1677
next
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1678
  case 2
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1679
  then show ?case by (simp only: Min_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1680
next
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1681
  case (3 bin bit)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1682
  show ?case
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1683
  proof (cases y rule: bin_exhaust)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1684
    case (1 bin' bit')
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1685
    from 3 have "0 \<le> bin"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1686
      by (cases bit) (simp_all add: Bit_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1687
    then have "0 \<le> bin AND bin'" by (rule 3)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1688
    with 1 show ?thesis
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1689
      by simp
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1690
  qed
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1691
qed
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1692
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1693
lemma OR_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1694
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1695
  assumes "0 \<le> x" "0 \<le> y"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1696
  shows "0 \<le> x OR y"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1697
  using assms
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1698
proof (induct x arbitrary: y rule: bin_induct)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1699
  case (3 bin bit)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1700
  show ?case
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1701
  proof (cases y rule: bin_exhaust)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1702
    case (1 bin' bit')
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1703
    from 3 have "0 \<le> bin"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1704
      by (cases bit) (simp_all add: Bit_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1705
    moreover from 1 3 have "0 \<le> bin'"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1706
      by (cases bit') (simp_all add: Bit_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1707
    ultimately have "0 \<le> bin OR bin'" by (rule 3)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1708
    with 1 show ?thesis
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1709
      by simp
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1710
  qed
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1711
qed simp_all
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1712
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1713
lemma XOR_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1714
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1715
  assumes "0 \<le> x" "0 \<le> y"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1716
  shows "0 \<le> x XOR y"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1717
  using assms
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1718
proof (induct x arbitrary: y rule: bin_induct)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1719
  case (3 bin bit)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1720
  show ?case
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1721
  proof (cases y rule: bin_exhaust)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1722
    case (1 bin' bit')
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1723
    from 3 have "0 \<le> bin"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1724
      by (cases bit) (simp_all add: Bit_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1725
    moreover from 1 3 have "0 \<le> bin'"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1726
      by (cases bit') (simp_all add: Bit_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1727
    ultimately have "0 \<le> bin XOR bin'" by (rule 3)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1728
    with 1 show ?thesis
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1729
      by simp
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1730
  qed
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1731
next
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1732
  case 2
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1733
  then show ?case by (simp only: Min_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1734
qed simp
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1735
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1736
lemma AND_upper1 [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1737
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1738
  assumes "0 \<le> x"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1739
  shows "x AND y \<le> x"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1740
  using assms
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1741
proof (induct x arbitrary: y rule: bin_induct)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1742
  case (3 bin bit)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1743
  show ?case
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1744
  proof (cases y rule: bin_exhaust)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1745
    case (1 bin' bit')
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1746
    from 3 have "0 \<le> bin"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1747
      by (cases bit) (simp_all add: Bit_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1748
    then have "bin AND bin' \<le> bin" by (rule 3)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1749
    with 1 show ?thesis
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1750
      by simp (simp add: Bit_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1751
  qed
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1752
next
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1753
  case 2
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1754
  then show ?case by (simp only: Min_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1755
qed simp
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1756
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1757
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
  1758
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
  1759
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1760
lemma AND_upper2 [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1761
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1762
  assumes "0 \<le> y"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1763
  shows "x AND y \<le> y"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1764
  using assms
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1765
proof (induct y arbitrary: x rule: bin_induct)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1766
  case 1
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1767
  then show ?case by simp
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1768
next
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1769
  case 2
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1770
  then show ?case by (simp only: Min_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1771
next
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1772
  case (3 bin bit)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1773
  show ?case
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1774
  proof (cases x rule: bin_exhaust)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1775
    case (1 bin' bit')
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1776
    from 3 have "0 \<le> bin"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1777
      by (cases bit) (simp_all add: Bit_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1778
    then have "bin' AND bin \<le> bin" by (rule 3)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1779
    with 1 show ?thesis
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1780
      by simp (simp add: Bit_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1781
  qed
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1782
qed
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1783
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1784
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
  1785
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
  1786
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1787
lemma OR_upper: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1788
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1789
  assumes "0 \<le> x" "x < 2 ^ n" "y < 2 ^ n"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1790
  shows "x OR y < 2 ^ n"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1791
  using assms
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1792
proof (induct x arbitrary: y n rule: bin_induct)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1793
  case (3 bin bit)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1794
  show ?case
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1795
  proof (cases y rule: bin_exhaust)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1796
    case (1 bin' bit')
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1797
    show ?thesis
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1798
    proof (cases n)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1799
      case 0
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1800
      with 3 have "bin BIT bit = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1801
        by (simp add: Bit_def)
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1802
      then have "bin = 0" and "\<not> bit"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1803
        by (auto simp add: Bit_def split: if_splits) arith
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1804
      then show ?thesis using 0 1 \<open>y < 2 ^ n\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1805
        by simp
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1806
    next
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1807
      case (Suc m)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1808
      from 3 have "0 \<le> bin"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1809
        by (cases bit) (simp_all add: Bit_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1810
      moreover from 3 Suc have "bin < 2 ^ m"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1811
        by (cases bit) (simp_all add: Bit_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1812
      moreover from 1 3 Suc have "bin' < 2 ^ m"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1813
        by (cases bit') (simp_all add: Bit_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1814
      ultimately have "bin OR bin' < 2 ^ m" by (rule 3)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1815
      with 1 Suc show ?thesis
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1816
        by simp (simp add: Bit_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1817
    qed
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1818
  qed
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1819
qed simp_all
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1820
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1821
lemma XOR_upper: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1822
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1823
  assumes "0 \<le> x" "x < 2 ^ n" "y < 2 ^ n"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1824
  shows "x XOR y < 2 ^ n"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1825
  using assms
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1826
proof (induct x arbitrary: y n rule: bin_induct)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1827
  case 1
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1828
  then show ?case by simp
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1829
next
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1830
  case 2
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1831
  then show ?case by (simp only: Min_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1832
next
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1833
  case (3 bin bit)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1834
  show ?case
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1835
  proof (cases y rule: bin_exhaust)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1836
    case (1 bin' bit')
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1837
    show ?thesis
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1838
    proof (cases n)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1839
      case 0
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1840
      with 3 have "bin BIT bit = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1841
        by (simp add: Bit_def)
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1842
      then have "bin = 0" and "\<not> bit"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1843
        by (auto simp add: Bit_def split: if_splits) arith
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1844
      then show ?thesis using 0 1 \<open>y < 2 ^ n\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1845
        by simp
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1846
    next
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1847
      case (Suc m)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1848
      from 3 have "0 \<le> bin"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1849
        by (cases bit) (simp_all add: Bit_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1850
      moreover from 3 Suc have "bin < 2 ^ m"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1851
        by (cases bit) (simp_all add: Bit_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1852
      moreover from 1 3 Suc have "bin' < 2 ^ m"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1853
        by (cases bit') (simp_all add: Bit_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1854
      ultimately have "bin XOR bin' < 2 ^ m" by (rule 3)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1855
      with 1 Suc show ?thesis
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1856
        by simp (simp add: Bit_def)
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1857
    qed
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1858
  qed
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1859
qed
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1860
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1861
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1862
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1863
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
  1864
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1865
lemma bin_trunc_ao:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1866
  "bintrunc n x AND bintrunc n y = bintrunc n (x AND y)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1867
  "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
  1868
  by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1869
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1870
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
  1871
  by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1872
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1873
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
  1874
  by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1875
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1876
text \<open>Want theorems of the form of \<open>bin_trunc_xor\<close>.\<close>
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1877
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
  1878
  by auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1879
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1880
lemmas bin_trunc_and = bin_trunc_ao(1) [THEN bintr_bintr_i]
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1881
lemmas bin_trunc_or = bin_trunc_ao(2) [THEN bintr_bintr_i]
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1882
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1883
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1884
subsubsection \<open>More lemmas\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1885
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1886
lemma not_int_cmp_0 [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1887
  fixes i :: int shows
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1888
  "0 < NOT i \<longleftrightarrow> i < -1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1889
  "0 \<le> NOT i \<longleftrightarrow> i < 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1890
  "NOT i < 0 \<longleftrightarrow> i \<ge> 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1891
  "NOT i \<le> 0 \<longleftrightarrow> i \<ge> -1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1892
by(simp_all add: int_not_def) arith+
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1893
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1894
lemma bbw_ao_dist2: "(x :: int) AND (y OR z) = x AND y OR x AND z"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1895
by(metis int_and_comm bbw_ao_dist)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1896
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1897
lemmas int_and_ac = bbw_lcs(1) int_and_comm int_and_assoc
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1898
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1899
lemma int_nand_same [simp]: fixes x :: int shows "x AND NOT x = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1900
by(induct x y\<equiv>"NOT x" rule: bitAND_int.induct)(subst bitAND_int.simps, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1901
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1902
lemma int_nand_same_middle: fixes x :: int shows "x AND y AND NOT x = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1903
by (metis bbw_lcs(1) int_and_0 int_nand_same)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1904
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1905
lemma and_xor_dist: fixes x :: int shows
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1906
  "x AND (y XOR z) = (x AND y) XOR (x AND z)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1907
by(simp add: int_xor_def bbw_ao_dist2 bbw_not_dist int_and_ac int_nand_same_middle)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1908
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1909
lemma int_and_lt0 [simp]: fixes x y :: int shows
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1910
  "x AND y < 0 \<longleftrightarrow> x < 0 \<and> y < 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1911
by(induct x y rule: bitAND_int.induct)(subst bitAND_int.simps, simp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1912
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1913
lemma int_and_ge0 [simp]: fixes x y :: int shows 
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1914
  "x AND y \<ge> 0 \<longleftrightarrow> x \<ge> 0 \<or> y \<ge> 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1915
by (metis int_and_lt0 linorder_not_less)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1916
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1917
lemma int_and_1: fixes x :: int shows "x AND 1 = x mod 2"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1918
by(subst bitAND_int.simps)(simp add: Bit_def bin_last_def zmod_minus1)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1919
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1920
lemma int_1_and: fixes x :: int shows "1 AND x = x mod 2"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1921
by(subst int_and_comm)(simp add: int_and_1)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1922
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1923
lemma int_or_lt0 [simp]: fixes x y :: int shows 
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1924
  "x OR y < 0 \<longleftrightarrow> x < 0 \<or> y < 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1925
by(simp add: int_or_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1926
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1927
lemma int_xor_lt0 [simp]: fixes x y :: int shows
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1928
  "x XOR y < 0 \<longleftrightarrow> ((x < 0) \<noteq> (y < 0))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1929
by(auto simp add: int_xor_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1930
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1931
lemma int_xor_ge0 [simp]: fixes x y :: int shows
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1932
  "x XOR y \<ge> 0 \<longleftrightarrow> ((x \<ge> 0) \<longleftrightarrow> (y \<ge> 0))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1933
by (metis int_xor_lt0 linorder_not_le)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1934
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1935
lemma even_conv_AND:
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1936
  \<open>even i \<longleftrightarrow> i AND 1 = 0\<close> for i :: int
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1937
proof -
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1938
  obtain x b where \<open>i = x BIT b\<close>
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1939
    by (cases i rule: bin_exhaust)
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1940
  then have "i AND 1 = 0 BIT b"
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1941
    by (simp add: BIT_special_simps(2) [symmetric] del: BIT_special_simps(2))
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1942
  then show ?thesis
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1943
    using \<open>i = x BIT b\<close> by (cases b) simp_all
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1944
qed
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1945
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1946
lemma bin_last_conv_AND:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1947
  "bin_last i \<longleftrightarrow> i AND 1 \<noteq> 0"
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1948
  by (simp add: even_conv_AND)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1949
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1950
lemma bitval_bin_last:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1951
  "of_bool (bin_last i) = i AND 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1952
proof -
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1953
  obtain x b where "i = x BIT b" by(cases i rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1954
  hence "i AND 1 = 0 BIT b"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1955
    by(simp add: BIT_special_simps(2)[symmetric] del: BIT_special_simps(2))
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1956
  thus ?thesis by(cases b)(simp_all add: bin_last_conv_AND)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1957
qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1958
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1959
lemma bin_sign_and:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1960
  "bin_sign (i AND j) = - (bin_sign i * bin_sign j)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1961
by(simp add: bin_sign_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1962
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1963
lemma minus_BIT_0: fixes x y :: int shows "x BIT b - y BIT False = (x - y) BIT b"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1964
by(simp add: Bit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1965
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1966
lemma int_not_neg_numeral: "NOT (- numeral n) = (Num.sub n num.One :: int)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1967
by(simp add: int_not_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1968
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1969
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
  1970
by(simp add: int_not_def)
70169
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1971
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1972
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1973
subsection \<open>Setting and clearing bits\<close>
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1974
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1975
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
  1976
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1977
lemma int_lsb_BIT [simp]: fixes x :: int shows
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1978
  "lsb (x BIT b) \<longleftrightarrow> b"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1979
by(simp add: lsb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1980
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1981
lemma bin_last_conv_lsb: "bin_last = lsb"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1982
by(clarsimp simp add: lsb_int_def fun_eq_iff)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1983
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1984
lemma int_lsb_numeral [simp]:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1985
  "lsb (0 :: int) = False"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1986
  "lsb (1 :: int) = True"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1987
  "lsb (Numeral1 :: int) = True"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1988
  "lsb (- 1 :: int) = True"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1989
  "lsb (- Numeral1 :: int) = True"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1990
  "lsb (numeral (num.Bit0 w) :: int) = False"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1991
  "lsb (numeral (num.Bit1 w) :: int) = True"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1992
  "lsb (- numeral (num.Bit0 w) :: int) = False"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1993
  "lsb (- numeral (num.Bit1 w) :: int) = True"
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1994
  by (simp_all add: lsb_int_def)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1995
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1996
lemma int_set_bit_0 [simp]: fixes x :: int shows
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1997
  "set_bit x 0 b = bin_rest x BIT b"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1998
by(auto simp add: set_bit_int_def intro: bin_rl_eqI)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1999
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2000
lemma int_set_bit_Suc: fixes x :: int shows
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2001
  "set_bit x (Suc n) b = set_bit (bin_rest x) n b BIT bin_last x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2002
by(auto simp add: set_bit_int_def twice_conv_BIT intro: bin_rl_eqI)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2003
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2004
lemma bin_last_set_bit:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2005
  "bin_last (set_bit x n b) = (if n > 0 then bin_last x else b)"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2006
by(cases n)(simp_all add: int_set_bit_Suc)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2007
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2008
lemma bin_rest_set_bit: 
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2009
  "bin_rest (set_bit x n b) = (if n > 0 then set_bit (bin_rest x) (n - 1) b else bin_rest x)"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2010
by(cases n)(simp_all add: int_set_bit_Suc)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2011
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2012
lemma int_set_bit_numeral: fixes x :: int shows
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2013
  "set_bit x (numeral w) b = set_bit (bin_rest x) (pred_numeral w) b BIT bin_last x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2014
by(simp add: set_bit_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2015
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2016
lemmas int_set_bit_numerals [simp] =
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2017
  int_set_bit_numeral[where x="numeral w'"] 
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2018
  int_set_bit_numeral[where x="- numeral w'"]
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2019
  int_set_bit_numeral[where x="Numeral1"]
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2020
  int_set_bit_numeral[where x="1"]
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2021
  int_set_bit_numeral[where x="0"]
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2022
  int_set_bit_Suc[where x="numeral w'"]
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2023
  int_set_bit_Suc[where x="- numeral w'"]
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2024
  int_set_bit_Suc[where x="Numeral1"]
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2025
  int_set_bit_Suc[where x="1"]
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2026
  int_set_bit_Suc[where x="0"]
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2027
  for w'
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2028
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2029
lemma int_shiftl_BIT: fixes x :: int
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2030
  shows int_shiftl0 [simp]: "x << 0 = x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2031
  and int_shiftl_Suc [simp]: "x << Suc n = (x << n) BIT False"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2032
by(auto simp add: shiftl_int_def Bit_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2033
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2034
lemma int_0_shiftl [simp]: "0 << n = (0 :: int)"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2035
by(induct n) simp_all
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2036
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2037
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
  2038
by(cases n)(simp_all)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2039
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2040
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
  2041
by(cases n)(simp_all)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2042
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2043
lemma bin_nth_shiftl [simp]: "bin_nth (x << n) m \<longleftrightarrow> n \<le> m \<and> bin_nth x (m - n)"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2044
proof(induct n arbitrary: x m)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2045
  case (Suc n)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2046
  thus ?case by(cases m) simp_all
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2047
qed simp
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2048
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2049
lemma int_shiftr_BIT [simp]: fixes x :: int
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2050
  shows int_shiftr0: "x >> 0 = x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2051
  and int_shiftr_Suc: "x BIT b >> Suc n = x >> n"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2052
proof -
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2053
  show "x >> 0 = x" by (simp add: shiftr_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2054
  show "x BIT b >> Suc n = x >> n" by (cases b)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2055
   (simp_all add: shiftr_int_def Bit_def add.commute pos_zdiv_mult_2)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2056
qed
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2057
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2058
lemma bin_last_shiftr: "bin_last (x >> n) \<longleftrightarrow> x !! n"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2059
proof(induct n arbitrary: x)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2060
  case 0 thus ?case by simp
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2061
next
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2062
  case (Suc n)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2063
  thus ?case by(cases x rule: bin_exhaust) simp
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2064
qed
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2065
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2066
lemma bin_rest_shiftr [simp]: "bin_rest (x >> n) = x >> Suc n"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2067
proof(induct n arbitrary: x)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2068
  case 0
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2069
  thus ?case by(cases x rule: bin_exhaust) auto
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2070
next
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2071
  case (Suc n)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2072
  thus ?case by(cases x rule: bin_exhaust) auto
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2073
qed
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2074
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2075
lemma bin_nth_shiftr [simp]: "bin_nth (x >> n) m = bin_nth x (n + m)"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2076
proof(induct n arbitrary: x m)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2077
  case (Suc n)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2078
  thus ?case by(cases x rule: bin_exhaust) simp_all
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2079
qed simp
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2080
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2081
lemma bin_nth_conv_AND:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2082
  fixes x :: int shows 
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2083
  "bin_nth x n \<longleftrightarrow> x AND (1 << n) \<noteq> 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2084
proof(induct n arbitrary: x)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2085
  case 0 
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2086
  thus ?case by(simp add: int_and_1 bin_last_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2087
next
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2088
  case (Suc n)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2089
  thus ?case by(cases x rule: bin_exhaust)(simp_all add: bin_nth_ops Bit_eq_0_iff)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2090
qed
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2091
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2092
lemma int_shiftl_numeral [simp]: 
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2093
  "(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
  2094
  "(- 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
  2095
by(simp_all add: numeral_eq_Suc Bit_def shiftl_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2096
  (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
  2097
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2098
lemma int_shiftl_One_numeral [simp]: "(1 :: int) << numeral w = 2 << pred_numeral w"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2099
by(metis int_shiftl_numeral numeral_One)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2100
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2101
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
  2102
by(induct n) simp_all
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2103
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2104
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
  2105
by (metis not_le shiftl_ge_0)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2106
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2107
lemma int_shiftl_test_bit: "(n << i :: int) !! m \<longleftrightarrow> m \<ge> i \<and> n !! (m - i)"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2108
proof(induction i)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2109
  case (Suc n)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2110
  thus ?case by(cases m) simp_all
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2111
qed simp
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2112
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2113
lemma int_0shiftr [simp]: "(0 :: int) >> x = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2114
by(simp add: shiftr_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2115
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2116
lemma int_minus1_shiftr [simp]: "(-1 :: int) >> x = -1"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2117
by(simp add: shiftr_int_def div_eq_minus1)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2118
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2119
lemma int_shiftr_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
  2120
proof(induct n arbitrary: i)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2121
  case (Suc n)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2122
  thus ?case by(cases i rule: bin_exhaust) simp_all
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2123
qed simp
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2124
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2125
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
  2126
by (metis int_shiftr_ge_0 not_less)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2127
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2128
lemma int_shiftr_numeral [simp]:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2129
  "(1 :: int) >> numeral w' = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2130
  "(numeral num.One :: int) >> numeral w' = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2131
  "(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
  2132
  "(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
  2133
  "(- 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
  2134
  "(- numeral (num.Bit1 w) :: int) >> numeral w' = - numeral (Num.inc w) >> pred_numeral w'"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2135
  by (simp_all only: numeral_One expand_BIT numeral_eq_Suc int_shiftr_Suc BIT_special_simps(2)[symmetric] int_0shiftr add_One uminus_Bit_eq)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2136
    (simp_all add: add_One)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2137
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2138
lemma int_shiftr_numeral_Suc0 [simp]:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2139
  "(1 :: int) >> Suc 0 = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2140
  "(numeral num.One :: int) >> Suc 0 = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2141
  "(numeral (num.Bit0 w) :: int) >> Suc 0 = numeral w"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2142
  "(numeral (num.Bit1 w) :: int) >> Suc 0 = numeral w"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2143
  "(- numeral (num.Bit0 w) :: int) >> Suc 0 = - numeral w"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2144
  "(- numeral (num.Bit1 w) :: int) >> Suc 0 = - numeral (Num.inc w)"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2145
by(simp_all only: One_nat_def[symmetric] numeral_One[symmetric] int_shiftr_numeral pred_numeral_simps int_shiftr0)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2146
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2147
lemma bin_nth_minus_p2:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2148
  assumes sign: "bin_sign x = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2149
  and y: "y = 1 << n"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2150
  and m: "m < n"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2151
  and x: "x < y"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2152
  shows "bin_nth (x - y) m = bin_nth x m"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2153
using sign m x unfolding y
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2154
proof(induction m arbitrary: x y n)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2155
  case 0
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2156
  thus ?case
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  2157
    by (simp add: bin_last_def shiftl_int_def)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2158
next
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2159
  case (Suc m)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2160
  from \<open>Suc m < n\<close> obtain n' where [simp]: "n = Suc n'" by(cases n) auto
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2161
  obtain x' b where [simp]: "x = x' BIT b" by(cases x rule: bin_exhaust)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2162
  from \<open>bin_sign x = 0\<close> have "bin_sign x' = 0" by simp
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2163
  moreover from \<open>x < 1 << n\<close> have "x' < 1 << n'"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2164
    by(cases b)(simp_all add: Bit_def shiftl_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2165
  moreover have "(2 * x' + of_bool b - 2 * 2 ^ n') div 2 = x' + (- (2 ^ n') + of_bool b div 2)"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2166
    by(simp only: add_diff_eq[symmetric] add.commute div_mult_self2[OF zero_neq_numeral[symmetric]])
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2167
  ultimately show ?case using Suc.IH[of x' n'] Suc.prems
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2168
    by(cases b)(simp_all add: Bit_def bin_rest_def shiftl_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2169
qed
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2170
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2171
lemma bin_clr_conv_NAND:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2172
  "bin_sc n False i = i AND NOT (1 << n)"
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  2173
  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
  2174
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2175
lemma bin_set_conv_OR:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2176
  "bin_sc n True i = i OR (1 << n)"
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  2177
  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
  2178
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2179
lemma msb_conv_bin_sign: "msb x \<longleftrightarrow> bin_sign x = -1"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2180
by(simp add: bin_sign_def not_le msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2181
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2182
lemma msb_BIT [simp]: "msb (x BIT b) = msb x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2183
by(simp add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2184
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2185
lemma msb_bin_rest [simp]: "msb (bin_rest x) = msb x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2186
by(simp add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2187
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2188
lemma int_msb_and [simp]: "msb ((x :: int) AND y) \<longleftrightarrow> msb x \<and> msb y"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2189
by(simp add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2190
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2191
lemma int_msb_or [simp]: "msb ((x :: int) OR y) \<longleftrightarrow> msb x \<or> msb y"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2192
by(simp add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2193
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2194
lemma int_msb_xor [simp]: "msb ((x :: int) XOR y) \<longleftrightarrow> msb x \<noteq> msb y"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2195
by(simp add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2196
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2197
lemma int_msb_not [simp]: "msb (NOT (x :: int)) \<longleftrightarrow> \<not> msb x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2198
by(simp add: msb_int_def not_less)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2199
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2200
lemma msb_shiftl [simp]: "msb ((x :: int) << n) \<longleftrightarrow> msb x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2201
by(simp add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2202
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2203
lemma msb_shiftr [simp]: "msb ((x :: int) >> r) \<longleftrightarrow> msb x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2204
by(simp add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2205
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2206
lemma msb_bin_sc [simp]: "msb (bin_sc n b x) \<longleftrightarrow> msb x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2207
by(simp add: msb_conv_bin_sign)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2208
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2209
lemma msb_set_bit [simp]: "msb (set_bit (x :: int) n b) \<longleftrightarrow> msb x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2210
by(simp add: msb_conv_bin_sign set_bit_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2211
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2212
lemma msb_0 [simp]: "msb (0 :: int) = False"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2213
by(simp add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2214
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2215
lemma msb_1 [simp]: "msb (1 :: int) = False"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2216
by(simp add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2217
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2218
lemma msb_numeral [simp]:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2219
  "msb (numeral n :: int) = False"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2220
  "msb (- numeral n :: int) = True"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2221
by(simp_all add: msb_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2222
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2223
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2224
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
  2225
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2226
lemma bin_bl_bin': "bl_to_bin (bin_to_bl_aux n w bs) = bl_to_bin_aux bs (bintrunc n w)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2227
  by (induct n arbitrary: w bs) (auto simp: bl_to_bin_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2228
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2229
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
  2230
  by (auto simp: bin_to_bl_def bin_bl_bin')
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2231
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2232
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
  2233
  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
  2234
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2235
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
  2236
  by (auto intro: bl_to_bin_inj)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2237
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2238
lemma bin_to_bl_aux_bintr:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2239
  "bin_to_bl_aux n (bintrunc m bin) bl =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2240
    replicate (n - m) False @ bin_to_bl_aux (min n m) bin bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2241
  apply (induct n arbitrary: m bin bl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2242
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2243
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2244
  apply (case_tac "m")
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2245
   apply (clarsimp simp: bin_to_bl_zero_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2246
   apply (erule thin_rl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2247
   apply (induct_tac n)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2248
    apply auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2249
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2250
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2251
lemma bin_to_bl_bintr:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2252
  "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
  2253
  unfolding bin_to_bl_def by (rule bin_to_bl_aux_bintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2254
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2255
lemma bl_to_bin_rep_False: "bl_to_bin (replicate n False) = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2256
  by (induct n) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2257
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2258
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
  2259
  by (fact size_bin_to_bl_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2260
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2261
lemma len_bin_to_bl: "length (bin_to_bl n w) = n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2262
  by (fact size_bin_to_bl) (* FIXME: duplicate *)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2263
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2264
lemma sign_bl_bin': "bin_sign (bl_to_bin_aux bs w) = bin_sign w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2265
  by (induct bs arbitrary: w) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2266
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2267
lemma sign_bl_bin: "bin_sign (bl_to_bin bs) = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2268
  by (simp add: bl_to_bin_def sign_bl_bin')
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2269
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2270
lemma bl_sbin_sign_aux: "hd (bin_to_bl_aux (Suc n) w bs) = (bin_sign (sbintrunc n w) = -1)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2271
  apply (induct n arbitrary: w bs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2272
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2273
   apply (cases w rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2274
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2275
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2276
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2277
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
  2278
  unfolding bin_to_bl_def by (rule bl_sbin_sign_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2279
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2280
lemma bin_nth_of_bl_aux:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2281
  "bin_nth (bl_to_bin_aux bl w) n =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2282
    (n < size bl \<and> rev bl ! n \<or> n \<ge> length bl \<and> bin_nth w (n - size bl))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2283
  apply (induct bl arbitrary: w)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2284
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2285
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2286
  apply (cut_tac x=n and y="size bl" in linorder_less_linear)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2287
  apply (erule disjE, simp add: nth_append)+
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2288
  apply auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2289
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2290
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2291
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
  2292
  by (simp add: bl_to_bin_def bin_nth_of_bl_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2293
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2294
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
  2295
  apply (induct n arbitrary: m w)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2296
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2297
   apply (case_tac m, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2298
   apply (clarsimp simp: bin_to_bl_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2299
   apply (simp add: bin_to_bl_aux_alt)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2300
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2301
  apply (case_tac m, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2302
  apply (clarsimp simp: bin_to_bl_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2303
  apply (simp add: bin_to_bl_aux_alt)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2304
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2305
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2306
lemma nth_bin_to_bl_aux:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2307
  "n < m + length bl \<Longrightarrow> (bin_to_bl_aux m w bl) ! n =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2308
    (if n < m then bin_nth w (m - 1 - n) else bl ! (n - m))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2309
  apply (induct m arbitrary: w n bl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2310
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2311
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2312
  apply (case_tac w rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2313
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2314
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2315
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2316
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
  2317
  by (simp add: bin_to_bl_def nth_bin_to_bl_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2318
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2319
lemma bl_to_bin_lt2p_aux: "bl_to_bin_aux bs w < (w + 1) * (2 ^ length bs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2320
  apply (induct bs arbitrary: w)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2321
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2322
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2323
  apply (drule meta_spec, erule xtrans(8) [rotated], simp add: Bit_def)+
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2324
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2325
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2326
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
  2327
proof (induct bs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2328
  case Nil
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2329
  then show ?case by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2330
next
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2331
  case (Cons b bs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2332
  with bl_to_bin_lt2p_aux[where w=1] show ?case
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2333
    by (simp add: bl_to_bin_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2334
qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2335
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2336
lemma bl_to_bin_lt2p: "bl_to_bin bs < 2 ^ length bs"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2337
  by (metis bin_bl_bin bintr_lt2p bl_bin_bl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2338
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2339
lemma bl_to_bin_ge2p_aux: "bl_to_bin_aux bs w \<ge> w * (2 ^ length bs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2340
  apply (induct bs arbitrary: w)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2341
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2342
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2343
   apply (drule meta_spec, erule order_trans [rotated],
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2344
          simp add: Bit_B0_2t Bit_B1_2t algebra_simps)+
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2345
   apply (simp add: Bit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2346
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2347
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2348
lemma bl_to_bin_ge0: "bl_to_bin bs \<ge> 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2349
  apply (unfold bl_to_bin_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2350
  apply (rule xtrans(4))
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2351
   apply (rule bl_to_bin_ge2p_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2352
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2353
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2354
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2355
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
  2356
  apply (unfold bin_to_bl_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2357
  apply (cases w rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2358
  apply (cases n, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2359
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2360
  apply (auto simp add: bin_to_bl_aux_alt)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2361
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2362
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2363
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
  2364
  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
  2365
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2366
lemma butlast_rest_bl2bin_aux:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2367
  "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
  2368
  by (induct bl arbitrary: w) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2369
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2370
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
  2371
  by (cases bl) (auto simp: bl_to_bin_def butlast_rest_bl2bin_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2372
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2373
lemma trunc_bl2bin_aux:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2374
  "bintrunc m (bl_to_bin_aux bl w) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2375
    bl_to_bin_aux (drop (length bl - m) bl) (bintrunc (m - length bl) w)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2376
proof (induct bl arbitrary: w)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2377
  case Nil
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2378
  show ?case by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2379
next
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2380
  case (Cons b bl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2381
  show ?case
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2382
  proof (cases "m - length bl")
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2383
    case 0
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2384
    then have "Suc (length bl) - m = Suc (length bl - m)" by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2385
    with Cons show ?thesis by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2386
  next
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2387
    case (Suc n)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2388
    then have "m - Suc (length bl) = n" by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2389
    with Cons Suc show ?thesis by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2390
  qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2391
qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2392
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2393
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
  2394
  by (simp add: bl_to_bin_def trunc_bl2bin_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2395
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2396
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
  2397
  by (simp add: trunc_bl2bin)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2398
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2399
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
  2400
  apply (rule trans)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2401
   prefer 2
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2402
   apply (rule trunc_bl2bin [symmetric])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2403
  apply (cases "k \<le> length bl")
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2404
   apply auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2405
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2406
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2407
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
  2408
  apply (rule nth_equalityI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2409
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2410
  apply (clarsimp simp add: nth_bin_to_bl nth_rest_power_bin)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2411
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2412
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2413
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
  2414
  by (induct xs arbitrary: w) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2415
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2416
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
  2417
  unfolding bl_to_bin_def by (erule last_bin_last')
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2418
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2419
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
  2420
  by (simp add: bin_to_bl_def) (auto simp: bin_to_bl_aux_alt)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2421
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2422
lemma drop_bin2bl_aux:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2423
  "drop m (bin_to_bl_aux n bin bs) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2424
    bin_to_bl_aux (n - m) bin (drop (m - n) bs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2425
  apply (induct n arbitrary: m bin bs, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2426
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2427
  apply (case_tac bin rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2428
  apply (case_tac "m \<le> n", simp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2429
  apply (case_tac "m - n", simp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2430
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2431
  apply (rule_tac f = "\<lambda>nat. drop nat bs" in arg_cong)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2432
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2433
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2434
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2435
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
  2436
  by (simp add: bin_to_bl_def drop_bin2bl_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2437
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2438
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
  2439
  apply (induct m arbitrary: w bs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2440
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2441
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2442
  apply (simp add: bin_to_bl_aux_alt)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2443
  apply (simp add: bin_to_bl_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2444
  apply (simp add: bin_to_bl_aux_alt)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2445
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2446
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2447
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
  2448
  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
  2449
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2450
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
  2451
  apply (induct n arbitrary: b c)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2452
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2453
  apply (clarsimp simp: Let_def split: prod.split_asm)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2454
  apply (simp add: bin_to_bl_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2455
  apply (simp add: take_bin2bl_lem)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2456
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2457
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2458
lemma bin_split_take1:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2459
  "k = m + n \<Longrightarrow> bin_split n c = (a, b) \<Longrightarrow> bin_to_bl m a = take m (bin_to_bl k c)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2460
  by (auto elim: bin_split_take)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2461
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2462
lemma takefill_bintrunc: "takefill False n bl = rev (bin_to_bl n (bl_to_bin (rev bl)))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2463
  apply (rule nth_equalityI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2464
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2465
  apply (clarsimp simp: nth_takefill nth_rev nth_bin_to_bl bin_nth_of_bl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2466
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2467
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2468
lemma bl_bin_bl_rtf: "bin_to_bl n (bl_to_bin bl) = rev (takefill False n (rev bl))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2469
  by (simp add: takefill_bintrunc)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2470
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2471
lemma bl_bin_bl_rep_drop:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2472
  "bin_to_bl n (bl_to_bin bl) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2473
    replicate (n - length bl) False @ drop (length bl - n) bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2474
  by (simp add: bl_bin_bl_rtf takefill_alt rev_take)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2475
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2476
lemma bl_to_bin_aux_cat:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2477
  "\<And>nv v. bl_to_bin_aux bs (bin_cat w nv v) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2478
    bin_cat w (nv + length bs) (bl_to_bin_aux bs v)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2479
  by (induct bs) (simp, simp add: bin_cat_Suc_Bit [symmetric] del: bin_cat.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2480
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2481
lemma bin_to_bl_aux_cat:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2482
  "\<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
  2483
    bin_to_bl_aux nv v (bin_to_bl_aux nw w bs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2484
  by (induct nw) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2485
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2486
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
  2487
  using bl_to_bin_aux_cat [where nv = "0" and v = "0"]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2488
  by (simp add: bl_to_bin_def [symmetric])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2489
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2490
lemma bin_to_bl_cat:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2491
  "bin_to_bl (nv + nw) (bin_cat v nw w) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2492
    bin_to_bl_aux nv v (bin_to_bl nw w)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2493
  by (simp add: bin_to_bl_def bin_to_bl_aux_cat)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2494
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2495
lemmas bl_to_bin_aux_app_cat =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2496
  trans [OF bl_to_bin_aux_append bl_to_bin_aux_alt]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2497
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2498
lemmas bin_to_bl_aux_cat_app =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2499
  trans [OF bin_to_bl_aux_cat bin_to_bl_aux_alt]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2500
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2501
lemma bl_to_bin_app_cat:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2502
  "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
  2503
  by (simp only: bl_to_bin_aux_app_cat bl_to_bin_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2504
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2505
lemma bin_to_bl_cat_app:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2506
  "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
  2507
  by (simp only: bin_to_bl_def bin_to_bl_aux_cat_app)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2508
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2509
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
  2510
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
  2511
  by (simp add: bl_to_bin_app_cat)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2512
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2513
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
  2514
  apply (unfold bl_to_bin_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2515
  apply (induct n)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2516
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2517
  apply (simp only: Suc_eq_plus1 replicate_add append_Cons [symmetric] bl_to_bin_aux_append)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2518
  apply (simp add: Bit_B0_2t Bit_B1_2t)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2519
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2520
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2521
primrec rbl_succ :: "bool list \<Rightarrow> bool list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2522
  where
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2523
    Nil: "rbl_succ Nil = Nil"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2524
  | 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
  2525
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2526
primrec rbl_pred :: "bool list \<Rightarrow> bool list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2527
  where
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2528
    Nil: "rbl_pred Nil = Nil"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2529
  | 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
  2530
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2531
primrec rbl_add :: "bool list \<Rightarrow> bool list \<Rightarrow> bool list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2532
  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
  2533
    Nil: "rbl_add Nil x = Nil"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2534
  | Cons: "rbl_add (y # ys) x =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2535
      (let ws = rbl_add ys (tl x)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2536
       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
  2537
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2538
primrec rbl_mult :: "bool list \<Rightarrow> bool list \<Rightarrow> bool list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2539
  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
  2540
    Nil: "rbl_mult Nil x = Nil"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2541
  | Cons: "rbl_mult (y # ys) x =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2542
      (let ws = False # rbl_mult ys x
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2543
       in if y then rbl_add ws x else ws)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2544
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2545
lemma size_rbl_pred: "length (rbl_pred bl) = length bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2546
  by (induct bl) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2547
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2548
lemma size_rbl_succ: "length (rbl_succ bl) = length bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2549
  by (induct bl) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2550
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2551
lemma size_rbl_add: "length (rbl_add bl cl) = length bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2552
  by (induct bl arbitrary: cl) (auto simp: Let_def size_rbl_succ)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2553
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2554
lemma size_rbl_mult: "length (rbl_mult bl cl) = length bl"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2555
  by (induct bl arbitrary: cl) (auto simp add: Let_def size_rbl_add)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2556
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2557
lemmas rbl_sizes [simp] =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2558
  size_rbl_pred size_rbl_succ size_rbl_add size_rbl_mult
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2559
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2560
lemmas rbl_Nils =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2561
  rbl_pred.Nil rbl_succ.Nil rbl_add.Nil rbl_mult.Nil
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2562
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2563
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
  2564
  apply (induct bla arbitrary: blb)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2565
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2566
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2567
  apply (case_tac blb, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2568
  apply (clarsimp simp: Let_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2569
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2570
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2571
lemma rbl_add_take2:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2572
  "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
  2573
  apply (induct bla arbitrary: blb)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2574
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2575
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2576
  apply (case_tac blb, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2577
  apply (clarsimp simp: Let_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2578
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2579
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2580
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
  2581
  apply (induct bla arbitrary: blb)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2582
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2583
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2584
  apply (case_tac blb, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2585
  apply (clarsimp simp: Let_def rbl_add_app2)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2586
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2587
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2588
lemma rbl_mult_take2:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2589
  "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
  2590
  apply (rule trans)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2591
   apply (rule rbl_mult_app2 [symmetric])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2592
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2593
  apply (rule_tac f = "rbl_mult bla" in arg_cong)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2594
  apply (rule append_take_drop_id)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2595
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2596
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2597
lemma rbl_add_split:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2598
  "P (rbl_add (y # ys) (x # xs)) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2599
    (\<forall>ws. length ws = length ys \<longrightarrow> ws = rbl_add ys xs \<longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2600
      (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
  2601
      (\<not> y \<longrightarrow> P (x # ws)))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2602
  by (cases y) (auto simp: Let_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2603
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2604
lemma rbl_mult_split:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2605
  "P (rbl_mult (y # ys) xs) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2606
    (\<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
  2607
      (y \<longrightarrow> P (rbl_add ws xs)) \<and> (\<not> y \<longrightarrow> P ws))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2608
  by (auto simp: Let_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2609
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2610
lemma rbl_pred: "rbl_pred (rev (bin_to_bl n bin)) = rev (bin_to_bl n (bin - 1))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2611
  apply (unfold bin_to_bl_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2612
  apply (induct n arbitrary: bin)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2613
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2614
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2615
  apply (case_tac bin rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2616
  apply (case_tac b)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2617
   apply (clarsimp simp: bin_to_bl_aux_alt)+
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2618
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2619
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2620
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
  2621
  apply (unfold bin_to_bl_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2622
  apply (induct n arbitrary: bin)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2623
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2624
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2625
  apply (case_tac bin rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2626
  apply (case_tac b)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2627
   apply (clarsimp simp: bin_to_bl_aux_alt)+
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2628
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2629
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2630
lemma rbl_add:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2631
  "\<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
  2632
    rev (bin_to_bl n (bina + binb))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2633
  apply (unfold bin_to_bl_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2634
  apply (induct n)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2635
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2636
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2637
  apply (case_tac bina rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2638
  apply (case_tac binb rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2639
  apply (case_tac b)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2640
   apply (case_tac [!] "ba")
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2641
     apply (auto simp: rbl_succ bin_to_bl_aux_alt Let_def ac_simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2642
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2643
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2644
lemma rbl_add_long:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2645
  "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
  2646
    rev (bin_to_bl n (bina + binb))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2647
  apply (rule box_equals [OF _ rbl_add_take2 rbl_add])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2648
   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
  2649
   apply (rule rev_swap [THEN iffD1])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2650
   apply (simp add: rev_take drop_bin2bl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2651
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2652
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2653
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2654
lemma rbl_mult_gt1:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2655
  "m \<ge> length bl \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2656
    rbl_mult bl (rev (bin_to_bl m binb)) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2657
    rbl_mult bl (rev (bin_to_bl (length bl) binb))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2658
  apply (rule trans)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2659
   apply (rule rbl_mult_take2 [symmetric])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2660
   apply simp_all
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2661
  apply (rule_tac f = "rbl_mult bl" in arg_cong)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2662
  apply (rule rev_swap [THEN iffD1])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2663
  apply (simp add: rev_take drop_bin2bl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2664
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2665
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2666
lemma rbl_mult_gt:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2667
  "m > n \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2668
    rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl m binb)) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2669
    rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2670
  by (auto intro: trans [OF rbl_mult_gt1])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2671
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2672
lemmas rbl_mult_Suc = lessI [THEN rbl_mult_gt]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2673
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2674
lemma rbbl_Cons: "b # rev (bin_to_bl n x) = rev (bin_to_bl (Suc n) (x BIT b))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2675
  by (simp add: bin_to_bl_def) (simp add: bin_to_bl_aux_alt)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2676
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2677
lemma rbl_mult:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2678
  "rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb)) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2679
    rev (bin_to_bl n (bina * binb))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2680
  apply (induct n arbitrary: bina binb)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2681
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2682
  apply (unfold bin_to_bl_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2683
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2684
  apply (case_tac bina rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2685
  apply (case_tac binb rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2686
  apply (case_tac b)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2687
   apply (case_tac [!] "ba")
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2688
     apply (auto simp: bin_to_bl_aux_alt Let_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2689
     apply (auto simp: rbbl_Cons rbl_mult_Suc rbl_add)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2690
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2691
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2692
lemma sclem: "size (concat (map (bin_to_bl n) xs)) = length xs * n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2693
  by (induct xs) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2694
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2695
lemma bin_cat_foldl_lem:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2696
  "foldl (\<lambda>u. bin_cat u n) x xs =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2697
    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
  2698
  apply (induct xs arbitrary: x)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2699
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2700
  apply (simp (no_asm))
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2701
  apply (frule asm_rl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2702
  apply (drule meta_spec)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2703
  apply (erule trans)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2704
  apply (drule_tac x = "bin_cat y n a" in meta_spec)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2705
  apply (simp add: bin_cat_assoc_sym min.absorb2)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2706
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2707
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2708
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
  2709
  apply (unfold bin_rcat_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2710
  apply (rule sym)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2711
  apply (induct wl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2712
   apply (auto simp add: bl_to_bin_append)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2713
  apply (simp add: bl_to_bin_aux_alt sclem)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2714
  apply (simp add: bin_cat_foldl_lem [symmetric])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2715
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2716
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2717
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
  2718
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
  2719
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2720
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
  2721
by(cases "bs = []")(simp_all add: bl_to_bin_def butlast_rest_bl2bin_aux)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2722
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2723
lemma bl_xor_aux_bin:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2724
  "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
  2725
    bin_to_bl_aux n (v XOR w) (map2 (\<lambda>x y. x \<noteq> y) bs cs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2726
  apply (induct n arbitrary: v w bs cs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2727
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2728
  apply (case_tac v rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2729
  apply (case_tac w rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2730
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2731
  apply (case_tac b)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2732
   apply auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2733
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2734
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2735
lemma bl_or_aux_bin:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2736
  "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
  2737
    bin_to_bl_aux n (v OR w) (map2 (\<or>) bs cs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2738
  apply (induct n arbitrary: v w bs cs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2739
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2740
  apply (case_tac v rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2741
  apply (case_tac w rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2742
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2743
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2744
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2745
lemma bl_and_aux_bin:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2746
  "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
  2747
    bin_to_bl_aux n (v AND w) (map2 (\<and>) bs cs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2748
  apply (induct n arbitrary: v w bs cs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2749
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2750
  apply (case_tac v rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2751
  apply (case_tac w rule: bin_exhaust)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2752
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2753
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2754
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2755
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
  2756
  by (induct n arbitrary: w cs) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2757
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2758
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
  2759
  by (simp add: bin_to_bl_def bl_not_aux_bin)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2760
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2761
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
  2762
  by (simp add: bin_to_bl_def bl_and_aux_bin)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2763
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2764
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
  2765
  by (simp add: bin_to_bl_def bl_or_aux_bin)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2766
70193
49a65e3f04c9 consolidated map2 clones
haftmann
parents: 70192
diff changeset
  2767
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
  2768
  using bl_xor_aux_bin by (simp add: bin_to_bl_def)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  2769
70169
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  2770
end