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