src/HOL/Library/Executable_Real.thy
author chaieb
Tue, 15 May 2007 18:28:02 +0200
changeset 22981 cf071f3fc4ae
child 22982 bff3fcdeecd3
permissions -rw-r--r--
A verified theory for rational numbers representation and simple calculations; verified with respect to the real numbers;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
     1
(*  Title:      HOL/Library/Executable_Real.thy
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
     2
    ID:         $Id$
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
     3
    Author:     Amine Chaieb, TU Muenchen
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
     4
*)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
     5
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
     6
header {* Implementation of rational real numbers as pairs of integers *}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
     7
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
     8
theory Executable_Real
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
     9
imports GCD "~~/src/HOL/Real/Real"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    10
begin
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    11
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    12
subsection {* Implementation of operations on pair of integers *}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    13
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    14
types Num = "int * int"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    15
syntax "_Num0" :: "Num" ("0\<^sub>N")
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    16
translations "0\<^sub>N" \<rightleftharpoons> "(0,0)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    17
syntax "_Numi" :: "int \<Rightarrow> Num" ("_\<^sub>N")
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    18
translations "i\<^sub>N" \<rightleftharpoons> "(i,1)::Num"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    19
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    20
constdefs isnormNum :: "Num \<Rightarrow> bool"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    21
  "isnormNum \<equiv> \<lambda>(a,b). (if a = 0 then b = 0 else b > 0 \<and> igcd a b = 1)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    22
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    23
constdefs normNum :: "Num \<Rightarrow> Num"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    24
  "normNum \<equiv> \<lambda>(a,b). (if a=0 \<or> b = 0 then (0,0) else 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    25
  (let g = igcd a b 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    26
   in if b > 0 then (a div g, b div g) else (- (a div g), - (b div g))))"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    27
lemma normNum_isnormNum[simp]: "isnormNum (normNum x)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    28
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    29
  have " \<exists> a b. x = (a,b)" by auto
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    30
  then obtain a b where x[simp]: "x = (a,b)" by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    31
  {assume "a=0 \<or> b = 0" hence ?thesis by (simp add: normNum_def isnormNum_def)}  
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    32
  moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    33
  {assume anz: "a \<noteq> 0" and bnz: "b \<noteq> 0" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    34
    let ?g = "igcd a b"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    35
    let ?a' = "a div ?g"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    36
    let ?b' = "b div ?g"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    37
    let ?g' = "igcd ?a' ?b'"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    38
    from anz bnz have "?g \<noteq> 0" by simp  with igcd_pos[of a b] 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    39
    have gpos: "?g > 0"  by arith
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    40
    have gdvd: "?g dvd a" "?g dvd b" by (simp_all add: igcd_dvd1 igcd_dvd2)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    41
    from zdvd_mult_div_cancel[OF gdvd(1)] zdvd_mult_div_cancel[OF gdvd(2)]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    42
    anz bnz
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    43
    have nz':"?a' \<noteq> 0" "?b' \<noteq> 0" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    44
      by - (rule notI,simp add:igcd_def)+
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    45
    from anz bnz have stupid: "a \<noteq> 0 \<or> b \<noteq> 0" by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    46
    from div_igcd_relprime[OF stupid] have gp1: "?g' = 1" .
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    47
    from bnz have "b < 0 \<or> b > 0" by arith
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    48
    moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    49
    {assume b: "b > 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    50
      from pos_imp_zdiv_nonneg_iff[OF gpos] b
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    51
      have "?b' \<ge> 0" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    52
      with nz' have b': "?b' > 0" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    53
      from b b' anz bnz nz' gp1 have ?thesis 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    54
	by (simp add: isnormNum_def normNum_def Let_def split_def fst_conv snd_conv)}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    55
    moreover {assume b: "b < 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    56
      {assume b': "?b' \<ge> 0" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    57
	from gpos have th: "?g \<ge> 0" by arith
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    58
	from mult_nonneg_nonneg[OF th b'] zdvd_mult_div_cancel[OF gdvd(2)]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    59
	have False using b by simp }
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    60
      hence b': "?b' < 0" by arith
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    61
      from anz bnz nz' b b' gp1 have ?thesis 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    62
	by (simp add: isnormNum_def normNum_def Let_def split_def fst_conv snd_conv)}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    63
    ultimately have ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    64
  }
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    65
  ultimately show ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    66
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    67
    (* Arithmetic over Num *)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    68
constdefs Nadd :: "Num \<Rightarrow> Num \<Rightarrow> Num" (infixl "+\<^sub>N" 60)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    69
  "Nadd \<equiv> \<lambda>(a,b) (a',b'). if a = 0 \<or> b = 0 then normNum(a',b') 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    70
  else if a'=0 \<or> b' = 0 then normNum(a,b) 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    71
  else normNum(a*b' + b*a', b*b')"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    72
constdefs Nmul :: "Num \<Rightarrow> Num \<Rightarrow> Num" (infixl "*\<^sub>N" 60)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    73
  "Nmul \<equiv> \<lambda>(a,b) (a',b'). let g = igcd (a*a') (b*b') 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    74
  in (a*a' div g, b*b' div g)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    75
constdefs Nneg :: "Num \<Rightarrow> Num" ("~\<^sub>N")
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    76
  "Nneg \<equiv> \<lambda>(a,b). (-a,b)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    77
constdefs  Nsub :: "Num \<Rightarrow> Num \<Rightarrow> Num" (infixl "-\<^sub>N" 60)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    78
  "Nsub \<equiv> \<lambda>a b. a +\<^sub>N ~\<^sub>N b"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    79
constdefs Ninv :: "Num \<Rightarrow> Num" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    80
"Ninv \<equiv> \<lambda>(a,b). if a < 0 then (-b, \<bar>a\<bar>) else (b,a)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    81
constdefs Ndiv :: "Num \<Rightarrow> Num \<Rightarrow> Num" (infixl "\<div>\<^sub>N" 60)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    82
  "Ndiv \<equiv> \<lambda>a b. a *\<^sub>N Ninv b"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    83
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    84
lemma Nneg_normN[simp]: "isnormNum x \<Longrightarrow> isnormNum (~\<^sub>N x)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    85
  by(simp add: isnormNum_def Nneg_def split_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    86
lemma Nadd_normN[simp]: "isnormNum (x +\<^sub>N y)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    87
  by (simp add: Nadd_def split_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    88
lemma Nsub_normN[simp]: "\<lbrakk> isnormNum y\<rbrakk> \<Longrightarrow> isnormNum (x -\<^sub>N y)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    89
  by (simp add: Nsub_def split_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    90
lemma Nmul_normN[simp]: assumes xn:"isnormNum x" and yn: "isnormNum y"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    91
  shows "isnormNum (x *\<^sub>N y)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    92
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    93
  have "\<exists>a b. x = (a,b)" and "\<exists> a' b'. y = (a',b')" by auto
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    94
  then obtain a b a' b' where ab: "x = (a,b)"  and ab': "y = (a',b')" by blast 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    95
  {assume "a = 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    96
    hence ?thesis using xn ab ab'
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    97
      by (simp add: igcd_def isnormNum_def Let_def Nmul_def split_def)}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    98
  moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    99
  {assume "a' = 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   100
    hence ?thesis using yn ab ab' 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   101
      by (simp add: igcd_def isnormNum_def Let_def Nmul_def split_def)}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   102
  moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   103
  {assume a: "a \<noteq>0" and a': "a'\<noteq>0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   104
    hence bp: "b > 0" "b' > 0" using xn yn ab ab' by (simp_all add: isnormNum_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   105
    from mult_pos_pos[OF bp] have "x *\<^sub>N y = normNum (a*a', b*b')" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   106
      using ab ab' a a' bp by (simp add: Nmul_def Let_def split_def normNum_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   107
    hence ?thesis by simp}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   108
  ultimately show ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   109
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   110
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   111
lemma Ninv_normN[simp]: "isnormNum x \<Longrightarrow> isnormNum (Ninv x)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   112
by (simp add: Ninv_def isnormNum_def split_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   113
(cases "fst x = 0",auto simp add: igcd_commute)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   114
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   115
lemma isnormNum_int[simp]: 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   116
  "isnormNum 0\<^sub>N" "isnormNum (1::int)\<^sub>N" "i \<noteq> 0 \<Longrightarrow> isnormNum i\<^sub>N"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   117
 by (simp_all add: isnormNum_def igcd_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   118
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   119
    (* Relations over Num *)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   120
constdefs Nlt0:: "Num \<Rightarrow> bool" ("0>\<^sub>N")
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   121
  "Nlt0 \<equiv> \<lambda>(a,b). a < 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   122
constdefs Nle0:: "Num \<Rightarrow> bool" ("0\<ge>\<^sub>N")
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   123
  "Nle0 \<equiv> \<lambda>(a,b). a \<le> 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   124
constdefs Ngt0:: "Num \<Rightarrow> bool" ("0<\<^sub>N")
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   125
  "Ngt0 \<equiv> \<lambda>(a,b). a > 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   126
constdefs Nge0:: "Num \<Rightarrow> bool" ("0\<le>\<^sub>N")
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   127
  "Nge0 \<equiv> \<lambda>(a,b). a \<ge> 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   128
constdefs Nlt :: "Num \<Rightarrow> Num \<Rightarrow> bool" (infix "<\<^sub>N" 55)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   129
  "Nlt \<equiv> \<lambda>a b. 0>\<^sub>N (a -\<^sub>N b)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   130
constdefs Nle :: "Num \<Rightarrow> Num \<Rightarrow> bool" (infix "\<le>\<^sub>N" 55)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   131
  "Nle \<equiv> \<lambda>a b. 0\<ge>\<^sub>N (a -\<^sub>N b)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   132
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   133
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   134
subsection {* Interpretation of the normalized rats in \<real> *}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   135
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   136
definition
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   137
  INum:: "Num \<Rightarrow> real"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   138
where
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   139
  INum_def: "INum \<equiv> \<lambda>(a,b). real a / real b"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   140
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   141
code_datatype INum
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   142
instance real :: eq ..
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   143
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   144
definition
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   145
  real_int :: "int \<Rightarrow> real"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   146
where
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   147
  "real_int = real"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   148
lemmas [code unfold] = real_int_def [symmetric]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   149
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   150
lemma [code unfold]:
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   151
  "real = real_int o int"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   152
  by (auto simp add: real_int_def expand_fun_eq)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   153
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   154
lemma INum_int [simp]: "INum i\<^sub>N = real i" "INum 0\<^sub>N = 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   155
  by (simp_all add: INum_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   156
lemmas [code, code unfold] = INum_int [unfolded real_int_def [symmetric], symmetric]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   157
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   158
lemma [code, code unfold]: "1 = INum 1\<^sub>N" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   159
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   160
lemma isnormNum_unique[simp]: 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   161
  assumes na: "isnormNum x" and nb: "isnormNum y" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   162
  shows "(INum x = INum y) = (x = y)" (is "?lhs = ?rhs")
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   163
proof
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   164
  have "\<exists> a b a' b'. x = (a,b) \<and> y = (a',b')" by auto
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   165
  then obtain a b a' b' where xy[simp]: "x = (a,b)" "y=(a',b')" by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   166
  assume H: ?lhs 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   167
  {assume "a = 0 \<or> b = 0 \<or> a' = 0 \<or> b' = 0" hence ?rhs
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   168
      using na nb H
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   169
      by (simp add: INum_def split_def isnormNum_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   170
       (cases "a = 0", simp_all,cases "b = 0", simp_all,
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   171
      cases "a' = 0", simp_all,cases "a' = 0", simp_all)}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   172
  moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   173
  { assume az: "a \<noteq> 0" and bz: "b \<noteq> 0" and a'z: "a'\<noteq>0" and b'z: "b'\<noteq>0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   174
    from az bz a'z b'z na nb have pos: "b > 0" "b' > 0" by (simp_all add: isnormNum_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   175
    from prems have eq:"a * b' = a'*b" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   176
      by (simp add: INum_def  eq_divide_eq divide_eq_eq real_of_int_mult[symmetric] del: real_of_int_mult)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   177
    from prems have gcd1: "igcd a b = 1" "igcd b a = 1" "igcd a' b' = 1" "igcd b' a' = 1"       
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   178
      by (simp_all add: isnormNum_def add: igcd_commute)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   179
    from eq have raw_dvd: "a dvd a'*b" "b dvd b'*a" "a' dvd a*b'" "b' dvd b*a'" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   180
      apply(unfold dvd_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   181
      apply (rule_tac x="b'" in exI, simp add: mult_ac)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   182
      apply (rule_tac x="a'" in exI, simp add: mult_ac)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   183
      apply (rule_tac x="b" in exI, simp add: mult_ac)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   184
      apply (rule_tac x="a" in exI, simp add: mult_ac)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   185
      done
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   186
    from zdvd_dvd_eq[OF bz zrelprime_dvd_mult[OF gcd1(2) raw_dvd(2)]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   187
      zrelprime_dvd_mult[OF gcd1(4) raw_dvd(4)]]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   188
      have eq1: "b = b'" using pos by simp_all
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   189
      with eq have "a = a'" using pos by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   190
      with eq1 have ?rhs by simp}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   191
  ultimately show ?rhs by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   192
next
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   193
  assume ?rhs thus ?lhs by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   194
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   195
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   196
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   197
lemma isnormNum0[simp]: "isnormNum x \<Longrightarrow> (INum x = 0) = (x = 0\<^sub>N)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   198
  unfolding INum_int(2)[symmetric]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   199
  by (rule isnormNum_unique, simp_all)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   200
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   201
lemma normNum[simp]: "INum (normNum x) = INum x"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   202
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   203
  have "\<exists> a b. x = (a,b)" by auto
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   204
  then obtain a b where x[simp]: "x = (a,b)" by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   205
  {assume "a=0 \<or> b = 0" hence ?thesis
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   206
      by (simp add: INum_def normNum_def split_def Let_def)}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   207
  moreover 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   208
  {assume a: "a\<noteq>0" and b: "b\<noteq>0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   209
    let ?g = "igcd a b"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   210
    from a b have g: "?g \<noteq> 0"by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   211
    from real_of_int_div[OF g]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   212
    have ?thesis by (simp add: INum_def normNum_def split_def Let_def)}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   213
  ultimately show ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   214
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   215
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   216
lemma INum_normNum_iff [code]: "INum x = INum y \<longleftrightarrow> normNum x = normNum y" (is "?lhs = ?rhs")
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   217
proof -
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   218
  have "normNum x = normNum y \<longleftrightarrow> INum (normNum x) = INum (normNum y)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   219
    by (simp del: normNum)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   220
  also have "\<dots> = ?lhs" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   221
  finally show ?thesis by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   222
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   223
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   224
lemma Nadd[simp]: "INum (x +\<^sub>N y) = INum x + INum y"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   225
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   226
  have " \<exists> a b. x = (a,b)" " \<exists> a' b'. y = (a',b')" by auto
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   227
  then obtain a b a' b' where x[simp]: "x = (a,b)" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   228
    and y[simp]: "y = (a',b')" by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   229
  {assume "a=0 \<or> a'= 0 \<or> b =0 \<or> b' = 0" hence ?thesis 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   230
      apply (cases "a=0",simp_all add: Nadd_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   231
      apply (cases "b= 0",simp_all add: INum_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   232
       apply (cases "a'= 0",simp_all)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   233
       apply (cases "b'= 0",simp_all)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   234
       done }
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   235
  moreover 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   236
  {assume aa':"a \<noteq> 0" "a'\<noteq> 0" and bb': "b \<noteq> 0" "b' \<noteq> 0" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   237
    {assume z: "a * b' + b * a' = 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   238
      hence "real (a*b' + b*a') / (real b* real b') = 0" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   239
      hence "real b' * real a / (real b * real b') + real b * real a' / (real b * real b') = 0"  by (simp add:add_divide_distrib) 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   240
      hence th: "real a / real b + real a' / real b' = 0" using bb' aa' by simp 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   241
      from z aa' bb' have ?thesis 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   242
	by (simp add: th Nadd_def normNum_def INum_def split_def)}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   243
    moreover {assume z: "a * b' + b * a' \<noteq> 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   244
      let ?g = "igcd (a * b' + b * a') (b*b')"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   245
      have gz: "?g \<noteq> 0" using z by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   246
      have ?thesis using aa' bb' z gz
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   247
	real_of_int_div[OF gz igcd_dvd1[where i="a * b' + b * a'" and j="b*b'"]]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   248
	real_of_int_div[OF gz igcd_dvd2[where i="a * b' + b * a'" and j="b*b'"]]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   249
	by (simp add: x y Nadd_def INum_def normNum_def Let_def add_divide_distrib)}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   250
    ultimately have ?thesis using aa' bb' 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   251
      by (simp add: Nadd_def INum_def normNum_def x y Let_def) }
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   252
  ultimately show ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   253
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   254
lemmas [code] = Nadd [symmetric]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   255
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   256
lemma Nmul[simp]: "INum (x *\<^sub>N y) = INum x * INum y"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   257
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   258
  have " \<exists> a b. x = (a,b)" " \<exists> a' b'. y = (a',b')" by auto
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   259
  then obtain a b a' b' where x: "x = (a,b)" and y: "y = (a',b')" by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   260
  {assume "a=0 \<or> a'= 0 \<or> b = 0 \<or> b' = 0" hence ?thesis 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   261
      apply (cases "a=0",simp_all add: x y Nmul_def INum_def Let_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   262
      apply (cases "b=0",simp_all)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   263
      apply (cases "a'=0",simp_all) 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   264
      done }
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   265
  moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   266
  {assume z: "a \<noteq> 0" "a' \<noteq> 0" "b \<noteq> 0" "b' \<noteq> 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   267
    let ?g="igcd (a*a') (b*b')"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   268
    have gz: "?g \<noteq> 0" using z by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   269
    from z real_of_int_div[OF gz igcd_dvd1[where i="a*a'" and j="b*b'"]] 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   270
      real_of_int_div[OF gz igcd_dvd2[where i="a*a'" and j="b*b'"]] 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   271
    have ?thesis by (simp add: Nmul_def x y Let_def INum_def)}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   272
  ultimately show ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   273
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   274
lemmas [code] = Nmul [symmetric]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   275
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   276
lemma Nneg[simp]: "INum (~\<^sub>N x) = - INum x"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   277
  by (simp add: Nneg_def split_def INum_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   278
lemmas [code] = Nneg [symmetric]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   279
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   280
lemma Nsub[simp]: shows "INum (x -\<^sub>N y) = INum x - INum y"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   281
  by (simp add: Nsub_def split_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   282
lemmas [code] = Nsub [symmetric]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   283
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   284
lemma Ninv[simp]: "INum (Ninv x) = 1 / (INum x)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   285
  by (simp add: Ninv_def INum_def split_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   286
lemmas [code] = Ninv [symmetric]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   287
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   288
lemma Ndiv[simp]: "INum (x \<div>\<^sub>N y) = INum x / INum y" by (simp add: Ndiv_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   289
lemmas [code] = Ndiv [symmetric]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   290
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   291
lemma Nlt0_iff[simp]: assumes nx: "isnormNum x" shows "(INum x < 0) = 0>\<^sub>N x "
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   292
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   293
  have " \<exists> a b. x = (a,b)" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   294
  then obtain a b where x[simp]:"x = (a,b)" by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   295
  {assume "a = 0" hence ?thesis by (simp add: Nlt0_def INum_def) }
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   296
  moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   297
  {assume a: "a\<noteq>0" hence b: "real b > 0" using nx by (simp add: isnormNum_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   298
    from pos_divide_less_eq[OF b, where b="real a" and a="0"]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   299
    have ?thesis by (simp add: Nlt0_def INum_def)}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   300
  ultimately show ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   301
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   302
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   303
lemma   Nle0_iff[simp]:assumes nx: "isnormNum x" shows "(INum x \<le> 0) = 0\<ge>\<^sub>N x"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   304
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   305
  have " \<exists> a b. x = (a,b)" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   306
  then obtain a b where x[simp]:"x = (a,b)" by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   307
  {assume "a = 0" hence ?thesis by (simp add: Nle0_def INum_def) }
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   308
  moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   309
  {assume a: "a\<noteq>0" hence b: "real b > 0" using nx by (simp add: isnormNum_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   310
    from pos_divide_le_eq[OF b, where b="real a" and a="0"]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   311
    have ?thesis by (simp add: Nle0_def INum_def)}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   312
  ultimately show ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   313
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   314
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   315
lemma Ngt0_iff[simp]:assumes nx: "isnormNum x" shows "(INum x > 0) = 0<\<^sub>N x"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   316
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   317
  have " \<exists> a b. x = (a,b)" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   318
  then obtain a b where x[simp]:"x = (a,b)" by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   319
  {assume "a = 0" hence ?thesis by (simp add: Ngt0_def INum_def) }
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   320
  moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   321
  {assume a: "a\<noteq>0" hence b: "real b > 0" using nx by (simp add: isnormNum_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   322
    from pos_less_divide_eq[OF b, where b="real a" and a="0"]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   323
    have ?thesis by (simp add: Ngt0_def INum_def)}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   324
  ultimately show ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   325
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   326
lemma Nge0_iff[simp]:assumes nx: "isnormNum x" shows "(INum x \<ge> 0) = 0\<le>\<^sub>N x"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   327
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   328
  have " \<exists> a b. x = (a,b)" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   329
  then obtain a b where x[simp]:"x = (a,b)" by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   330
  {assume "a = 0" hence ?thesis by (simp add: Nge0_def INum_def) }
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   331
  moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   332
  {assume a: "a\<noteq>0" hence b: "real b > 0" using nx by (simp add: isnormNum_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   333
    from pos_le_divide_eq[OF b, where b="real a" and a="0"]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   334
    have ?thesis by (simp add: Nge0_def INum_def)}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   335
  ultimately show ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   336
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   337
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   338
lemma Nlt_iff[simp]: assumes nx: "isnormNum x" and ny: "isnormNum y"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   339
  shows "(INum x < INum y) = (x <\<^sub>N y)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   340
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   341
  have "(INum x < INum y) = (INum (x -\<^sub>N y) < 0)" using nx ny by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   342
  also have "\<dots> = (0>\<^sub>N (x -\<^sub>N y))" using Nlt0_iff[OF Nsub_normN[OF ny]] by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   343
  finally show ?thesis by (simp add: Nlt_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   344
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   345
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   346
lemma [code]: "INum x < INum y \<longleftrightarrow> normNum x <\<^sub>N normNum y"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   347
proof -
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   348
  have "normNum x <\<^sub>N normNum y \<longleftrightarrow> INum (normNum x) < INum (normNum y)" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   349
    by (simp del: normNum)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   350
  also have "\<dots> = (INum x < INum y)" by simp 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   351
  finally show ?thesis by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   352
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   353
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   354
lemma Nle_iff[simp]: assumes nx: "isnormNum x" and ny: "isnormNum y"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   355
  shows "(INum x \<le> INum y) = (x \<le>\<^sub>N y)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   356
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   357
  have "(INum x \<le> INum y) = (INum (x -\<^sub>N y) \<le> 0)" using nx ny by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   358
  also have "\<dots> = (0\<ge>\<^sub>N (x -\<^sub>N y))" using Nle0_iff[OF Nsub_normN[OF ny]] by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   359
  finally show ?thesis by (simp add: Nle_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   360
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   361
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   362
lemma [code]: "INum x \<le> INum y \<longleftrightarrow> normNum x \<le>\<^sub>N normNum y"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   363
proof -
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   364
  have "normNum x \<le>\<^sub>N normNum y \<longleftrightarrow> INum (normNum x) \<le> INum (normNum y)" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   365
    by (simp del: normNum)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   366
  also have "\<dots> = (INum x \<le> INum y)" by simp 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   367
  finally show ?thesis by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   368
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   369
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   370
lemma Nadd_commute: "x +\<^sub>N y = y +\<^sub>N x"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   371
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   372
  have n: "isnormNum (x +\<^sub>N y)" "isnormNum (y +\<^sub>N x)" by simp_all
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   373
  have "INum (x +\<^sub>N y) = INum (y +\<^sub>N x)" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   374
  with isnormNum_unique[OF n] show ?thesis by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   375
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   376
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   377
lemma[simp]: "(0, b) +\<^sub>N y = normNum y" "(a, 0) +\<^sub>N y = normNum y" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   378
  "x +\<^sub>N (0, b) = normNum x" "x +\<^sub>N (a, 0) = normNum x"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   379
  apply (simp add: Nadd_def split_def, simp add: Nadd_def split_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   380
  apply (subst Nadd_commute,simp add: Nadd_def split_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   381
  apply (subst Nadd_commute,simp add: Nadd_def split_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   382
  done
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   383
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   384
lemma normNum_nilpotent_aux[simp]: assumes nx: "isnormNum x" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   385
  shows "normNum x = x"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   386
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   387
  let ?a = "normNum x"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   388
  have n: "isnormNum ?a" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   389
  have th:"INum ?a = INum x" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   390
  with isnormNum_unique[OF n nx]  
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   391
  show ?thesis by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   392
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   393
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   394
lemma normNum_nilpotent[simp]: "normNum (normNum x) = normNum x"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   395
  by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   396
lemma normNum0[simp]: "normNum (0,b) = 0\<^sub>N" "normNum (a,0) = 0\<^sub>N"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   397
  by (simp_all add: normNum_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   398
lemma normNum_Nadd: "normNum (x +\<^sub>N y) = x +\<^sub>N y" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   399
lemma Nadd_normNum1[simp]: "normNum x +\<^sub>N y = x +\<^sub>N y"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   400
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   401
  have n: "isnormNum (normNum x +\<^sub>N y)" "isnormNum (x +\<^sub>N y)" by simp_all
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   402
  have "INum (normNum x +\<^sub>N y) = INum x + INum y" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   403
  also have "\<dots> = INum (x +\<^sub>N y)" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   404
  finally show ?thesis using isnormNum_unique[OF n] by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   405
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   406
lemma Nadd_normNum2[simp]: "x +\<^sub>N normNum y = x +\<^sub>N y"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   407
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   408
  have n: "isnormNum (x +\<^sub>N normNum y)" "isnormNum (x +\<^sub>N y)" by simp_all
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   409
  have "INum (x +\<^sub>N normNum y) = INum x + INum y" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   410
  also have "\<dots> = INum (x +\<^sub>N y)" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   411
  finally show ?thesis using isnormNum_unique[OF n] by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   412
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   413
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   414
lemma Nadd_assoc: "x +\<^sub>N y +\<^sub>N z = x +\<^sub>N (y +\<^sub>N z)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   415
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   416
  have n: "isnormNum (x +\<^sub>N y +\<^sub>N z)" "isnormNum (x +\<^sub>N (y +\<^sub>N z))" by simp_all
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   417
  have "INum (x +\<^sub>N y +\<^sub>N z) = INum (x +\<^sub>N (y +\<^sub>N z))" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   418
  with isnormNum_unique[OF n] show ?thesis by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   419
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   420
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   421
lemma Nmul_commute: "isnormNum x \<Longrightarrow> isnormNum y \<Longrightarrow> x *\<^sub>N y = y *\<^sub>N x"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   422
  by (simp add: Nmul_def split_def Let_def igcd_commute mult_commute)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   423
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   424
lemma Nmul_assoc: assumes nx: "isnormNum x" and ny:"isnormNum y" and nz:"isnormNum z"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   425
  shows "x *\<^sub>N y *\<^sub>N z = x *\<^sub>N (y *\<^sub>N z)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   426
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   427
  from nx ny nz have n: "isnormNum (x *\<^sub>N y *\<^sub>N z)" "isnormNum (x *\<^sub>N (y *\<^sub>N z))" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   428
    by simp_all
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   429
  have "INum (x +\<^sub>N y +\<^sub>N z) = INum (x +\<^sub>N (y +\<^sub>N z))" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   430
  with isnormNum_unique[OF n] show ?thesis by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   431
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   432
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   433
lemma Nsub0: assumes x: "isnormNum x" and y:"isnormNum y" shows "(x -\<^sub>N y = 0\<^sub>N) = (x = y)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   434
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   435
  from isnormNum_unique[OF Nsub_normN[OF y], where y="0\<^sub>N"] 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   436
  have "(x -\<^sub>N y = 0\<^sub>N) = (INum (x -\<^sub>N y) = INum 0\<^sub>N)" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   437
  also have "\<dots> = (INum x = INum y)" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   438
  also have "\<dots> = (x = y)" using x y by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   439
  finally show ?thesis .
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   440
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   441
lemma Nmul0[simp]: "c *\<^sub>N 0\<^sub>N = 0\<^sub>N" " 0\<^sub>N *\<^sub>N c = 0\<^sub>N"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   442
  by (simp_all add: Nmul_def Let_def split_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   443
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   444
lemma Nmul_eq0[simp]: assumes nx:"isnormNum x" and ny: "isnormNum y"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   445
  shows "(x*\<^sub>N y = 0\<^sub>N) = (x = 0\<^sub>N \<or> y = 0\<^sub>N)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   446
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   447
  have " \<exists> a b a' b'. x = (a,b) \<and> y= (a',b')" by auto
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   448
  then obtain a b a' b' where xy[simp]: "x = (a,b)" "y = (a',b')" by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   449
  have n0: "isnormNum 0\<^sub>N" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   450
  show ?thesis using nx ny 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   451
    apply (simp only: isnormNum_unique[OF  Nmul_normN[OF nx ny] n0, symmetric] Nmul)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   452
    apply (simp add: INum_def split_def isnormNum_def fst_conv snd_conv)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   453
    apply (cases "a=0",simp_all)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   454
    apply (cases "a'=0",simp_all)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   455
    done 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   456
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   457
lemma Nneg_Nneg[simp]: "~\<^sub>N (~\<^sub>N c) = c"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   458
  by (simp add: Nneg_def split_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   459
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   460
lemma Nmul1[simp]: 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   461
  "isnormNum c \<Longrightarrow> 1\<^sub>N *\<^sub>N c = c" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   462
  "isnormNum c \<Longrightarrow> c *\<^sub>N 1\<^sub>N  = c" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   463
  apply (simp_all add: Nmul_def Let_def split_def isnormNum_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   464
  by (cases "fst c = 0", simp_all,cases c, simp_all)+
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   465
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   466
lemma [code, code unfold]:
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   467
  "number_of k = real_int (number_of k)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   468
  by (simp add: real_int_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   469
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   470
types_code real ("{* int * int *}")
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   471
attach (term_of) {*
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   472
fun term_of_real (p, q) =
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   473
  let 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   474
    val rT = HOLogic.realT;
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   475
in if q = 1
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   476
  then HOLogic.mk_number rT p
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   477
  else Const ("HOL.divide", rT --> rT --> rT) $
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   478
    HOLogic.mk_number rT p $ HOLogic.mk_number rT q
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   479
end;
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   480
*}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   481
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   482
consts_code INum ("")
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   483
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   484
end