18 "realrel = {p. \<exists>x1 y1 x2 y2. p = ((x1,y1),(x2,y2)) & x1+y2 = x2+y1}" |
18 "realrel = {p. \<exists>x1 y1 x2 y2. p = ((x1,y1),(x2,y2)) & x1+y2 = x2+y1}" |
19 |
19 |
20 typedef (Real) real = "UNIV//realrel" |
20 typedef (Real) real = "UNIV//realrel" |
21 by (auto simp add: quotient_def) |
21 by (auto simp add: quotient_def) |
22 |
22 |
23 instance real :: "{ord, zero, one, plus, times, minus, inverse}" .. |
|
24 |
|
25 definition |
23 definition |
26 |
|
27 (** these don't use the overloaded "real" function: users don't see them **) |
24 (** these don't use the overloaded "real" function: users don't see them **) |
28 |
|
29 real_of_preal :: "preal => real" where |
25 real_of_preal :: "preal => real" where |
30 "real_of_preal m = Abs_Real(realrel``{(m + 1, 1)})" |
26 "real_of_preal m = Abs_Real(realrel``{(m + 1, 1)})" |
31 |
27 |
32 consts |
28 consts |
33 (*overloaded constant for injecting other types into "real"*) |
29 (*overloaded constant for injecting other types into "real"*) |
34 real :: "'a => real" |
30 real :: "'a => real" |
35 |
31 |
36 |
32 instance real :: zero |
37 defs (overloaded) |
33 real_zero_def: "0 == Abs_Real(realrel``{(1, 1)})" .. |
38 |
34 |
39 real_zero_def: |
35 instance real :: one |
40 "0 == Abs_Real(realrel``{(1, 1)})" |
36 real_one_def: "1 == Abs_Real(realrel``{(1 + 1, 1)})" .. |
41 |
37 |
42 real_one_def: |
38 instance real :: plus |
43 "1 == Abs_Real(realrel``{(1 + 1, 1)})" |
39 real_add_def: "z + w == |
44 |
|
45 real_minus_def: |
|
46 "- r == contents (\<Union>(x,y) \<in> Rep_Real(r). { Abs_Real(realrel``{(y,x)}) })" |
|
47 |
|
48 real_add_def: |
|
49 "z + w == |
|
50 contents (\<Union>(x,y) \<in> Rep_Real(z). \<Union>(u,v) \<in> Rep_Real(w). |
40 contents (\<Union>(x,y) \<in> Rep_Real(z). \<Union>(u,v) \<in> Rep_Real(w). |
51 { Abs_Real(realrel``{(x+u, y+v)}) })" |
41 { Abs_Real(realrel``{(x+u, y+v)}) })" .. |
52 |
42 |
53 real_diff_def: |
43 instance real :: minus |
54 "r - (s::real) == r + - s" |
44 real_minus_def: "- r == contents (\<Union>(x,y) \<in> Rep_Real(r). { Abs_Real(realrel``{(y,x)}) })" |
55 |
45 real_diff_def: "r - (s::real) == r + - s" .. |
|
46 |
|
47 instance real :: times |
56 real_mult_def: |
48 real_mult_def: |
57 "z * w == |
49 "z * w == |
58 contents (\<Union>(x,y) \<in> Rep_Real(z). \<Union>(u,v) \<in> Rep_Real(w). |
50 contents (\<Union>(x,y) \<in> Rep_Real(z). \<Union>(u,v) \<in> Rep_Real(w). |
59 { Abs_Real(realrel``{(x*u + y*v, x*v + y*u)}) })" |
51 { Abs_Real(realrel``{(x*u + y*v, x*v + y*u)}) })" .. |
60 |
52 |
61 real_inverse_def: |
53 instance real :: inverse |
62 "inverse (R::real) == (THE S. (R = 0 & S = 0) | S * R = 1)" |
54 real_inverse_def: "inverse (R::real) == (THE S. (R = 0 & S = 0) | S * R = 1)" |
63 |
55 real_divide_def: "R / (S::real) == R * inverse S" .. |
64 real_divide_def: |
56 |
65 "R / (S::real) == R * inverse S" |
57 instance real :: ord |
66 |
58 real_le_def: "z \<le> (w::real) == |
67 real_le_def: |
|
68 "z \<le> (w::real) == |
|
69 \<exists>x y u v. x+v \<le> u+y & (x,y) \<in> Rep_Real z & (u,v) \<in> Rep_Real w" |
59 \<exists>x y u v. x+v \<le> u+y & (x,y) \<in> Rep_Real z & (u,v) \<in> Rep_Real w" |
70 |
60 real_less_def: "(x < (y::real)) == (x \<le> y & x \<noteq> y)" .. |
71 real_less_def: "(x < (y::real)) == (x \<le> y & x \<noteq> y)" |
61 |
72 |
62 instance real :: abs |
73 real_abs_def: "abs (r::real) == (if r < 0 then - r else r)" |
63 real_abs_def: "abs (r::real) == (if r < 0 then - r else r)" .. |
74 |
64 |
75 |
65 |
76 subsection {* Equivalence relation over positive reals *} |
66 subsection {* Equivalence relation over positive reals *} |
77 |
67 |
78 lemma preal_trans_lemma: |
68 lemma preal_trans_lemma: |
944 "uminus :: real \<Rightarrow> real" ("Rat.neg") |
934 "uminus :: real \<Rightarrow> real" ("Rat.neg") |
945 "op + :: real \<Rightarrow> real \<Rightarrow> real" ("Rat.add") |
935 "op + :: real \<Rightarrow> real \<Rightarrow> real" ("Rat.add") |
946 "op * :: real \<Rightarrow> real \<Rightarrow> real" ("Rat.mult") |
936 "op * :: real \<Rightarrow> real \<Rightarrow> real" ("Rat.mult") |
947 "inverse :: real \<Rightarrow> real" ("Rat.inv") |
937 "inverse :: real \<Rightarrow> real" ("Rat.inv") |
948 "op \<le> :: real \<Rightarrow> real \<Rightarrow> bool" ("Rat.le") |
938 "op \<le> :: real \<Rightarrow> real \<Rightarrow> bool" ("Rat.le") |
949 "op < :: real \<Rightarrow> real \<Rightarrow> bool" ("(Rat.ord (_, _) = LESS)") |
939 "op < :: real \<Rightarrow> real \<Rightarrow> bool" ("Rat.lt") |
950 "op = :: real \<Rightarrow> real \<Rightarrow> bool" ("curry Rat.eq") |
940 "op = :: real \<Rightarrow> real \<Rightarrow> bool" ("curry Rat.eq") |
951 "real :: int \<Rightarrow> real" ("Rat.rat'_of'_int") |
941 "real :: int \<Rightarrow> real" ("Rat.rat'_of'_int") |
952 "real :: nat \<Rightarrow> real" ("(Rat.rat'_of'_int o {*int*})") |
942 "real :: nat \<Rightarrow> real" ("(Rat.rat'_of'_int o {*int*})") |
953 |
943 |
954 |
|
955 lemma [code, code unfold]: |
944 lemma [code, code unfold]: |
956 "number_of k = real (number_of k :: int)" |
945 "number_of k = real (number_of k :: int)" |
957 by simp |
946 by simp |
958 |
947 |
959 end |
948 end |