src/HOL/Library/ExecutableRat.thy
author haftmann
Tue, 06 Jun 2006 15:02:55 +0200
changeset 19791 ab326de16ad5
parent 19609 a677ac8c9b10
child 19889 2202a5648897
permissions -rw-r--r--
refined code generation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Library/ExecutableRat.thy
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
     2
    ID:         $Id$
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
     4
*)
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
     5
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
     6
header {* Executable implementation of rational numbers in HOL *}
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
     7
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
     8
theory ExecutableRat
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
     9
imports "~~/src/HOL/Real/Rational" "~~/src/HOL/NumberTheory/IntPrimes"
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    10
begin
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    11
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    12
text {*
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    13
  Actually nothing is proved about the implementation.
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    14
*}
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    15
19791
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    16
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    17
section {* HOL definitions *}
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    18
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    19
datatype erat = Rat bool int int
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    20
19233
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 19137
diff changeset
    21
instance erat :: zero ..
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 19137
diff changeset
    22
instance erat :: one ..
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 19137
diff changeset
    23
instance erat :: plus ..
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 19137
diff changeset
    24
instance erat :: minus ..
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 19137
diff changeset
    25
instance erat :: times ..
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 19137
diff changeset
    26
instance erat :: inverse ..
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 19137
diff changeset
    27
instance erat :: ord ..
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    28
19609
a677ac8c9b10 fixed codegen bug, cleanup
haftmann
parents: 19233
diff changeset
    29
definition
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    30
  norm :: "erat \<Rightarrow> erat"
19791
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    31
  norm_def: "norm r = (case r of (Rat a p q) \<Rightarrow>
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    32
     if p = 0 then Rat True 0 1
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    33
     else
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    34
       let
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    35
         absp = abs p
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    36
       in let
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    37
         m = zgcd (absp, q)
19609
a677ac8c9b10 fixed codegen bug, cleanup
haftmann
parents: 19233
diff changeset
    38
       in Rat (a = (0 <= p)) (absp div m) (q div m))"
a677ac8c9b10 fixed codegen bug, cleanup
haftmann
parents: 19233
diff changeset
    39
  common :: "(int * int) * (int * int) \<Rightarrow> (int * int) * int"
19791
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    40
  common_def: "common r = (case r of ((p1, q1), (p2, q2)) \<Rightarrow>
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    41
       let q' = q1 * q2 div int (gcd (nat q1, nat q2))
19609
a677ac8c9b10 fixed codegen bug, cleanup
haftmann
parents: 19233
diff changeset
    42
       in ((p1 * (q' div q1), p2 * (q' div q2)), q'))"
a677ac8c9b10 fixed codegen bug, cleanup
haftmann
parents: 19233
diff changeset
    43
  of_quotient :: "int * int \<Rightarrow> erat"
19791
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    44
  of_quotient_def: "of_quotient r = (case r of (a, b) \<Rightarrow>
19609
a677ac8c9b10 fixed codegen bug, cleanup
haftmann
parents: 19233
diff changeset
    45
       norm (Rat True a b))"
a677ac8c9b10 fixed codegen bug, cleanup
haftmann
parents: 19233
diff changeset
    46
  of_rat :: "rat \<Rightarrow> erat"
19791
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    47
  of_rat_def: "of_rat r = of_quotient (SOME s. s : Rep_Rat r)"
19609
a677ac8c9b10 fixed codegen bug, cleanup
haftmann
parents: 19233
diff changeset
    48
  to_rat :: "erat \<Rightarrow> rat"
19791
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    49
  to_rat_def: "to_rat r = (case r of (Rat a p q) \<Rightarrow>
19609
a677ac8c9b10 fixed codegen bug, cleanup
haftmann
parents: 19233
diff changeset
    50
       if a then Fract p q else Fract (uminus p) q)"
19791
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    51
  eq_rat :: "erat \<Rightarrow> erat \<Rightarrow> bool"
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    52
  "eq_rat r s = (norm r = norm s)"
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    53
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    54
defs (overloaded)
19791
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    55
  zero_rat_def: "0 == Rat True 0 1"
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    56
  one_rat_def: "1 == Rat True 1 1"
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    57
  add_rat_def: "r + s == case r of Rat a1 p1 q1 \<Rightarrow> case s of Rat a2 p2 q2 \<Rightarrow>
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    58
        let
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    59
          ((r1, r2), den) = common ((p1, q1), (p2, q2))
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    60
        in let
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    61
          num = (if a1 then r1 else -r1) + (if a2 then r2 else -r2)
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    62
        in norm (Rat True num den)"
19791
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    63
  uminus_rat_def: "- r == case r of Rat a p q \<Rightarrow>
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    64
        if p = 0 then Rat a p q
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    65
        else Rat (\<not> a) p q"
19791
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    66
  times_rat_def: "r * s == case r of Rat a1 p1 q1 \<Rightarrow> case s of Rat a2 p2 q2 \<Rightarrow>
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    67
        norm (Rat (a1 = a2) (p1 * p2) (q1 * q2))"
19791
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    68
  inverse_rat_def: "inverse r == case r of Rat a p q \<Rightarrow>
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    69
        if p = 0 then arbitrary
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    70
        else Rat a q p"
19791
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    71
  le_rat_def: "r <= s == case r of Rat a1 p1 q1 \<Rightarrow> case s of Rat a2 p2 q2 \<Rightarrow>
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    72
        (\<not> a1 \<and> a2) \<or>
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    73
        (\<not> (a1 \<and> \<not> a2) \<and>
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    74
          (let
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    75
            ((r1, r2), dummy) = common ((p1, q1), (p2, q2))
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    76
          in if a1 then r1 <= r2 else r2 <= r1))"
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    77
19791
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    78
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    79
section {* type serializations *}
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    80
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    81
types_code
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    82
  rat ("{*erat*}")
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    83
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    84
code_syntax_tyco rat
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    85
  ml (target_atom "{*erat*}")
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    86
  haskell (target_atom "{*erat*}")
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
    87
19791
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    88
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    89
section {* const serializations *}
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    90
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    91
consts_code
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    92
  arbitrary :: erat ("raise/ (Fail/ \"non-defined rational number\")")
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    93
  Fract ("{*of_quotient*}")
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    94
  0 :: rat ("{*0::erat*}")
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    95
  1 :: rat ("{*1::erat*}")
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    96
  HOL.plus :: "rat \<Rightarrow> rat \<Rightarrow> rat" ("{*op + :: erat \<Rightarrow> erat \<Rightarrow> erat*}")
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    97
  uminus :: "rat \<Rightarrow> rat" ("{*uminus :: erat \<Rightarrow> erat*}")
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    98
  HOL.times :: "rat \<Rightarrow> rat \<Rightarrow> rat" ("{*op * :: erat \<Rightarrow> erat \<Rightarrow> erat*}")
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
    99
  inverse :: "rat \<Rightarrow> rat" ("{*inverse :: erat \<Rightarrow> erat*}")
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
   100
  divide :: "rat \<Rightarrow> rat \<Rightarrow> rat" ("{*op * :: erat \<Rightarrow> erat \<Rightarrow> erat*}/ _/ ({*inverse :: erat \<Rightarrow> erat*}/ _)")
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
   101
  Orderings.less_eq :: "rat \<Rightarrow> rat \<Rightarrow> bool" ("{*op <= :: erat \<Rightarrow> erat \<Rightarrow> bool*}")
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
   102
  "op =" :: "rat \<Rightarrow> rat \<Rightarrow> bool" ("{*eq_rat*}")
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
   103
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   104
code_syntax_const
19609
a677ac8c9b10 fixed codegen bug, cleanup
haftmann
parents: 19233
diff changeset
   105
  "arbitrary :: erat"
a677ac8c9b10 fixed codegen bug, cleanup
haftmann
parents: 19233
diff changeset
   106
    ml ("raise/ (Fail/ \"non-defined rational number\")")
a677ac8c9b10 fixed codegen bug, cleanup
haftmann
parents: 19233
diff changeset
   107
    haskell ("error/ \"non-defined rational number\"")
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   108
  Fract
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   109
    ml ("{*of_quotient*}")
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   110
    haskell ("{*of_quotient*}")
19137
f92919b141b2 change in codegen syntax
haftmann
parents: 19082
diff changeset
   111
  "0 :: rat"
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   112
    ml ("{*0::erat*}")
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   113
    haskell ("{*1::erat*}")
19137
f92919b141b2 change in codegen syntax
haftmann
parents: 19082
diff changeset
   114
  "1 :: rat"
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   115
    ml ("{*1::erat*}")
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   116
    haskell ("{*1::erat*}")
19137
f92919b141b2 change in codegen syntax
haftmann
parents: 19082
diff changeset
   117
  "op + :: rat \<Rightarrow> rat \<Rightarrow> rat"
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   118
    ml ("{*op + :: erat \<Rightarrow> erat \<Rightarrow> erat*}")
19791
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
   119
    haskell ("{*op + :: erat \<Rightarrow> erat \<Rightarrow> erat*}")
19137
f92919b141b2 change in codegen syntax
haftmann
parents: 19082
diff changeset
   120
  "uminus :: rat \<Rightarrow> rat"
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   121
    ml ("{*uminus :: erat \<Rightarrow> erat*}")
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   122
    haskell ("{*uminus :: erat \<Rightarrow> erat*}")
19137
f92919b141b2 change in codegen syntax
haftmann
parents: 19082
diff changeset
   123
  "op * :: rat \<Rightarrow> rat \<Rightarrow> rat"
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   124
    ml ("{*op * :: erat \<Rightarrow> erat \<Rightarrow> erat*}")
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   125
    haskell ("{*op * :: erat \<Rightarrow> erat \<Rightarrow> erat*}")
19137
f92919b141b2 change in codegen syntax
haftmann
parents: 19082
diff changeset
   126
  "inverse :: rat \<Rightarrow> rat"
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   127
    ml ("{*inverse :: erat \<Rightarrow> erat*}")
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   128
    haskell ("{*inverse :: erat \<Rightarrow> erat*}")
19137
f92919b141b2 change in codegen syntax
haftmann
parents: 19082
diff changeset
   129
  "divide :: rat \<Rightarrow> rat \<Rightarrow> rat"
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   130
    ml ("{*op * :: erat \<Rightarrow> erat \<Rightarrow> erat*}/ _/ ({*inverse :: erat \<Rightarrow> erat*}/ _)")
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   131
    haskell ("{*op * :: erat \<Rightarrow> erat \<Rightarrow> erat*}/ _/ ({*inverse :: erat \<Rightarrow> erat*}/ _)")
19137
f92919b141b2 change in codegen syntax
haftmann
parents: 19082
diff changeset
   132
  "op <= :: rat \<Rightarrow> rat \<Rightarrow> bool"
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   133
    ml ("{*op <= :: erat \<Rightarrow> erat \<Rightarrow> bool*}")
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   134
    haskell ("{*op <= :: erat \<Rightarrow> erat \<Rightarrow> bool*}")
19791
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
   135
  "op = :: rat \<Rightarrow> rat \<Rightarrow> bool"
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
   136
    ml ("{*eq_rat*}")
ab326de16ad5 refined code generation
haftmann
parents: 19609
diff changeset
   137
    haskell ("{*eq_rat*}")
19039
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   138
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   139
end
8eae46249628 added theory of executable rational numbers
haftmann
parents:
diff changeset
   140