src/HOL/ex/Word_Type.thy
author haftmann
Tue, 22 Oct 2019 19:07:11 +0000
changeset 70925 525853e4ec80
parent 70903 c550368a4e29
child 70927 cc204e10385c
permissions -rw-r--r--
bit operations for word type
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
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
     6
theory Word_Type
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
70925
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
     9
    "HOL-ex.Bit_Lists"
66453
cc19f7ca2ed6 session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents: 64593
diff changeset
    10
    "HOL-Library.Type_Length"
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
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    15
lemma take_bit_uminus:
70171
haftmann
parents: 67961
diff changeset
    16
  "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
    17
  by (simp add: take_bit_eq_mod mod_minus_eq)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    18
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    19
lemma take_bit_minus:
70171
haftmann
parents: 67961
diff changeset
    20
  "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
    21
  by (simp add: take_bit_eq_mod mod_diff_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_nonnegative [simp]:
70171
haftmann
parents: 67961
diff changeset
    24
  "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
    25
  by (simp add: take_bit_eq_mod)
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
definition signed_take_bit :: "nat \<Rightarrow> int \<Rightarrow> int"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    28
  where signed_take_bit_eq_take_bit:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    29
    "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
    30
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    31
lemma signed_take_bit_eq_take_bit':
70171
haftmann
parents: 67961
diff changeset
    32
  "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
    33
  using that by (simp add: signed_take_bit_eq_take_bit)
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_0 [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    36
  "signed_take_bit 0 k = - (k mod 2)"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    37
proof (cases "even k")
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    38
  case True
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    39
  then have "odd (k + 1)"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    40
    by simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    41
  then have "(k + 1) mod 2 = 1"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    42
    by (simp add: even_iff_mod_2_eq_zero)
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    43
  with True show ?thesis
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    44
    by (simp add: signed_take_bit_eq_take_bit)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    45
next
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    46
  case False
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    47
  then 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 odd_iff_mod_2_eq_one)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    49
qed
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    50
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    51
lemma signed_take_bit_Suc [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    52
  "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
    53
  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
    54
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    55
lemma signed_take_bit_of_0 [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    56
  "signed_take_bit n 0 = 0"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    57
  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
    58
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    59
lemma signed_take_bit_of_minus_1 [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    60
  "signed_take_bit n (- 1) = - 1"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    61
  by (induct n) simp_all
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_eq_iff_take_bit_eq:
70171
haftmann
parents: 67961
diff changeset
    64
  "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
    65
  if "n > 0"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    66
proof -
70171
haftmann
parents: 67961
diff changeset
    67
  from that obtain m where m: "n = Suc m"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    68
    by (cases n) auto
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    69
  show ?thesis
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    70
  proof 
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    71
    assume ?Q
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    72
    have "take_bit (Suc m) (k + 2 ^ m) =
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    73
      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
    74
      by (simp only: take_bit_add)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    75
    also have "\<dots> =
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    76
      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
    77
      by (simp only: \<open>?Q\<close> m [symmetric])
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    78
    also have "\<dots> = take_bit (Suc m) (l + 2 ^ m)"
67961
9c31678d2139 even more on bit operations
haftmann
parents: 67907
diff changeset
    79
      by (simp only: take_bit_add)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    80
    finally show ?P
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    81
      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
    82
  next
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    83
    assume ?P
70171
haftmann
parents: 67961
diff changeset
    84
    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
    85
      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
    86
    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
    87
      by (metis mod_add_eq)
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    88
    then have "k mod 2 ^ n = l mod 2 ^ n"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    89
      by (metis add_diff_cancel_right' uminus_add_conv_diff)
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    90
    then show ?Q
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
    91
      by (simp add: take_bit_eq_mod)
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    92
  qed
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    93
qed 
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    94
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    95
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    96
subsection \<open>Bit strings as quotient type\<close>
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    97
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    98
subsubsection \<open>Basic properties\<close>
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
    99
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   100
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
   101
  by (auto intro!: equivpI reflpI sympI transpI)
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   102
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   103
instantiation word :: (len0) "{semiring_numeral, comm_semiring_0, comm_ring}"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   104
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   105
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   106
lift_definition zero_word :: "'a word"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   107
  is 0
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   108
  .
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 one_word :: "'a word"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   111
  is 1
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 plus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   115
  is plus
67961
9c31678d2139 even more on bit operations
haftmann
parents: 67907
diff changeset
   116
  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
   117
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   118
lift_definition uminus_word :: "'a word \<Rightarrow> 'a word"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   119
  is uminus
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   120
  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
   121
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   122
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
   123
  is minus
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   124
  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
   125
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   126
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
   127
  is times
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   128
  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
   129
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   130
instance
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   131
  by standard (transfer; simp add: algebra_simps)+
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   132
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   133
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   134
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   135
instance word :: (len) comm_ring_1
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   136
  by standard (transfer; simp)+
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   137
70903
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   138
quickcheck_generator word
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   139
  constructors:
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   140
    "zero_class.zero :: ('a::len0) word",
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   141
    "numeral :: num \<Rightarrow> ('a::len0) word",
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   142
    "uminus :: ('a::len0) word \<Rightarrow> ('a::len0) word"
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   143
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   144
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   145
subsubsection \<open>Conversions\<close>
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   146
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   147
lemma [transfer_rule]:
70348
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   148
  "rel_fun HOL.eq (pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool) numeral numeral"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   149
proof -
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   150
  note transfer_rule_numeral [transfer_rule]
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   151
  show ?thesis by transfer_prover
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   152
qed
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   153
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   154
lemma [transfer_rule]:
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   155
  "rel_fun HOL.eq pcr_word int of_nat"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   156
proof -
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   157
  note transfer_rule_of_nat [transfer_rule]
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   158
  show ?thesis by transfer_prover
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   159
qed
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   160
  
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   161
lemma [transfer_rule]:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   162
  "rel_fun HOL.eq pcr_word (\<lambda>k. k) of_int"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   163
proof -
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   164
  note transfer_rule_of_int [transfer_rule]
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   165
  have "rel_fun HOL.eq pcr_word (of_int :: int \<Rightarrow> int) (of_int :: int \<Rightarrow> 'a word)"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   166
    by transfer_prover
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   167
  then show ?thesis by (simp add: id_def)
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   168
qed
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   169
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   170
context semiring_1
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   171
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   172
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   173
lift_definition unsigned :: "'b::len0 word \<Rightarrow> 'a"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   174
  is "of_nat \<circ> nat \<circ> take_bit LENGTH('b)"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   175
  by simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   176
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   177
lemma unsigned_0 [simp]:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   178
  "unsigned 0 = 0"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   179
  by transfer simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   180
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   181
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   182
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   183
context semiring_char_0
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   184
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   185
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   186
lemma word_eq_iff_unsigned:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   187
  "a = b \<longleftrightarrow> unsigned a = unsigned b"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   188
  by safe (transfer; simp add: eq_nat_nat_iff)
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   189
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   190
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   191
70903
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   192
instantiation word :: (len0) equal
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   193
begin
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   194
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   195
definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   196
  where "equal_word a b \<longleftrightarrow> (unsigned a :: int) = unsigned b"
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   197
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   198
instance proof
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   199
  fix a b :: "'a word"
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   200
  show "HOL.equal a b \<longleftrightarrow> a = b"
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   201
    using word_eq_iff_unsigned [of a b] by (auto simp add: equal_word_def)
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   202
qed
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   203
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   204
end
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   205
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   206
context ring_1
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   207
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   208
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   209
lift_definition signed :: "'b::len word \<Rightarrow> 'a"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   210
  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
   211
  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
   212
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   213
lemma signed_0 [simp]:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   214
  "signed 0 = 0"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   215
  by transfer simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   216
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   217
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   218
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   219
lemma unsigned_of_nat [simp]:
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   220
  "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
   221
  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
   222
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   223
lemma of_nat_unsigned [simp]:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   224
  "of_nat (unsigned a) = a"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   225
  by transfer simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   226
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   227
lemma of_int_unsigned [simp]:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   228
  "of_int (unsigned a) = a"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   229
  by transfer simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   230
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   231
context ring_char_0
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   232
begin
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 word_eq_iff_signed:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   235
  "a = b \<longleftrightarrow> signed a = signed b"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   236
  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
   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 signed_of_int [simp]:
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   241
  "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
   242
  by transfer simp
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_int_signed [simp]:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   245
  "of_int (signed a) = a"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   246
  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
   247
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   248
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   249
subsubsection \<open>Properties\<close>
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   250
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   251
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   252
subsubsection \<open>Division\<close>
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   253
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   254
instantiation word :: (len0) modulo
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   255
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   256
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   257
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
   258
  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
   259
  by simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   260
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   261
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
   262
  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
   263
  by simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   264
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   265
instance ..
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
70348
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   269
lemma [transfer_rule]:
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   270
  "rel_fun pcr_word (\<longleftrightarrow>) even ((dvd) 2 :: 'a::len word \<Rightarrow> bool)"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   271
proof -
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   272
  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
   273
    for k :: int
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   274
  proof
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   275
    assume ?P
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   276
    then show ?Q
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   277
      by auto
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   278
  next
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   279
    assume ?Q
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   280
    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
   281
    then have "even (take_bit LENGTH('a) k)"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   282
      by simp
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   283
    then show ?P
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   284
      by simp
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   285
  qed
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   286
  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
   287
    transfer_prover
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   288
qed
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   289
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   290
instance word :: (len) semiring_modulo
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   291
proof
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   292
  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
   293
  proof transfer
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   294
    fix k l :: int
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   295
    define r :: int where "r = 2 ^ LENGTH('a)"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   296
    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
   297
      by (simp add: take_bit_eq_mod)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   298
    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
   299
      + (k mod r) mod (l mod r)) mod r"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   300
      by (simp add: div_mult_mod_eq)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   301
    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
   302
      + (k mod r) mod (l mod r)) mod r"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   303
      by (simp add: mod_add_left_eq)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   304
    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
   305
      + (k mod r) mod (l mod r)) mod r"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   306
      by (simp add: mod_mult_right_eq)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   307
    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
   308
      + (k mod r) mod (l mod r)) mod r"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   309
      by (simp add: mod_simps)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   310
    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
   311
      + 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
   312
      by simp
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   313
  qed
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   314
qed
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   315
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   316
instance word :: (len) semiring_parity
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   317
proof
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   318
  show "\<not> 2 dvd (1::'a word)"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   319
    by transfer simp
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   320
  consider (triv) "LENGTH('a) = 1" "take_bit LENGTH('a) 2 = (0 :: int)"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   321
    | (take_bit_2) "take_bit LENGTH('a) 2 = (2 :: int)"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   322
  proof (cases "LENGTH('a) \<ge> 2")
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   323
    case False
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   324
    then have "LENGTH('a) = 1"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   325
      by (auto simp add: not_le dest: less_2_cases)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   326
    then have "take_bit LENGTH('a) 2 = (0 :: int)"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   327
      by simp
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   328
    with \<open>LENGTH('a) = 1\<close> triv show ?thesis
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   329
      by simp
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   330
  next
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   331
    case True
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   332
    then obtain n where "LENGTH('a) = Suc (Suc n)"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   333
      by (auto dest: le_Suc_ex)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   334
    then have "take_bit LENGTH('a) 2 = (2 :: int)"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   335
      by simp
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   336
    with take_bit_2 show ?thesis
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   337
      by simp
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   338
  qed
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   339
  note * = this
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   340
  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
   341
    for a :: "'a word"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   342
    by (transfer; cases rule: *) (simp_all add: mod_2_eq_odd)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   343
  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
   344
    for a :: "'a word"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   345
    by (transfer; cases rule: *) (simp_all add: mod_2_eq_odd)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   346
qed
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   347
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   348
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   349
subsubsection \<open>Orderings\<close>
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   350
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   351
instantiation word :: (len0) linorder
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   352
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   353
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   354
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
   355
  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
   356
  by simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   357
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   358
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
   359
  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
   360
  by simp
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   361
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   362
instance
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   363
  by standard (transfer; auto)+
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   364
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   365
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   366
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   367
context linordered_semidom
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   368
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   369
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   370
lemma word_less_eq_iff_unsigned:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   371
  "a \<le> b \<longleftrightarrow> unsigned a \<le> unsigned b"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   372
  by (transfer fixing: less_eq) (simp add: nat_le_eq_zle)
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   373
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   374
lemma word_less_iff_unsigned:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   375
  "a < b \<longleftrightarrow> unsigned a < unsigned b"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   376
  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
   377
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   378
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   379
70925
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   380
subsection \<open>Bit operation on \<^typ>\<open>'a word\<close>\<close>
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   381
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   382
context unique_euclidean_semiring_with_nat
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   383
begin
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   384
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   385
primrec n_bits_of :: "nat \<Rightarrow> 'a \<Rightarrow> bool list"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   386
  where
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   387
    "n_bits_of 0 a = []"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   388
  | "n_bits_of (Suc n) a = odd a # n_bits_of n (a div 2)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   389
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   390
lemma n_bits_of_eq_iff:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   391
  "n_bits_of n a = n_bits_of n b \<longleftrightarrow> take_bit n a = take_bit n b"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   392
  apply (induction n arbitrary: a b)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   393
   apply auto
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   394
   apply (metis local.dvd_add_times_triv_left_iff local.dvd_triv_right local.odd_one)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   395
  apply (metis local.dvd_add_times_triv_left_iff local.dvd_triv_right local.odd_one)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   396
  done
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   397
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   398
lemma take_n_bits_of [simp]:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   399
  "take m (n_bits_of n a) = n_bits_of (min m n) a"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   400
proof -
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   401
  define q and v and w where "q = min m n" and "v = m - q" and "w = n - q"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   402
  then have "v = 0 \<or> w = 0"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   403
    by auto
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   404
  then have "take (q + v) (n_bits_of (q + w) a) = n_bits_of q a"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   405
    by (induction q arbitrary: a) auto
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   406
  with q_def v_def w_def show ?thesis
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   407
    by simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   408
qed
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   409
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   410
lemma unsigned_of_bits_n_bits_of [simp]:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   411
  "unsigned_of_bits (n_bits_of n a) = take_bit n a"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   412
  by (induction n arbitrary: a) (simp_all add: ac_simps)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   413
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   414
end
70925
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   415
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   416
lemma unsigned_of_bits_eq_of_bits:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   417
  "unsigned_of_bits bs = (of_bits (bs @ [False]) :: int)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   418
  by (simp add: of_bits_int_def)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   419
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   420
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   421
instantiation word :: (len) bit_representation
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   422
begin
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   423
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   424
lift_definition bits_of_word :: "'a word \<Rightarrow> bool list"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   425
  is "n_bits_of LENGTH('a)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   426
  by (simp add: n_bits_of_eq_iff)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   427
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   428
lift_definition of_bits_word :: "bool list \<Rightarrow> 'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   429
  is unsigned_of_bits .
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   430
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   431
instance proof
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   432
  fix a :: "'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   433
  show "of_bits (bits_of a) = a"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   434
    by transfer simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   435
qed
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   436
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   437
end
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   438
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   439
lemma take_bit_complement_iff:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   440
  "take_bit n (complement k) = take_bit n (complement l) \<longleftrightarrow> take_bit n k = take_bit n l"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   441
  for k l :: int
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   442
  by (simp add: take_bit_eq_mod mod_eq_dvd_iff dvd_diff_commute)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   443
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   444
lemma take_bit_not_iff:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   445
  "take_bit n (NOT k) = take_bit n (NOT l) \<longleftrightarrow> take_bit n k = take_bit n l"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   446
  for k l :: int
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   447
  by (simp add: not_int_def take_bit_complement_iff)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   448
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   449
lemma n_bits_of_not:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   450
  "n_bits_of n (NOT k) = map Not (n_bits_of n k)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   451
  for k :: int
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   452
  by (induction n arbitrary: k) (simp_all add: not_div_2)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   453
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   454
lemma take_bit_and [simp]:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   455
  "take_bit n (k AND l) = take_bit n k AND take_bit n l"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   456
  for k l :: int
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   457
  apply (induction n arbitrary: k l)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   458
   apply simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   459
  apply (subst and_int.rec)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   460
  apply (subst (2) and_int.rec)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   461
  apply simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   462
  done
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   463
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   464
lemma take_bit_or [simp]:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   465
  "take_bit n (k OR l) = take_bit n k OR take_bit n l"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   466
  for k l :: int
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   467
  apply (induction n arbitrary: k l)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   468
   apply simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   469
  apply (subst or_int.rec)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   470
  apply (subst (2) or_int.rec)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   471
  apply simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   472
  done
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   473
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   474
lemma take_bit_xor [simp]:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   475
  "take_bit n (k XOR l) = take_bit n k XOR take_bit n l"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   476
  for k l :: int
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   477
  apply (induction n arbitrary: k l)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   478
   apply simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   479
  apply (subst xor_int.rec)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   480
  apply (subst (2) xor_int.rec)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   481
  apply simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   482
  done
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   483
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   484
instantiation word :: (len) bit_operations
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   485
begin
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   486
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   487
lift_definition not_word :: "'a word \<Rightarrow> 'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   488
  is not
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   489
  by (simp add: take_bit_not_iff)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   490
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   491
lift_definition and_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   492
  is "and"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   493
  by simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   494
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   495
lift_definition or_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   496
  is or
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   497
  by simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   498
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   499
lift_definition xor_word ::  "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   500
  is xor
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   501
  by simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   502
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   503
lift_definition shift_left_word :: "'a word \<Rightarrow> nat \<Rightarrow> 'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   504
  is shift_left
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   505
proof -
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   506
  show "take_bit LENGTH('a) (k << n) = take_bit LENGTH('a) (l << n)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   507
    if "take_bit LENGTH('a) k = take_bit LENGTH('a) l" for k l :: int and n :: nat
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   508
  proof -
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   509
    from that
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   510
    have "take_bit (LENGTH('a) - n) (take_bit LENGTH('a) k)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   511
      = take_bit (LENGTH('a) - n) (take_bit LENGTH('a) l)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   512
      by simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   513
    moreover have "min (LENGTH('a) - n) LENGTH('a) = LENGTH('a) - n"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   514
      by simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   515
    ultimately show ?thesis by (simp add: take_bit_push_bit)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   516
  qed
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   517
qed
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   518
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   519
lift_definition shift_right_word :: "'a word \<Rightarrow> nat \<Rightarrow> 'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   520
  is "\<lambda>k n. drop_bit n (take_bit LENGTH('a) k)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   521
  by simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   522
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   523
instance proof
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   524
  show "semilattice ((AND) :: 'a word \<Rightarrow> _)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   525
    by standard (transfer; simp add: ac_simps)+
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   526
  show "semilattice ((OR) :: 'a word \<Rightarrow> _)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   527
    by standard (transfer; simp add: ac_simps)+
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   528
  show "abel_semigroup ((XOR) :: 'a word \<Rightarrow> _)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   529
    by standard (transfer; simp add: ac_simps)+
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   530
  show "not = (of_bits \<circ> map Not \<circ> bits_of :: 'a word \<Rightarrow> 'a word)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   531
  proof
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   532
    fix a :: "'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   533
    have "NOT a = of_bits (map Not (bits_of a))"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   534
      by transfer (simp flip: unsigned_of_bits_take n_bits_of_not add: take_map)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   535
    then show "NOT a = (of_bits \<circ> map Not \<circ> bits_of) a"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   536
      by simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   537
  qed
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   538
  show "of_bits bs AND of_bits cs = (of_bits (map2 (\<and>) bs cs) :: 'a word)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   539
    if "length bs = length cs" for bs cs
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   540
    using that apply transfer
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   541
    apply (simp only: unsigned_of_bits_eq_of_bits)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   542
    apply (subst and_eq)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   543
    apply simp_all
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   544
    done
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   545
  show "of_bits bs OR of_bits cs = (of_bits (map2 (\<or>) bs cs) :: 'a word)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   546
    if "length bs = length cs" for bs cs
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   547
    using that apply transfer
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   548
    apply (simp only: unsigned_of_bits_eq_of_bits)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   549
    apply (subst or_eq)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   550
    apply simp_all
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   551
    done
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   552
  show "of_bits bs XOR of_bits cs = (of_bits (map2 (\<noteq>) bs cs) :: 'a word)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   553
    if "length bs = length cs" for bs cs
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   554
    using that apply transfer
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   555
    apply (simp only: unsigned_of_bits_eq_of_bits)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   556
    apply (subst xor_eq)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   557
    apply simp_all
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   558
    done
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   559
  show "a << n = of_bits (replicate n False @ bits_of a)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   560
    for a :: "'a word" and n :: nat
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   561
    by transfer (simp add: push_bit_take_bit)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   562
  show "a >> n = of_bits (drop n (bits_of a))"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   563
    if "n < length (bits_of a)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   564
    for a :: "'a word" and n :: nat
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   565
    using that by transfer simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   566
qed
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   567
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   568
end
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   569
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   570
global_interpretation bit_word: boolean_algebra "(AND)" "(OR)" NOT 0 "- 1 :: 'a::len word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   571
  rewrites "bit_word.xor = ((XOR) :: 'a word \<Rightarrow> _)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   572
proof -
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   573
  interpret bit_word: boolean_algebra "(AND)" "(OR)" NOT 0 "- 1 :: 'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   574
  proof
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   575
    show "a AND (b OR c) = a AND b OR a AND c"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   576
      for a b c :: "'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   577
      by transfer (simp add: bit_int.conj_disj_distrib)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   578
    show "a OR b AND c = (a OR b) AND (a OR c)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   579
      for a b c :: "'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   580
      by transfer (simp add: bit_int.disj_conj_distrib)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   581
    show "a AND NOT a = 0" for a :: "'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   582
      by transfer simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   583
    show "a OR NOT a = - 1" for a :: "'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   584
      by transfer simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   585
  qed (transfer; simp)+
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   586
  show "boolean_algebra (AND) (OR) NOT 0 (- 1 :: 'a word)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   587
    by (fact bit_word.boolean_algebra_axioms)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   588
  show "bit_word.xor = ((XOR) :: 'a word \<Rightarrow> _)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   589
  proof (rule ext)+
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   590
    fix a b :: "'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   591
    have "a XOR b = a AND NOT b OR NOT a AND b"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   592
      by transfer (simp add: bit_int.xor_def)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   593
    then show "bit_word.xor a b = a XOR b"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   594
      by (simp add: bit_word.xor_def)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   595
  qed
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   596
qed
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   597
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   598
end