test/Tools/isac/ProgLang/calculate-float.sml
author Walther Neuper <neuper@ist.tugraz.at>
Thu, 23 Sep 2010 14:49:23 +0200
branchisac-update-Isa09-2
changeset 38014 3e11e3c2dc42
parent 37960 ec20007095f2
permissions -rw-r--r--
updated "op +", "op -", "op *". "HOL.divide" in src & test

find . -type f -exec sed -i s/"\"op +\""/"\"Groups.plus_class.plus\""/g {} \;
find . -type f -exec sed -i s/"\"op -\""/"\"Groups.minus_class.minus\""/g {} \;
find . -type f -exec sed -i s/"\"op *\""/"\"Groups.times_class.times\""/g {} \;
find . -type f -exec sed -i s/"\"HOL.divide\""/"\"Rings.inverse_class.divide\""/g {} \;
neuper@37906
     1
(* (c) Stefan Rath 2005
neuper@37906
     2
   tests for sml/Scripts/calculate.sml
neuper@37906
     3
neuper@37906
     4
   use"~/proto2/isac/src/smltest/Scripts/calculate-float.sml";
neuper@37906
     5
   use"calculate-float.sml";
neuper@37906
     6
   *)
neuper@37906
     7
neuper@37906
     8
neuper@37906
     9
(*WN.28.3.03 fuer Matthias*)
neuper@37906
    10
(*Floatingpointnumbers, direkte Darstellung der abstrakten Syntax:*)
neuper@37906
    11
 val thy = Float.thy;
neuper@37906
    12
 val t = str2term "Float ((1,2),(0,0))";
neuper@37906
    13
 atomt t;
neuper@37906
    14
 val t = (term_of o the o (parse thy)) 
neuper@37906
    15
	     "Float ((1,2),(0,0)) + Float ((3,4),(0,0)) * \
neuper@37906
    16
	     \Float ((5,6),(0,0)) / Float ((7,8),(0,0))";
neuper@37906
    17
 atomt t;
neuper@37906
    18
(*die konkrete Syntax wird noch verschoenert*)
neuper@37906
    19
neuper@37906
    20
 val thy = "Test.thy";
neuper@37906
    21
 val op_ = "divide_";
neuper@37906
    22
 val ct = "-6 / 3";
neuper@37926
    23
 val SOME (ct,_) = calculate thy (the (assoc (calclist, op_))) ct;
neuper@37906
    24
neuper@37906
    25
(*-----WN050315------------------------------------------------------*)
neuper@37906
    26
(*..*)
neuper@37906
    27
val t = str2term "Float ((1,2),(0,0))";
neuper@37906
    28
atomty t;
neuper@37906
    29
val Const ("Float.Float",_) $
neuper@37906
    30
	  (Const ("Pair",_) $ 
neuper@37906
    31
		 (Const ("Pair",_) $ Free (i1,_) $ Free (i2,_)) $ _) = t;
neuper@37906
    32
    
neuper@37906
    33
val t = str2term "Float ((1,2),(0,0)) + Float ((3,4),(0,0))";
neuper@37906
    34
atomty t;
neuper@37906
    35
(*-----WN050315------------------------------------------------------*)
neuper@37906
    36
neuper@37906
    37
neuper@37906
    38
val thy = Float.thy;
neuper@37906
    39
neuper@37906
    40
(*.calculate the value of a pair of floats.*)
neuper@38014
    41
val ((a,b),(c,d)) = calc "Groups.plus_class.plus" ((~1,0),(0,0)) ((2,0),(0,0));
neuper@37906
    42
neuper@37906
    43
neuper@37906
    44
(*.build the term.*)
neuper@37906
    45
term_of_float HOLogic.realT ((~1,0),(0,0));
neuper@37906
    46
term_of_float HOLogic.realT ((~1,11),(0,0));
neuper@37906
    47
neuper@37906
    48
(*--18.3.05-------------------------*)
neuper@37906
    49
val t = Free ("sdfsdfsdf", HOLogic.realT);
neuper@37906
    50
val t = Free ("-123,456", HOLogic.realT);
neuper@37906
    51
val t = Free ("0,123456", HOLogic.realT);
neuper@37906
    52
term2str t;
neuper@37906
    53
(*----(1)------------------------------*)
neuper@37906
    54
val t = str2term "IFloat (1, 2, 3)";
neuper@37906
    55
val t = str2term "CFloat (1, 2)";
neuper@37906
    56
atomt t;
neuper@37906
    57
atomty t;
neuper@37926
    58
val SOME ct = parse Float.thy "IFloat (1, 2, 3)";
neuper@37926
    59
val SOME ct = parse Float.thy "CFloat (1, 2)";
neuper@37906
    60
atomt (term_of ct);
neuper@37906
    61
atomty (term_of ct);
neuper@37906
    62
(*----(2)------------------------------*)
neuper@37926
    63
val SOME ct = parse Float.thy "IFloat (-1, 2, 3)";
neuper@37906
    64
val t = (term_of ct);
neuper@37906
    65
atomty t;
neuper@37906
    66
(*#######################################################################3
neuper@37906
    67
val Const
neuper@37906
    68
         ("Float.IFloat", _) $
neuper@37906
    69
	 (Const
neuper@37906
    70
              ("Pair", _) $
neuper@37906
    71
              Free (no, _) $
neuper@37906
    72
              (Const
neuper@37906
    73
                   ("Pair", _) $
neuper@37906
    74
		   Free (commas, _) $ Free (exp, _))) = t;
neuper@37906
    75
neuper@37906
    76
fun IFloat2CFloat 
neuper@37906
    77
	(Const
neuper@37906
    78
        ("Float.IFloat", _) $
neuper@37906
    79
	(Const
neuper@37906
    80
             ("Pair", _) $
neuper@37906
    81
             Free (no, _) $
neuper@37906
    82
             (Const
neuper@37906
    83
                  ("Pair", _) $
neuper@37906
    84
		  Free (commas, _) $ Free (exp, _)))) =
neuper@37906
    85
	Const ("Float.CFloat", HOLogic.realT(*wrong type*)) $
neuper@37906
    86
	      (Const
neuper@37906
    87
		   ("Pair", HOLogic.realT(*wrong type*)) $
neuper@37906
    88
		   Free (no^"."^commas, HOLogic.realT) 
neuper@37906
    89
		   $ Free ("accuracy", HOLogic.realT))
neuper@37906
    90
neuper@37906
    91
  | IFloat2CFloat t =
neuper@37906
    92
    raise error ("IFloat2CFloat: invalid argument "^term2str t);
neuper@37906
    93
neuper@37906
    94
val cf = IFloat2CFloat t;
neuper@37906
    95
term2str cf;
neuper@37906
    96
neuper@37906
    97
(*in IsacKnowledge/Float.ML: fun pairt -> Pair-term*)
neuper@37906
    98
neuper@37906
    99
fun CFloat2Free (Const ("Float.CFloat", _) $
neuper@37906
   100
		       (Const ("Pair", _) $ Free (no_commas, _) $ _)) =
neuper@37906
   101
    Free (no_commas, HOLogic.realT);
neuper@37906
   102
neuper@37906
   103
val t' = CFloat2Free cf;
neuper@37906
   104
term2str t';
neuper@37906
   105
neuper@37906
   106
fun CFloat2sml (Const ("Float.CFloat", _) $
neuper@37906
   107
		      (Const ("Pair", _) $ Free (float, _) $ _(**))) =
neuper@37906
   108
    float;
neuper@37906
   109
CFloat2sml cf;
neuper@37906
   110
neuper@37906
   111
(*--18.3.05-------------------------*)
neuper@37906
   112
neuper@37906
   113
neuper@37906
   114
(*.the function evaluating a binary operator.*)
neuper@37906
   115
val t = str2term "Float ((1,2),(0,0)) + Float ((3,4),(0,0))";
neuper@38014
   116
val SOME (thmid, t) = eval_binop "#add_" "Groups.plus_class.plus" t thy;
neuper@37906
   117
term2str t;
neuper@37906
   118
neuper@37906
   119
neuper@37906
   120
(*.scan a term for a pair of floats.*)
neuper@37926
   121
 val SOME (thmid,t') = get_pair thy op_ eval_fn t;
neuper@37906
   122
neuper@37906
   123
neuper@37906
   124
(*.use 'calculate' explicitly.*)
neuper@37906
   125
 val thy = Test.thy;
neuper@37906
   126
 val op_ = "divide_";
neuper@37906
   127
 val t = str2term "sqrt (x ^^^ 2 + -3 * x) =\
neuper@37906
   128
		  \Float ((5,6),(0,0)) / Float ((7,8),(0,0))";
neuper@37926
   129
 val SOME (t',_) = calculate_ thy (the (assoc (calclist, op_))) t;
neuper@37906
   130
 term2str t';
neuper@37906
   131
neuper@37906
   132
neuper@37906
   133
(*.rewrite with ruleset TEST...simplify (calling calculate internally.*)
neuper@37906
   134
val t = str2term "a + Float ((1,2),(0,0)) + a + Float ((3,4),(0,0)) * \
neuper@37906
   135
		 \Float ((5,6),(0,0)) / Float ((7,8),(0,0))";
neuper@37926
   136
val SOME (t',_) = rewrite_set_ thy false norm_Rational(*///*) t; term2str t';
neuper@37906
   137
(*Float ((...,...) + 2*a*)
neuper@37906
   138
neuper@37906
   139
neuper@37906
   140
(*. parse a float as seen by the user  .*)
neuper@37926
   141
val SOME t = parse Float.thy "123.456"; 
neuper@37926
   142
val SOME t = parse Float.thy "-123.456"; 
neuper@37926
   143
val SOME t = parse Float.thy "123.456 E6789"; 
neuper@37926
   144
val SOME t = parse Float.thy "123.456 E-6789"; 
neuper@37926
   145
val SOME t = parse Float.thy "-123.456 E-6789"; 
neuper@37906
   146
neuper@37906
   147
################################################################*)