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