src/HOL/Library/Executable_Real.thy
author wenzelm
Fri, 17 Aug 2007 00:03:50 +0200
changeset 24300 e170cee91c66
parent 24197 c9e3cb5e5681
permissions -rw-r--r--
proper signature for Meson;
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$
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
22981
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
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
     9
imports Abstract_Rat "~~/src/HOL/Real/Real"
22981
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
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    12
hide (open) const Real
22981
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
definition
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    15
  Real :: "int \<times> int \<Rightarrow> real"
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    16
where
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    17
  "Real = INum"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    18
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    19
code_datatype Real
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    20
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    21
lemma Real_simp:
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    22
  "Real (k, l) = real_of_int k / real_of_int l"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    23
  unfolding Real_def INum_def by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    24
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    25
lemma Real_zero [simp]: "Real 0\<^sub>N = 0"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    26
  by (simp add: Real_simp)
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    27
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    28
lemma Real_lit [simp]: "Real i\<^sub>N = real_of_int i"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    29
  by (simp add: Real_simp)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    30
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    31
lemma zero_real_code [code]:
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    32
  "0 = Real 0\<^sub>N" by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    33
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    34
lemma zero_real_code [code]:
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    35
  "1 = Real 1\<^sub>N" by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    36
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    37
lemma [code, code unfold]:
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    38
  "number_of k = real_of_int (number_of k)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    39
  by (simp add: number_of_is_id real_number_of_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    40
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    41
instance real :: eq ..
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    42
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    43
lemma real_eq_code [code]: "Real x = Real y \<longleftrightarrow> normNum x = normNum y"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    44
  unfolding Real_def INum_normNum_iff ..
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    45
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    46
lemma real_less_eq_code [code]: "Real x \<le> Real y \<longleftrightarrow> normNum x \<le>\<^sub>N normNum y"
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    47
proof -
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    48
  have "normNum x \<le>\<^sub>N normNum y \<longleftrightarrow> Real (normNum x) \<le> Real (normNum y)" 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    49
    by (simp add: Real_def del: normNum)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    50
  also have "\<dots> = (Real x \<le> Real y)" by (simp add: Real_def)
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    51
  finally show ?thesis by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    52
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    53
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    54
lemma real_less_code [code]: "Real x < Real y \<longleftrightarrow> normNum x <\<^sub>N normNum y"
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    55
proof -
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    56
  have "normNum x <\<^sub>N normNum y \<longleftrightarrow> Real (normNum x) < Real (normNum y)" 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    57
    by (simp add: Real_def del: normNum)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    58
  also have "\<dots> = (Real x < Real y)" by (simp add: Real_def)
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    59
  finally show ?thesis by simp
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    60
qed
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    61
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    62
lemma real_add_code [code]: "Real x + Real y = Real (x +\<^sub>N y)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    63
  unfolding Real_def by simp
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    64
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    65
lemma real_mul_code [code]: "Real x * Real y = Real (x *\<^sub>N y)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    66
  unfolding Real_def by simp
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    67
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    68
lemma real_neg_code [code]: "- Real x = Real (~\<^sub>N x)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    69
  unfolding Real_def by simp
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    70
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    71
lemma real_sub_code [code]: "Real x - Real y = Real (x -\<^sub>N y)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    72
  unfolding Real_def by simp
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    73
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    74
lemma real_inv_code [code]: "inverse (Real x) = Real (Ninv x)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    75
  unfolding Real_def Ninv real_divide_def by simp
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    76
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    77
lemma real_div_code [code]: "Real x / Real y = Real (x \<div>\<^sub>N y)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23315
diff changeset
    78
  unfolding Real_def by simp
22981
cf071f3fc4ae A verified theory for rational numbers representation and simple calculations;
chaieb
parents:
diff changeset
    79
23017
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
    80
code_modulename SML
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
    81
  RealDef Real
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
    82
  Executable_Real Real
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
    83
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
    84
code_modulename OCaml
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
    85
  RealDef Real
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
    86
  Executable_Real Real
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
    87
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
    88
code_modulename Haskell
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
    89
  RealDef Real
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
    90
  Executable_Real Real
00c0e4c42396 uniform module names for code generation
haftmann
parents: 22997
diff changeset
    91
23030
c7ff1537c4bf Disabled Stefancs code generator - already enabled in RealDef.
nipkow
parents: 23017
diff changeset
    92
end