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