948 by simp |
948 by simp |
949 |
949 |
950 lemma abs_sum_triangle_ineq: "abs ((x::real) + y + (-l + -m)) \<le> abs(x + -l) + abs(y + -m)" |
950 lemma abs_sum_triangle_ineq: "abs ((x::real) + y + (-l + -m)) \<le> abs(x + -l) + abs(y + -m)" |
951 by simp |
951 by simp |
952 |
952 |
953 |
|
954 subsection {* Implementation of rational real numbers as pairs of integers *} |
|
955 |
|
956 definition |
|
957 Ratreal :: "int \<times> int \<Rightarrow> real" |
|
958 where |
|
959 "Ratreal = INum" |
|
960 |
|
961 code_datatype Ratreal |
|
962 |
|
963 lemma Ratreal_simp: |
|
964 "Ratreal (k, l) = real_of_int k / real_of_int l" |
|
965 unfolding Ratreal_def INum_def by simp |
|
966 |
|
967 lemma Ratreal_zero [simp]: "Ratreal 0\<^sub>N = 0" |
|
968 by (simp add: Ratreal_simp) |
|
969 |
|
970 lemma Ratreal_lit [simp]: "Ratreal i\<^sub>N = real_of_int i" |
|
971 by (simp add: Ratreal_simp) |
|
972 |
|
973 lemma zero_real_code [code, code unfold]: |
|
974 "0 = Ratreal 0\<^sub>N" by simp |
|
975 declare zero_real_code [symmetric, code post] |
|
976 |
|
977 lemma one_real_code [code, code unfold]: |
|
978 "1 = Ratreal 1\<^sub>N" by simp |
|
979 declare one_real_code [symmetric, code post] |
|
980 |
|
981 instantiation real :: eq |
|
982 begin |
|
983 |
|
984 definition "eq (x\<Colon>real) y \<longleftrightarrow> x = y" |
|
985 |
|
986 instance by default (simp add: eq_real_def) |
|
987 |
|
988 lemma real_eq_code [code]: "Ratreal x = Ratreal y \<longleftrightarrow> normNum x = normNum y" |
|
989 unfolding Ratreal_def INum_normNum_iff eq .. |
|
990 |
|
991 end |
|
992 |
|
993 lemma real_less_eq_code [code]: "Ratreal x \<le> Ratreal y \<longleftrightarrow> normNum x \<le>\<^sub>N normNum y" |
|
994 proof - |
|
995 have "normNum x \<le>\<^sub>N normNum y \<longleftrightarrow> Ratreal (normNum x) \<le> Ratreal (normNum y)" |
|
996 by (simp add: Ratreal_def del: normNum) |
|
997 also have "\<dots> = (Ratreal x \<le> Ratreal y)" by (simp add: Ratreal_def) |
|
998 finally show ?thesis by simp |
|
999 qed |
|
1000 |
|
1001 lemma real_less_code [code]: "Ratreal x < Ratreal y \<longleftrightarrow> normNum x <\<^sub>N normNum y" |
|
1002 proof - |
|
1003 have "normNum x <\<^sub>N normNum y \<longleftrightarrow> Ratreal (normNum x) < Ratreal (normNum y)" |
|
1004 by (simp add: Ratreal_def del: normNum) |
|
1005 also have "\<dots> = (Ratreal x < Ratreal y)" by (simp add: Ratreal_def) |
|
1006 finally show ?thesis by simp |
|
1007 qed |
|
1008 |
|
1009 lemma real_add_code [code]: "Ratreal x + Ratreal y = Ratreal (x +\<^sub>N y)" |
|
1010 unfolding Ratreal_def by simp |
|
1011 |
|
1012 lemma real_mul_code [code]: "Ratreal x * Ratreal y = Ratreal (x *\<^sub>N y)" |
|
1013 unfolding Ratreal_def by simp |
|
1014 |
|
1015 lemma real_neg_code [code]: "- Ratreal x = Ratreal (~\<^sub>N x)" |
|
1016 unfolding Ratreal_def by simp |
|
1017 |
|
1018 lemma real_sub_code [code]: "Ratreal x - Ratreal y = Ratreal (x -\<^sub>N y)" |
|
1019 unfolding Ratreal_def by simp |
|
1020 |
|
1021 lemma real_inv_code [code]: "inverse (Ratreal x) = Ratreal (Ninv x)" |
|
1022 unfolding Ratreal_def Ninv real_divide_def by simp |
|
1023 |
|
1024 lemma real_div_code [code]: "Ratreal x / Ratreal y = Ratreal (x \<div>\<^sub>N y)" |
|
1025 unfolding Ratreal_def by simp |
|
1026 |
|
1027 instance real :: lordered_ring |
953 instance real :: lordered_ring |
1028 proof |
954 proof |
1029 fix a::real |
955 fix a::real |
1030 show "abs a = sup a (-a)" |
956 show "abs a = sup a (-a)" |
1031 by (auto simp add: real_abs_def sup_real_def) |
957 by (auto simp add: real_abs_def sup_real_def) |
1032 qed |
958 qed |
|
959 |
|
960 |
|
961 subsection {* Implementation of rational real numbers as pairs of integers *} |
|
962 |
|
963 definition |
|
964 Ratreal :: "int \<times> int \<Rightarrow> real" |
|
965 where |
|
966 "Ratreal = INum" |
|
967 |
|
968 code_datatype Ratreal |
|
969 |
|
970 lemma Ratreal_simp: |
|
971 "Ratreal (k, l) = real_of_int k / real_of_int l" |
|
972 unfolding Ratreal_def INum_def by simp |
|
973 |
|
974 lemma Ratreal_zero [simp]: "Ratreal 0\<^sub>N = 0" |
|
975 by (simp add: Ratreal_simp) |
|
976 |
|
977 lemma Ratreal_lit [simp]: "Ratreal i\<^sub>N = real_of_int i" |
|
978 by (simp add: Ratreal_simp) |
|
979 |
|
980 lemma zero_real_code [code, code unfold]: |
|
981 "0 = Ratreal 0\<^sub>N" by simp |
|
982 declare zero_real_code [symmetric, code post] |
|
983 |
|
984 lemma one_real_code [code, code unfold]: |
|
985 "1 = Ratreal 1\<^sub>N" by simp |
|
986 declare one_real_code [symmetric, code post] |
|
987 |
|
988 instantiation real :: eq |
|
989 begin |
|
990 |
|
991 definition "eq_class.eq (x\<Colon>real) y \<longleftrightarrow> x = y" |
|
992 |
|
993 instance by default (simp add: eq_real_def) |
|
994 |
|
995 lemma real_eq_code [code]: "eq_class.eq (Ratreal x) (Ratreal y) \<longleftrightarrow> eq_class.eq (normNum x) (normNum y)" |
|
996 unfolding Ratreal_def INum_normNum_iff eq .. |
|
997 |
|
998 end |
|
999 |
|
1000 lemma real_less_eq_code [code]: "Ratreal x \<le> Ratreal y \<longleftrightarrow> normNum x \<le>\<^sub>N normNum y" |
|
1001 proof - |
|
1002 have "normNum x \<le>\<^sub>N normNum y \<longleftrightarrow> Ratreal (normNum x) \<le> Ratreal (normNum y)" |
|
1003 by (simp add: Ratreal_def del: normNum) |
|
1004 also have "\<dots> = (Ratreal x \<le> Ratreal y)" by (simp add: Ratreal_def) |
|
1005 finally show ?thesis by simp |
|
1006 qed |
|
1007 |
|
1008 lemma real_less_code [code]: "Ratreal x < Ratreal y \<longleftrightarrow> normNum x <\<^sub>N normNum y" |
|
1009 proof - |
|
1010 have "normNum x <\<^sub>N normNum y \<longleftrightarrow> Ratreal (normNum x) < Ratreal (normNum y)" |
|
1011 by (simp add: Ratreal_def del: normNum) |
|
1012 also have "\<dots> = (Ratreal x < Ratreal y)" by (simp add: Ratreal_def) |
|
1013 finally show ?thesis by simp |
|
1014 qed |
|
1015 |
|
1016 lemma real_add_code [code]: "Ratreal x + Ratreal y = Ratreal (x +\<^sub>N y)" |
|
1017 unfolding Ratreal_def by simp |
|
1018 |
|
1019 lemma real_mul_code [code]: "Ratreal x * Ratreal y = Ratreal (x *\<^sub>N y)" |
|
1020 unfolding Ratreal_def by simp |
|
1021 |
|
1022 lemma real_neg_code [code]: "- Ratreal x = Ratreal (~\<^sub>N x)" |
|
1023 unfolding Ratreal_def by simp |
|
1024 |
|
1025 lemma real_sub_code [code]: "Ratreal x - Ratreal y = Ratreal (x -\<^sub>N y)" |
|
1026 unfolding Ratreal_def by simp |
|
1027 |
|
1028 lemma real_inv_code [code]: "inverse (Ratreal x) = Ratreal (Ninv x)" |
|
1029 unfolding Ratreal_def Ninv real_divide_def by simp |
|
1030 |
|
1031 lemma real_div_code [code]: "Ratreal x / Ratreal y = Ratreal (x \<div>\<^sub>N y)" |
|
1032 unfolding Ratreal_def by simp |
1033 |
1033 |
1034 text {* Setup for SML code generator *} |
1034 text {* Setup for SML code generator *} |
1035 |
1035 |
1036 types_code |
1036 types_code |
1037 real ("(int */ int)") |
1037 real ("(int */ int)") |