src/HOL/Library/Abstract_Rat.thy
author huffman
Wed, 18 Feb 2009 19:32:26 -0800
changeset 29985 57975b45ab70
parent 29667 53103fc8ffa3
child 30042 31039ee583fa
permissions -rw-r--r--
split polynomial-related stuff from Deriv.thy into Library/Poly_Deriv.thy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Library/Abstract_Rat.thy
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
     2
    ID:         $Id$
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
     3
    Author:     Amine Chaieb
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
     4
*)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
     5
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
     6
header {* Abstract rational numbers *}
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
     7
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
     8
theory Abstract_Rat
27368
9f90ac19e32b established Plain theory and image
haftmann
parents: 26509
diff changeset
     9
imports Plain GCD
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    10
begin
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    11
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    12
types Num = "int \<times> int"
25005
60e5516c7b06 replaced syntax/translations by abbreviation;
wenzelm
parents: 24197
diff changeset
    13
60e5516c7b06 replaced syntax/translations by abbreviation;
wenzelm
parents: 24197
diff changeset
    14
abbreviation
60e5516c7b06 replaced syntax/translations by abbreviation;
wenzelm
parents: 24197
diff changeset
    15
  Num0_syn :: Num ("0\<^sub>N")
60e5516c7b06 replaced syntax/translations by abbreviation;
wenzelm
parents: 24197
diff changeset
    16
where "0\<^sub>N \<equiv> (0, 0)"
60e5516c7b06 replaced syntax/translations by abbreviation;
wenzelm
parents: 24197
diff changeset
    17
60e5516c7b06 replaced syntax/translations by abbreviation;
wenzelm
parents: 24197
diff changeset
    18
abbreviation
60e5516c7b06 replaced syntax/translations by abbreviation;
wenzelm
parents: 24197
diff changeset
    19
  Numi_syn :: "int \<Rightarrow> Num" ("_\<^sub>N")
60e5516c7b06 replaced syntax/translations by abbreviation;
wenzelm
parents: 24197
diff changeset
    20
where "i\<^sub>N \<equiv> (i, 1)"
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    21
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    22
definition
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    23
  isnormNum :: "Num \<Rightarrow> bool"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    24
where
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
    25
  "isnormNum = (\<lambda>(a,b). (if a = 0 then b = 0 else b > 0 \<and> zgcd a b = 1))"
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    26
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    27
definition
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    28
  normNum :: "Num \<Rightarrow> Num"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    29
where
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    30
  "normNum = (\<lambda>(a,b). (if a=0 \<or> b = 0 then (0,0) else 
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
    31
  (let g = zgcd a b 
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    32
   in if b > 0 then (a div g, b div g) else (- (a div g), - (b div g)))))"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    33
27668
6eb20b2cecf8 Tuned and simplified proofs
chaieb
parents: 27567
diff changeset
    34
declare zgcd_zdvd1[presburger] 
6eb20b2cecf8 Tuned and simplified proofs
chaieb
parents: 27567
diff changeset
    35
declare zgcd_zdvd2[presburger]
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    36
lemma normNum_isnormNum [simp]: "isnormNum (normNum x)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    37
proof -
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    38
  have " \<exists> a b. x = (a,b)" by auto
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    39
  then obtain a b where x[simp]: "x = (a,b)" by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    40
  {assume "a=0 \<or> b = 0" hence ?thesis by (simp add: normNum_def isnormNum_def)}  
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    41
  moreover
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    42
  {assume anz: "a \<noteq> 0" and bnz: "b \<noteq> 0" 
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
    43
    let ?g = "zgcd a b"
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    44
    let ?a' = "a div ?g"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    45
    let ?b' = "b div ?g"
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
    46
    let ?g' = "zgcd ?a' ?b'"
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
    47
    from anz bnz have "?g \<noteq> 0" by simp  with zgcd_pos[of a b] 
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    48
    have gpos: "?g > 0"  by arith
27668
6eb20b2cecf8 Tuned and simplified proofs
chaieb
parents: 27567
diff changeset
    49
    have gdvd: "?g dvd a" "?g dvd b" by arith+ 
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    50
    from zdvd_mult_div_cancel[OF gdvd(1)] zdvd_mult_div_cancel[OF gdvd(2)]
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    51
    anz bnz
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    52
    have nz':"?a' \<noteq> 0" "?b' \<noteq> 0" 
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
    53
      by - (rule notI,simp add:zgcd_def)+
27668
6eb20b2cecf8 Tuned and simplified proofs
chaieb
parents: 27567
diff changeset
    54
    from anz bnz have stupid: "a \<noteq> 0 \<or> b \<noteq> 0" by arith 
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
    55
    from div_zgcd_relprime[OF stupid] have gp1: "?g' = 1" .
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    56
    from bnz have "b < 0 \<or> b > 0" by arith
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    57
    moreover
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    58
    {assume b: "b > 0"
27668
6eb20b2cecf8 Tuned and simplified proofs
chaieb
parents: 27567
diff changeset
    59
      from b have "?b' \<ge> 0" 
6eb20b2cecf8 Tuned and simplified proofs
chaieb
parents: 27567
diff changeset
    60
	by (presburger add: pos_imp_zdiv_nonneg_iff[OF gpos])  
6eb20b2cecf8 Tuned and simplified proofs
chaieb
parents: 27567
diff changeset
    61
      with nz' have b': "?b' > 0" by arith 
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    62
      from b b' anz bnz nz' gp1 have ?thesis 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    63
	by (simp add: isnormNum_def normNum_def Let_def split_def fst_conv snd_conv)}
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    64
    moreover {assume b: "b < 0"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    65
      {assume b': "?b' \<ge> 0" 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    66
	from gpos have th: "?g \<ge> 0" by arith
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    67
	from mult_nonneg_nonneg[OF th b'] zdvd_mult_div_cancel[OF gdvd(2)]
27668
6eb20b2cecf8 Tuned and simplified proofs
chaieb
parents: 27567
diff changeset
    68
	have False using b by arith }
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    69
      hence b': "?b' < 0" by (presburger add: linorder_not_le[symmetric]) 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    70
      from anz bnz nz' b b' gp1 have ?thesis 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    71
	by (simp add: isnormNum_def normNum_def Let_def split_def fst_conv snd_conv)}
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    72
    ultimately have ?thesis by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    73
  }
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    74
  ultimately show ?thesis by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    75
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    76
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    77
text {* Arithmetic over Num *}
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    78
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    79
definition
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    80
  Nadd :: "Num \<Rightarrow> Num \<Rightarrow> Num" (infixl "+\<^sub>N" 60)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    81
where
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    82
  "Nadd = (\<lambda>(a,b) (a',b'). if a = 0 \<or> b = 0 then normNum(a',b') 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    83
    else if a'=0 \<or> b' = 0 then normNum(a,b) 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    84
    else normNum(a*b' + b*a', b*b'))"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    85
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    86
definition
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    87
  Nmul :: "Num \<Rightarrow> Num \<Rightarrow> Num" (infixl "*\<^sub>N" 60)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    88
where
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
    89
  "Nmul = (\<lambda>(a,b) (a',b'). let g = zgcd (a*a') (b*b') 
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    90
    in (a*a' div g, b*b' div g))"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    91
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    92
definition
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    93
  Nneg :: "Num \<Rightarrow> Num" ("~\<^sub>N")
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    94
where
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    95
  "Nneg \<equiv> (\<lambda>(a,b). (-a,b))"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    96
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    97
definition
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    98
  Nsub :: "Num \<Rightarrow> Num \<Rightarrow> Num" (infixl "-\<^sub>N" 60)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
    99
where
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   100
  "Nsub = (\<lambda>a b. a +\<^sub>N ~\<^sub>N b)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   101
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   102
definition
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   103
  Ninv :: "Num \<Rightarrow> Num" 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   104
where
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   105
  "Ninv \<equiv> \<lambda>(a,b). if a < 0 then (-b, \<bar>a\<bar>) else (b,a)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   106
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   107
definition
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   108
  Ndiv :: "Num \<Rightarrow> Num \<Rightarrow> Num" (infixl "\<div>\<^sub>N" 60)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   109
where
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   110
  "Ndiv \<equiv> \<lambda>a b. a *\<^sub>N Ninv b"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   111
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   112
lemma Nneg_normN[simp]: "isnormNum x \<Longrightarrow> isnormNum (~\<^sub>N x)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   113
  by(simp add: isnormNum_def Nneg_def split_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   114
lemma Nadd_normN[simp]: "isnormNum (x +\<^sub>N y)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   115
  by (simp add: Nadd_def split_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   116
lemma Nsub_normN[simp]: "\<lbrakk> isnormNum y\<rbrakk> \<Longrightarrow> isnormNum (x -\<^sub>N y)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   117
  by (simp add: Nsub_def split_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   118
lemma Nmul_normN[simp]: assumes xn:"isnormNum x" and yn: "isnormNum y"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   119
  shows "isnormNum (x *\<^sub>N y)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   120
proof-
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   121
  have "\<exists>a b. x = (a,b)" and "\<exists> a' b'. y = (a',b')" by auto
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   122
  then obtain a b a' b' where ab: "x = (a,b)"  and ab': "y = (a',b')" by blast 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   123
  {assume "a = 0"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   124
    hence ?thesis using xn ab ab'
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   125
      by (simp add: zgcd_def isnormNum_def Let_def Nmul_def split_def)}
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   126
  moreover
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   127
  {assume "a' = 0"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   128
    hence ?thesis using yn ab ab' 
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   129
      by (simp add: zgcd_def isnormNum_def Let_def Nmul_def split_def)}
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   130
  moreover
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   131
  {assume a: "a \<noteq>0" and a': "a'\<noteq>0"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   132
    hence bp: "b > 0" "b' > 0" using xn yn ab ab' by (simp_all add: isnormNum_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   133
    from mult_pos_pos[OF bp] have "x *\<^sub>N y = normNum (a*a', b*b')" 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   134
      using ab ab' a a' bp by (simp add: Nmul_def Let_def split_def normNum_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   135
    hence ?thesis by simp}
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   136
  ultimately show ?thesis by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   137
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   138
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   139
lemma Ninv_normN[simp]: "isnormNum x \<Longrightarrow> isnormNum (Ninv x)"
25005
60e5516c7b06 replaced syntax/translations by abbreviation;
wenzelm
parents: 24197
diff changeset
   140
  by (simp add: Ninv_def isnormNum_def split_def)
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   141
    (cases "fst x = 0", auto simp add: zgcd_commute)
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   142
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   143
lemma isnormNum_int[simp]: 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   144
  "isnormNum 0\<^sub>N" "isnormNum (1::int)\<^sub>N" "i \<noteq> 0 \<Longrightarrow> isnormNum i\<^sub>N"
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   145
  by (simp_all add: isnormNum_def zgcd_def)
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   146
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   147
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   148
text {* Relations over Num *}
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   149
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   150
definition
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   151
  Nlt0:: "Num \<Rightarrow> bool" ("0>\<^sub>N")
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   152
where
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   153
  "Nlt0 = (\<lambda>(a,b). a < 0)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   154
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   155
definition
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   156
  Nle0:: "Num \<Rightarrow> bool" ("0\<ge>\<^sub>N")
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   157
where
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   158
  "Nle0 = (\<lambda>(a,b). a \<le> 0)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   159
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   160
definition
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   161
  Ngt0:: "Num \<Rightarrow> bool" ("0<\<^sub>N")
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   162
where
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   163
  "Ngt0 = (\<lambda>(a,b). a > 0)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   164
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   165
definition
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   166
  Nge0:: "Num \<Rightarrow> bool" ("0\<le>\<^sub>N")
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   167
where
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   168
  "Nge0 = (\<lambda>(a,b). a \<ge> 0)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   169
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   170
definition
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   171
  Nlt :: "Num \<Rightarrow> Num \<Rightarrow> bool" (infix "<\<^sub>N" 55)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   172
where
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   173
  "Nlt = (\<lambda>a b. 0>\<^sub>N (a -\<^sub>N b))"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   174
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   175
definition
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   176
  Nle :: "Num \<Rightarrow> Num \<Rightarrow> bool" (infix "\<le>\<^sub>N" 55)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   177
where
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   178
  "Nle = (\<lambda>a b. 0\<ge>\<^sub>N (a -\<^sub>N b))"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   179
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   180
definition
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   181
  "INum = (\<lambda>(a,b). of_int a / of_int b)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   182
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   183
lemma INum_int [simp]: "INum i\<^sub>N = ((of_int i) ::'a::field)" "INum 0\<^sub>N = (0::'a::field)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   184
  by (simp_all add: INum_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   185
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   186
lemma isnormNum_unique[simp]: 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   187
  assumes na: "isnormNum x" and nb: "isnormNum y" 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   188
  shows "((INum x ::'a::{ring_char_0,field, division_by_zero}) = INum y) = (x = y)" (is "?lhs = ?rhs")
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   189
proof
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   190
  have "\<exists> a b a' b'. x = (a,b) \<and> y = (a',b')" by auto
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   191
  then obtain a b a' b' where xy[simp]: "x = (a,b)" "y=(a',b')" by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   192
  assume H: ?lhs 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   193
  {assume "a = 0 \<or> b = 0 \<or> a' = 0 \<or> b' = 0" hence ?rhs
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   194
      using na nb H
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   195
      apply (simp add: INum_def split_def isnormNum_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   196
      apply (cases "a = 0", simp_all)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   197
      apply (cases "b = 0", simp_all)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   198
      apply (cases "a' = 0", simp_all)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   199
      apply (cases "a' = 0", simp_all add: of_int_eq_0_iff)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   200
      done}
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   201
  moreover
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   202
  { assume az: "a \<noteq> 0" and bz: "b \<noteq> 0" and a'z: "a'\<noteq>0" and b'z: "b'\<noteq>0"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   203
    from az bz a'z b'z na nb have pos: "b > 0" "b' > 0" by (simp_all add: isnormNum_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   204
    from prems have eq:"a * b' = a'*b" 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   205
      by (simp add: INum_def  eq_divide_eq divide_eq_eq of_int_mult[symmetric] del: of_int_mult)
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   206
    from prems have gcd1: "zgcd a b = 1" "zgcd b a = 1" "zgcd a' b' = 1" "zgcd b' a' = 1"       
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   207
      by (simp_all add: isnormNum_def add: zgcd_commute)
27668
6eb20b2cecf8 Tuned and simplified proofs
chaieb
parents: 27567
diff changeset
   208
    from eq have raw_dvd: "a dvd a'*b" "b dvd b'*a" "a' dvd a*b'" "b' dvd b*a'"
6eb20b2cecf8 Tuned and simplified proofs
chaieb
parents: 27567
diff changeset
   209
      apply - 
6eb20b2cecf8 Tuned and simplified proofs
chaieb
parents: 27567
diff changeset
   210
      apply algebra
6eb20b2cecf8 Tuned and simplified proofs
chaieb
parents: 27567
diff changeset
   211
      apply algebra
6eb20b2cecf8 Tuned and simplified proofs
chaieb
parents: 27567
diff changeset
   212
      apply simp
6eb20b2cecf8 Tuned and simplified proofs
chaieb
parents: 27567
diff changeset
   213
      apply algebra
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   214
      done
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   215
    from zdvd_dvd_eq[OF bz zrelprime_dvd_mult[OF gcd1(2) raw_dvd(2)]
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   216
      zrelprime_dvd_mult[OF gcd1(4) raw_dvd(4)]]
27668
6eb20b2cecf8 Tuned and simplified proofs
chaieb
parents: 27567
diff changeset
   217
      have eq1: "b = b'" using pos by arith  
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   218
      with eq have "a = a'" using pos by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   219
      with eq1 have ?rhs by simp}
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   220
  ultimately show ?rhs by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   221
next
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   222
  assume ?rhs thus ?lhs by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   223
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   224
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   225
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   226
lemma isnormNum0[simp]: "isnormNum x \<Longrightarrow> (INum x = (0::'a::{ring_char_0, field,division_by_zero})) = (x = 0\<^sub>N)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   227
  unfolding INum_int(2)[symmetric]
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   228
  by (rule isnormNum_unique, simp_all)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   229
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   230
lemma of_int_div_aux: "d ~= 0 ==> ((of_int x)::'a::{field, ring_char_0}) / (of_int d) = 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   231
    of_int (x div d) + (of_int (x mod d)) / ((of_int d)::'a)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   232
proof -
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   233
  assume "d ~= 0"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   234
  hence dz: "of_int d \<noteq> (0::'a)" by (simp add: of_int_eq_0_iff)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   235
  let ?t = "of_int (x div d) * ((of_int d)::'a) + of_int(x mod d)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   236
  let ?f = "\<lambda>x. x / of_int d"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   237
  have "x = (x div d) * d + x mod d"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   238
    by auto
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   239
  then have eq: "of_int x = ?t"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   240
    by (simp only: of_int_mult[symmetric] of_int_add [symmetric])
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   241
  then have "of_int x / of_int d = ?t / of_int d" 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   242
    using cong[OF refl[of ?f] eq] by simp
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 28615
diff changeset
   243
  then show ?thesis by (simp add: add_divide_distrib algebra_simps prems)
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   244
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   245
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   246
lemma of_int_div: "(d::int) ~= 0 ==> d dvd n ==>
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   247
    (of_int(n div d)::'a::{field, ring_char_0}) = of_int n / of_int d"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   248
  apply (frule of_int_div_aux [of d n, where ?'a = 'a])
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   249
  apply simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   250
  apply (simp add: zdvd_iff_zmod_eq_0)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   251
done
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   252
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   253
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   254
lemma normNum[simp]: "INum (normNum x) = (INum x :: 'a::{ring_char_0,field, division_by_zero})"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   255
proof-
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   256
  have "\<exists> a b. x = (a,b)" by auto
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   257
  then obtain a b where x[simp]: "x = (a,b)" by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   258
  {assume "a=0 \<or> b = 0" hence ?thesis
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   259
      by (simp add: INum_def normNum_def split_def Let_def)}
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   260
  moreover 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   261
  {assume a: "a\<noteq>0" and b: "b\<noteq>0"
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   262
    let ?g = "zgcd a b"
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   263
    from a b have g: "?g \<noteq> 0"by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   264
    from of_int_div[OF g, where ?'a = 'a]
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   265
    have ?thesis by (auto simp add: INum_def normNum_def split_def Let_def)}
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   266
  ultimately show ?thesis by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   267
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   268
26509
294708d83e83 dropped wrong code lemma
haftmann
parents: 25005
diff changeset
   269
lemma INum_normNum_iff: "(INum x ::'a::{field, division_by_zero, ring_char_0}) = INum y \<longleftrightarrow> normNum x = normNum y" (is "?lhs = ?rhs")
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   270
proof -
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   271
  have "normNum x = normNum y \<longleftrightarrow> (INum (normNum x) :: 'a) = INum (normNum y)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   272
    by (simp del: normNum)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   273
  also have "\<dots> = ?lhs" by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   274
  finally show ?thesis by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   275
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   276
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   277
lemma Nadd[simp]: "INum (x +\<^sub>N y) = INum x + (INum y :: 'a :: {ring_char_0,division_by_zero,field})"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   278
proof-
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   279
let ?z = "0:: 'a"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   280
  have " \<exists> a b. x = (a,b)" " \<exists> a' b'. y = (a',b')" by auto
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   281
  then obtain a b a' b' where x[simp]: "x = (a,b)" 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   282
    and y[simp]: "y = (a',b')" by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   283
  {assume "a=0 \<or> a'= 0 \<or> b =0 \<or> b' = 0" hence ?thesis 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   284
      apply (cases "a=0",simp_all add: Nadd_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   285
      apply (cases "b= 0",simp_all add: INum_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   286
       apply (cases "a'= 0",simp_all)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   287
       apply (cases "b'= 0",simp_all)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   288
       done }
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   289
  moreover 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   290
  {assume aa':"a \<noteq> 0" "a'\<noteq> 0" and bb': "b \<noteq> 0" "b' \<noteq> 0" 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   291
    {assume z: "a * b' + b * a' = 0"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   292
      hence "of_int (a*b' + b*a') / (of_int b* of_int b') = ?z" by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   293
      hence "of_int b' * of_int a / (of_int b * of_int b') + of_int b * of_int a' / (of_int b * of_int b') = ?z"  by (simp add:add_divide_distrib) 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   294
      hence th: "of_int a / of_int b + of_int a' / of_int b' = ?z" using bb' aa' by simp 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   295
      from z aa' bb' have ?thesis 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   296
	by (simp add: th Nadd_def normNum_def INum_def split_def)}
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   297
    moreover {assume z: "a * b' + b * a' \<noteq> 0"
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   298
      let ?g = "zgcd (a * b' + b * a') (b*b')"
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   299
      have gz: "?g \<noteq> 0" using z by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   300
      have ?thesis using aa' bb' z gz
27567
e3fe9a327c63 Fixed proofs.
chaieb
parents: 27556
diff changeset
   301
	of_int_div[where ?'a = 'a, OF gz zgcd_zdvd1[where i="a * b' + b * a'" and j="b*b'"]]	of_int_div[where ?'a = 'a,
e3fe9a327c63 Fixed proofs.
chaieb
parents: 27556
diff changeset
   302
	OF gz zgcd_zdvd2[where i="a * b' + b * a'" and j="b*b'"]]
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   303
	by (simp add: x y Nadd_def INum_def normNum_def Let_def add_divide_distrib)}
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   304
    ultimately have ?thesis using aa' bb' 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   305
      by (simp add: Nadd_def INum_def normNum_def x y Let_def) }
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   306
  ultimately show ?thesis by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   307
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   308
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   309
lemma Nmul[simp]: "INum (x *\<^sub>N y) = INum x * (INum y:: 'a :: {ring_char_0,division_by_zero,field}) "
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   310
proof-
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   311
  let ?z = "0::'a"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   312
  have " \<exists> a b. x = (a,b)" " \<exists> a' b'. y = (a',b')" by auto
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   313
  then obtain a b a' b' where x: "x = (a,b)" and y: "y = (a',b')" by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   314
  {assume "a=0 \<or> a'= 0 \<or> b = 0 \<or> b' = 0" hence ?thesis 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   315
      apply (cases "a=0",simp_all add: x y Nmul_def INum_def Let_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   316
      apply (cases "b=0",simp_all)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   317
      apply (cases "a'=0",simp_all) 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   318
      done }
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   319
  moreover
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   320
  {assume z: "a \<noteq> 0" "a' \<noteq> 0" "b \<noteq> 0" "b' \<noteq> 0"
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   321
    let ?g="zgcd (a*a') (b*b')"
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   322
    have gz: "?g \<noteq> 0" using z by simp
27567
e3fe9a327c63 Fixed proofs.
chaieb
parents: 27556
diff changeset
   323
    from z of_int_div[where ?'a = 'a, OF gz zgcd_zdvd1[where i="a*a'" and j="b*b'"]] 
e3fe9a327c63 Fixed proofs.
chaieb
parents: 27556
diff changeset
   324
      of_int_div[where ?'a = 'a , OF gz zgcd_zdvd2[where i="a*a'" and j="b*b'"]] 
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   325
    have ?thesis by (simp add: Nmul_def x y Let_def INum_def)}
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   326
  ultimately show ?thesis by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   327
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   328
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   329
lemma Nneg[simp]: "INum (~\<^sub>N x) = - (INum x ::'a:: field)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   330
  by (simp add: Nneg_def split_def INum_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   331
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   332
lemma Nsub[simp]: shows "INum (x -\<^sub>N y) = INum x - (INum y:: 'a :: {ring_char_0,division_by_zero,field})"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   333
by (simp add: Nsub_def split_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   334
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   335
lemma Ninv[simp]: "INum (Ninv x) = (1::'a :: {division_by_zero,field}) / (INum x)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   336
  by (simp add: Ninv_def INum_def split_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   337
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   338
lemma Ndiv[simp]: "INum (x \<div>\<^sub>N y) = INum x / (INum y ::'a :: {ring_char_0, division_by_zero,field})" by (simp add: Ndiv_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   339
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   340
lemma Nlt0_iff[simp]: assumes nx: "isnormNum x" 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   341
  shows "((INum x :: 'a :: {ring_char_0,division_by_zero,ordered_field})< 0) = 0>\<^sub>N x "
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   342
proof-
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   343
  have " \<exists> a b. x = (a,b)" by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   344
  then obtain a b where x[simp]:"x = (a,b)" by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   345
  {assume "a = 0" hence ?thesis by (simp add: Nlt0_def INum_def) }
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   346
  moreover
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   347
  {assume a: "a\<noteq>0" hence b: "(of_int b::'a) > 0" using nx by (simp add: isnormNum_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   348
    from pos_divide_less_eq[OF b, where b="of_int a" and a="0::'a"]
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   349
    have ?thesis by (simp add: Nlt0_def INum_def)}
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   350
  ultimately show ?thesis by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   351
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   352
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   353
lemma Nle0_iff[simp]:assumes nx: "isnormNum x" 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   354
  shows "((INum x :: 'a :: {ring_char_0,division_by_zero,ordered_field}) \<le> 0) = 0\<ge>\<^sub>N x"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   355
proof-
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   356
  have " \<exists> a b. x = (a,b)" by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   357
  then obtain a b where x[simp]:"x = (a,b)" by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   358
  {assume "a = 0" hence ?thesis by (simp add: Nle0_def INum_def) }
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   359
  moreover
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   360
  {assume a: "a\<noteq>0" hence b: "(of_int b :: 'a) > 0" using nx by (simp add: isnormNum_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   361
    from pos_divide_le_eq[OF b, where b="of_int a" and a="0::'a"]
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   362
    have ?thesis by (simp add: Nle0_def INum_def)}
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   363
  ultimately show ?thesis by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   364
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   365
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   366
lemma Ngt0_iff[simp]:assumes nx: "isnormNum x" shows "((INum x :: 'a :: {ring_char_0,division_by_zero,ordered_field})> 0) = 0<\<^sub>N x"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   367
proof-
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   368
  have " \<exists> a b. x = (a,b)" by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   369
  then obtain a b where x[simp]:"x = (a,b)" by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   370
  {assume "a = 0" hence ?thesis by (simp add: Ngt0_def INum_def) }
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   371
  moreover
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   372
  {assume a: "a\<noteq>0" hence b: "(of_int b::'a) > 0" using nx by (simp add: isnormNum_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   373
    from pos_less_divide_eq[OF b, where b="of_int a" and a="0::'a"]
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   374
    have ?thesis by (simp add: Ngt0_def INum_def)}
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   375
  ultimately show ?thesis by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   376
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   377
lemma Nge0_iff[simp]:assumes nx: "isnormNum x" 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   378
  shows "((INum x :: 'a :: {ring_char_0,division_by_zero,ordered_field}) \<ge> 0) = 0\<le>\<^sub>N x"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   379
proof-
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   380
  have " \<exists> a b. x = (a,b)" by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   381
  then obtain a b where x[simp]:"x = (a,b)" by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   382
  {assume "a = 0" hence ?thesis by (simp add: Nge0_def INum_def) }
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   383
  moreover
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   384
  {assume a: "a\<noteq>0" hence b: "(of_int b::'a) > 0" using nx by (simp add: isnormNum_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   385
    from pos_le_divide_eq[OF b, where b="of_int a" and a="0::'a"]
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   386
    have ?thesis by (simp add: Nge0_def INum_def)}
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   387
  ultimately show ?thesis by blast
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   388
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   389
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   390
lemma Nlt_iff[simp]: assumes nx: "isnormNum x" and ny: "isnormNum y"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   391
  shows "((INum x :: 'a :: {ring_char_0,division_by_zero,ordered_field}) < INum y) = (x <\<^sub>N y)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   392
proof-
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   393
  let ?z = "0::'a"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   394
  have "((INum x ::'a) < INum y) = (INum (x -\<^sub>N y) < ?z)" using nx ny by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   395
  also have "\<dots> = (0>\<^sub>N (x -\<^sub>N y))" using Nlt0_iff[OF Nsub_normN[OF ny]] by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   396
  finally show ?thesis by (simp add: Nlt_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   397
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   398
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   399
lemma Nle_iff[simp]: assumes nx: "isnormNum x" and ny: "isnormNum y"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   400
  shows "((INum x :: 'a :: {ring_char_0,division_by_zero,ordered_field})\<le> INum y) = (x \<le>\<^sub>N y)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   401
proof-
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   402
  have "((INum x ::'a) \<le> INum y) = (INum (x -\<^sub>N y) \<le> (0::'a))" using nx ny by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   403
  also have "\<dots> = (0\<ge>\<^sub>N (x -\<^sub>N y))" using Nle0_iff[OF Nsub_normN[OF ny]] by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   404
  finally show ?thesis by (simp add: Nle_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   405
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   406
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   407
lemma Nadd_commute:
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   408
  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   409
  shows "x +\<^sub>N y = y +\<^sub>N x"
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   410
proof-
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   411
  have n: "isnormNum (x +\<^sub>N y)" "isnormNum (y +\<^sub>N x)" by simp_all
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   412
  have "(INum (x +\<^sub>N y)::'a) = INum (y +\<^sub>N x)" by simp
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   413
  with isnormNum_unique[OF n] show ?thesis by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   414
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   415
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   416
lemma [simp]:
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   417
  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   418
  shows "(0, b) +\<^sub>N y = normNum y"
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   419
    and "(a, 0) +\<^sub>N y = normNum y" 
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   420
    and "x +\<^sub>N (0, b) = normNum x"
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   421
    and "x +\<^sub>N (a, 0) = normNum x"
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   422
  apply (simp add: Nadd_def split_def)
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   423
  apply (simp add: Nadd_def split_def)
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   424
  apply (subst Nadd_commute, simp add: Nadd_def split_def)
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   425
  apply (subst Nadd_commute, simp add: Nadd_def split_def)
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   426
  done
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   427
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   428
lemma normNum_nilpotent_aux[simp]:
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   429
  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   430
  assumes nx: "isnormNum x" 
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   431
  shows "normNum x = x"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   432
proof-
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   433
  let ?a = "normNum x"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   434
  have n: "isnormNum ?a" by simp
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   435
  have th:"INum ?a = (INum x ::'a)" by simp
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   436
  with isnormNum_unique[OF n nx]  
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   437
  show ?thesis by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   438
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   439
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   440
lemma normNum_nilpotent[simp]:
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   441
  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   442
  shows "normNum (normNum x) = normNum x"
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   443
  by simp
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   444
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   445
lemma normNum0[simp]: "normNum (0,b) = 0\<^sub>N" "normNum (a,0) = 0\<^sub>N"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   446
  by (simp_all add: normNum_def)
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   447
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   448
lemma normNum_Nadd:
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   449
  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   450
  shows "normNum (x +\<^sub>N y) = x +\<^sub>N y" by simp
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   451
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   452
lemma Nadd_normNum1[simp]:
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   453
  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   454
  shows "normNum x +\<^sub>N y = x +\<^sub>N y"
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   455
proof-
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   456
  have n: "isnormNum (normNum x +\<^sub>N y)" "isnormNum (x +\<^sub>N y)" by simp_all
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   457
  have "INum (normNum x +\<^sub>N y) = INum x + (INum y :: 'a)" by simp
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   458
  also have "\<dots> = INum (x +\<^sub>N y)" by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   459
  finally show ?thesis using isnormNum_unique[OF n] by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   460
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   461
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   462
lemma Nadd_normNum2[simp]:
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   463
  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   464
  shows "x +\<^sub>N normNum y = x +\<^sub>N y"
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   465
proof-
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   466
  have n: "isnormNum (x +\<^sub>N normNum y)" "isnormNum (x +\<^sub>N y)" by simp_all
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   467
  have "INum (x +\<^sub>N normNum y) = INum x + (INum y :: 'a)" by simp
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   468
  also have "\<dots> = INum (x +\<^sub>N y)" by simp
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   469
  finally show ?thesis using isnormNum_unique[OF n] by simp
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   470
qed
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   471
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   472
lemma Nadd_assoc:
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   473
  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   474
  shows "x +\<^sub>N y +\<^sub>N z = x +\<^sub>N (y +\<^sub>N z)"
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   475
proof-
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   476
  have n: "isnormNum (x +\<^sub>N y +\<^sub>N z)" "isnormNum (x +\<^sub>N (y +\<^sub>N z))" by simp_all
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   477
  have "INum (x +\<^sub>N y +\<^sub>N z) = (INum (x +\<^sub>N (y +\<^sub>N z)) :: 'a)" by simp
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   478
  with isnormNum_unique[OF n] show ?thesis by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   479
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   480
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   481
lemma Nmul_commute: "isnormNum x \<Longrightarrow> isnormNum y \<Longrightarrow> x *\<^sub>N y = y *\<^sub>N x"
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   482
  by (simp add: Nmul_def split_def Let_def zgcd_commute mult_commute)
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   483
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   484
lemma Nmul_assoc:
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   485
  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   486
  assumes nx: "isnormNum x" and ny:"isnormNum y" and nz:"isnormNum z"
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   487
  shows "x *\<^sub>N y *\<^sub>N z = x *\<^sub>N (y *\<^sub>N z)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   488
proof-
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   489
  from nx ny nz have n: "isnormNum (x *\<^sub>N y *\<^sub>N z)" "isnormNum (x *\<^sub>N (y *\<^sub>N z))" 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   490
    by simp_all
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   491
  have "INum (x +\<^sub>N y +\<^sub>N z) = (INum (x +\<^sub>N (y +\<^sub>N z)) :: 'a)" by simp
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   492
  with isnormNum_unique[OF n] show ?thesis by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   493
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   494
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   495
lemma Nsub0:
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   496
  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   497
  assumes x: "isnormNum x" and y:"isnormNum y" shows "(x -\<^sub>N y = 0\<^sub>N) = (x = y)"
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   498
proof-
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   499
  { fix h :: 'a
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   500
    from isnormNum_unique[where 'a = 'a, OF Nsub_normN[OF y], where y="0\<^sub>N"] 
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   501
    have "(x -\<^sub>N y = 0\<^sub>N) = (INum (x -\<^sub>N y) = (INum 0\<^sub>N :: 'a)) " by simp
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   502
    also have "\<dots> = (INum x = (INum y :: 'a))" by simp
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   503
    also have "\<dots> = (x = y)" using x y by simp
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   504
    finally show ?thesis . }
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   505
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   506
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   507
lemma Nmul0[simp]: "c *\<^sub>N 0\<^sub>N = 0\<^sub>N" " 0\<^sub>N *\<^sub>N c = 0\<^sub>N"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   508
  by (simp_all add: Nmul_def Let_def split_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   509
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   510
lemma Nmul_eq0[simp]:
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   511
  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   512
  assumes nx:"isnormNum x" and ny: "isnormNum y"
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   513
  shows "(x*\<^sub>N y = 0\<^sub>N) = (x = 0\<^sub>N \<or> y = 0\<^sub>N)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   514
proof-
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   515
  { fix h :: 'a
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   516
    have " \<exists> a b a' b'. x = (a,b) \<and> y= (a',b')" by auto
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   517
    then obtain a b a' b' where xy[simp]: "x = (a,b)" "y = (a',b')" by blast
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   518
    have n0: "isnormNum 0\<^sub>N" by simp
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   519
    show ?thesis using nx ny 
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   520
      apply (simp only: isnormNum_unique[where ?'a = 'a, OF  Nmul_normN[OF nx ny] n0, symmetric] Nmul[where ?'a = 'a])
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   521
      apply (simp add: INum_def split_def isnormNum_def fst_conv snd_conv)
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   522
      apply (cases "a=0",simp_all)
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   523
      apply (cases "a'=0",simp_all)
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   524
      done
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   525
  }
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   526
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   527
lemma Nneg_Nneg[simp]: "~\<^sub>N (~\<^sub>N c) = c"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   528
  by (simp add: Nneg_def split_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   529
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   530
lemma Nmul1[simp]: 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   531
  "isnormNum c \<Longrightarrow> 1\<^sub>N *\<^sub>N c = c" 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   532
  "isnormNum c \<Longrightarrow> c *\<^sub>N 1\<^sub>N  = c" 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   533
  apply (simp_all add: Nmul_def Let_def split_def isnormNum_def)
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   534
  apply (cases "fst c = 0", simp_all, cases c, simp_all)+
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   535
  done
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents:
diff changeset
   536
28615
4c8fa015ec7f explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
wenzelm
parents: 27668
diff changeset
   537
end