src/HOL/ex/Word.thy
author haftmann
Sun, 17 Nov 2019 20:44:35 +0000
changeset 71138 9de7f1067520
parent 71095 038727567817
child 71181 8331063570d6
permissions -rw-r--r--
strengthened type class for bit operations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
     1
(*  Author:  Florian Haftmann, TUM
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
     2
*)
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
     3
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
     4
section \<open>Proof of concept for algebraically founded bit word types\<close>
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
     5
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
     6
theory Word
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
     7
  imports
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
     8
    Main
66453
cc19f7ca2ed6 session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents: 64593
diff changeset
     9
    "HOL-Library.Type_Length"
71095
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
    10
    "HOL-ex.Bit_Operations"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    11
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    12
70925
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
    13
subsection \<open>Preliminaries\<close>
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
    14
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
    15
lemma length_not_greater_eq_2_iff [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
    16
  \<open>\<not> 2 \<le> LENGTH('a::len) \<longleftrightarrow> LENGTH('a) = 1\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
    17
  by (auto simp add: not_le dest: less_2_cases)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
    18
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    19
lemma take_bit_uminus:
70171
haftmann
parents: 67961
diff changeset
    20
  "take_bit n (- (take_bit n k)) = take_bit n (- k)" for k :: int
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    21
  by (simp add: take_bit_eq_mod mod_minus_eq)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    22
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    23
lemma take_bit_minus:
70171
haftmann
parents: 67961
diff changeset
    24
  "take_bit n (take_bit n k - take_bit n l) = take_bit n (k - l)" for k l :: int
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    25
  by (simp add: take_bit_eq_mod mod_diff_eq)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    26
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    27
lemma take_bit_nonnegative [simp]:
70171
haftmann
parents: 67961
diff changeset
    28
  "take_bit n k \<ge> 0" for k :: int
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    29
  by (simp add: take_bit_eq_mod)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    30
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    31
definition signed_take_bit :: "nat \<Rightarrow> int \<Rightarrow> int"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    32
  where signed_take_bit_eq_take_bit:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    33
    "signed_take_bit n k = take_bit (Suc n) (k + 2 ^ n) - 2 ^ n"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    34
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    35
lemma signed_take_bit_eq_take_bit':
70171
haftmann
parents: 67961
diff changeset
    36
  "signed_take_bit (n - Suc 0) k = take_bit n (k + 2 ^ (n - 1)) - 2 ^ (n - 1)" if "n > 0"
haftmann
parents: 67961
diff changeset
    37
  using that by (simp add: signed_take_bit_eq_take_bit)
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
    38
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    39
lemma signed_take_bit_0 [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    40
  "signed_take_bit 0 k = - (k mod 2)"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    41
proof (cases "even k")
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    42
  case True
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    43
  then have "odd (k + 1)"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    44
    by simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    45
  then have "(k + 1) mod 2 = 1"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    46
    by (simp add: even_iff_mod_2_eq_zero)
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    47
  with True show ?thesis
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    48
    by (simp add: signed_take_bit_eq_take_bit)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    49
next
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    50
  case False
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    51
  then show ?thesis
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    52
    by (simp add: signed_take_bit_eq_take_bit odd_iff_mod_2_eq_one)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    53
qed
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    54
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    55
lemma signed_take_bit_Suc [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    56
  "signed_take_bit (Suc n) k = signed_take_bit n (k div 2) * 2 + k mod 2"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    57
  by (simp add: odd_iff_mod_2_eq_one signed_take_bit_eq_take_bit algebra_simps)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    58
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    59
lemma signed_take_bit_of_0 [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    60
  "signed_take_bit n 0 = 0"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    61
  by (simp add: signed_take_bit_eq_take_bit take_bit_eq_mod)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    62
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    63
lemma signed_take_bit_of_minus_1 [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    64
  "signed_take_bit n (- 1) = - 1"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    65
  by (induct n) simp_all
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    66
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    67
lemma signed_take_bit_eq_iff_take_bit_eq:
70171
haftmann
parents: 67961
diff changeset
    68
  "signed_take_bit (n - Suc 0) k = signed_take_bit (n - Suc 0) l \<longleftrightarrow> take_bit n k = take_bit n l" (is "?P \<longleftrightarrow> ?Q")
haftmann
parents: 67961
diff changeset
    69
  if "n > 0"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    70
proof -
70171
haftmann
parents: 67961
diff changeset
    71
  from that obtain m where m: "n = Suc m"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    72
    by (cases n) auto
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    73
  show ?thesis
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
    74
  proof
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    75
    assume ?Q
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    76
    have "take_bit (Suc m) (k + 2 ^ m) =
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    77
      take_bit (Suc m) (take_bit (Suc m) k + take_bit (Suc m) (2 ^ m))"
67961
9c31678d2139 even more on bit operations
haftmann
parents: 67907
diff changeset
    78
      by (simp only: take_bit_add)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    79
    also have "\<dots> =
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    80
      take_bit (Suc m) (take_bit (Suc m) l + take_bit (Suc m) (2 ^ m))"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    81
      by (simp only: \<open>?Q\<close> m [symmetric])
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    82
    also have "\<dots> = take_bit (Suc m) (l + 2 ^ m)"
67961
9c31678d2139 even more on bit operations
haftmann
parents: 67907
diff changeset
    83
      by (simp only: take_bit_add)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    84
    finally show ?P
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    85
      by (simp only: signed_take_bit_eq_take_bit m) simp
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    86
  next
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    87
    assume ?P
70171
haftmann
parents: 67961
diff changeset
    88
    with that have "(k + 2 ^ (n - Suc 0)) mod 2 ^ n = (l + 2 ^ (n - Suc 0)) mod 2 ^ n"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    89
      by (simp add: signed_take_bit_eq_take_bit' take_bit_eq_mod)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    90
    then have "(i + (k + 2 ^ (n - Suc 0))) mod 2 ^ n = (i + (l + 2 ^ (n - Suc 0))) mod 2 ^ n" for i
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    91
      by (metis mod_add_eq)
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    92
    then have "k mod 2 ^ n = l mod 2 ^ n"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    93
      by (metis add_diff_cancel_right' uminus_add_conv_diff)
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    94
    then show ?Q
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    95
      by (simp add: take_bit_eq_mod)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    96
  qed
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
    97
qed
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    98
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    99
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   100
subsection \<open>Bit strings as quotient type\<close>
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   101
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   102
subsubsection \<open>Basic properties\<close>
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   103
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   104
quotient_type (overloaded) 'a word = int / "\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a::len0) l"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   105
  by (auto intro!: equivpI reflpI sympI transpI)
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   106
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   107
instantiation word :: (len0) "{semiring_numeral, comm_semiring_0, comm_ring}"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   108
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   109
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   110
lift_definition zero_word :: "'a word"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   111
  is 0
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   112
  .
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   113
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   114
lift_definition one_word :: "'a word"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   115
  is 1
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   116
  .
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   117
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   118
lift_definition plus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   119
  is plus
67961
9c31678d2139 even more on bit operations
haftmann
parents: 67907
diff changeset
   120
  by (subst take_bit_add [symmetric]) (simp add: take_bit_add)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   121
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   122
lift_definition uminus_word :: "'a word \<Rightarrow> 'a word"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   123
  is uminus
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   124
  by (subst take_bit_uminus [symmetric]) (simp add: take_bit_uminus)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   125
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   126
lift_definition minus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   127
  is minus
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   128
  by (subst take_bit_minus [symmetric]) (simp add: take_bit_minus)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   129
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   130
lift_definition times_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   131
  is times
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   132
  by (auto simp add: take_bit_eq_mod intro: mod_mult_cong)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   133
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   134
instance
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   135
  by standard (transfer; simp add: algebra_simps)+
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   136
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   137
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   138
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   139
instance word :: (len) comm_ring_1
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   140
  by standard (transfer; simp)+
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   141
70903
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   142
quickcheck_generator word
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   143
  constructors:
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   144
    "zero_class.zero :: ('a::len0) word",
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   145
    "numeral :: num \<Rightarrow> ('a::len0) word",
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   146
    "uminus :: ('a::len0) word \<Rightarrow> ('a::len0) word"
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   147
70973
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   148
context
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   149
  includes lifting_syntax
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   150
  notes power_transfer [transfer_rule]
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   151
begin
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   152
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   153
lemma power_transfer_word [transfer_rule]:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   154
  \<open>(pcr_word ===> (=) ===> pcr_word) (^) (^)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   155
  by transfer_prover
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   156
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   157
end
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   158
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   159
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   160
subsubsection \<open>Conversions\<close>
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   161
70927
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   162
context
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   163
  includes lifting_syntax
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   164
  notes transfer_rule_numeral [transfer_rule]
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   165
    transfer_rule_of_nat [transfer_rule]
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   166
    transfer_rule_of_int [transfer_rule]
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   167
begin
70348
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   168
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   169
lemma [transfer_rule]:
70927
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   170
  "((=) ===> (pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool)) numeral numeral"
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   171
  by transfer_prover
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   172
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   173
lemma [transfer_rule]:
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   174
  "((=) ===> pcr_word) int of_nat"
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   175
  by transfer_prover
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   176
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   177
lemma [transfer_rule]:
70927
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   178
  "((=) ===> pcr_word) (\<lambda>k. k) of_int"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   179
proof -
70927
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   180
  have "((=) ===> pcr_word) of_int of_int"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   181
    by transfer_prover
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   182
  then show ?thesis by (simp add: id_def)
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   183
qed
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   184
70927
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   185
end
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   186
70973
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   187
lemma abs_word_eq:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   188
  "abs_word = of_int"
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   189
  by (rule ext) (transfer, rule)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   190
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   191
context semiring_1
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   192
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   193
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   194
lift_definition unsigned :: "'b::len0 word \<Rightarrow> 'a"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   195
  is "of_nat \<circ> nat \<circ> take_bit LENGTH('b)"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   196
  by simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   197
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   198
lemma unsigned_0 [simp]:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   199
  "unsigned 0 = 0"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   200
  by transfer simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   201
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   202
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   203
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   204
context semiring_char_0
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   205
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   206
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   207
lemma word_eq_iff_unsigned:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   208
  "a = b \<longleftrightarrow> unsigned a = unsigned b"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   209
  by safe (transfer; simp add: eq_nat_nat_iff)
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   210
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   211
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   212
70903
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   213
instantiation word :: (len0) equal
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   214
begin
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   215
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   216
definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   217
  where "equal_word a b \<longleftrightarrow> (unsigned a :: int) = unsigned b"
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   218
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   219
instance proof
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   220
  fix a b :: "'a word"
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   221
  show "HOL.equal a b \<longleftrightarrow> a = b"
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   222
    using word_eq_iff_unsigned [of a b] by (auto simp add: equal_word_def)
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   223
qed
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   224
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   225
end
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   226
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   227
context ring_1
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   228
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   229
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   230
lift_definition signed :: "'b::len word \<Rightarrow> 'a"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   231
  is "of_int \<circ> signed_take_bit (LENGTH('b) - 1)"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   232
  by (simp add: signed_take_bit_eq_iff_take_bit_eq [symmetric])
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   233
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   234
lemma signed_0 [simp]:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   235
  "signed 0 = 0"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   236
  by transfer simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   237
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   238
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   239
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   240
lemma unsigned_of_nat [simp]:
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   241
  "unsigned (of_nat n :: 'a word) = take_bit LENGTH('a::len) n"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   242
  by transfer (simp add: nat_eq_iff take_bit_eq_mod zmod_int)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   243
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   244
lemma of_nat_unsigned [simp]:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   245
  "of_nat (unsigned a) = a"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   246
  by transfer simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   247
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   248
lemma of_int_unsigned [simp]:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   249
  "of_int (unsigned a) = a"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   250
  by transfer simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   251
70973
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   252
lemma unsigned_nat_less:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   253
  \<open>unsigned a < (2 ^ LENGTH('a) :: nat)\<close> for a :: \<open>'a::len0 word\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   254
  by transfer (simp add: take_bit_eq_mod)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   255
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   256
lemma unsigned_int_less:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   257
  \<open>unsigned a < (2 ^ LENGTH('a) :: int)\<close> for a :: \<open>'a::len0 word\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   258
  by transfer (simp add: take_bit_eq_mod)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   259
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   260
context ring_char_0
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   261
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   262
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   263
lemma word_eq_iff_signed:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   264
  "a = b \<longleftrightarrow> signed a = signed b"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   265
  by safe (transfer; auto simp add: signed_take_bit_eq_iff_take_bit_eq)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   266
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   267
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   268
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   269
lemma signed_of_int [simp]:
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   270
  "signed (of_int k :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) k"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   271
  by transfer simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   272
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   273
lemma of_int_signed [simp]:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   274
  "of_int (signed a) = a"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   275
  by transfer (simp add: signed_take_bit_eq_take_bit take_bit_eq_mod mod_simps)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   276
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   277
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   278
subsubsection \<open>Properties\<close>
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   279
70973
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   280
lemma length_cases:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   281
  obtains (triv) "LENGTH('a::len) = 1" "take_bit LENGTH('a) 2 = (0 :: int)"
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   282
    | (take_bit_2) "take_bit LENGTH('a) 2 = (2 :: int)"
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   283
proof (cases "LENGTH('a) \<ge> 2")
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   284
  case False
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   285
  then have "LENGTH('a) = 1"
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   286
    by (auto simp add: not_le dest: less_2_cases)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   287
  then have "take_bit LENGTH('a) 2 = (0 :: int)"
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   288
    by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   289
  with \<open>LENGTH('a) = 1\<close> triv show ?thesis
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   290
    by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   291
next
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   292
  case True
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   293
  then obtain n where "LENGTH('a) = Suc (Suc n)"
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   294
    by (auto dest: le_Suc_ex)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   295
  then have "take_bit LENGTH('a) 2 = (2 :: int)"
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   296
    by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   297
  with take_bit_2 show ?thesis
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   298
    by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   299
qed
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   300
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   301
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   302
subsubsection \<open>Division\<close>
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   303
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   304
instantiation word :: (len0) modulo
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   305
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   306
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   307
lift_definition divide_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   308
  is "\<lambda>a b. take_bit LENGTH('a) a div take_bit LENGTH('a) b"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   309
  by simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   310
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   311
lift_definition modulo_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   312
  is "\<lambda>a b. take_bit LENGTH('a) a mod take_bit LENGTH('a) b"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   313
  by simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   314
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   315
instance ..
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   316
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   317
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   318
70973
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   319
lemma zero_word_div_eq [simp]:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   320
  \<open>0 div a = 0\<close> for a :: \<open>'a::len0 word\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   321
  by transfer simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   322
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   323
lemma div_zero_word_eq [simp]:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   324
  \<open>a div 0 = 0\<close> for a :: \<open>'a::len0 word\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   325
  by transfer simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   326
70927
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   327
context
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   328
  includes lifting_syntax
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   329
begin
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   330
70348
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   331
lemma [transfer_rule]:
70927
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   332
  "(pcr_word ===> (\<longleftrightarrow>)) even ((dvd) 2 :: 'a::len word \<Rightarrow> bool)"
70348
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   333
proof -
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   334
  have even_word_unfold: "even k \<longleftrightarrow> (\<exists>l. take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l))" (is "?P \<longleftrightarrow> ?Q")
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   335
    for k :: int
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   336
  proof
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   337
    assume ?P
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   338
    then show ?Q
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   339
      by auto
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   340
  next
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   341
    assume ?Q
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   342
    then obtain l where "take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l)" ..
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   343
    then have "even (take_bit LENGTH('a) k)"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   344
      by simp
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   345
    then show ?P
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   346
      by simp
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   347
  qed
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   348
  show ?thesis by (simp only: even_word_unfold [abs_def] dvd_def [where ?'a = "'a word", abs_def])
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   349
    transfer_prover
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   350
qed
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   351
70927
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   352
end
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   353
70348
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   354
instance word :: (len) semiring_modulo
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   355
proof
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   356
  show "a div b * b + a mod b = a" for a b :: "'a word"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   357
  proof transfer
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   358
    fix k l :: int
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   359
    define r :: int where "r = 2 ^ LENGTH('a)"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   360
    then have r: "take_bit LENGTH('a) k = k mod r" for k
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   361
      by (simp add: take_bit_eq_mod)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   362
    have "k mod r = ((k mod r) div (l mod r) * (l mod r)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   363
      + (k mod r) mod (l mod r)) mod r"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   364
      by (simp add: div_mult_mod_eq)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   365
    also have "... = (((k mod r) div (l mod r) * (l mod r)) mod r
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   366
      + (k mod r) mod (l mod r)) mod r"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   367
      by (simp add: mod_add_left_eq)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   368
    also have "... = (((k mod r) div (l mod r) * l) mod r
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   369
      + (k mod r) mod (l mod r)) mod r"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   370
      by (simp add: mod_mult_right_eq)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   371
    finally have "k mod r = ((k mod r) div (l mod r) * l
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   372
      + (k mod r) mod (l mod r)) mod r"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   373
      by (simp add: mod_simps)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   374
    with r show "take_bit LENGTH('a) (take_bit LENGTH('a) k div take_bit LENGTH('a) l * l
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   375
      + take_bit LENGTH('a) k mod take_bit LENGTH('a) l) = take_bit LENGTH('a) k"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   376
      by simp
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   377
  qed
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   378
qed
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   379
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   380
instance word :: (len) semiring_parity
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   381
proof
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   382
  show "\<not> 2 dvd (1::'a word)"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   383
    by transfer simp
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   384
  show even_iff_mod_2_eq_0: "2 dvd a \<longleftrightarrow> a mod 2 = 0"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   385
    for a :: "'a word"
70973
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   386
    by (transfer; cases rule: length_cases [where ?'a = 'a]) (simp_all add: mod_2_eq_odd)
70348
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   387
  show "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   388
    for a :: "'a word"
70973
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   389
    by (transfer; cases rule: length_cases [where ?'a = 'a]) (simp_all add: mod_2_eq_odd)
70348
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   390
qed
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   391
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   392
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   393
subsubsection \<open>Orderings\<close>
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   394
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   395
instantiation word :: (len0) linorder
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   396
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   397
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   398
lift_definition less_eq_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   399
  is "\<lambda>a b. take_bit LENGTH('a) a \<le> take_bit LENGTH('a) b"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   400
  by simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   401
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   402
lift_definition less_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   403
  is "\<lambda>a b. take_bit LENGTH('a) a < take_bit LENGTH('a) b"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   404
  by simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   405
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   406
instance
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   407
  by standard (transfer; auto)+
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   408
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   409
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   410
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   411
context linordered_semidom
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   412
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   413
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   414
lemma word_less_eq_iff_unsigned:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   415
  "a \<le> b \<longleftrightarrow> unsigned a \<le> unsigned b"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   416
  by (transfer fixing: less_eq) (simp add: nat_le_eq_zle)
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   417
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   418
lemma word_less_iff_unsigned:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   419
  "a < b \<longleftrightarrow> unsigned a < unsigned b"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   420
  by (transfer fixing: less) (auto dest: preorder_class.le_less_trans [OF take_bit_nonnegative])
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   421
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   422
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   423
70973
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   424
lemma word_greater_zero_iff:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   425
  \<open>a > 0 \<longleftrightarrow> a \<noteq> 0\<close> for a :: \<open>'a::len0 word\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   426
  by transfer (simp add: less_le)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   427
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   428
lemma of_nat_word_eq_iff:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   429
  \<open>of_nat m = (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m = take_bit LENGTH('a) n\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   430
  by transfer (simp add: take_bit_of_nat)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   431
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   432
lemma of_nat_word_less_eq_iff:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   433
  \<open>of_nat m \<le> (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m \<le> take_bit LENGTH('a) n\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   434
  by transfer (simp add: take_bit_of_nat)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   435
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   436
lemma of_nat_word_less_iff:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   437
  \<open>of_nat m < (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m < take_bit LENGTH('a) n\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   438
  by transfer (simp add: take_bit_of_nat)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   439
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   440
lemma of_nat_word_eq_0_iff:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   441
  \<open>of_nat n = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd n\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   442
  using of_nat_word_eq_iff [where ?'a = 'a, of n 0] by (simp add: take_bit_eq_0_iff)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   443
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   444
lemma of_int_word_eq_iff:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   445
  \<open>of_int k = (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   446
  by transfer rule
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   447
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   448
lemma of_int_word_less_eq_iff:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   449
  \<open>of_int k \<le> (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k \<le> take_bit LENGTH('a) l\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   450
  by transfer rule
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   451
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   452
lemma of_int_word_less_iff:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   453
  \<open>of_int k < (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k < take_bit LENGTH('a) l\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   454
  by transfer rule
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   455
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   456
lemma of_int_word_eq_0_iff:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   457
  \<open>of_int k = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd k\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   458
  using of_int_word_eq_iff [where ?'a = 'a, of k 0] by (simp add: take_bit_eq_0_iff)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   459
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   460
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   461
subsection \<open>Bit structure on \<^typ>\<open>'a word\<close>\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   462
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   463
lemma word_bit_induct [case_names zero even odd]:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   464
  \<open>P a\<close> if word_zero: \<open>P 0\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   465
    and word_even: \<open>\<And>a. P a \<Longrightarrow> 0 < a \<Longrightarrow> a < 2 ^ (LENGTH('a) - 1) \<Longrightarrow> P (2 * a)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   466
    and word_odd: \<open>\<And>a. P a \<Longrightarrow> a < 2 ^ (LENGTH('a) - 1) \<Longrightarrow> P (1 + 2 * a)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   467
  for P and a :: \<open>'a::len word\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   468
proof -
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   469
  define m :: nat where \<open>m = LENGTH('a) - 1\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   470
  then have l: \<open>LENGTH('a) = Suc m\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   471
    by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   472
  define n :: nat where \<open>n = unsigned a\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   473
  then have \<open>n < 2 ^ LENGTH('a)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   474
    by (simp add: unsigned_nat_less)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   475
  then have \<open>n < 2 * 2 ^ m\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   476
    by (simp add: l)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   477
  then have \<open>P (of_nat n)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   478
  proof (induction n rule: nat_bit_induct)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   479
    case zero
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   480
    show ?case
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   481
      by simp (rule word_zero)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   482
  next
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   483
    case (even n)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   484
    then have \<open>n < 2 ^ m\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   485
      by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   486
    with even.IH have \<open>P (of_nat n)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   487
      by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   488
    moreover from \<open>n < 2 ^ m\<close> even.hyps have \<open>0 < (of_nat n :: 'a word)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   489
      by (auto simp add: word_greater_zero_iff of_nat_word_eq_0_iff l)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   490
    moreover from \<open>n < 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a) - 1)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   491
      using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>]
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   492
      by (cases \<open>m = 0\<close>) (simp_all add: not_less take_bit_eq_self ac_simps l)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   493
    ultimately have \<open>P (2 * of_nat n)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   494
      by (rule word_even)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   495
    then show ?case
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   496
      by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   497
  next
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   498
    case (odd n)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   499
    then have \<open>Suc n \<le> 2 ^ m\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   500
      by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   501
    with odd.IH have \<open>P (of_nat n)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   502
      by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   503
    moreover from \<open>Suc n \<le> 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a) - 1)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   504
      using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>]
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   505
      by (cases \<open>m = 0\<close>) (simp_all add: not_less take_bit_eq_self ac_simps l)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   506
    ultimately have \<open>P (1 + 2 * of_nat n)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   507
      by (rule word_odd)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   508
    then show ?case
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   509
      by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   510
  qed
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   511
  then show ?thesis
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   512
    by (simp add: n_def)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   513
qed
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   514
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   515
lemma bit_word_half_eq:
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   516
  \<open>(of_bool b + a * 2) div 2 = a\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   517
    if \<open>a < 2 ^ (LENGTH('a) - Suc 0)\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   518
    for a :: \<open>'a::len word\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   519
proof (cases rule: length_cases [where ?'a = 'a])
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   520
  case triv
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   521
  have \<open>of_bool (odd k) < (1 :: int) \<longleftrightarrow> even k\<close> for k :: int
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   522
    by auto
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   523
  with triv that show ?thesis
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   524
    by (auto; transfer) simp_all
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   525
next
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   526
  case take_bit_2
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   527
  obtain n where length: \<open>LENGTH('a) = Suc n\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   528
    by (cases \<open>LENGTH('a)\<close>) simp_all
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   529
  show ?thesis proof (cases b)
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   530
    case False
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   531
    moreover have \<open>a * 2 div 2 = a\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   532
    using that proof transfer
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   533
      fix k :: int
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   534
      from length have \<open>k * 2 mod 2 ^ LENGTH('a) = (k mod 2 ^ n) * 2\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   535
        by simp
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   536
      moreover assume \<open>take_bit LENGTH('a) k < take_bit LENGTH('a) (2 ^ (LENGTH('a) - Suc 0))\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   537
      with \<open>LENGTH('a) = Suc n\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   538
      have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   539
        by (simp add: take_bit_eq_mod divmod_digit_0)
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   540
      ultimately have \<open>take_bit LENGTH('a) (k * 2) = take_bit LENGTH('a) k * 2\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   541
        by (simp add: take_bit_eq_mod)
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   542
      with take_bit_2 show \<open>take_bit LENGTH('a) (take_bit LENGTH('a) (k * 2) div take_bit LENGTH('a) 2)
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   543
        = take_bit LENGTH('a) k\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   544
        by simp
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   545
    qed
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   546
    ultimately show ?thesis
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   547
      by simp
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   548
  next
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   549
    case True
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   550
    moreover have \<open>(1 + a * 2) div 2 = a\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   551
    using that proof transfer
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   552
      fix k :: int
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   553
      from length have \<open>(1 + k * 2) mod 2 ^ LENGTH('a) = 1 + (k mod 2 ^ n) * 2\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   554
        using pos_zmod_mult_2 [of \<open>2 ^ n\<close> k] by (simp add: ac_simps)
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   555
      moreover assume \<open>take_bit LENGTH('a) k < take_bit LENGTH('a) (2 ^ (LENGTH('a) - Suc 0))\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   556
      with \<open>LENGTH('a) = Suc n\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   557
      have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   558
        by (simp add: take_bit_eq_mod divmod_digit_0)
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   559
      ultimately have \<open>take_bit LENGTH('a) (1 + k * 2) = 1 + take_bit LENGTH('a) k * 2\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   560
        by (simp add: take_bit_eq_mod)
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   561
      with take_bit_2 show \<open>take_bit LENGTH('a) (take_bit LENGTH('a) (1 + k * 2) div take_bit LENGTH('a) 2)
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   562
        = take_bit LENGTH('a) k\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   563
        by simp
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   564
    qed
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   565
    ultimately show ?thesis
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70973
diff changeset
   566
      by simp
70925
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   567
  qed
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   568
qed
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   569
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   570
instance word :: (len) semiring_bits
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   571
proof
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   572
  show \<open>a = b \<longleftrightarrow> (even a \<longleftrightarrow> even b) \<and> a div 2 = b div 2\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   573
    for a b :: \<open>'a word\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   574
    apply transfer
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   575
    apply auto
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   576
       apply (metis bit_ident drop_bit_eq_div drop_bit_half even_take_bit_eq even_two_times_div_two mod_div_trivial odd_two_times_div_two_succ take_bit_eq_mod)
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   577
      apply (metis even_take_bit_eq len_not_eq_0)
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   578
     apply (metis even_take_bit_eq len_not_eq_0)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   579
    apply (metis (no_types, hide_lams) div_0 drop_bit_eq_div drop_bit_half dvd_mult_div_cancel even_take_bit_eq mod_div_trivial mod_eq_self_iff_div_eq_0 take_bit_eq_mod)
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   580
    done
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   581
  show \<open>P a\<close> if stable: \<open>\<And>a. a div 2 = a \<Longrightarrow> P a\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   582
    and rec: \<open>\<And>a b. P a \<Longrightarrow> (of_bool b + 2 * a) div 2 = a \<Longrightarrow> P (of_bool b + 2 * a)\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   583
  for P and a :: \<open>'a word\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   584
  proof (induction a rule: word_bit_induct)
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   585
    case zero
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   586
    from stable [of 0] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   587
      by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   588
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   589
    case (even a)
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   590
    with rec [of a False] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   591
      using bit_word_half_eq [of a False] by (simp add: ac_simps)
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   592
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   593
    case (odd a)
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   594
    with rec [of a True] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   595
      using bit_word_half_eq [of a True] by (simp add: ac_simps)
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   596
  qed
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   597
  show \<open>0 div a = 0\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   598
    for a :: \<open>'a word\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   599
    by transfer simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   600
  show \<open>a div 1 = a\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   601
    for a :: \<open>'a word\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   602
    by transfer simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   603
  show \<open>a mod b div b = 0\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   604
    for a b :: \<open>'a word\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   605
    apply transfer
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   606
    apply (simp add: take_bit_eq_mod)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   607
    apply (subst (3) mod_pos_pos_trivial [of _ \<open>2 ^ LENGTH('a)\<close>])
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   608
      apply simp_all
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   609
     apply (metis le_less mod_by_0 pos_mod_conj zero_less_numeral zero_less_power)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   610
    using pos_mod_bound [of \<open>2 ^ LENGTH('a)\<close>] apply simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   611
  proof -
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   612
    fix aa :: int and ba :: int
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   613
    have f1: "\<And>i n. (i::int) mod 2 ^ n = 0 \<or> 0 < i mod 2 ^ n"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   614
      by (metis le_less take_bit_eq_mod take_bit_nonnegative)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   615
    have "(0::int) < 2 ^ len_of (TYPE('a)::'a itself) \<and> ba mod 2 ^ len_of (TYPE('a)::'a itself) \<noteq> 0 \<or> aa mod 2 ^ len_of (TYPE('a)::'a itself) mod (ba mod 2 ^ len_of (TYPE('a)::'a itself)) < 2 ^ len_of (TYPE('a)::'a itself)"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   616
      by (metis (no_types) mod_by_0 unique_euclidean_semiring_numeral_class.pos_mod_bound zero_less_numeral zero_less_power)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   617
    then show "aa mod 2 ^ len_of (TYPE('a)::'a itself) mod (ba mod 2 ^ len_of (TYPE('a)::'a itself)) < 2 ^ len_of (TYPE('a)::'a itself)"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   618
      using f1 by (meson le_less less_le_trans unique_euclidean_semiring_numeral_class.pos_mod_bound)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   619
  qed
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   620
  show \<open>(1 + a) div 2 = a div 2\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   621
    if \<open>even a\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   622
    for a :: \<open>'a word\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   623
    using that by transfer (auto dest: le_Suc_ex)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   624
  show "a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   625
    for a :: "'a word" and m n :: nat
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   626
    apply transfer
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   627
    apply (auto simp add: not_less take_bit_drop_bit ac_simps simp flip: drop_bit_eq_div)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   628
    apply (simp add: drop_bit_take_bit)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   629
    done
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   630
  show "a mod 2 ^ m mod 2 ^ n = a mod 2 ^ min m n"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   631
    for a :: "'a word" and m n :: nat
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   632
    apply transfer
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   633
    apply (auto simp flip: take_bit_eq_mod)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   634
    apply (simp add: ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   635
    done
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   636
  show \<open>a * 2 ^ m mod 2 ^ n = a mod 2 ^ (n - m) * 2 ^ m\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   637
    if \<open>m \<le> n\<close> for a :: "'a word" and m n :: nat
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   638
    using that apply transfer
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   639
    apply (auto simp flip: take_bit_eq_mod)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   640
           apply (auto simp flip: push_bit_eq_mult simp add: push_bit_take_bit split: split_min_lin)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   641
    done
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   642
  show \<open>a div 2 ^ n mod 2 ^ m = a mod (2 ^ (n + m)) div 2 ^ n\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   643
    for a :: "'a word" and m n :: nat
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   644
    apply transfer
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   645
    apply (auto simp add: not_less take_bit_drop_bit ac_simps simp flip: take_bit_eq_mod drop_bit_eq_div split: split_min_lin)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71095
diff changeset
   646
    done
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   647
qed
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   648
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   649
instantiation word :: (len) semiring_bit_shifts
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   650
begin
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   651
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   652
lift_definition push_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   653
  is push_bit
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   654
proof -
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   655
  show \<open>Parity.take_bit LENGTH('a) (push_bit n k) = Parity.take_bit LENGTH('a) (push_bit n l)\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   656
    if \<open>Parity.take_bit LENGTH('a) k = Parity.take_bit LENGTH('a) l\<close> for k l :: int and n :: nat
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   657
  proof -
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   658
    from that
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   659
    have \<open>Parity.take_bit (LENGTH('a) - n) (Parity.take_bit LENGTH('a) k)
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   660
      = Parity.take_bit (LENGTH('a) - n) (Parity.take_bit LENGTH('a) l)\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   661
      by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   662
    moreover have \<open>min (LENGTH('a) - n) LENGTH('a) = LENGTH('a) - n\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   663
      by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   664
    ultimately show ?thesis
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   665
      by (simp add: take_bit_push_bit)
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   666
  qed
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   667
qed
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   668
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   669
lift_definition drop_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   670
  is \<open>\<lambda>n. drop_bit n \<circ> take_bit LENGTH('a)\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   671
  by (simp add: take_bit_eq_mod)
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   672
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   673
instance proof
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   674
  show \<open>push_bit n a = a * 2 ^ n\<close> for n :: nat and a :: "'a word"
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   675
    by transfer (simp add: push_bit_eq_mult)
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   676
  show \<open>drop_bit n a = a div 2 ^ n\<close> for n :: nat and a :: "'a word"
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   677
  proof (cases \<open>n < LENGTH('a)\<close>)
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   678
    case True
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   679
    then show ?thesis
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   680
      by transfer
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   681
        (simp add: take_bit_eq_mod drop_bit_eq_div)
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   682
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   683
    case False
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   684
    then obtain m where n: \<open>n = LENGTH('a) + m\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   685
      by (auto simp add: not_less dest: le_Suc_ex)
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   686
    then show ?thesis
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   687
      by transfer
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   688
        (simp add: take_bit_eq_mod drop_bit_eq_div power_add zdiv_zmult2_eq)
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   689
  qed
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   690
qed
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   691
70925
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   692
end
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   693
71095
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   694
instantiation word :: (len) ring_bit_operations
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   695
begin
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   696
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   697
lift_definition not_word :: "'a word \<Rightarrow> 'a word"
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   698
  is not
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   699
  by (simp add: take_bit_not_iff)
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   700
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   701
lift_definition and_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   702
  is "and"
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   703
  by simp
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   704
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   705
lift_definition or_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   706
  is or
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   707
  by simp
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   708
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   709
lift_definition xor_word ::  "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   710
  is xor
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   711
  by simp
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   712
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   713
instance proof
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   714
  interpret bit_word: boolean_algebra "(AND)" "(OR)" NOT 0 "- 1 :: 'a word"
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   715
  proof
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   716
    show "a AND (b OR c) = a AND b OR a AND c"
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   717
      for a b c :: "'a word"
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   718
      by transfer (simp add: bit.conj_disj_distrib)
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   719
    show "a OR b AND c = (a OR b) AND (a OR c)"
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   720
      for a b c :: "'a word"
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   721
      by transfer (simp add: bit.disj_conj_distrib)
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   722
  qed (transfer; simp add: ac_simps)+
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   723
  show "boolean_algebra (AND) (OR) NOT 0 (- 1 :: 'a word)"
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   724
    by (fact bit_word.boolean_algebra_axioms)
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   725
  show "bit_word.xor = ((XOR) :: 'a word \<Rightarrow> _)"
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   726
  proof (rule ext)+
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   727
    fix a b :: "'a word"
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   728
    have "a XOR b = a AND NOT b OR NOT a AND b"
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   729
      by transfer (simp add: bit.xor_def)
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   730
    then show "bit_word.xor a b = a XOR b"
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   731
      by (simp add: bit_word.xor_def)
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   732
  qed
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   733
qed
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   734
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 71093
diff changeset
   735
end
71095
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   736
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
   737
end