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