src/HOL/ex/Word_Type.thy
author haftmann
Thu, 31 Oct 2019 09:02:02 +0000
changeset 70973 a7a52ba0717d
parent 70927 cc204e10385c
permissions -rw-r--r--
more lemmas
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
70973
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   144
context
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   145
  includes lifting_syntax
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   146
  notes power_transfer [transfer_rule]
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   147
begin
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   148
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   149
lemma power_transfer_word [transfer_rule]:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   150
  \<open>(pcr_word ===> (=) ===> pcr_word) (^) (^)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   151
  by transfer_prover
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   152
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   153
end
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   154
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   155
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   156
subsubsection \<open>Conversions\<close>
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   157
70927
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   158
context
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   159
  includes lifting_syntax
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   160
  notes transfer_rule_numeral [transfer_rule]
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   161
    transfer_rule_of_nat [transfer_rule]
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   162
    transfer_rule_of_int [transfer_rule]
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   163
begin
70348
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   164
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   165
lemma [transfer_rule]:
70927
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   166
  "((=) ===> (pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool)) numeral numeral"
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   167
  by transfer_prover
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   168
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   169
lemma [transfer_rule]:
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   170
  "((=) ===> pcr_word) int of_nat"
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   171
  by transfer_prover
64015
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
lemma [transfer_rule]:
70927
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   174
  "((=) ===> pcr_word) (\<lambda>k. k) of_int"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   175
proof -
70927
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   176
  have "((=) ===> pcr_word) of_int of_int"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   177
    by transfer_prover
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   178
  then show ?thesis by (simp add: id_def)
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   179
qed
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   180
70927
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   181
end
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   182
70973
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   183
lemma abs_word_eq:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   184
  "abs_word = of_int"
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   185
  by (rule ext) (transfer, rule)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   186
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   187
context semiring_1
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   188
begin
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
lift_definition unsigned :: "'b::len0 word \<Rightarrow> 'a"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   191
  is "of_nat \<circ> nat \<circ> take_bit LENGTH('b)"
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   192
  by simp
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
lemma unsigned_0 [simp]:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   195
  "unsigned 0 = 0"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   196
  by transfer 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
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   199
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   200
context semiring_char_0
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   201
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   202
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   203
lemma word_eq_iff_unsigned:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   204
  "a = b \<longleftrightarrow> unsigned a = unsigned b"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   205
  by safe (transfer; simp add: eq_nat_nat_iff)
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
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   208
70903
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   209
instantiation word :: (len0) equal
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   210
begin
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   211
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   212
definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   213
  where "equal_word a b \<longleftrightarrow> (unsigned a :: int) = unsigned b"
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   214
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   215
instance proof
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   216
  fix a b :: "'a word"
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   217
  show "HOL.equal a b \<longleftrightarrow> a = b"
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   218
    using word_eq_iff_unsigned [of a b] by (auto simp add: equal_word_def)
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   219
qed
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   220
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   221
end
c550368a4e29 added quickcheck setup
haftmann
parents: 70348
diff changeset
   222
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   223
context ring_1
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   224
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   225
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   226
lift_definition signed :: "'b::len word \<Rightarrow> 'a"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   227
  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
   228
  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
   229
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   230
lemma signed_0 [simp]:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   231
  "signed 0 = 0"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   232
  by transfer simp
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
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   235
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   236
lemma unsigned_of_nat [simp]:
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   237
  "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
   238
  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
   239
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   240
lemma of_nat_unsigned [simp]:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   241
  "of_nat (unsigned a) = a"
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_unsigned [simp]:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   245
  "of_int (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
70973
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   248
lemma unsigned_nat_less:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   249
  \<open>unsigned a < (2 ^ LENGTH('a) :: nat)\<close> for a :: \<open>'a::len0 word\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   250
  by transfer (simp add: take_bit_eq_mod)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   251
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   252
lemma unsigned_int_less:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   253
  \<open>unsigned a < (2 ^ LENGTH('a) :: int)\<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
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   256
context ring_char_0
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   257
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   258
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   259
lemma word_eq_iff_signed:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   260
  "a = b \<longleftrightarrow> signed a = signed b"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   261
  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
   262
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   263
end
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
lemma signed_of_int [simp]:
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   266
  "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
   267
  by transfer simp
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 of_int_signed [simp]:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   270
  "of_int (signed a) = a"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   271
  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
   272
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   273
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   274
subsubsection \<open>Properties\<close>
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   275
70973
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   276
lemma length_cases:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   277
  obtains (triv) "LENGTH('a::len) = 1" "take_bit LENGTH('a) 2 = (0 :: int)"
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   278
    | (take_bit_2) "take_bit LENGTH('a) 2 = (2 :: int)"
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   279
proof (cases "LENGTH('a) \<ge> 2")
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   280
  case False
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   281
  then have "LENGTH('a) = 1"
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   282
    by (auto simp add: not_le dest: less_2_cases)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   283
  then have "take_bit LENGTH('a) 2 = (0 :: int)"
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   284
    by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   285
  with \<open>LENGTH('a) = 1\<close> triv show ?thesis
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   286
    by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   287
next
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   288
  case True
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   289
  then obtain n where "LENGTH('a) = Suc (Suc n)"
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   290
    by (auto dest: le_Suc_ex)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   291
  then have "take_bit LENGTH('a) 2 = (2 :: int)"
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   292
    by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   293
  with take_bit_2 show ?thesis
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   294
    by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   295
qed
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   296
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   297
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   298
subsubsection \<open>Division\<close>
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   299
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   300
instantiation word :: (len0) modulo
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   301
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   302
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   303
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
   304
  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
   305
  by simp
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 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
   308
  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
   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
instance ..
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   312
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   313
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   314
70973
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   315
lemma zero_word_div_eq [simp]:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   316
  \<open>0 div a = 0\<close> for a :: \<open>'a::len0 word\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   317
  by transfer simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   318
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   319
lemma div_zero_word_eq [simp]:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   320
  \<open>a div 0 = 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
70927
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   323
context
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   324
  includes lifting_syntax
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   325
begin
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   326
70348
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   327
lemma [transfer_rule]:
70927
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   328
  "(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
   329
proof -
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   330
  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
   331
    for k :: int
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   332
  proof
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   333
    assume ?P
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   334
    then show ?Q
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   335
      by auto
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   336
  next
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   337
    assume ?Q
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   338
    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
   339
    then have "even (take_bit LENGTH('a) k)"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   340
      by simp
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   341
    then show ?P
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   342
      by simp
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   343
  qed
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   344
  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
   345
    transfer_prover
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
70927
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   348
end
cc204e10385c tuned syntax
haftmann
parents: 70925
diff changeset
   349
70348
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   350
instance word :: (len) semiring_modulo
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   351
proof
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   352
  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
   353
  proof transfer
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   354
    fix k l :: int
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   355
    define r :: int where "r = 2 ^ LENGTH('a)"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   356
    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
   357
      by (simp add: take_bit_eq_mod)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   358
    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
   359
      + (k mod r) mod (l mod r)) mod r"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   360
      by (simp add: div_mult_mod_eq)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   361
    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
   362
      + (k mod r) mod (l mod r)) mod r"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   363
      by (simp add: mod_add_left_eq)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   364
    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
   365
      + (k mod r) mod (l mod r)) mod r"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   366
      by (simp add: mod_mult_right_eq)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   367
    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
   368
      + (k mod r) mod (l mod r)) mod r"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   369
      by (simp add: mod_simps)
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   370
    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
   371
      + 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
   372
      by simp
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   373
  qed
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   374
qed
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   375
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   376
instance word :: (len) semiring_parity
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   377
proof
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   378
  show "\<not> 2 dvd (1::'a word)"
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   379
    by transfer simp
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   380
  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
   381
    for a :: "'a word"
70973
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   382
    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
   383
  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
   384
    for a :: "'a word"
70973
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   385
    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
   386
qed
bde161c740ca more theorems for proof of concept for word type
haftmann
parents: 70171
diff changeset
   387
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   388
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   389
subsubsection \<open>Orderings\<close>
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   390
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   391
instantiation word :: (len0) linorder
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   392
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   393
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   394
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
   395
  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
   396
  by simp
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_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 < 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
instance
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   403
  by standard (transfer; auto)+
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   404
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   405
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   406
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   407
context linordered_semidom
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   408
begin
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   409
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   410
lemma word_less_eq_iff_unsigned:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   411
  "a \<le> b \<longleftrightarrow> unsigned a \<le> unsigned b"
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   412
  by (transfer fixing: less_eq) (simp add: nat_le_eq_zle)
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_iff_unsigned:
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   415
  "a < b \<longleftrightarrow> unsigned a < unsigned b"
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67816
diff changeset
   416
  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
   417
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   418
end
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   419
70973
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   420
lemma word_greater_zero_iff:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   421
  \<open>a > 0 \<longleftrightarrow> a \<noteq> 0\<close> for a :: \<open>'a::len0 word\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   422
  by transfer (simp add: less_le)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   423
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   424
lemma of_nat_word_eq_iff:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   425
  \<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
   426
  by transfer (simp add: take_bit_of_nat)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   427
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   428
lemma of_nat_word_less_eq_iff:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   429
  \<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
   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_iff:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   433
  \<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
   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_eq_0_iff:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   437
  \<open>of_nat n = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd n\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   438
  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
   439
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   440
lemma of_int_word_eq_iff:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   441
  \<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
   442
  by transfer rule
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   443
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   444
lemma of_int_word_less_eq_iff:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   445
  \<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
   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_iff:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   449
  \<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
   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_eq_0_iff:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   453
  \<open>of_int k = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd k\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   454
  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
   455
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   456
70925
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   457
subsection \<open>Bit operation on \<^typ>\<open>'a word\<close>\<close>
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   458
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   459
context unique_euclidean_semiring_with_nat
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   460
begin
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   461
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   462
primrec n_bits_of :: "nat \<Rightarrow> 'a \<Rightarrow> bool list"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   463
  where
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   464
    "n_bits_of 0 a = []"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   465
  | "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
   466
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   467
lemma n_bits_of_eq_iff:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   468
  "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
   469
  apply (induction n arbitrary: a b)
70973
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   470
   apply (auto elim!: evenE oddE)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   471
   apply (metis dvd_triv_right even_plus_one_iff)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   472
  apply (metis dvd_triv_right even_plus_one_iff)
70925
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   473
  done
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   474
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   475
lemma take_n_bits_of [simp]:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   476
  "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
   477
proof -
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   478
  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
   479
  then have "v = 0 \<or> w = 0"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   480
    by auto
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   481
  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
   482
    by (induction q arbitrary: a) auto
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   483
  with q_def v_def w_def show ?thesis
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   484
    by simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   485
qed
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   486
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   487
lemma unsigned_of_bits_n_bits_of [simp]:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   488
  "unsigned_of_bits (n_bits_of n a) = take_bit n a"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   489
  by (induction n arbitrary: a) (simp_all add: ac_simps)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   490
64015
c9f3a94cb825 proof of concept for algebraically founded word types
haftmann
parents:
diff changeset
   491
end
70925
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   492
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   493
lemma unsigned_of_bits_eq_of_bits:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   494
  "unsigned_of_bits bs = (of_bits (bs @ [False]) :: int)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   495
  by (simp add: of_bits_int_def)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   496
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   497
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   498
instantiation word :: (len) bit_representation
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   499
begin
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   500
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   501
lift_definition bits_of_word :: "'a word \<Rightarrow> bool list"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   502
  is "n_bits_of LENGTH('a)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   503
  by (simp add: n_bits_of_eq_iff)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   504
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   505
lift_definition of_bits_word :: "bool list \<Rightarrow> 'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   506
  is unsigned_of_bits .
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   507
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   508
instance proof
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   509
  fix a :: "'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   510
  show "of_bits (bits_of a) = a"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   511
    by transfer simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   512
qed
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   513
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   514
end
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   515
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   516
lemma take_bit_complement_iff:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   517
  "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
   518
  for k l :: int
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   519
  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
   520
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   521
lemma take_bit_not_iff:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   522
  "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
   523
  for k l :: int
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   524
  by (simp add: not_int_def take_bit_complement_iff)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   525
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   526
lemma n_bits_of_not:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   527
  "n_bits_of n (NOT k) = map Not (n_bits_of n k)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   528
  for k :: int
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   529
  by (induction n arbitrary: k) (simp_all add: not_div_2)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   530
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   531
lemma take_bit_and [simp]:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   532
  "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
   533
  for k l :: int
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   534
  apply (induction n arbitrary: k l)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   535
   apply simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   536
  apply (subst and_int.rec)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   537
  apply (subst (2) and_int.rec)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   538
  apply simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   539
  done
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   540
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   541
lemma take_bit_or [simp]:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   542
  "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
   543
  for k l :: int
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   544
  apply (induction n arbitrary: k l)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   545
   apply simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   546
  apply (subst or_int.rec)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   547
  apply (subst (2) or_int.rec)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   548
  apply simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   549
  done
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   550
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   551
lemma take_bit_xor [simp]:
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   552
  "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
   553
  for k l :: int
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   554
  apply (induction n arbitrary: k l)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   555
   apply simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   556
  apply (subst xor_int.rec)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   557
  apply (subst (2) xor_int.rec)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   558
  apply simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   559
  done
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   560
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   561
instantiation word :: (len) bit_operations
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   562
begin
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   563
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   564
lift_definition not_word :: "'a word \<Rightarrow> 'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   565
  is not
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   566
  by (simp add: take_bit_not_iff)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   567
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   568
lift_definition and_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   569
  is "and"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   570
  by simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   571
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   572
lift_definition or_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   573
  is or
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   574
  by simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   575
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   576
lift_definition xor_word ::  "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   577
  is xor
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   578
  by simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   579
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   580
lift_definition shift_left_word :: "'a word \<Rightarrow> nat \<Rightarrow> 'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   581
  is shift_left
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   582
proof -
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   583
  show "take_bit LENGTH('a) (k << n) = take_bit LENGTH('a) (l << n)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   584
    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
   585
  proof -
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   586
    from that
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   587
    have "take_bit (LENGTH('a) - n) (take_bit LENGTH('a) k)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   588
      = take_bit (LENGTH('a) - n) (take_bit LENGTH('a) l)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   589
      by simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   590
    moreover have "min (LENGTH('a) - n) LENGTH('a) = LENGTH('a) - n"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   591
      by simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   592
    ultimately show ?thesis by (simp add: take_bit_push_bit)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   593
  qed
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   594
qed
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   595
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   596
lift_definition shift_right_word :: "'a word \<Rightarrow> nat \<Rightarrow> 'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   597
  is "\<lambda>k n. drop_bit n (take_bit LENGTH('a) k)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   598
  by simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   599
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   600
instance proof
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   601
  show "semilattice ((AND) :: 'a word \<Rightarrow> _)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   602
    by standard (transfer; simp add: ac_simps)+
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   603
  show "semilattice ((OR) :: 'a word \<Rightarrow> _)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   604
    by standard (transfer; simp add: ac_simps)+
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   605
  show "abel_semigroup ((XOR) :: 'a word \<Rightarrow> _)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   606
    by standard (transfer; simp add: ac_simps)+
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   607
  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
   608
  proof
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   609
    fix a :: "'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   610
    have "NOT a = of_bits (map Not (bits_of a))"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   611
      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
   612
    then show "NOT a = (of_bits \<circ> map Not \<circ> bits_of) a"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   613
      by simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   614
  qed
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   615
  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
   616
    if "length bs = length cs" for bs cs
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   617
    using that apply transfer
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   618
    apply (simp only: unsigned_of_bits_eq_of_bits)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   619
    apply (subst and_eq)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   620
    apply simp_all
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   621
    done
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   622
  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
   623
    if "length bs = length cs" for bs cs
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   624
    using that apply transfer
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   625
    apply (simp only: unsigned_of_bits_eq_of_bits)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   626
    apply (subst or_eq)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   627
    apply simp_all
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   628
    done
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   629
  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
   630
    if "length bs = length cs" for bs cs
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   631
    using that apply transfer
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   632
    apply (simp only: unsigned_of_bits_eq_of_bits)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   633
    apply (subst xor_eq)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   634
    apply simp_all
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   635
    done
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   636
  show "a << n = of_bits (replicate n False @ bits_of a)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   637
    for a :: "'a word" and n :: nat
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   638
    by transfer (simp add: push_bit_take_bit)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   639
  show "a >> n = of_bits (drop n (bits_of a))"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   640
    if "n < length (bits_of a)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   641
    for a :: "'a word" and n :: nat
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   642
    using that by transfer simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   643
qed
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   644
70973
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   645
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   646
subsection \<open>Bit structure on \<^typ>\<open>'a word\<close>\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   647
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   648
lemma word_bit_induct [case_names zero even odd]:
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   649
  \<open>P a\<close> if word_zero: \<open>P 0\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   650
    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
   651
    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
   652
  for P and a :: \<open>'a::len word\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   653
proof -
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   654
  define m :: nat where \<open>m = LENGTH('a) - 1\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   655
  then have l: \<open>LENGTH('a) = Suc m\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   656
    by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   657
  define n :: nat where \<open>n = unsigned a\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   658
  then have \<open>n < 2 ^ LENGTH('a)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   659
    by (simp add: unsigned_nat_less)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   660
  then have \<open>n < 2 * 2 ^ m\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   661
    by (simp add: l)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   662
  then have \<open>P (of_nat n)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   663
  proof (induction n rule: nat_bit_induct)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   664
    case zero
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   665
    show ?case
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   666
      by simp (rule word_zero)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   667
  next
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   668
    case (even n)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   669
    then have \<open>n < 2 ^ m\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   670
      by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   671
    with even.IH have \<open>P (of_nat n)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   672
      by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   673
    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
   674
      by (auto simp add: word_greater_zero_iff of_nat_word_eq_0_iff l)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   675
    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
   676
      using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>]
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   677
      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
   678
    ultimately have \<open>P (2 * of_nat n)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   679
      by (rule word_even)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   680
    then show ?case
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   681
      by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   682
  next
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   683
    case (odd n)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   684
    then have \<open>Suc n \<le> 2 ^ m\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   685
      by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   686
    with odd.IH have \<open>P (of_nat n)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   687
      by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   688
    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
   689
      using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>]
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   690
      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
   691
    ultimately have \<open>P (1 + 2 * of_nat n)\<close>
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   692
      by (rule word_odd)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   693
    then show ?case
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   694
      by simp
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   695
  qed
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   696
  then show ?thesis
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   697
    by (simp add: n_def)
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   698
qed
a7a52ba0717d more lemmas
haftmann
parents: 70927
diff changeset
   699
70925
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   700
end
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   701
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   702
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
   703
  rewrites "bit_word.xor = ((XOR) :: 'a word \<Rightarrow> _)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   704
proof -
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   705
  interpret bit_word: boolean_algebra "(AND)" "(OR)" NOT 0 "- 1 :: 'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   706
  proof
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   707
    show "a AND (b OR c) = a AND b OR a AND c"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   708
      for a b c :: "'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   709
      by transfer (simp add: bit_int.conj_disj_distrib)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   710
    show "a OR b AND c = (a OR b) AND (a OR c)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   711
      for a b c :: "'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   712
      by transfer (simp add: bit_int.disj_conj_distrib)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   713
    show "a AND NOT a = 0" for a :: "'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   714
      by transfer simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   715
    show "a OR NOT a = - 1" for a :: "'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   716
      by transfer simp
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   717
  qed (transfer; simp)+
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   718
  show "boolean_algebra (AND) (OR) NOT 0 (- 1 :: 'a word)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   719
    by (fact bit_word.boolean_algebra_axioms)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   720
  show "bit_word.xor = ((XOR) :: 'a word \<Rightarrow> _)"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   721
  proof (rule ext)+
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   722
    fix a b :: "'a word"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   723
    have "a XOR b = a AND NOT b OR NOT a AND b"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   724
      by transfer (simp add: bit_int.xor_def)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   725
    then show "bit_word.xor a b = a XOR b"
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   726
      by (simp add: bit_word.xor_def)
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   727
  qed
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   728
qed
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   729
525853e4ec80 bit operations for word type
haftmann
parents: 70903
diff changeset
   730
end