1 (* Title: ProgLang/Calculate.thy
2 Author: Walther Neuper 000301
3 (c) copyright due to lincense terms.
7 imports "$ISABELLE_ISAC/BaseDefinitions/BaseDefinitions"
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 + - * / ^.
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.
25 (*** evaluate binary associative operations ***)
27 val is_num = can HOLogic.dest_number;
30 member (op =) [\<^const_name>\<open>plus\<close>, \<^const_name>\<open>minus\<close>, \<^const_name>\<open>times\<close>, \<^const_name>\<open>realpow\<close>];
36 Proof_Context.init_global thy
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);
43 fun eval_binop (_: string) (_: string) t ctxt =
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
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
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
58 val res = calcul ctxt (opp $ t1 $ t2);
59 val prop = HOLogic.Trueprop $ HOLogic.mk_eq (t, opp $ res $ v);
61 SOME ("#: " ^ UnparseC.term prop, prop)
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
67 val res = calcul ctxt t;
68 val prop = HOLogic.Trueprop $ (HOLogic.mk_eq (t, res));
70 SOME ("#: " ^ UnparseC.term prop, prop)