\\replace is_const with is_num: STRANGE ERROR assoc_calc: 'Prog_Expr.is_num' not found in theory Poly
1 (* title: ProgLang/Prog_Expr.thy
2 functions for expressions (which do NOT contain Prog_Tac and Tactical, by definition))
3 author: Walther Neuper, Aug.2019
4 (c) copyright due to lincense terms.
8 imports Calculate ListC Program
12 Specific constants are defined for pre-conditions of programs and for program-expressions.
13 The constants are connected with ML functions for evaluation of respective expressions
14 (all named eval_*), such that Isac's rewrite engine can handle it.
17 subsection \<open>Power re-defined for a specific type\<close>
18 abbreviation real_powr :: "real \<Rightarrow> real \<Rightarrow> real" (infixr "\<up>" 80)
19 where "x \<up> a \<equiv> x powr a"
21 subsection \<open>consts of functions in pre-conditions and program-expressions\<close>
23 lhs :: "bool => real" (*of an equality*)
24 rhs :: "bool => real" (*of an equality*)
25 Vars :: "'a => real list" (*get the variables of a term *)
26 matches :: "['a, 'a] => bool"
27 matchsub :: "['a, 'a] => bool"
28 some_occur_in :: "[real list, 'a] => bool" ("some'_of _ occur'_in _")
29 occurs_in :: "[real , 'a] => bool" ("_ occurs'_in _")
31 abs :: "real => real" ("(|| _ ||)")
32 (* ~~~ FIXXXME Isabelle2002 has abs already !!!*)
33 absset :: "real set => real" ("(||| _ |||)")
34 is_atom :: "real => bool" ("_ is'_atom" 10)
35 is_num :: "real => bool" ("_ is'_num" 10)
36 is_even :: "real => bool" ("_ is'_even" 10)
38 (* identity on term level*)
39 ident :: "['a, 'a] => bool" ("(_ =!=/ _)" [51, 51] 50)
40 argument_in :: "real => real" ("argument'_in _" 10)
41 sameFunId :: "[real, bool] => bool" (* "same_funid _ _" 10
42 WN0609 changed the id, because ".. _ _" inhibits currying *)
43 filter_sameFunId:: "[real, bool list] => bool list" ("filter'_sameFunId _ _" 10)
44 boollist2sum :: "bool list => real"
45 lastI :: "'a list \<Rightarrow> 'a"
47 subsection \<open>ML code for functions in pre-conditions and program-expressions\<close>
51 (*//------------------------- from Tools .thy-------------------------------------------------\\*)
53 val eval_lhs: 'a -> string -> term -> 'b -> (string * term) option
54 val eval_matches: string -> string -> term -> theory -> (string * term) option
55 val matchsub: theory -> term -> term -> bool
56 val eval_matchsub: string -> string -> term -> theory -> (string * term) option
58 val eval_rhs: 'a -> string -> term -> 'b -> (string * term) option
59 val eval_var: string -> string -> term -> theory -> (string * term) option
61 val or2list: term -> term
62 (*\\------------------------- from Tools .thy-------------------------------------------------//*)
63 (*//------------------------- from Atools .thy------------------------------------------------\\*)
64 val occurs_in: term -> term -> bool
65 val eval_occurs_in: 'a -> string -> term -> 'b -> (string * term) option
66 val some_occur_in: term list -> term -> bool
67 val eval_some_occur_in: 'a -> string -> term -> 'b -> (string * term) option
68 val eval_is_atom: string -> string -> term -> 'a -> (string * term) option
69 val eval_is_num: string -> string -> term -> 'a -> (string * term) option
71 val eval_is_even: string -> string -> term -> 'a -> (string * term) option
72 val eval_const: string -> string -> term -> 'a -> (string * term) option
73 val eval_equ: string -> string -> term -> 'a -> (string * term) option
74 val eval_ident: string -> string -> term -> theory -> (string * term) option
75 val eval_equal: string -> string -> term -> theory -> (string * term) option
76 val eval_cancel: string -> string -> term -> 'a -> (string * term) option
77 val eval_argument_in: string -> string -> term -> 'a -> (string * term) option
78 val same_funid: term -> term -> bool
79 val eval_sameFunId: string -> string -> term -> 'a -> (string * term) option
80 val eval_filter_sameFunId: string -> string -> term -> 'a -> (string * term) option
81 val list2sum: term list -> term
82 val eval_boollist2sum: string -> string -> term -> 'a -> (string * term) option
84 val mk_thmid_f: string -> (int * int) * (int * int) -> (int * int) * (int * int) -> string
88 structure Prog_Expr(**): PROG_EXPR =(**)
92 (*+ for Or_to_List +*)
93 fun or2list (Const (\<^const_name>\<open>True\<close>,_)) = ((*tracing"### or2list True";*) UniversalList)
94 | or2list (Const (\<^const_name>\<open>False\<close>,_)) = ((*tracing"### or2list False";*) EmptyList)
95 | or2list (t as Const (\<^const_name>\<open>HOL.eq\<close>,_) $ _ $ _) = TermC.list2isalist HOLogic.boolT [t]
98 fun get ls (Const (\<^const_name>\<open>disj\<close>,_) $ o1 $ o2) =
100 Const (\<^const_name>\<open>disj\<close>,_) $ _ $ _ => get (ls @ [o1]) o2
101 | _ => ls @ [o1, o2])
102 | get _ t = raise TERM ("or2list: missing pattern in fun.def", [t])
103 in (((TermC.list2isalist HOLogic.boolT) o (get [])) ors) end
104 (*>val t = @{term True};
105 > val t' = or2list t;
107 "ListC.UniversalList"
108 > val t = @{term False};
109 > val t' = or2list t;
112 > val t=(Thm.term_of o the o (parse thy)) "x=3";
113 > val t' = or2list t;
116 > val t=(Thm.term_of o the o (parse thy))"(x=3) | (x=-3) | (x=0)";
117 > val t' = or2list t;
119 "[x = #3, x = #-3, x = #0]" : string *)
122 (** evaluation on the meta-level **)
124 (*. evaluate the predicate matches (match on whole term only) .*)
125 (*("matches",("Prog_Expr.matches", eval_matches "#matches_")):calc*)
126 fun eval_matches (_:string) "Prog_Expr.matches" (t as Const (\<^const_name>\<open>Prog_Expr.matches\<close>, _) $ pat $ tst) thy =
127 if TermC.matches thy tst pat
130 val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True}))
131 in SOME (UnparseC.term_in_thy thy prop, prop) end
134 val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False}))
135 in SOME (UnparseC.term_in_thy thy prop, prop) end
136 | eval_matches _ _ _ _ = NONE;
138 > val t = (Thm.term_of o the o (parse thy))
139 "matches (?x = 0) (1 * x \<up> 2 = 0)";
140 > eval_matches "/thmid/" "/op_/" t thy;
143 ("matches (x = 0) (1 * x \<up> 2 = 0) = False",
144 Const (#,#) $ (# $ # $ Const #)) : (string * term) option
146 > val t = (Thm.term_of o the o (parse thy))
147 "matches (?a = #0) (#1 * x \<up> #2 = #0)";
148 > eval_matches "/thmid/" "/op_/" t thy;
151 ("matches (?a = #0) (#1 * x \<up> #2 = #0) = True",
152 Const (#,#) $ (# $ # $ Const #)) : (string * term) option
154 > val t = (Thm.term_of o the o (parse thy))
155 "matches (?a * x = #0) (#1 * x \<up> #2 = #0)";
156 > eval_matches "/thmid/" "/op_/" t thy;
159 ("matches (?a * x = #0) (#1 * x \<up> #2 = #0) = False",
160 Const (#,#) $ (# $ # $ Const #)) : (string * term) option
162 > val t = (Thm.term_of o the o (parse thy))
163 "matches (?a * x \<up> #2 = #0) (#1 * x \<up> #2 = #0)";
164 > eval_matches "/thmid/" "/op_/" t thy;
167 ("matches (?a * x \<up> #2 = #0) (#1 * x \<up> #2 = #0) = True",
168 Const (#,#) $ (# $ # $ Const #)) : (string * term) option
169 ----- before ?patterns ---:
170 > val t = (Thm.term_of o the o (parse thy))
171 "matches (a * b \<up> #2 = c) (#3 * x \<up> #2 = #1)";
172 > eval_matches "/thmid/" "/op_/" t thy;
174 ("matches (a * b \<up> #2 = c) (#3 * x \<up> #2 = #1) = True",
175 Const ("HOL.Trueprop", "bool => prop") $ (Const # $ (# $ #) $ Const (#,#)))
176 : (string * term) option
178 > val t = (Thm.term_of o the o (parse thy))
179 "matches (a * b \<up> #2 = c) (#3 * x \<up> #2222 = #1)";
180 > eval_matches "/thmid/" "/op_/" t thy;
181 SOME ("matches (a * b \<up> #2 = c) (#3 * x \<up> #2222 = #1) = False",
182 Const ("HOL.Trueprop", "bool => prop") $ (Const # $ (# $ #) $ Const (#,#)))
184 > val t = (Thm.term_of o the o (parse thy))
185 "matches (a = b) (x + #1 + #-1 * #2 = #0)";
186 > eval_matches "/thmid/" "/op_/" t thy;
187 SOME ("matches (a = b) (x + #1 + #-1 * #2 = #0) = True",Const # $ (# $ #))
190 (*.does a pattern match some subterm ?.*)
191 fun matchsub thy t pat =
193 fun matchs (t as Const _) = TermC.matches thy t pat
194 | matchs (t as Free _) = TermC.matches thy t pat
195 | matchs (t as Var _) = TermC.matches thy t pat
196 | matchs (Bound _) = false
197 | matchs (t as Abs (_, _, body)) =
198 if TermC.matches thy t pat then true else TermC.matches thy body pat
199 | matchs (t as f1 $ f2) =
200 if TermC.matches thy t pat then true
201 else if matchs f1 then true else matchs f2
204 (*("matchsub",("Prog_Expr.matchsub", eval_matchsub "#matchsub_")):calc*)
205 fun eval_matchsub (_:string) "Prog_Expr.matchsub"
206 (t as Const (\<^const_name>\<open>Prog_Expr.matchsub\<close>, _) $ pat $ tst) thy =
207 if matchsub thy tst pat
209 let val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True}))
210 in SOME (UnparseC.term_in_thy thy prop, prop) end
212 let val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False}))
213 in SOME (UnparseC.term_in_thy thy prop, prop) end
214 | eval_matchsub _ _ _ _ = NONE;
216 (*get the variables in an isabelle-term*)
217 (*("Vars" ,("Prog_Expr.Vars" , eval_var "#Vars_")):calc*)
218 fun eval_var (thmid:string) "Prog_Expr.Vars" (t as (Const _ $ arg)) thy =
220 val t' = ((TermC.list2isalist HOLogic.realT) o TermC.vars) t;
221 val thmId = thmid ^ UnparseC.term_in_thy thy arg;
222 in SOME (thmId, HOLogic.Trueprop $ (TermC.mk_equality (t, t'))) end
223 | eval_var _ _ _ _ = NONE;
225 (* refer to Thm.lhs_of *)
226 fun lhs (Const (\<^const_name>\<open>HOL.eq\<close>,_) $ l $ _) = l
227 | lhs t = raise ERROR("lhs called with (" ^ UnparseC.term t ^ ")");
228 (*("lhs" ,("Prog_Expr.lhs" , eval_lhs "")):calc*)
229 fun eval_lhs _ "Prog_Expr.lhs" (t as (Const (\<^const_name>\<open>lhs\<close>,_) $ (Const (\<^const_name>\<open>HOL.eq\<close>,_) $ l $ _))) _ =
230 SOME ((UnparseC.term t) ^ " = " ^ (UnparseC.term l),
231 HOLogic.Trueprop $ (TermC.mk_equality (t, l)))
232 | eval_lhs _ _ _ _ = NONE;
234 > val t = (Thm.term_of o the o (parse thy)) "lhs (1 * x \<up> 2 = 0)";
235 > val SOME (id,t') = eval_lhs 0 0 t 0;
236 val id = "Prog_Expr.lhs (1 * x \<up> 2 = 0) = 1 * x \<up> 2" : string
238 val it = "Prog_Expr.lhs (1 * x \<up> 2 = 0) = 1 * x \<up> 2" : string
241 fun rhs (Const (\<^const_name>\<open>HOL.eq\<close>,_) $ _ $ r) = r
242 | rhs t = raise ERROR("rhs called with (" ^ UnparseC.term t ^ ")");
243 (*("rhs" ,("Prog_Expr.rhs" , eval_rhs "")):calc*)
244 fun eval_rhs _ "Prog_Expr.rhs" (t as (Const (\<^const_name>\<open>rhs\<close>,_) $ (Const (\<^const_name>\<open>HOL.eq\<close>,_) $ _ $ r))) _ =
245 SOME (UnparseC.term t ^ " = " ^ UnparseC.term r,
246 HOLogic.Trueprop $ (TermC.mk_equality (t, r)))
247 | eval_rhs _ _ _ _ = NONE;
248 (*\\------------------------- from Prog_Expr.thy-------------------------------------------------//*)
250 (*//------------------------- from Atools.thy------------------------------------------------\\*)
251 fun occurs_in v t = member op = (((map strip_thy) o TermC.ids2str) t) (Term.term_name v);
253 (*("occurs_in", ("Prog_Expr.occurs_in", Prog_Expr.eval_occurs_in ""))*)
254 fun eval_occurs_in _ "Prog_Expr.occurs_in" (p as (Const (\<^const_name>\<open>occurs_in\<close>, _) $ v $ t)) _ =
255 ((*tracing("#>@ eval_occurs_in: v= "^(UnparseC.term v));
256 tracing("#>@ eval_occurs_in: t= "^(UnparseC.term t));*)
258 then SOME ((UnparseC.term p) ^ " = True",
259 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
260 else SOME ((UnparseC.term p) ^ " = False",
261 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False}))))
262 | eval_occurs_in _ _ _ _ = NONE;
264 (*some of the (bound) variables (eg. in an eqsys) "vs" occur in term "t"*)
265 fun some_occur_in vs t =
266 let fun occurs_in' a b = occurs_in b a
267 in foldl or_ (false, map (occurs_in' t) vs) end;
269 (*("some_occur_in", ("Prog_Expr.some_occur_in",
270 eval_some_occur_in "#eval_some_occur_in_"))*)
271 fun eval_some_occur_in _ "Prog_Expr.some_occur_in"
272 (p as (Const (\<^const_name>\<open>some_occur_in\<close>,_) $ vs $ t)) _ =
273 if some_occur_in (TermC.isalist2list vs) t
274 then SOME ((UnparseC.term p) ^ " = True",
275 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
276 else SOME ((UnparseC.term p) ^ " = False",
277 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
278 | eval_some_occur_in _ _ _ _ = NONE;
280 (*("is_atom",("Prog_Expr.is_atom", Prog_Expr.eval_is_atom "#is_atom_"))*)
281 fun eval_is_atom (thmid:string) "Prog_Expr.is_atom" (t as (Const _ $ arg)) _ =
283 then SOME (TermC.mk_thmid thmid (TermC.string_of_atom arg) "",
284 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
285 else SOME (TermC.mk_thmid thmid (TermC.string_of_atom arg) "",
286 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
287 | eval_is_atom _ _ _ _ = NONE;
289 (*("is_num",("Prog_Expr.is_num", Prog_Expr.eval_is_num "#is_num_"))*)
290 fun eval_is_num (thmid:string) "Prog_Expr.is_num" (t as (Const _ $ arg)) _ =
292 then SOME (TermC.mk_thmid thmid (TermC.string_of_atom arg) "",
293 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
294 else SOME (TermC.mk_thmid thmid (TermC.string_of_atom arg) "",
295 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
296 | eval_is_num _ _ _ _ = NONE;
298 fun even i = (i div 2) * 2 = i;
299 (*("is_even",("Prog_Expr.is_even", eval_is_even "#is_even_"))*)
300 fun eval_is_even (thmid:string) "Prog_Expr.is_even" (t as (Const _ $ arg)) _ =
304 val i = arg |> HOLogic.dest_number |> snd
307 then SOME (TermC.mk_thmid thmid (string_of_int i) "",
308 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
309 else SOME (TermC.mk_thmid thmid "" "",
310 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
313 | eval_is_even _ _ _ _ = NONE;
315 (*("is_num",("Prog_Expr.is_num", Prog_Expr.eval_is_num "#is_num_"))*)
316 fun eval_const thmid _ (t as (Const (\<^const_name>\<open>is_num\<close>, _) $ Const ("Partial_Fractions.AA", _))) _ =
317 (*TODO get rid of this special case*)
318 SOME (TermC.mk_thmid thmid (UnparseC.term t) "",
319 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
320 | eval_const thmid _ (t as (Const (\<^const_name>\<open>is_num\<close>, _) $ Const _)) _ =
321 SOME (TermC.mk_thmid thmid (UnparseC.term t) "",
322 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
323 | eval_const thmid _ (t as (Const (\<^const_name>\<open>is_num\<close>, _) $ n)) _ =
325 then SOME (TermC.mk_thmid thmid (TermC.string_of_num n) "",
326 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
327 else SOME (TermC.mk_thmid thmid (UnparseC.term n) "",
328 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
329 | eval_const _ _ _ _ = NONE;
331 (*. evaluate binary, associative, commutative operators: *,+,^ .*)
332 (*("PLUS" ,(\<^const_name>\<open>plus\<close> , (**)eval_binop "#add_")),
333 ("TIMES" ,(\<^const_name>\<open>times\<close> , (**)eval_binop "#mult_")),
334 ("POWER" ,(\<^const_name>\<open>powr\<close> , (**)eval_binop "#power_"))*)
336 (* val (thmid,op_,t as(Const (op0,t0) $ Free (n1,t1) $ Free(n2,t2)),thy) =
337 ("xxxxxx",op_,t,thy);
339 fun mk_thmid_f thmid ((v11, v12), (p11, p12)) ((v21, v22), (p21, p22)) = (* dropped *)
341 (string_of_int v11)^", "^(string_of_int v12)^"), ("^
342 (string_of_int p11)^", "^(string_of_int p12)^")) __ (("^
343 (string_of_int v21)^", "^(string_of_int v22)^"), ("^
344 (string_of_int p21)^", "^(string_of_int p22)^"))";
346 (*.evaluate < and <= for numerals.*)
347 (*("le" ,(\<^const_name>\<open>less\<close> , Prog_Expr.eval_equ "#less_")),
348 ("leq" ,(\<^const_name>\<open>less_eq\<close> , Prog_Expr.eval_equ "#less_equal_"))*)
350 fun eval_equ (thmid:string) (_(*op_*)) (t as (Const (op0, _)) $ t1 $ t2) _ =
351 if TermC.is_num t1 andalso TermC.is_num t2 then
353 val n1 = t1 |> HOLogic.dest_number |> snd
354 val n2 = t2 |> HOLogic.dest_number |> snd
356 if Eval.calc_equ (strip_thy op0) (n1, n2)
357 then SOME (TermC.mk_thmid thmid (string_of_int n1) (string_of_int n2),
358 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
359 else SOME (TermC.mk_thmid thmid (string_of_int n1) (string_of_int n2),
360 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
363 | eval_equ _ _ _ _ = NONE;
368 val it = "(?t = ?t) = True"
369 > val t = str2term "x = 0";
370 > val NONE = rewrite_ thy Rewrite_Ord.dummy_ord Rule_Set.empty false reflI t;
372 > val t = str2term "1 = 0";
373 > val NONE = rewrite_ thy Rewrite_Ord.dummy_ord Rule_Set.empty false reflI t;
374 ----------- thus needs Rule.Eval !
375 > val t = str2term "0 = 0";
376 > val SOME (t',_) = rewrite_ thy Rewrite_Ord.dummy_ord Rule_Set.empty false reflI t;
378 val it = \<^const_name>\<open>True\<close>
380 val t = str2term "Not (x = 0)";
381 atomt t; UnparseC.term t;
387 val it = "x ~= 0" : string*)
389 (*.evaluate identity on the term-level, =!= ,i.e. without evaluation of
390 the arguments: thus special handling by 'fun eval_binop'*)
391 (*("ident" ,("Prog_Expr.ident", Prog_Expr.eval_ident "#ident_")):calc*)
392 fun eval_ident (thmid:string) "Prog_Expr.ident" (t as
393 (Const _(*op0, t0*) $ t1 $ t2 )) thy =
395 then SOME (TermC.mk_thmid thmid
396 ("(" ^ (UnparseC.term_in_thy thy t1) ^ ")")
397 ("(" ^ (UnparseC.term_in_thy thy t2) ^ ")"),
398 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
399 else SOME (TermC.mk_thmid thmid
400 ("(" ^ (UnparseC.term_in_thy thy t1) ^ ")")
401 ("(" ^ (UnparseC.term_in_thy thy t2) ^ ")"),
402 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
403 | eval_ident _ _ _ _ = NONE;
405 > val t = str2term "x =!= 0";
406 > val SOME (str, t') = eval_ident "ident_" "b" t thy;
408 val str = "ident_(x)_(0)" : string
409 val it = "(x =!= 0) = False" : string
410 > val t = str2term "1 =!= 0";
411 > val SOME (str, t') = eval_ident "ident_" "b" t thy;
413 val str = "ident_(1)_(0)" : string
414 val it = "(1 =!= 0) = False" : string
415 > val t = str2term "0 =!= 0";
416 > val SOME (str, t') = eval_ident "ident_" "b" t thy;
418 val str = "ident_(0)_(0)" : string
419 val it = "(0 =!= 0) = True" : string
423 (* evaluate identity of terms, which stay ready for further evaluation;
424 thus returns False only for atoms *)
425 (*("equal" ,(\<^const_name>\<open>HOL.eq\<close>, Prog_Expr.eval_equal "#equal_")):calc*)
426 fun eval_equal (thmid : string) \<^const_name>\<open>HOL.eq\<close> (t as (Const _(*op0,t0*) $ t1 $ t2 )) thy =
429 SOME (TermC.mk_thmid thmid
430 ("(" ^ UnparseC.term_in_thy thy t1 ^ ")") ("(" ^ UnparseC.term_in_thy thy t2 ^ ")"),
431 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
433 (case (TermC.is_atom t1, TermC.is_atom t2) of
435 if TermC.variable_constant_pair (t1, t2)
437 else SOME (TermC.mk_thmid thmid
438 ("(" ^ UnparseC.term_in_thy thy t1 ^ ")") ("(" ^ UnparseC.term_in_thy thy t2 ^ ")"),
439 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
440 | _ => NONE) (* NOT is_atom t1,t2 --> rew_sub *)
441 | eval_equal _ _ _ _ = NONE; (* error-exit *)
443 (*. evaluate HOL.divide .*)
444 (*("DIVIDE" ,("Rings.divide_class.divide" , eval_cancel "#divide_e"))*)
445 fun eval_cancel thmid "Rings.divide_class.divide" (t as (Const _ $ t1 $ t2)) _ =
446 if TermC.is_num t1 andalso TermC.is_num t2 then
448 val (T, _) = HOLogic.dest_number t1;
449 val (i1, i2) = (Eval.int_of_numeral t1, Eval.int_of_numeral t2);
450 val res_int as (_, (i1', i2')) = Eval.cancel_int (i1, i2);
452 if (i1', i2') = (i1, i2) then NONE
455 val res = TermC.mk_frac T res_int;
456 val prop = HOLogic.Trueprop $ (HOLogic.mk_eq (t, res));
457 in SOME (TermC.mk_thmid thmid (string_of_int i1) (string_of_int i2), prop) end
460 | eval_cancel _ _ _ _ = NONE;
462 (* get the argument from a function-definition *)
463 (*("argument_in" ,("Prog_Expr.argument_in", Prog_Expr.eval_argument_in "Prog_Expr.argument_in"))*)
464 fun eval_argument_in _ "Prog_Expr.argument_in"
465 (t as (Const ("Prog_Expr.argument_in", _) $ (_(*f*) $ arg))) _ =
466 if is_Free arg (*could be something to be simplified before*)
468 SOME (UnparseC.term t ^ " = " ^ UnparseC.term arg,
469 HOLogic.Trueprop $ (TermC.mk_equality (t, arg)))
471 | eval_argument_in _ _ _ _ = NONE;
473 (* check if the function-identifier of the first argument matches
474 the function-identifier of the lhs of the second argument *)
475 (*("sameFunId" ,("Prog_Expr.sameFunId", eval_same_funid "Prog_Expr.sameFunId"))*)
476 fun eval_sameFunId _ "Prog_Expr.sameFunId"
477 (p as Const ("Prog_Expr.sameFunId",_) $ (f1 $ _) $ (Const (\<^const_name>\<open>HOL.eq\<close>, _) $ (f2 $ _) $ _)) _ =
479 then SOME ((UnparseC.term p) ^ " = True",
480 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
481 else SOME ((UnparseC.term p) ^ " = False",
482 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
483 | eval_sameFunId _ _ _ _ = NONE;
486 (*.from a list of fun-definitions "f x = ..." as 2nd argument
487 filter the elements with the same fun-identfier in "f y"
489 this is, because Isabelles filter takes more than 1 sec.*)
490 fun same_funid f1 (Const (\<^const_name>\<open>HOL.eq\<close>, _) $ (f2 $ _) $ _) = f1 = f2
491 | same_funid f1 t = raise ERROR ("same_funid called with t = ("
492 ^ UnparseC.term f1 ^ ") (" ^ UnparseC.term t ^ ")");
493 (*("filter_sameFunId" ,("Prog_Expr.filter_sameFunId",
494 eval_filter_sameFunId "Prog_Expr.filter_sameFunId"))*)
495 fun eval_filter_sameFunId _ "Prog_Expr.filter_sameFunId"
496 (p as Const ("Prog_Expr.filter_sameFunId",_) $
498 let val fs' = ((TermC.list2isalist HOLogic.boolT) o
499 (filter (same_funid fid))) (TermC.isalist2list fs)
500 in SOME (UnparseC.term (TermC.mk_equality (p, fs')),
501 HOLogic.Trueprop $ (TermC.mk_equality (p, fs'))) end
502 | eval_filter_sameFunId _ _ _ _ = NONE;
504 (* make a list of terms to a sum *)
505 fun list2sum [] = raise ERROR ("list2sum called with []")
509 fun sum su [s'] = Const (\<^const_name>\<open>plus\<close>,
510 [HOLogic.realT,HOLogic.realT] ---> HOLogic.realT) $ su $ s'
511 | sum su (s'::ss') = sum (Const (\<^const_name>\<open>plus\<close>,
512 [HOLogic.realT,HOLogic.realT] ---> HOLogic.realT) $ su $ s') ss'
513 | sum _ _ = raise ERROR "list2sum: pattern in fun.def is missing"
516 (* make a list of equalities to the sum of the lhs *)
517 (*("boollist2sum" ,("Prog_Expr.boollist2sum" , eval_boollist2sum "")):calc*)
518 fun eval_boollist2sum _ "Prog_Expr.boollist2sum"
519 (p as Const ("Prog_Expr.boollist2sum", _) $ (l as Const (\<^const_name>\<open>Cons\<close>, _) $ _ $ _)) _ =
521 val isal = TermC.isalist2list l
522 val lhss = map lhs isal
523 val sum = list2sum lhss
525 SOME ((UnparseC.term p) ^ " = " ^ (UnparseC.term sum),
526 HOLogic.Trueprop $ (TermC.mk_equality (p, sum)))
528 | eval_boollist2sum _ _ _ _ = NONE;
531 \<close> text \<open>
537 subsection \<open>extend rule-set for evaluating pre-conditions and program-expressions\<close>
539 val prog_expr = Rule_Set.append_rules "prog_expr" prog_expr [\<^rule_eval>\<open>Prog_Expr.rhs\<close> (Prog_Expr.eval_rhs "")];
544 subsection \<open>setup for rule-sets and ML-functions\<close>
546 rule_set_knowledge prog_expr = prog_expr
548 calculation matches = \<open>Prog_Expr.eval_matches "#matches_"\<close>
549 calculation matchsub = \<open>Prog_Expr.eval_matchsub "#matchsub_"\<close>
550 calculation Vars = \<open>Prog_Expr.eval_var "#Vars_"\<close>
551 calculation lhs = \<open>Prog_Expr.eval_lhs ""\<close>
552 calculation rhs = \<open>Prog_Expr.eval_rhs ""\<close>
553 (*\\------------------------- from Tools .thy-------------------------------------------------//*)