src/sml/IsacKnowledge/Float.ML
branchgriesmayer
changeset 318 a0c068d1e54e
child 838 cca6f87a8963
equal deleted inserted replaced
317:25a17e919d55 318:a0c068d1e54e
       
     1 (* use"Float.ML";
       
     2    *)
       
     3 
       
     4 (*WN.28.3.03 fuer Matthias*)
       
     5 (*Floatingpointnumbers, direkte Darstellung der abstrakten Syntax:*)
       
     6  val t = (term_of o the o (parse thy)) "Float ((1,2),(0,0))";
       
     7  atomt t;
       
     8  val t = (term_of o the o (parse thy)) 
       
     9 	     "Float ((1,2),(0,0)) + Float ((3,4),(0,0)) * \
       
    10 	     \Float ((5,6),(0,0)) / Float ((7,8),(0,0))";
       
    11  atomt t;
       
    12 (*die konkrete Syntax wird noch verschoenert*)
       
    13 
       
    14 (*--- 15.4.03
       
    15 fun calc "op +"  (n1, n2) = n1+n2
       
    16   | calc "op -"  (n1, n2) = n1-n2
       
    17   | calc "op *"  (n1, n2) = n1*n2
       
    18   | calc "HOL.divide"(n1, n2) = n1 div n2
       
    19   | calc "Atools.pow"(n1, n2) = power n1 n2
       
    20   | calc op_ _ = raise error ("calc: operator = "^op_^" not defined");
       
    21 ---*)
       
    22 (*.used for calculating built in binary operations in Isabelle2002.
       
    23    integer numerals n are ((n,0),(0,0)) i.e. precision is (0,0)*)
       
    24 fun calc "op +" ((a, b), _:int * int) ((c, d), _:int * int)  = 
       
    25     if b < d 
       
    26     then ((a + c * power 10 (d - b), b), (0, 0))(*FIXXXME precision*)
       
    27     else ((a * power 10 (b - d) + c, d), (0, 0))(*FIXXXME precision*)
       
    28   | calc "op -" ((a, 0), _) ((c, 0), _) =       (*FIXXXME float + prec.*)
       
    29     ((a - c,0),(0,0))
       
    30   | calc "op *" ((a, b), _) ((c, d), _) =       (*FIXXXME precision*)
       
    31     ((a * c, b + d), (0, 0))
       
    32   | calc "HOL.divide" ((a, 0), _) ((c, 0), _) = (*FIXXXME float + prec.*)
       
    33     ((a div c, 0), (0, 0))
       
    34   | calc "Atools.pow" ((a, b), _) ((c, d), _) = (*FIXXXME float + prec.*)
       
    35     ((power a c, 0), (0, 0))
       
    36   | calc op_ ((a, b), (p11, p12)) ((c, d), (p21, p22)) = 
       
    37     raise error ("calc: not impl. for Float (("^
       
    38 		 (string_of_int a  )^","^(string_of_int b  )^"), ("^
       
    39 		 (string_of_int p11)^","^(string_of_int p12)^")) "^op_^" (("^
       
    40 		 (string_of_int c  )^","^(string_of_int d  )^"), ("^
       
    41 		 (string_of_int p21)^","^(string_of_int p22)^"))");
       
    42 (*> calc "op +" ((~1,0),(0,0)) ((2,0),(0,0)); 
       
    43 val it = ((1,0),(0,0))*)
       
    44 
       
    45 
       
    46 (*.convert int and float to internal floatingpoint prepresentation.*)
       
    47 fun numeral (Free (str, T)) = 
       
    48     (case int_of_str str of
       
    49 	 Some i => Some ((i, 0), (0, 0))
       
    50        | None => None)
       
    51   | numeral (Const ("Float.Float", _) $
       
    52 		   (Const ("Pair", _) $
       
    53 			  (Const ("Pair", T) $ Free (v1, _) $ Free (v2,_)) $
       
    54 			  (Const ("Pair", _) $ Free (p1, _) $ Free (p2,_))))=
       
    55     (case (int_of_str v1, int_of_str v2, int_of_str p1, int_of_str p2) of
       
    56 	(Some v1', Some v2', Some p1', Some p2') =>
       
    57 	Some ((v1', v2'), (p1', p2'))
       
    58       | _ => None)
       
    59   | numeral _ = None;
       
    60 
       
    61 (*.convert internal floatingpoint prepresentation to int and float.*)
       
    62 fun term_of_float T ((val1,    0), (         0,          0)) =
       
    63     term_of_num T val1
       
    64   | term_of_float T ((val1, val2), (precision1, precision2)) =
       
    65     let val pT = pairT T T
       
    66     in Const ("Float.Float", (pairT pT pT) --> T)
       
    67 	     $ (pairt (pairt (Free (str_of_int val1, T))
       
    68 			     (Free (str_of_int val2, T)))
       
    69 		      (pairt (Free (str_of_int precision1, T))
       
    70 			     (Free (str_of_int precision2, T))))
       
    71     end;
       
    72 (*> val t = str2term "Float ((1,2),(0,0))";
       
    73 > val Const ("Float.Float", fT) $ _ = t;
       
    74 > atomtyp fT;
       
    75 > val ffT = (pairT (pairT HOLogic.realT HOLogic.realT) 
       
    76 > 	     (pairT HOLogic.realT HOLogic.realT)) --> HOLogic.realT;
       
    77 > atomtyp ffT;
       
    78 > fT = ffT;
       
    79 val it = true : bool
       
    80 
       
    81 t = float_term_of_num HOLogic.realT ((1,2),(0,0));
       
    82 val it = true : bool*)
       
    83 
       
    84 (*.assoc. convert internal floatingpoint prepresentation to int and float.*)
       
    85 fun var_op_float v op_ optype ntyp ((v1, 0), (0, 0)) =
       
    86     var_op_num v op_ optype ntyp v1
       
    87   | var_op_float v op_ optype T ((v1, v2), (p1, p2)) =
       
    88     let val pT = pairT T T
       
    89     in Const (op_, optype) $ v $ 
       
    90 	     (Const ("Float.Float", (pairT pT pT) --> T)
       
    91 		    $ (pairt (pairt (Free (str_of_int v1, T))
       
    92 				    (Free (str_of_int v2, T)))
       
    93 			     (pairt (Free (str_of_int p1, T))
       
    94 				    (Free (str_of_int p2, T)))))
       
    95     end;
       
    96 (*> val t = str2term "a + b";
       
    97 > val Const ("op +", optype) $ _ $ _ = t;
       
    98 > val t = str2term "v + Float ((11,-1),(0,0))";val v = str2term "v";
       
    99 > t = var_op_float v "op +" optype HOLogic.realT ((11,~1),(0,0));
       
   100 val it = true : bool*)
       
   101 
       
   102 (*.assoc. convert internal floatingpoint prepresentation to int and float.*)
       
   103 fun float_op_var v op_ optype ntyp ((v1, 0), (0, 0)) =
       
   104     num_op_var v op_ optype ntyp v1
       
   105   | float_op_var v op_ optype T ((v1, v2), (p1, p2)) =
       
   106     let val pT = pairT T T
       
   107     in Const (op_,optype) $ 
       
   108 	     (Const ("Float.Float", (pairT pT pT) --> T)
       
   109 		    $ (pairt (pairt (Free (str_of_int v1, T))
       
   110 				    (Free (str_of_int v2, T)))
       
   111 			     (pairt (Free (str_of_int p1, T))
       
   112 				    (Free (str_of_int p2, T))))) $ v
       
   113     end;
       
   114 (*> val t = str2term "a + b";
       
   115 > val Const ("op +", optype) $ _ $ _ = t;
       
   116 > val t = str2term "Float ((11,-1),(0,0)) + v";val v = str2term "v";
       
   117 > t = float_op_var v "op +" optype HOLogic.realT ((11,~1),(0,0));
       
   118 val it = true : bool*)
       
   119 
       
   120 
       
   121 
       
   122 
       
   123 
       
   124