src/HOL/Library/Executable_Real.thy
author haftmann
Thu, 28 Jun 2007 19:09:34 +0200
changeset 23512 770e7f9f715b
parent 23315 df3a7e9ebadb
child 24197 c9e3cb5e5681
permissions -rw-r--r--
code generation for dvd
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))))"
23315
df3a7e9ebadb tuned Proof
chaieb
parents: 23030
diff changeset
    27
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    28
lemma normNum_isnormNum[simp]: "isnormNum (normNum x)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    29
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    30
  have " \<exists> a b. x = (a,b)" by auto
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    31
  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
    32
  {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
    33
  moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    34
  {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
    35
    let ?g = "igcd a b"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    36
    let ?a' = "a div ?g"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    37
    let ?b' = "b div ?g"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    38
    let ?g' = "igcd ?a' ?b'"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    39
    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
    40
    have gpos: "?g > 0"  by arith
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    41
    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
    42
    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
    43
    anz bnz
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    44
    have nz':"?a' \<noteq> 0" "?b' \<noteq> 0" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    45
      by - (rule notI,simp add:igcd_def)+
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    46
    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
    47
    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
    48
    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
    49
    moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    50
    {assume b: "b > 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    51
      from pos_imp_zdiv_nonneg_iff[OF gpos] b
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    52
      have "?b' \<ge> 0" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    53
      with nz' have b': "?b' > 0" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    54
      from b b' anz bnz nz' gp1 have ?thesis 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    55
	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
    56
    moreover {assume b: "b < 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    57
      {assume b': "?b' \<ge> 0" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    58
	from gpos have th: "?g \<ge> 0" by arith
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    59
	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
    60
	have False using b by simp }
23315
df3a7e9ebadb tuned Proof
chaieb
parents: 23030
diff changeset
    61
      hence b': "?b' < 0" by (auto simp add: linorder_not_le[symmetric])
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    62
      from anz bnz nz' b b' gp1 have ?thesis 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    63
	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
    64
    ultimately have ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    65
  }
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    66
  ultimately show ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    67
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    68
    (* Arithmetic over Num *)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    69
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
    70
  "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
    71
  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
    72
  else normNum(a*b' + b*a', b*b')"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    73
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
    74
  "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
    75
  in (a*a' div g, b*b' div g)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    76
constdefs Nneg :: "Num \<Rightarrow> Num" ("~\<^sub>N")
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    77
  "Nneg \<equiv> \<lambda>(a,b). (-a,b)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    78
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
    79
  "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
    80
constdefs Ninv :: "Num \<Rightarrow> Num" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    81
"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
    82
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
    83
  "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
    84
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    85
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
    86
  by(simp add: isnormNum_def Nneg_def split_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    87
lemma Nadd_normN[simp]: "isnormNum (x +\<^sub>N y)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    88
  by (simp add: Nadd_def split_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    89
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
    90
  by (simp add: Nsub_def split_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    91
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
    92
  shows "isnormNum (x *\<^sub>N y)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    93
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    94
  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
    95
  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
    96
  {assume "a = 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    97
    hence ?thesis using xn ab ab'
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    98
      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
    99
  moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   100
  {assume "a' = 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   101
    hence ?thesis using yn ab ab' 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   102
      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
   103
  moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   104
  {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
   105
    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
   106
    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
   107
      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
   108
    hence ?thesis by simp}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   109
  ultimately show ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   110
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   111
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   112
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
   113
by (simp add: Ninv_def isnormNum_def split_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   114
(cases "fst x = 0",auto simp add: igcd_commute)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   115
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   116
lemma isnormNum_int[simp]: 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   117
  "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
   118
 by (simp_all add: isnormNum_def igcd_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   119
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   120
    (* Relations over Num *)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   121
constdefs Nlt0:: "Num \<Rightarrow> bool" ("0>\<^sub>N")
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   122
  "Nlt0 \<equiv> \<lambda>(a,b). a < 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   123
constdefs Nle0:: "Num \<Rightarrow> bool" ("0\<ge>\<^sub>N")
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   124
  "Nle0 \<equiv> \<lambda>(a,b). a \<le> 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   125
constdefs Ngt0:: "Num \<Rightarrow> bool" ("0<\<^sub>N")
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   126
  "Ngt0 \<equiv> \<lambda>(a,b). a > 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   127
constdefs Nge0:: "Num \<Rightarrow> bool" ("0\<le>\<^sub>N")
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   128
  "Nge0 \<equiv> \<lambda>(a,b). a \<ge> 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   129
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
   130
  "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
   131
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
   132
  "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
   133
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   134
22982
bff3fcdeecd3 dropped |R
chaieb
parents: 22981
diff changeset
   135
subsection {* Interpretation of the normalized rats in reals *}
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   136
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   137
definition
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   138
  INum:: "Num \<Rightarrow> real"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   139
where
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   140
  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
   141
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   142
code_datatype INum
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   143
instance real :: eq ..
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   144
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   145
definition
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   146
  real_int :: "int \<Rightarrow> real"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   147
where
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   148
  "real_int = real"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   149
lemmas [code unfold] = real_int_def [symmetric]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   150
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   151
lemma [code unfold]:
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   152
  "real = real_int o int"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   153
  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
   154
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   155
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
   156
  by (simp_all add: INum_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   157
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
   158
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   159
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
   160
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   161
lemma isnormNum_unique[simp]: 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   162
  assumes na: "isnormNum x" and nb: "isnormNum y" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   163
  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
   164
proof
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   165
  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
   166
  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
   167
  assume H: ?lhs 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   168
  {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
   169
      using na nb H
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   170
      by (simp add: INum_def split_def isnormNum_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   171
       (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
   172
      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
   173
  moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   174
  { 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
   175
    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
   176
    from prems have eq:"a * b' = a'*b" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   177
      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
   178
    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
   179
      by (simp_all add: isnormNum_def add: igcd_commute)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   180
    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
   181
      apply(unfold dvd_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   182
      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
   183
      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
   184
      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
   185
      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
   186
      done
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   187
    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
   188
      zrelprime_dvd_mult[OF gcd1(4) raw_dvd(4)]]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   189
      have eq1: "b = b'" using pos by simp_all
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   190
      with eq have "a = a'" using pos by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   191
      with eq1 have ?rhs by simp}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   192
  ultimately show ?rhs by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   193
next
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   194
  assume ?rhs thus ?lhs by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   195
qed
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
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   198
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
   199
  unfolding INum_int(2)[symmetric]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   200
  by (rule isnormNum_unique, simp_all)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   201
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   202
lemma normNum[simp]: "INum (normNum x) = INum x"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   203
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   204
  have "\<exists> a b. x = (a,b)" by auto
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   205
  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
   206
  {assume "a=0 \<or> b = 0" hence ?thesis
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   207
      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
   208
  moreover 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   209
  {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
   210
    let ?g = "igcd a b"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   211
    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
   212
    from real_of_int_div[OF g]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   213
    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
   214
  ultimately show ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   215
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   216
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   217
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
   218
proof -
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   219
  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
   220
    by (simp del: normNum)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   221
  also have "\<dots> = ?lhs" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   222
  finally show ?thesis by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   223
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   224
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   225
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
   226
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   227
  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
   228
  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
   229
    and y[simp]: "y = (a',b')" by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   230
  {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
   231
      apply (cases "a=0",simp_all add: Nadd_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   232
      apply (cases "b= 0",simp_all add: INum_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   233
       apply (cases "a'= 0",simp_all)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   234
       apply (cases "b'= 0",simp_all)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   235
       done }
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   236
  moreover 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   237
  {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
   238
    {assume z: "a * b' + b * a' = 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   239
      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
   240
      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
   241
      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
   242
      from z aa' bb' have ?thesis 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   243
	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
   244
    moreover {assume z: "a * b' + b * a' \<noteq> 0"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   245
      let ?g = "igcd (a * b' + b * a') (b*b')"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   246
      have gz: "?g \<noteq> 0" using z by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   247
      have ?thesis using aa' bb' z gz
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   248
	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
   249
	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
   250
	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
   251
    ultimately have ?thesis using aa' bb' 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   252
      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
   253
  ultimately show ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   254
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   255
lemmas [code] = Nadd [symmetric]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   256
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   257
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
   258
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   259
  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
   260
  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
   261
  {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
   262
      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
   263
      apply (cases "b=0",simp_all)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   264
      apply (cases "a'=0",simp_all) 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   265
      done }
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   266
  moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   267
  {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
   268
    let ?g="igcd (a*a') (b*b')"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   269
    have gz: "?g \<noteq> 0" using z by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   270
    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
   271
      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
   272
    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
   273
  ultimately show ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   274
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   275
lemmas [code] = Nmul [symmetric]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   276
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   277
lemma Nneg[simp]: "INum (~\<^sub>N x) = - INum x"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   278
  by (simp add: Nneg_def split_def INum_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   279
lemmas [code] = Nneg [symmetric]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   280
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   281
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
   282
  by (simp add: Nsub_def split_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   283
lemmas [code] = Nsub [symmetric]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   284
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   285
lemma Ninv[simp]: "INum (Ninv x) = 1 / (INum x)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   286
  by (simp add: Ninv_def INum_def split_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   287
lemmas [code] = Ninv [symmetric]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   288
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   289
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
   290
lemmas [code] = Ndiv [symmetric]
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   291
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   292
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
   293
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   294
  have " \<exists> a b. x = (a,b)" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   295
  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
   296
  {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
   297
  moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   298
  {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
   299
    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
   300
    have ?thesis by (simp add: Nlt0_def INum_def)}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   301
  ultimately show ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   302
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   303
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   304
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
   305
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   306
  have " \<exists> a b. x = (a,b)" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   307
  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
   308
  {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
   309
  moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   310
  {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
   311
    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
   312
    have ?thesis by (simp add: Nle0_def INum_def)}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   313
  ultimately show ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   314
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   315
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   316
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
   317
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   318
  have " \<exists> a b. x = (a,b)" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   319
  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
   320
  {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
   321
  moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   322
  {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
   323
    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
   324
    have ?thesis by (simp add: Ngt0_def INum_def)}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   325
  ultimately show ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   326
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   327
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
   328
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   329
  have " \<exists> a b. x = (a,b)" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   330
  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
   331
  {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
   332
  moreover
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   333
  {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
   334
    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
   335
    have ?thesis by (simp add: Nge0_def INum_def)}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   336
  ultimately show ?thesis by blast
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   337
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   338
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   339
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
   340
  shows "(INum x < INum y) = (x <\<^sub>N y)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   341
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   342
  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
   343
  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
   344
  finally show ?thesis by (simp add: Nlt_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   345
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   346
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   347
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
   348
proof -
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   349
  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
   350
    by (simp del: normNum)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   351
  also have "\<dots> = (INum x < INum y)" by simp 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   352
  finally show ?thesis by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   353
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   354
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   355
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
   356
  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
   357
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   358
  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
   359
  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
   360
  finally show ?thesis by (simp add: Nle_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   361
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   362
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   363
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
   364
proof -
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   365
  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
   366
    by (simp del: normNum)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   367
  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
   368
  finally show ?thesis by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   369
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   370
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   371
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
   372
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   373
  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
   374
  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
   375
  with isnormNum_unique[OF n] show ?thesis by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   376
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   377
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   378
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
   379
  "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
   380
  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
   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
  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
   383
  done
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   384
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   385
lemma normNum_nilpotent_aux[simp]: assumes nx: "isnormNum x" 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   386
  shows "normNum x = x"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   387
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   388
  let ?a = "normNum x"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   389
  have n: "isnormNum ?a" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   390
  have th:"INum ?a = INum x" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   391
  with isnormNum_unique[OF n nx]  
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   392
  show ?thesis by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   393
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   394
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   395
lemma normNum_nilpotent[simp]: "normNum (normNum x) = normNum x"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   396
  by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   397
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
   398
  by (simp_all add: normNum_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   399
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
   400
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
   401
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   402
  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
   403
  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
   404
  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
   405
  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
   406
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   407
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
   408
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   409
  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
   410
  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
   411
  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
   412
  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
   413
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   414
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   415
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
   416
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   417
  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
   418
  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
   419
  with isnormNum_unique[OF n] show ?thesis by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   420
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   421
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   422
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
   423
  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
   424
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   425
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
   426
  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
   427
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   428
  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
   429
    by simp_all
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   430
  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
   431
  with isnormNum_unique[OF n] show ?thesis by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   432
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   433
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   434
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
   435
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   436
  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
   437
  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
   438
  also have "\<dots> = (INum x = INum y)" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   439
  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
   440
  finally show ?thesis .
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   441
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   442
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
   443
  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
   444
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   445
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
   446
  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
   447
proof-
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   448
  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
   449
  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
   450
  have n0: "isnormNum 0\<^sub>N" by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   451
  show ?thesis using nx ny 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   452
    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
   453
    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
   454
    apply (cases "a=0",simp_all)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   455
    apply (cases "a'=0",simp_all)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   456
    done 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   457
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   458
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
   459
  by (simp add: Nneg_def split_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   460
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   461
lemma Nmul1[simp]: 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   462
  "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
   463
  "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
   464
  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
   465
  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
   466
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   467
lemma [code, code unfold]:
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   468
  "number_of k = real_int (number_of k)"
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   469
  by (simp add: real_int_def)
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   470
23017
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
   471
code_modulename SML
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
   472
  RealDef Real
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
   473
  Executable_Real Real
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
   474
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
   475
code_modulename OCaml
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
   476
  RealDef Real
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
   477
  Executable_Real Real
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
   478
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
   479
code_modulename Haskell
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
   480
  RealDef Real
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
   481
  Executable_Real Real
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
   482
23030
c7ff1537c4bf Disabled Stefancs code generator - already enabled in RealDef.
nipkow
parents: 23017
diff changeset
   483
(* There is already an implementation in RealDef
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   484
types_code real ("{* int * int *}")
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   485
attach (term_of) {*
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   486
fun term_of_real (p, q) =
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   487
  let 
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   488
    val rT = HOLogic.realT;
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   489
in if q = 1
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   490
  then HOLogic.mk_number rT p
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22982
diff changeset
   491
  else @{term "op / \<Colon> real \<Rightarrow> real \<Rightarrow> real"} $
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   492
    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
   493
end;
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   494
*}
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   495
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
   496
consts_code INum ("")
23030
c7ff1537c4bf Disabled Stefancs code generator - already enabled in RealDef.
nipkow
parents: 23017
diff changeset
   497
*)
c7ff1537c4bf Disabled Stefancs code generator - already enabled in RealDef.
nipkow
parents: 23017
diff changeset
   498
end