src/Tools/isac/ProgLang/Calculate.thy
author wneuper <Walther.Neuper@jku.at>
Thu, 04 Aug 2022 12:48:37 +0200
changeset 60509 2e0b7ca391dc
parent 60504 8cc1415b3530
child 60515 03e19793d81e
permissions -rw-r--r--
polish naming in Rewrite_Order
     1 (* Title:  ProgLang/Calculate.thy
     2    Author: Walther Neuper 000301
     3    (c) copyright due to lincense terms.
     4 *)
     5 
     6 theory Calculate
     7   imports "$ISABELLE_ISAC/BaseDefinitions/BaseDefinitions"
     8 begin
     9 
    10 text \<open>Abstract:
    11   The Isac project required floating point numbers and complex numbers in early 2000,
    12   before these numbers were available in Isabelle.
    13   Since that time numeral calculations are done by ML functions, all named eval_ 
    14   and integrated into rewriting. eval_binop below is not bound to a specific constant
    15   (like in Prog_Expr.thy), rather calculates results of Isabelle's binary operations + - * / ^.
    16 
    17   A specialty are the ad-hoc theorems for numeral calculations created in order to provide
    18   the rewrite engine with theorems as done in general.
    19 \<close>
    20 
    21 ML_file evaluate.sml
    22 
    23 ML \<open>
    24 \<close> ML \<open>
    25 (*** evaluate binary associative operations ***)
    26 
    27 val is_num = can HOLogic.dest_number;
    28 
    29 val is_calcul_op =
    30   member (op =) [\<^const_name>\<open>plus\<close>, \<^const_name>\<open>minus\<close>, \<^const_name>\<open>times\<close>, \<^const_name>\<open>realpow\<close>];
    31 
    32 fun calcul ctxt lhs =
    33   let
    34     val simp_ctxt =
    35 (*
    36       Proof_Context.init_global thy
    37 *)
    38       ctxt |> put_simpset (Simplifier.simpset_of @{theory_context BaseDefinitions});
    39     val eq = Simplifier.rewrite simp_ctxt (Thm.cterm_of ctxt lhs);
    40     val rhs = Thm.term_of (Thm.rhs_of eq);
    41   in rhs end;
    42 
    43 fun eval_binop (_: string) (_: string) t ctxt =
    44   (case t of
    45     (opp as Const (c1, T)) $ (Const (c2, _) $ v $ t1) $ t2 =>         (* binary \<circ> (v \<circ> n1) \<circ> n2 *)
    46       if c1 = c2 andalso is_calcul_op c1 andalso is_num t1 andalso is_num t2 then
    47         let
    48           val opp' = Const (if c1 = \<^const_name>\<open>minus\<close> then \<^const_name>\<open>plus\<close> else c1, T);
    49           val res = calcul ctxt (opp' $ t1 $ t2);
    50           val prop = HOLogic.Trueprop $ (HOLogic.mk_eq (t, opp $ v $ res));
    51         in SOME ("#: " ^ UnparseC.term prop, prop) end
    52       else NONE
    53   | (opp as Const (c1, _)) $ t1 $ (Const (c2, _) $ t2 $ v) =>         (* binary \<circ> n1 \<circ> (n2 \<circ> v) *)
    54       if c1 = c2 andalso is_calcul_op c1 andalso c1 <> \<^const_name>\<open>minus\<close>
    55         andalso is_num t1 andalso is_num t2
    56       then
    57         let
    58           val res = calcul ctxt (opp $ t1 $ t2);
    59           val prop = HOLogic.Trueprop $ HOLogic.mk_eq (t, opp $ res $ v);
    60         in
    61           SOME ("#: " ^ UnparseC.term prop, prop)
    62         end
    63       else NONE
    64   | Const (c, _) $ t1 $ t2 =>                                               (* binary \<circ> n1 \<circ> n2 *)
    65       if is_calcul_op c andalso is_num t1 andalso is_num t2 then
    66         let
    67           val res = calcul ctxt t;
    68           val prop = HOLogic.Trueprop $ (HOLogic.mk_eq (t, res));
    69         in
    70           SOME ("#: " ^ UnparseC.term prop, prop)
    71         end
    72       else NONE
    73   | _ => NONE);
    74 
    75 \<close> ML \<open>
    76 \<close> ML \<open>
    77 \<close>
    78 
    79 end