src/HOL/ex/Numeral.thy
author wenzelm
Thu, 02 Jul 2009 17:34:14 +0200
changeset 31902 862ae16a799d
parent 31029 e564767f8f78
child 31998 2c7a24f74db9
permissions -rw-r--r--
renamed NamedThmsFun to Named_Thms; simplified/unified names of instances of Named_Thms;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/ex/Numeral.thy
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann
29947
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
     3
*)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
     4
29947
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
     5
header {* An experimental alternative numeral representation. *}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
     6
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
     7
theory Numeral
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
     8
imports Int Inductive
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
     9
begin
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    10
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    11
subsection {* The @{text num} type *}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    12
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    13
datatype num = One | Dig0 num | Dig1 num
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    14
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    15
text {* Increment function for type @{typ num} *}
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    16
31021
53642251a04f farewell to class recpower
haftmann
parents: 30792
diff changeset
    17
primrec inc :: "num \<Rightarrow> num" where
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    18
  "inc One = Dig0 One"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    19
| "inc (Dig0 x) = Dig1 x"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    20
| "inc (Dig1 x) = Dig0 (inc x)"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    21
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    22
text {* Converting between type @{typ num} and type @{typ nat} *}
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    23
31021
53642251a04f farewell to class recpower
haftmann
parents: 30792
diff changeset
    24
primrec nat_of_num :: "num \<Rightarrow> nat" where
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    25
  "nat_of_num One = Suc 0"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    26
| "nat_of_num (Dig0 x) = nat_of_num x + nat_of_num x"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    27
| "nat_of_num (Dig1 x) = Suc (nat_of_num x + nat_of_num x)"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    28
31021
53642251a04f farewell to class recpower
haftmann
parents: 30792
diff changeset
    29
primrec num_of_nat :: "nat \<Rightarrow> num" where
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    30
  "num_of_nat 0 = One"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    31
| "num_of_nat (Suc n) = (if 0 < n then inc (num_of_nat n) else One)"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    32
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
    33
lemma nat_of_num_pos: "0 < nat_of_num x"
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    34
  by (induct x) simp_all
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    35
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
    36
lemma nat_of_num_neq_0: "nat_of_num x \<noteq> 0"
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    37
  by (induct x) simp_all
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    38
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    39
lemma nat_of_num_inc: "nat_of_num (inc x) = Suc (nat_of_num x)"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    40
  by (induct x) simp_all
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    41
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    42
lemma num_of_nat_double:
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    43
  "0 < n \<Longrightarrow> num_of_nat (n + n) = Dig0 (num_of_nat n)"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    44
  by (induct n) simp_all
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    45
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    46
text {*
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    47
  Type @{typ num} is isomorphic to the strictly positive
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    48
  natural numbers.
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    49
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    50
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    51
lemma nat_of_num_inverse: "num_of_nat (nat_of_num x) = x"
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
    52
  by (induct x) (simp_all add: num_of_nat_double nat_of_num_pos)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    53
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    54
lemma num_of_nat_inverse: "0 < n \<Longrightarrow> nat_of_num (num_of_nat n) = n"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    55
  by (induct n) (simp_all add: nat_of_num_inc)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    56
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
    57
lemma num_eq_iff: "x = y \<longleftrightarrow> nat_of_num x = nat_of_num y"
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
    58
  apply safe
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    59
  apply (drule arg_cong [where f=num_of_nat])
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
    60
  apply (simp add: nat_of_num_inverse)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    61
  done
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    62
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    63
lemma num_induct [case_names One inc]:
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    64
  fixes P :: "num \<Rightarrow> bool"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    65
  assumes One: "P One"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    66
    and inc: "\<And>x. P x \<Longrightarrow> P (inc x)"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    67
  shows "P x"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    68
proof -
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    69
  obtain n where n: "Suc n = nat_of_num x"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    70
    by (cases "nat_of_num x", simp_all add: nat_of_num_neq_0)
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    71
  have "P (num_of_nat (Suc n))"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    72
  proof (induct n)
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    73
    case 0 show ?case using One by simp
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    74
  next
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    75
    case (Suc n)
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    76
    then have "P (inc (num_of_nat (Suc n)))" by (rule inc)
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    77
    then show "P (num_of_nat (Suc (Suc n)))" by simp
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    78
  qed
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    79
  with n show "P x"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    80
    by (simp add: nat_of_num_inverse)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    81
qed
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    82
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    83
text {*
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    84
  From now on, there are two possible models for @{typ num}:
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    85
  as positive naturals (rule @{text "num_induct"})
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    86
  and as digit representation (rules @{text "num.induct"}, @{text "num.cases"}).
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    87
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    88
  It is not entirely clear in which context it is better to use
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    89
  the one or the other, or whether the construction should be reversed.
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    90
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    91
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    92
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
    93
subsection {* Numeral operations *}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    94
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    95
ML {*
31902
862ae16a799d renamed NamedThmsFun to Named_Thms;
wenzelm
parents: 31029
diff changeset
    96
structure Dig_Simps = Named_Thms
862ae16a799d renamed NamedThmsFun to Named_Thms;
wenzelm
parents: 31029
diff changeset
    97
(
862ae16a799d renamed NamedThmsFun to Named_Thms;
wenzelm
parents: 31029
diff changeset
    98
  val name = "numeral"
862ae16a799d renamed NamedThmsFun to Named_Thms;
wenzelm
parents: 31029
diff changeset
    99
  val description = "Simplification rules for numerals"
862ae16a799d renamed NamedThmsFun to Named_Thms;
wenzelm
parents: 31029
diff changeset
   100
)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   101
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   102
31902
862ae16a799d renamed NamedThmsFun to Named_Thms;
wenzelm
parents: 31029
diff changeset
   103
setup Dig_Simps.setup
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   104
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   105
instantiation num :: "{plus,times,ord}"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   106
begin
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   107
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   108
definition plus_num :: "num \<Rightarrow> num \<Rightarrow> num" where
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28367
diff changeset
   109
  [code del]: "m + n = num_of_nat (nat_of_num m + nat_of_num n)"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   110
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   111
definition times_num :: "num \<Rightarrow> num \<Rightarrow> num" where
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28367
diff changeset
   112
  [code del]: "m * n = num_of_nat (nat_of_num m * nat_of_num n)"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   113
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   114
definition less_eq_num :: "num \<Rightarrow> num \<Rightarrow> bool" where
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   115
  [code del]: "m \<le> n \<longleftrightarrow> nat_of_num m \<le> nat_of_num n"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   116
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   117
definition less_num :: "num \<Rightarrow> num \<Rightarrow> bool" where
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   118
  [code del]: "m < n \<longleftrightarrow> nat_of_num m < nat_of_num n"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   119
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   120
instance ..
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   121
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   122
end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   123
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   124
lemma nat_of_num_add: "nat_of_num (x + y) = nat_of_num x + nat_of_num y"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   125
  unfolding plus_num_def
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   126
  by (intro num_of_nat_inverse add_pos_pos nat_of_num_pos)
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   127
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   128
lemma nat_of_num_mult: "nat_of_num (x * y) = nat_of_num x * nat_of_num y"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   129
  unfolding times_num_def
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   130
  by (intro num_of_nat_inverse mult_pos_pos nat_of_num_pos)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   131
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   132
lemma Dig_plus [numeral, simp, code]:
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   133
  "One + One = Dig0 One"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   134
  "One + Dig0 m = Dig1 m"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   135
  "One + Dig1 m = Dig0 (m + One)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   136
  "Dig0 n + One = Dig1 n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   137
  "Dig0 n + Dig0 m = Dig0 (n + m)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   138
  "Dig0 n + Dig1 m = Dig1 (n + m)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   139
  "Dig1 n + One = Dig0 (n + One)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   140
  "Dig1 n + Dig0 m = Dig1 (n + m)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   141
  "Dig1 n + Dig1 m = Dig0 (n + m + One)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   142
  by (simp_all add: num_eq_iff nat_of_num_add)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   143
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   144
lemma Dig_times [numeral, simp, code]:
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   145
  "One * One = One"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   146
  "One * Dig0 n = Dig0 n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   147
  "One * Dig1 n = Dig1 n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   148
  "Dig0 n * One = Dig0 n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   149
  "Dig0 n * Dig0 m = Dig0 (n * Dig0 m)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   150
  "Dig0 n * Dig1 m = Dig0 (n * Dig1 m)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   151
  "Dig1 n * One = Dig1 n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   152
  "Dig1 n * Dig0 m = Dig0 (n * Dig0 m + m)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   153
  "Dig1 n * Dig1 m = Dig1 (n * Dig1 m + m)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   154
  by (simp_all add: num_eq_iff nat_of_num_add nat_of_num_mult
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   155
                    left_distrib right_distrib)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   156
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   157
lemma Dig_eq:
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   158
  "One = One \<longleftrightarrow> True"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   159
  "One = Dig0 n \<longleftrightarrow> False"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   160
  "One = Dig1 n \<longleftrightarrow> False"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   161
  "Dig0 m = One \<longleftrightarrow> False"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   162
  "Dig1 m = One \<longleftrightarrow> False"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   163
  "Dig0 m = Dig0 n \<longleftrightarrow> m = n"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   164
  "Dig0 m = Dig1 n \<longleftrightarrow> False"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   165
  "Dig1 m = Dig0 n \<longleftrightarrow> False"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   166
  "Dig1 m = Dig1 n \<longleftrightarrow> m = n"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   167
  by simp_all
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   168
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   169
lemma less_eq_num_code [numeral, simp, code]:
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   170
  "One \<le> n \<longleftrightarrow> True"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   171
  "Dig0 m \<le> One \<longleftrightarrow> False"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   172
  "Dig1 m \<le> One \<longleftrightarrow> False"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   173
  "Dig0 m \<le> Dig0 n \<longleftrightarrow> m \<le> n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   174
  "Dig0 m \<le> Dig1 n \<longleftrightarrow> m \<le> n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   175
  "Dig1 m \<le> Dig1 n \<longleftrightarrow> m \<le> n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   176
  "Dig1 m \<le> Dig0 n \<longleftrightarrow> m < n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   177
  using nat_of_num_pos [of n] nat_of_num_pos [of m]
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   178
  by (auto simp add: less_eq_num_def less_num_def)
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   179
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   180
lemma less_num_code [numeral, simp, code]:
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   181
  "m < One \<longleftrightarrow> False"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   182
  "One < One \<longleftrightarrow> False"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   183
  "One < Dig0 n \<longleftrightarrow> True"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   184
  "One < Dig1 n \<longleftrightarrow> True"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   185
  "Dig0 m < Dig0 n \<longleftrightarrow> m < n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   186
  "Dig0 m < Dig1 n \<longleftrightarrow> m \<le> n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   187
  "Dig1 m < Dig1 n \<longleftrightarrow> m < n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   188
  "Dig1 m < Dig0 n \<longleftrightarrow> m < n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   189
  using nat_of_num_pos [of n] nat_of_num_pos [of m]
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   190
  by (auto simp add: less_eq_num_def less_num_def)
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   191
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   192
text {* Rules using @{text One} and @{text inc} as constructors *}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   193
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   194
lemma add_One: "x + One = inc x"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   195
  by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   196
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   197
lemma add_inc: "x + inc y = inc (x + y)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   198
  by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   199
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   200
lemma mult_One: "x * One = x"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   201
  by (simp add: num_eq_iff nat_of_num_mult)
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   202
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   203
lemma mult_inc: "x * inc y = x * y + x"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   204
  by (simp add: num_eq_iff nat_of_num_mult nat_of_num_add nat_of_num_inc)
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   205
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   206
text {* A double-and-decrement function *}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   207
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   208
primrec DigM :: "num \<Rightarrow> num" where
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   209
  "DigM One = One"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   210
  | "DigM (Dig0 n) = Dig1 (DigM n)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   211
  | "DigM (Dig1 n) = Dig1 (Dig0 n)"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   212
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   213
lemma DigM_plus_one: "DigM n + One = Dig0 n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   214
  by (induct n) simp_all
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   215
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   216
lemma add_One_commute: "One + n = n + One"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   217
  by (induct n) simp_all
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   218
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   219
lemma one_plus_DigM: "One + DigM n = Dig0 n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   220
  unfolding add_One_commute DigM_plus_one ..
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   221
29954
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   222
text {* Squaring and exponentiation *}
29947
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   223
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   224
primrec square :: "num \<Rightarrow> num" where
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   225
  "square One = One"
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   226
| "square (Dig0 n) = Dig0 (Dig0 (square n))"
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   227
| "square (Dig1 n) = Dig1 (Dig0 (square n + n))"
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   228
29954
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   229
primrec pow :: "num \<Rightarrow> num \<Rightarrow> num"
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   230
where
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   231
  "pow x One = x"
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   232
| "pow x (Dig0 y) = square (pow x y)"
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   233
| "pow x (Dig1 y) = x * square (pow x y)"
29947
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   234
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   235
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   236
subsection {* Binary numerals *}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   237
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   238
text {*
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   239
  We embed binary representations into a generic algebraic
29934
5d81dd706206 tuned texts
haftmann
parents: 29667
diff changeset
   240
  structure using @{text of_num}.
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   241
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   242
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   243
class semiring_numeral = semiring + monoid_mult
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   244
begin
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   245
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   246
primrec of_num :: "num \<Rightarrow> 'a" where
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   247
  of_num_One [numeral]: "of_num One = 1"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   248
  | "of_num (Dig0 n) = of_num n + of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   249
  | "of_num (Dig1 n) = of_num n + of_num n + 1"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   250
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
   251
lemma of_num_inc: "of_num (inc x) = of_num x + 1"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
   252
  by (induct x) (simp_all add: add_ac)
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
   253
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   254
lemma of_num_add: "of_num (m + n) = of_num m + of_num n"
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   255
  apply (induct n rule: num_induct)
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   256
  apply (simp_all add: add_One add_inc of_num_inc add_ac)
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   257
  done
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   258
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   259
lemma of_num_mult: "of_num (m * n) = of_num m * of_num n"
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   260
  apply (induct n rule: num_induct)
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   261
  apply (simp add: mult_One)
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   262
  apply (simp add: mult_inc of_num_add of_num_inc right_distrib)
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   263
  done
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   264
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   265
declare of_num.simps [simp del]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   266
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   267
end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   268
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   269
text {*
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   270
  ML stuff and syntax.
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   271
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   272
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   273
ML {*
31027
b5a35bfb3dab detect error cases in mk_num, dest_num
huffman
parents: 31026
diff changeset
   274
fun mk_num k =
b5a35bfb3dab detect error cases in mk_num, dest_num
huffman
parents: 31026
diff changeset
   275
  if k > 1 then
b5a35bfb3dab detect error cases in mk_num, dest_num
huffman
parents: 31026
diff changeset
   276
    let
b5a35bfb3dab detect error cases in mk_num, dest_num
huffman
parents: 31026
diff changeset
   277
      val (l, b) = Integer.div_mod k 2;
b5a35bfb3dab detect error cases in mk_num, dest_num
huffman
parents: 31026
diff changeset
   278
      val bit = (if b = 0 then @{term Dig0} else @{term Dig1});
b5a35bfb3dab detect error cases in mk_num, dest_num
huffman
parents: 31026
diff changeset
   279
    in bit $ (mk_num l) end
b5a35bfb3dab detect error cases in mk_num, dest_num
huffman
parents: 31026
diff changeset
   280
  else if k = 1 then @{term One}
b5a35bfb3dab detect error cases in mk_num, dest_num
huffman
parents: 31026
diff changeset
   281
  else error ("mk_num " ^ string_of_int k);
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   282
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   283
fun dest_num @{term One} = 1
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   284
  | dest_num (@{term Dig0} $ n) = 2 * dest_num n
31027
b5a35bfb3dab detect error cases in mk_num, dest_num
huffman
parents: 31026
diff changeset
   285
  | dest_num (@{term Dig1} $ n) = 2 * dest_num n + 1
b5a35bfb3dab detect error cases in mk_num, dest_num
huffman
parents: 31026
diff changeset
   286
  | dest_num t = raise TERM ("dest_num", [t]);
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   287
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   288
(*FIXME these have to gain proper context via morphisms phi*)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   289
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   290
fun mk_numeral T k = Const (@{const_name of_num}, @{typ num} --> T)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   291
  $ mk_num k
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   292
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   293
fun dest_numeral (Const (@{const_name of_num}, Type ("fun", [@{typ num}, T])) $ t) =
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   294
  (T, dest_num t)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   295
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   296
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   297
syntax
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   298
  "_Numerals" :: "xnum \<Rightarrow> 'a"    ("_")
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   299
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   300
parse_translation {*
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   301
let
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   302
  fun num_of_int n = if n > 0 then case IntInf.quotRem (n, 2)
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   303
     of (0, 1) => Const (@{const_name One}, dummyT)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   304
      | (n, 0) => Const (@{const_name Dig0}, dummyT) $ num_of_int n
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   305
      | (n, 1) => Const (@{const_name Dig1}, dummyT) $ num_of_int n
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   306
    else raise Match;
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   307
  fun numeral_tr [Free (num, _)] =
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   308
        let
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   309
          val {leading_zeros, value, ...} = Syntax.read_xnum num;
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   310
          val _ = leading_zeros = 0 andalso value > 0
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   311
            orelse error ("Bad numeral: " ^ num);
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   312
        in Const (@{const_name of_num}, @{typ num} --> dummyT) $ num_of_int value end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   313
    | numeral_tr ts = raise TERM ("numeral_tr", ts);
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   314
in [("_Numerals", numeral_tr)] end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   315
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   316
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   317
typed_print_translation {*
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   318
let
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   319
  fun dig b n = b + 2 * n; 
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   320
  fun int_of_num' (Const (@{const_syntax Dig0}, _) $ n) =
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   321
        dig 0 (int_of_num' n)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   322
    | int_of_num' (Const (@{const_syntax Dig1}, _) $ n) =
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   323
        dig 1 (int_of_num' n)
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   324
    | int_of_num' (Const (@{const_syntax One}, _)) = 1;
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   325
  fun num_tr' show_sorts T [n] =
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   326
    let
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   327
      val k = int_of_num' n;
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   328
      val t' = Syntax.const "_Numerals" $ Syntax.free ("#" ^ string_of_int k);
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   329
    in case T
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   330
     of Type ("fun", [_, T']) =>
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   331
         if not (! show_types) andalso can Term.dest_Type T' then t'
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   332
         else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T'
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   333
      | T' => if T' = dummyT then t' else raise Match
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   334
    end;
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   335
in [(@{const_syntax of_num}, num_tr')] end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   336
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   337
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   338
subsection {* Class-specific numeral rules *}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   339
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   340
text {*
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   341
  @{const of_num} is a morphism.
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   342
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   343
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   344
subsubsection {* Class @{text semiring_numeral} *}
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   345
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   346
context semiring_numeral
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   347
begin
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   348
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
   349
abbreviation "Num1 \<equiv> of_num One"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   350
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   351
text {*
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   352
  Alas, there is still the duplication of @{term 1},
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   353
  thought the duplicated @{term 0} has disappeared.
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   354
  We could get rid of it by replacing the constructor
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   355
  @{term 1} in @{typ num} by two constructors
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   356
  @{text two} and @{text three}, resulting in a further
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   357
  blow-up.  But it could be worth the effort.
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   358
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   359
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   360
lemma of_num_plus_one [numeral]:
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   361
  "of_num n + 1 = of_num (n + One)"
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   362
  by (simp only: of_num_add of_num_One)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   363
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   364
lemma of_num_one_plus [numeral]:
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   365
  "1 + of_num n = of_num (One + n)"
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   366
  by (simp only: of_num_add of_num_One)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   367
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   368
lemma of_num_plus [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   369
  "of_num m + of_num n = of_num (m + n)"
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   370
  unfolding of_num_add ..
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   371
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   372
lemma of_num_times_one [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   373
  "of_num n * 1 = of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   374
  by simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   375
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   376
lemma of_num_one_times [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   377
  "1 * of_num n = of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   378
  by simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   379
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   380
lemma of_num_times [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   381
  "of_num m * of_num n = of_num (m * n)"
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   382
  unfolding of_num_mult ..
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   383
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   384
end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   385
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   386
subsubsection {*
29947
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   387
  Structures with a zero: class @{text semiring_1}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   388
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   389
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   390
context semiring_1
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   391
begin
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   392
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   393
subclass semiring_numeral ..
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   394
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   395
lemma of_nat_of_num [numeral]: "of_nat (of_num n) = of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   396
  by (induct n)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   397
    (simp_all add: semiring_numeral_class.of_num.simps of_num.simps add_ac)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   398
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   399
declare of_nat_1 [numeral]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   400
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   401
lemma Dig_plus_zero [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   402
  "0 + 1 = 1"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   403
  "0 + of_num n = of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   404
  "1 + 0 = 1"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   405
  "of_num n + 0 = of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   406
  by simp_all
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   407
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   408
lemma Dig_times_zero [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   409
  "0 * 1 = 0"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   410
  "0 * of_num n = 0"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   411
  "1 * 0 = 0"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   412
  "of_num n * 0 = 0"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   413
  by simp_all
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   414
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   415
end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   416
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   417
lemma nat_of_num_of_num: "nat_of_num = of_num"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   418
proof
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   419
  fix n
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
   420
  have "of_num n = nat_of_num n"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
   421
    by (induct n) (simp_all add: of_num.simps)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   422
  then show "nat_of_num n = of_num n" by simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   423
qed
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   424
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   425
subsubsection {*
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   426
  Equality: class @{text semiring_char_0}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   427
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   428
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   429
context semiring_char_0
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   430
begin
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   431
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   432
lemma of_num_eq_iff [numeral]: "of_num m = of_num n \<longleftrightarrow> m = n"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   433
  unfolding of_nat_of_num [symmetric] nat_of_num_of_num [symmetric]
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
   434
    of_nat_eq_iff num_eq_iff ..
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   435
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   436
lemma of_num_eq_one_iff [numeral]: "of_num n = 1 \<longleftrightarrow> n = One"
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   437
  using of_num_eq_iff [of n One] by (simp add: of_num_One)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   438
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   439
lemma one_eq_of_num_iff [numeral]: "1 = of_num n \<longleftrightarrow> One = n"
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   440
  using of_num_eq_iff [of One n] by (simp add: of_num_One)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   441
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   442
end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   443
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   444
subsubsection {*
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   445
  Comparisons: class @{text ordered_semidom}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   446
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   447
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   448
text {*  Could be perhaps more general than here. *}
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   449
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   450
context ordered_semidom
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   451
begin
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   452
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   453
lemma of_num_pos [numeral]: "0 < of_num n"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   454
  by (induct n) (simp_all add: of_num.simps add_pos_pos)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   455
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   456
lemma of_num_less_eq_iff [numeral]: "of_num m \<le> of_num n \<longleftrightarrow> m \<le> n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   457
proof -
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   458
  have "of_nat (of_num m) \<le> of_nat (of_num n) \<longleftrightarrow> m \<le> n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   459
    unfolding less_eq_num_def nat_of_num_of_num of_nat_le_iff ..
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   460
  then show ?thesis by (simp add: of_nat_of_num)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   461
qed
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   462
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   463
lemma of_num_less_eq_one_iff [numeral]: "of_num n \<le> 1 \<longleftrightarrow> n \<le> One"
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   464
  using of_num_less_eq_iff [of n One] by (simp add: of_num_One)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   465
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   466
lemma one_less_eq_of_num_iff [numeral]: "1 \<le> of_num n"
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   467
  using of_num_less_eq_iff [of One n] by (simp add: of_num_One)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   468
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   469
lemma of_num_less_iff [numeral]: "of_num m < of_num n \<longleftrightarrow> m < n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   470
proof -
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   471
  have "of_nat (of_num m) < of_nat (of_num n) \<longleftrightarrow> m < n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   472
    unfolding less_num_def nat_of_num_of_num of_nat_less_iff ..
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   473
  then show ?thesis by (simp add: of_nat_of_num)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   474
qed
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   475
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   476
lemma of_num_less_one_iff [numeral]: "\<not> of_num n < 1"
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   477
  using of_num_less_iff [of n One] by (simp add: of_num_One)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   478
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   479
lemma one_less_of_num_iff [numeral]: "1 < of_num n \<longleftrightarrow> One < n"
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   480
  using of_num_less_iff [of One n] by (simp add: of_num_One)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   481
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   482
lemma of_num_nonneg [numeral]: "0 \<le> of_num n"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   483
  by (induct n) (simp_all add: of_num.simps add_nonneg_nonneg)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   484
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   485
lemma of_num_less_zero_iff [numeral]: "\<not> of_num n < 0"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   486
  by (simp add: not_less of_num_nonneg)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   487
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   488
lemma of_num_le_zero_iff [numeral]: "\<not> of_num n \<le> 0"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   489
  by (simp add: not_le of_num_pos)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   490
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   491
end
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   492
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   493
context ordered_idom
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   494
begin
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   495
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   496
lemma minus_of_num_less_of_num_iff: "- of_num m < of_num n"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   497
proof -
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   498
  have "- of_num m < 0" by (simp add: of_num_pos)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   499
  also have "0 < of_num n" by (simp add: of_num_pos)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   500
  finally show ?thesis .
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   501
qed
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   502
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   503
lemma minus_of_num_less_one_iff: "- of_num n < 1"
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   504
  using minus_of_num_less_of_num_iff [of n One] by (simp add: of_num_One)
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   505
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   506
lemma minus_one_less_of_num_iff: "- 1 < of_num n"
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   507
  using minus_of_num_less_of_num_iff [of One n] by (simp add: of_num_One)
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   508
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   509
lemma minus_one_less_one_iff: "- 1 < 1"
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   510
  using minus_of_num_less_of_num_iff [of One One] by (simp add: of_num_One)
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   511
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   512
lemma minus_of_num_le_of_num_iff: "- of_num m \<le> of_num n"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   513
  by (simp add: less_imp_le minus_of_num_less_of_num_iff)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   514
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   515
lemma minus_of_num_le_one_iff: "- of_num n \<le> 1"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   516
  by (simp add: less_imp_le minus_of_num_less_one_iff)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   517
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   518
lemma minus_one_le_of_num_iff: "- 1 \<le> of_num n"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   519
  by (simp add: less_imp_le minus_one_less_of_num_iff)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   520
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   521
lemma minus_one_le_one_iff: "- 1 \<le> 1"
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   522
  by (simp add: less_imp_le minus_one_less_one_iff)
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   523
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   524
lemma of_num_le_minus_of_num_iff: "\<not> of_num m \<le> - of_num n"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   525
  by (simp add: not_le minus_of_num_less_of_num_iff)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   526
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   527
lemma one_le_minus_of_num_iff: "\<not> 1 \<le> - of_num n"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   528
  by (simp add: not_le minus_of_num_less_one_iff)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   529
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   530
lemma of_num_le_minus_one_iff: "\<not> of_num n \<le> - 1"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   531
  by (simp add: not_le minus_one_less_of_num_iff)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   532
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   533
lemma one_le_minus_one_iff: "\<not> 1 \<le> - 1"
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   534
  by (simp add: not_le minus_one_less_one_iff)
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   535
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   536
lemma of_num_less_minus_of_num_iff: "\<not> of_num m < - of_num n"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   537
  by (simp add: not_less minus_of_num_le_of_num_iff)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   538
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   539
lemma one_less_minus_of_num_iff: "\<not> 1 < - of_num n"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   540
  by (simp add: not_less minus_of_num_le_one_iff)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   541
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   542
lemma of_num_less_minus_one_iff: "\<not> of_num n < - 1"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   543
  by (simp add: not_less minus_one_le_of_num_iff)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   544
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   545
lemma one_less_minus_one_iff: "\<not> 1 < - 1"
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   546
  by (simp add: not_less minus_one_le_one_iff)
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   547
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   548
lemmas le_signed_numeral_special [numeral] =
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   549
  minus_of_num_le_of_num_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   550
  minus_of_num_le_one_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   551
  minus_one_le_of_num_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   552
  minus_one_le_one_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   553
  of_num_le_minus_of_num_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   554
  one_le_minus_of_num_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   555
  of_num_le_minus_one_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   556
  one_le_minus_one_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   557
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   558
lemmas less_signed_numeral_special [numeral] =
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   559
  minus_of_num_less_of_num_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   560
  minus_of_num_less_one_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   561
  minus_one_less_of_num_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   562
  minus_one_less_one_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   563
  of_num_less_minus_of_num_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   564
  one_less_minus_of_num_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   565
  of_num_less_minus_one_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   566
  one_less_minus_one_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   567
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   568
end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   569
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   570
subsubsection {*
29947
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   571
  Structures with subtraction: class @{text semiring_1_minus}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   572
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   573
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   574
class semiring_minus = semiring + minus + zero +
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   575
  assumes minus_inverts_plus1: "a + b = c \<Longrightarrow> c - b = a"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   576
  assumes minus_minus_zero_inverts_plus1: "a + b = c \<Longrightarrow> b - c = 0 - a"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   577
begin
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   578
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   579
lemma minus_inverts_plus2: "a + b = c \<Longrightarrow> c - a = b"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   580
  by (simp add: add_ac minus_inverts_plus1 [of b a])
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   581
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   582
lemma minus_minus_zero_inverts_plus2: "a + b = c \<Longrightarrow> a - c = 0 - b"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   583
  by (simp add: add_ac minus_minus_zero_inverts_plus1 [of b a])
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   584
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   585
end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   586
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   587
class semiring_1_minus = semiring_1 + semiring_minus
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   588
begin
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   589
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   590
lemma Dig_of_num_pos:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   591
  assumes "k + n = m"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   592
  shows "of_num m - of_num n = of_num k"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   593
  using assms by (simp add: of_num_plus minus_inverts_plus1)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   594
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   595
lemma Dig_of_num_zero:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   596
  shows "of_num n - of_num n = 0"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   597
  by (rule minus_inverts_plus1) simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   598
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   599
lemma Dig_of_num_neg:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   600
  assumes "k + m = n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   601
  shows "of_num m - of_num n = 0 - of_num k"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   602
  by (rule minus_minus_zero_inverts_plus1) (simp add: of_num_plus assms)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   603
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   604
lemmas Dig_plus_eval =
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   605
  of_num_plus of_num_eq_iff Dig_plus refl [of One, THEN eqTrueI] num.inject
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   606
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   607
simproc_setup numeral_minus ("of_num m - of_num n") = {*
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   608
  let
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   609
    (*TODO proper implicit use of morphism via pattern antiquotations*)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   610
    fun cdest_of_num ct = (snd o split_last o snd o Drule.strip_comb) ct;
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   611
    fun cdest_minus ct = case (rev o snd o Drule.strip_comb) ct of [n, m] => (m, n);
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   612
    fun attach_num ct = (dest_num (Thm.term_of ct), ct);
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   613
    fun cdifference t = (pairself (attach_num o cdest_of_num) o cdest_minus) t;
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   614
    val simplify = MetaSimplifier.rewrite false (map mk_meta_eq @{thms Dig_plus_eval});
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   615
    fun cert ck cl cj = @{thm eqTrueE} OF [@{thm meta_eq_to_obj_eq} OF [simplify (Drule.list_comb (@{cterm "op = :: num \<Rightarrow> _"},
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   616
      [Drule.list_comb (@{cterm "op + :: num \<Rightarrow> _"}, [ck, cl]), cj]))]];
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   617
  in fn phi => fn _ => fn ct => case try cdifference ct
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   618
   of NONE => (NONE)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   619
    | SOME ((k, ck), (l, cl)) => SOME (let val j = k - l in if j = 0
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   620
        then MetaSimplifier.rewrite false [mk_meta_eq (Morphism.thm phi @{thm Dig_of_num_zero})] ct
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   621
        else mk_meta_eq (let
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   622
          val cj = Thm.cterm_of (Thm.theory_of_cterm ct) (mk_num (abs j));
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   623
        in
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   624
          (if j > 0 then (Morphism.thm phi @{thm Dig_of_num_pos}) OF [cert cj cl ck]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   625
          else (Morphism.thm phi @{thm Dig_of_num_neg}) OF [cert cj ck cl])
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   626
        end) end)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   627
  end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   628
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   629
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   630
lemma Dig_of_num_minus_zero [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   631
  "of_num n - 0 = of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   632
  by (simp add: minus_inverts_plus1)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   633
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   634
lemma Dig_one_minus_zero [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   635
  "1 - 0 = 1"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   636
  by (simp add: minus_inverts_plus1)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   637
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   638
lemma Dig_one_minus_one [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   639
  "1 - 1 = 0"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   640
  by (simp add: minus_inverts_plus1)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   641
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   642
lemma Dig_of_num_minus_one [numeral]:
29941
b951d80774d5 replace dec with double-and-decrement function
huffman
parents: 29667
diff changeset
   643
  "of_num (Dig0 n) - 1 = of_num (DigM n)"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   644
  "of_num (Dig1 n) - 1 = of_num (Dig0 n)"
29941
b951d80774d5 replace dec with double-and-decrement function
huffman
parents: 29667
diff changeset
   645
  by (auto intro: minus_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   646
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   647
lemma Dig_one_minus_of_num [numeral]:
29941
b951d80774d5 replace dec with double-and-decrement function
huffman
parents: 29667
diff changeset
   648
  "1 - of_num (Dig0 n) = 0 - of_num (DigM n)"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   649
  "1 - of_num (Dig1 n) = 0 - of_num (Dig0 n)"
29941
b951d80774d5 replace dec with double-and-decrement function
huffman
parents: 29667
diff changeset
   650
  by (auto intro: minus_minus_zero_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   651
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   652
end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   653
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   654
subsubsection {*
29947
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   655
  Structures with negation: class @{text ring_1}
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   656
*}
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   657
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   658
context ring_1
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   659
begin
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   660
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   661
subclass semiring_1_minus
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 28823
diff changeset
   662
  proof qed (simp_all add: algebra_simps)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   663
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   664
lemma Dig_zero_minus_of_num [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   665
  "0 - of_num n = - of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   666
  by simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   667
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   668
lemma Dig_zero_minus_one [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   669
  "0 - 1 = - 1"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   670
  by simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   671
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   672
lemma Dig_uminus_uminus [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   673
  "- (- of_num n) = of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   674
  by simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   675
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   676
lemma Dig_plus_uminus [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   677
  "of_num m + - of_num n = of_num m - of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   678
  "- of_num m + of_num n = of_num n - of_num m"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   679
  "- of_num m + - of_num n = - (of_num m + of_num n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   680
  "of_num m - - of_num n = of_num m + of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   681
  "- of_num m - of_num n = - (of_num m + of_num n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   682
  "- of_num m - - of_num n = of_num n - of_num m"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   683
  by (simp_all add: diff_minus add_commute)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   684
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   685
lemma Dig_times_uminus [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   686
  "- of_num n * of_num m = - (of_num n * of_num m)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   687
  "of_num n * - of_num m = - (of_num n * of_num m)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   688
  "- of_num n * - of_num m = of_num n * of_num m"
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   689
  by simp_all
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   690
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   691
lemma of_int_of_num [numeral]: "of_int (of_num n) = of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   692
by (induct n)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   693
  (simp_all only: of_num.simps semiring_numeral_class.of_num.simps of_int_add, simp_all)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   694
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   695
declare of_int_1 [numeral]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   696
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   697
end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   698
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   699
subsubsection {*
29954
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   700
  Structures with exponentiation
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   701
*}
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   702
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   703
lemma of_num_square: "of_num (square x) = of_num x * of_num x"
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   704
by (induct x)
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   705
   (simp_all add: of_num.simps of_num_add algebra_simps)
29954
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   706
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   707
lemma of_num_pow: "of_num (pow x y) = of_num x ^ of_num y"
29954
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   708
by (induct y)
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   709
   (simp_all add: of_num.simps of_num_square of_num_mult power_add)
29954
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   710
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   711
lemma power_of_num [numeral]: "of_num x ^ of_num y = of_num (pow x y)"
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   712
  unfolding of_num_pow ..
29954
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   713
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   714
lemma power_zero_of_num [numeral]:
31029
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   715
  "0 ^ of_num n = (0::'a::semiring_1)"
29954
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   716
  using of_num_pos [where n=n and ?'a=nat]
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   717
  by (simp add: power_0_left)
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   718
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   719
lemma power_minus_Dig0 [numeral]:
31029
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   720
  fixes x :: "'a::ring_1"
29954
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   721
  shows "(- x) ^ of_num (Dig0 n) = x ^ of_num (Dig0 n)"
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   722
  by (induct n rule: num_induct) (simp_all add: of_num.simps of_num_inc)
29954
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   723
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   724
lemma power_minus_Dig1 [numeral]:
31029
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   725
  fixes x :: "'a::ring_1"
29954
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   726
  shows "(- x) ^ of_num (Dig1 n) = - (x ^ of_num (Dig1 n))"
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   727
  by (induct n rule: num_induct) (simp_all add: of_num.simps of_num_inc)
29954
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   728
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   729
declare power_one [numeral]
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   730
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   731
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   732
subsubsection {*
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   733
  Greetings to @{typ nat}.
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   734
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   735
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   736
instance nat :: semiring_1_minus proof qed simp_all
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   737
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   738
lemma Suc_of_num [numeral]: "Suc (of_num n) = of_num (n + One)"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   739
  unfolding of_num_plus_one [symmetric] by simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   740
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   741
lemma nat_number:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   742
  "1 = Suc 0"
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   743
  "of_num One = Suc 0"
29941
b951d80774d5 replace dec with double-and-decrement function
huffman
parents: 29667
diff changeset
   744
  "of_num (Dig0 n) = Suc (of_num (DigM n))"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   745
  "of_num (Dig1 n) = Suc (of_num (Dig0 n))"
29941
b951d80774d5 replace dec with double-and-decrement function
huffman
parents: 29667
diff changeset
   746
  by (simp_all add: of_num.simps DigM_plus_one Suc_of_num)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   747
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   748
declare diff_0_eq_0 [numeral]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   749
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   750
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   751
subsection {* Code generator setup for @{typ int} *}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   752
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   753
definition Pls :: "num \<Rightarrow> int" where
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   754
  [simp, code post]: "Pls n = of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   755
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   756
definition Mns :: "num \<Rightarrow> int" where
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   757
  [simp, code post]: "Mns n = - of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   758
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   759
code_datatype "0::int" Pls Mns
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   760
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   761
lemmas [code inline] = Pls_def [symmetric] Mns_def [symmetric]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   762
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   763
definition sub :: "num \<Rightarrow> num \<Rightarrow> int" where
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28367
diff changeset
   764
  [simp, code del]: "sub m n = (of_num m - of_num n)"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   765
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   766
definition dup :: "int \<Rightarrow> int" where
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28367
diff changeset
   767
  [code del]: "dup k = 2 * k"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   768
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   769
lemma Dig_sub [code]:
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   770
  "sub One One = 0"
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   771
  "sub (Dig0 m) One = of_num (DigM m)"
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   772
  "sub (Dig1 m) One = of_num (Dig0 m)"
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   773
  "sub One (Dig0 n) = - of_num (DigM n)"
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   774
  "sub One (Dig1 n) = - of_num (Dig0 n)"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   775
  "sub (Dig0 m) (Dig0 n) = dup (sub m n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   776
  "sub (Dig1 m) (Dig1 n) = dup (sub m n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   777
  "sub (Dig1 m) (Dig0 n) = dup (sub m n) + 1"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   778
  "sub (Dig0 m) (Dig1 n) = dup (sub m n) - 1"
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 28823
diff changeset
   779
  apply (simp_all add: dup_def algebra_simps)
29941
b951d80774d5 replace dec with double-and-decrement function
huffman
parents: 29667
diff changeset
   780
  apply (simp_all add: of_num_plus one_plus_DigM)[4]
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   781
  apply (simp_all add: of_num.simps)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   782
  done
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   783
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   784
lemma dup_code [code]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   785
  "dup 0 = 0"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   786
  "dup (Pls n) = Pls (Dig0 n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   787
  "dup (Mns n) = Mns (Dig0 n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   788
  by (simp_all add: dup_def of_num.simps)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   789
  
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28367
diff changeset
   790
lemma [code, code del]:
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   791
  "(1 :: int) = 1"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   792
  "(op + :: int \<Rightarrow> int \<Rightarrow> int) = op +"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   793
  "(uminus :: int \<Rightarrow> int) = uminus"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   794
  "(op - :: int \<Rightarrow> int \<Rightarrow> int) = op -"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   795
  "(op * :: int \<Rightarrow> int \<Rightarrow> int) = op *"
28367
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   796
  "(eq_class.eq :: int \<Rightarrow> int \<Rightarrow> bool) = eq_class.eq"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   797
  "(op \<le> :: int \<Rightarrow> int \<Rightarrow> bool) = op \<le>"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   798
  "(op < :: int \<Rightarrow> int \<Rightarrow> bool) = op <"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   799
  by rule+
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   800
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   801
lemma one_int_code [code]:
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   802
  "1 = Pls One"
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   803
  by (simp add: of_num_One)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   804
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   805
lemma plus_int_code [code]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   806
  "k + 0 = (k::int)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   807
  "0 + l = (l::int)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   808
  "Pls m + Pls n = Pls (m + n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   809
  "Pls m - Pls n = sub m n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   810
  "Mns m + Mns n = Mns (m + n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   811
  "Mns m - Mns n = sub n m"
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   812
  by (simp_all add: of_num_add)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   813
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   814
lemma uminus_int_code [code]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   815
  "uminus 0 = (0::int)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   816
  "uminus (Pls m) = Mns m"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   817
  "uminus (Mns m) = Pls m"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   818
  by simp_all
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   819
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   820
lemma minus_int_code [code]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   821
  "k - 0 = (k::int)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   822
  "0 - l = uminus (l::int)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   823
  "Pls m - Pls n = sub m n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   824
  "Pls m - Mns n = Pls (m + n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   825
  "Mns m - Pls n = Mns (m + n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   826
  "Mns m - Mns n = sub n m"
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   827
  by (simp_all add: of_num_add)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   828
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   829
lemma times_int_code [code]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   830
  "k * 0 = (0::int)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   831
  "0 * l = (0::int)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   832
  "Pls m * Pls n = Pls (m * n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   833
  "Pls m * Mns n = Mns (m * n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   834
  "Mns m * Pls n = Mns (m * n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   835
  "Mns m * Mns n = Pls (m * n)"
31028
9c5b6a92da39 clean up unsigned numeral proofs
huffman
parents: 31027
diff changeset
   836
  by (simp_all add: of_num_mult)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   837
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   838
lemma eq_int_code [code]:
28367
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   839
  "eq_class.eq 0 (0::int) \<longleftrightarrow> True"
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   840
  "eq_class.eq 0 (Pls l) \<longleftrightarrow> False"
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   841
  "eq_class.eq 0 (Mns l) \<longleftrightarrow> False"
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   842
  "eq_class.eq (Pls k) 0 \<longleftrightarrow> False"
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   843
  "eq_class.eq (Pls k) (Pls l) \<longleftrightarrow> eq_class.eq k l"
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   844
  "eq_class.eq (Pls k) (Mns l) \<longleftrightarrow> False"
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   845
  "eq_class.eq (Mns k) 0 \<longleftrightarrow> False"
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   846
  "eq_class.eq (Mns k) (Pls l) \<longleftrightarrow> False"
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   847
  "eq_class.eq (Mns k) (Mns l) \<longleftrightarrow> eq_class.eq k l"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   848
  using of_num_pos [of l, where ?'a = int] of_num_pos [of k, where ?'a = int]
28367
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   849
  by (simp_all add: of_num_eq_iff eq)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   850
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   851
lemma less_eq_int_code [code]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   852
  "0 \<le> (0::int) \<longleftrightarrow> True"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   853
  "0 \<le> Pls l \<longleftrightarrow> True"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   854
  "0 \<le> Mns l \<longleftrightarrow> False"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   855
  "Pls k \<le> 0 \<longleftrightarrow> False"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   856
  "Pls k \<le> Pls l \<longleftrightarrow> k \<le> l"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   857
  "Pls k \<le> Mns l \<longleftrightarrow> False"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   858
  "Mns k \<le> 0 \<longleftrightarrow> True"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   859
  "Mns k \<le> Pls l \<longleftrightarrow> True"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   860
  "Mns k \<le> Mns l \<longleftrightarrow> l \<le> k"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   861
  using of_num_pos [of l, where ?'a = int] of_num_pos [of k, where ?'a = int]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   862
  by (simp_all add: of_num_less_eq_iff)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   863
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   864
lemma less_int_code [code]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   865
  "0 < (0::int) \<longleftrightarrow> False"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   866
  "0 < Pls l \<longleftrightarrow> True"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   867
  "0 < Mns l \<longleftrightarrow> False"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   868
  "Pls k < 0 \<longleftrightarrow> False"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   869
  "Pls k < Pls l \<longleftrightarrow> k < l"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   870
  "Pls k < Mns l \<longleftrightarrow> False"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   871
  "Mns k < 0 \<longleftrightarrow> True"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   872
  "Mns k < Pls l \<longleftrightarrow> True"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   873
  "Mns k < Mns l \<longleftrightarrow> l < k"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   874
  using of_num_pos [of l, where ?'a = int] of_num_pos [of k, where ?'a = int]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   875
  by (simp_all add: of_num_less_iff)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   876
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   877
lemma [code inline del]: "(0::int) \<equiv> Numeral0" by simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   878
lemma [code inline del]: "(1::int) \<equiv> Numeral1" by simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   879
declare zero_is_num_zero [code inline del]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   880
declare one_is_num_one [code inline del]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   881
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   882
hide (open) const sub dup
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   883
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   884
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   885
subsection {* Numeral equations as default simplification rules *}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   886
31029
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   887
declare (in semiring_numeral) of_num_One [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   888
declare (in semiring_numeral) of_num_plus_one [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   889
declare (in semiring_numeral) of_num_one_plus [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   890
declare (in semiring_numeral) of_num_plus [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   891
declare (in semiring_numeral) of_num_times [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   892
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   893
declare (in semiring_1) of_nat_of_num [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   894
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   895
declare (in semiring_char_0) of_num_eq_iff [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   896
declare (in semiring_char_0) of_num_eq_one_iff [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   897
declare (in semiring_char_0) one_eq_of_num_iff [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   898
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   899
declare (in ordered_semidom) of_num_pos [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   900
declare (in ordered_semidom) of_num_less_eq_iff [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   901
declare (in ordered_semidom) of_num_less_eq_one_iff [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   902
declare (in ordered_semidom) one_less_eq_of_num_iff [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   903
declare (in ordered_semidom) of_num_less_iff [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   904
declare (in ordered_semidom) of_num_less_one_iff [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   905
declare (in ordered_semidom) one_less_of_num_iff [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   906
declare (in ordered_semidom) of_num_nonneg [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   907
declare (in ordered_semidom) of_num_less_zero_iff [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   908
declare (in ordered_semidom) of_num_le_zero_iff [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   909
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   910
declare (in ordered_idom) le_signed_numeral_special [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   911
declare (in ordered_idom) less_signed_numeral_special [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   912
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   913
declare (in semiring_1_minus) Dig_of_num_minus_one [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   914
declare (in semiring_1_minus) Dig_one_minus_of_num [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   915
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   916
declare (in ring_1) Dig_plus_uminus [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   917
declare (in ring_1) of_int_of_num [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   918
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   919
declare power_of_num [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   920
declare power_zero_of_num [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   921
declare power_minus_Dig0 [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   922
declare power_minus_Dig1 [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   923
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   924
declare Suc_of_num [simp]
e564767f8f78 used named theorems for declaring numeral simps
huffman
parents: 31028
diff changeset
   925
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   926
thm numeral
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   927
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   928
31025
6d2188134536 reorient simproc for unsigned numerals
huffman
parents: 31021
diff changeset
   929
subsection {* Simplification Procedures *}
6d2188134536 reorient simproc for unsigned numerals
huffman
parents: 31021
diff changeset
   930
31026
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   931
subsubsection {* Reorientation of equalities *}
31025
6d2188134536 reorient simproc for unsigned numerals
huffman
parents: 31021
diff changeset
   932
6d2188134536 reorient simproc for unsigned numerals
huffman
parents: 31021
diff changeset
   933
setup {*
6d2188134536 reorient simproc for unsigned numerals
huffman
parents: 31021
diff changeset
   934
  ReorientProc.add
6d2188134536 reorient simproc for unsigned numerals
huffman
parents: 31021
diff changeset
   935
    (fn Const(@{const_name of_num}, _) $ _ => true
6d2188134536 reorient simproc for unsigned numerals
huffman
parents: 31021
diff changeset
   936
      | Const(@{const_name uminus}, _) $
6d2188134536 reorient simproc for unsigned numerals
huffman
parents: 31021
diff changeset
   937
          (Const(@{const_name of_num}, _) $ _) => true
6d2188134536 reorient simproc for unsigned numerals
huffman
parents: 31021
diff changeset
   938
      | _ => false)
6d2188134536 reorient simproc for unsigned numerals
huffman
parents: 31021
diff changeset
   939
*}
6d2188134536 reorient simproc for unsigned numerals
huffman
parents: 31021
diff changeset
   940
6d2188134536 reorient simproc for unsigned numerals
huffman
parents: 31021
diff changeset
   941
simproc_setup reorient_num ("of_num n = x" | "- of_num m = y") = ReorientProc.proc
6d2188134536 reorient simproc for unsigned numerals
huffman
parents: 31021
diff changeset
   942
31026
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   943
subsubsection {* Constant folding for multiplication in semirings *}
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   944
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   945
context semiring_numeral
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   946
begin
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   947
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   948
lemma mult_of_num_commute: "x * of_num n = of_num n * x"
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   949
by (induct n)
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   950
  (simp_all only: of_num.simps left_distrib right_distrib mult_1_left mult_1_right)
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   951
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   952
definition
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   953
  "commutes_with a b \<longleftrightarrow> a * b = b * a"
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   954
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   955
lemma commutes_with_commute: "commutes_with a b \<Longrightarrow> a * b = b * a"
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   956
unfolding commutes_with_def .
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   957
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   958
lemma commutes_with_left_commute: "commutes_with a b \<Longrightarrow> a * (b * c) = b * (a * c)"
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   959
unfolding commutes_with_def by (simp only: mult_assoc [symmetric])
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   960
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   961
lemma commutes_with_numeral: "commutes_with x (of_num n)" "commutes_with (of_num n) x"
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   962
unfolding commutes_with_def by (simp_all add: mult_of_num_commute)
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   963
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   964
lemmas mult_ac_numeral =
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   965
  mult_assoc
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   966
  commutes_with_commute
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   967
  commutes_with_left_commute
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   968
  commutes_with_numeral
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   969
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   970
end
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   971
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   972
ML {*
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   973
structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA =
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   974
struct
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   975
  val assoc_ss = HOL_ss addsimps @{thms mult_ac_numeral}
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   976
  val eq_reflection = eq_reflection
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   977
  fun is_numeral (Const(@{const_name of_num}, _) $ _) = true
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   978
    | is_numeral _ = false;
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   979
end;
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   980
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   981
structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   982
*}
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   983
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   984
simproc_setup semiring_assoc_fold' ("(a::'a::semiring_numeral) * b") =
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   985
  {* fn phi => fn ss => fn ct =>
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   986
    Semiring_Times_Assoc.proc ss (Thm.term_of ct) *}
020efcbfe2d8 add semiring_assoc_fold simproc for unsigned numerals
huffman
parents: 31025
diff changeset
   987
31025
6d2188134536 reorient simproc for unsigned numerals
huffman
parents: 31021
diff changeset
   988
6d2188134536 reorient simproc for unsigned numerals
huffman
parents: 31021
diff changeset
   989
subsection {* Toy examples *}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   990
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   991
definition "bar \<longleftrightarrow> #4 * #2 + #7 = (#8 :: nat) \<and> #4 * #2 + #7 \<ge> (#8 :: int) - #3"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   992
code_thms bar
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   993
export_code bar in Haskell file -
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   994
export_code bar in OCaml module_name Foo file -
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   995
ML {* @{code bar} *}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   996
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   997
end