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.
7 theory Prog_Expr imports Calculate
10 subsection \<open>General constants\<close>
12 (*//------------------------- from Tools .thy-------------------------------------------------\\*)
13 EmptyList :: "bool list"
14 UniversalList :: "bool list"
15 (*\\------------------------- from Tools .thy ------------------------------------------------//*)
16 (*\\------------------------- from Atools .thy -----------------------------------------------//*)
18 Undef :: "real" (* WN190823 probably an outdated design for DiffApp *)
20 pow :: "[real, real] => real" (infixr "^^^" 80)
21 (*\\------------------------- from Atools.thy -----------------------------------------------//*)
23 subsection \<open>consts of functions in pre-conditions and program-expressions\<close>
24 (*//------------------------- from Tools .thy-------------------------------------------------\\*)
26 lhs :: "bool => real" (*of an equality*)
27 rhs :: "bool => real" (*of an equality*)
28 Vars :: "'a => real list" (*get the variables of a term *)
29 matches :: "['a, 'a] => bool"
30 matchsub :: "['a, 'a] => bool"
31 (*\\------------------------- from Tools .thy-------------------------------------------------//*)
32 some'_occur'_in :: "[real list, 'a] => bool" ("some'_of _ occur'_in _")
33 occurs'_in :: "[real , 'a] => bool" ("_ occurs'_in _")
35 abs :: "real => real" ("(|| _ ||)")
36 (* ~~~ FIXXXME Isabelle2002 has abs already !!!*)
37 absset :: "real set => real" ("(||| _ |||)")
38 (*is numeral constant ?*)
39 is'_const :: "real => bool" ("_ is'_const" 10)
40 (*is_const rename to is_num FIXXXME.WN.16.5.03 *)
41 is'_atom :: "real => bool" ("_ is'_atom" 10)
42 is'_even :: "real => bool" ("_ is'_even" 10)
44 (* identity on term level*)
45 ident :: "['a, 'a] => bool" ("(_ =!=/ _)" [51, 51] 50)
47 argument'_in :: "real => real" ("argument'_in _" 10)
48 sameFunId :: "[real, bool] => bool" (**"same'_funid _ _" 10
49 WN0609 changed the id, because ".. _ _" inhibits currying**)
50 filter'_sameFunId:: "[real, bool list] => bool list"
51 ("filter'_sameFunId _ _" 10)
52 boollist2sum :: "bool list => real"
53 lastI :: "'a list \<Rightarrow> 'a"
55 subsection \<open>Theorems for rule-sets TODO: find a better location in code\<close>
57 rle_refl: "(n::real) <= n" and
58 not_true: "(~ True) = False" and
59 not_false: "(~ False) = True" and
60 and_true: "(a & True) = a" and
61 and_false: "(a & False) = False" and
62 or_true: "(a | True) = True" and
63 or_false: "(a | False) = a" and
64 and_commute: "(a & b) = (b & a)" and
65 or_commute: "(a | b) = (b | a)"
67 subsection \<open>ML code for functions in pre-conditions and program-expressions\<close>
71 (*//------------------------- from Tools .thy-------------------------------------------------\\*)
73 val UniversalList: term
76 val eval_lhs: 'a -> string -> term -> 'b -> (string * term) option
77 val eval_matches: string -> string -> term -> theory -> (string * term) option
78 val matchsub: theory -> term -> term -> bool
79 val eval_matchsub: string -> string -> term -> theory -> (string * term) option
81 val eval_rhs: 'a -> string -> term -> 'b -> (string * term) option
82 val eval_var: string -> string -> term -> theory -> (string * term) option
84 val or2list: term -> term
85 (*\\------------------------- from Tools .thy-------------------------------------------------//*)
86 (*//------------------------- from Atools .thy------------------------------------------------\\*)
87 val occurs_in: term -> term -> bool
88 val eval_occurs_in: 'a -> string -> term -> 'b -> (string * term) option
89 val some_occur_in: term list -> term -> bool
90 val eval_some_occur_in: 'a -> string -> term -> 'b -> (string * term) option
91 val eval_is_atom: string -> string -> term -> 'a -> (string * term) option
93 val eval_is_even: string -> string -> term -> 'a -> (string * term) option
94 val eval_const: string -> string -> term -> 'a -> (string * term) option
95 val eval_equ: string -> string -> term -> 'a -> (string * term) option
96 val eval_ident: string -> string -> term -> theory -> (string * term) option
97 val eval_equal: string -> string -> term -> theory -> (string * term) option
98 val eval_cancel: string -> string -> term -> 'a -> (string * term) option
99 val eval_argument_in: string -> string -> term -> 'a -> (string * term) option
100 val same_funid: term -> term -> bool
101 val eval_sameFunId: string -> string -> term -> 'a -> (string * term) option
102 val eval_filter_sameFunId: string -> string -> term -> 'a -> (string * term) option
103 val list2sum: term list -> term
104 val eval_boollist2sum: string -> string -> term -> 'a -> (string * term) option
106 val mk_thmid_f: string -> (int * int) * (int * int) -> (int * int) * (int * int) -> string
107 (*\\------------------------- from Atools.thy------------------------------------------------//*)
111 structure Prog_Expr(**): PROG_EXPR =(**)
114 (*//------------------------- from Tools .thy-------------------------------------------------\\*)
115 (* auxiliary functions for scripts WN.9.00*)
116 (*11.02: for equation solving only*)
117 val UniversalList = (Thm.term_of o the o (TermC.parse @{theory})) "UniversalList";
118 val EmptyList = (Thm.term_of o the o (TermC.parse @{theory})) "[]::bool list";
120 (*+ for Or_to_List +*)
121 fun or2list (Const ("HOL.True",_)) = (tracing"### or2list True";UniversalList)
122 | or2list (Const ("HOL.False",_)) = (tracing"### or2list False";EmptyList)
123 | or2list (t as Const ("HOL.eq",_) $ _ $ _) = TermC.list2isalist HOLogic.boolT [t]
126 fun get ls (Const ("HOL.disj",_) $ o1 $ o2) =
128 Const ("HOL.disj",_) $ _ $ _ => get (ls @ [o1]) o2
129 | _ => ls @ [o1, o2])
130 | get _ t = raise TERM ("or2list: missing pattern in fun.def", [t])
131 in (((TermC.list2isalist HOLogic.boolT) o (get [])) ors) end
132 (*>val t = @{term True};
133 > val t' = or2list t;
135 "Prog_Expr.UniversalList"
136 > val t = @{term False};
137 > val t' = or2list t;
140 > val t=(Thm.term_of o the o (parse thy)) "x=3";
141 > val t' = or2list t;
144 > val t=(Thm.term_of o the o (parse thy))"(x=3) | (x=-3) | (x=0)";
145 > val t' = or2list t;
147 "[x = #3, x = #-3, x = #0]" : string *)
150 (** evaluation on the meta-level **)
152 (*. evaluate the predicate matches (match on whole term only) .*)
153 (*("matches",("Prog_Expr.matches", eval_matches "#matches_")):calc*)
154 fun eval_matches (_:string) "Prog_Expr.matches"
155 (t as Const ("Prog_Expr.matches",_) $ pat $ tst) thy =
156 if TermC.matches thy tst pat
159 val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True}))
160 in SOME (Rule.term_to_string''' thy prop, prop) end
163 val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False}))
164 in SOME (Rule.term_to_string''' thy prop, prop) end
165 | eval_matches _ _ _ _ = NONE;
167 > val t = (Thm.term_of o the o (parse thy))
168 "matches (?x = 0) (1 * x ^^^ 2 = 0)";
169 > eval_matches "/thmid/" "/op_/" t thy;
172 ("matches (x = 0) (1 * x ^^^ 2 = 0) = False",
173 Const (#,#) $ (# $ # $ Const #)) : (string * term) option
175 > val t = (Thm.term_of o the o (parse thy))
176 "matches (?a = #0) (#1 * x ^^^ #2 = #0)";
177 > eval_matches "/thmid/" "/op_/" t thy;
180 ("matches (?a = #0) (#1 * x ^^^ #2 = #0) = True",
181 Const (#,#) $ (# $ # $ Const #)) : (string * term) option
183 > val t = (Thm.term_of o the o (parse thy))
184 "matches (?a * x = #0) (#1 * x ^^^ #2 = #0)";
185 > eval_matches "/thmid/" "/op_/" t thy;
188 ("matches (?a * x = #0) (#1 * x ^^^ #2 = #0) = False",
189 Const (#,#) $ (# $ # $ Const #)) : (string * term) option
191 > val t = (Thm.term_of o the o (parse thy))
192 "matches (?a * x ^^^ #2 = #0) (#1 * x ^^^ #2 = #0)";
193 > eval_matches "/thmid/" "/op_/" t thy;
196 ("matches (?a * x ^^^ #2 = #0) (#1 * x ^^^ #2 = #0) = True",
197 Const (#,#) $ (# $ # $ Const #)) : (string * term) option
198 ----- before ?patterns ---:
199 > val t = (Thm.term_of o the o (parse thy))
200 "matches (a * b^^^#2 = c) (#3 * x^^^#2 = #1)";
201 > eval_matches "/thmid/" "/op_/" t thy;
203 ("matches (a * b ^^^ #2 = c) (#3 * x ^^^ #2 = #1) = True",
204 Const ("HOL.Trueprop","bool => prop") $ (Const # $ (# $ #) $ Const (#,#)))
205 : (string * term) option
207 > val t = (Thm.term_of o the o (parse thy))
208 "matches (a * b^^^#2 = c) (#3 * x^^^#2222 = #1)";
209 > eval_matches "/thmid/" "/op_/" t thy;
210 SOME ("matches (a * b ^^^ #2 = c) (#3 * x ^^^ #2222 = #1) = False",
211 Const ("HOL.Trueprop","bool => prop") $ (Const # $ (# $ #) $ Const (#,#)))
213 > val t = (Thm.term_of o the o (parse thy))
214 "matches (a = b) (x + #1 + #-1 * #2 = #0)";
215 > eval_matches "/thmid/" "/op_/" t thy;
216 SOME ("matches (a = b) (x + #1 + #-1 * #2 = #0) = True",Const # $ (# $ #))
219 (*.does a pattern match some subterm ?.*)
220 fun matchsub thy t pat =
222 fun matchs (t as Const _) = TermC.matches thy t pat
223 | matchs (t as Free _) = TermC.matches thy t pat
224 | matchs (t as Var _) = TermC.matches thy t pat
225 | matchs (Bound _) = false
226 | matchs (t as Abs (_, _, body)) =
227 if TermC.matches thy t pat then true else TermC.matches thy body pat
228 | matchs (t as f1 $ f2) =
229 if TermC.matches thy t pat then true
230 else if matchs f1 then true else matchs f2
233 (*("matchsub",("Prog_Expr.matchsub", eval_matchsub "#matchsub_")):calc*)
234 fun eval_matchsub (_:string) "Prog_Expr.matchsub"
235 (t as Const ("Prog_Expr.matchsub",_) $ pat $ tst) thy =
236 if matchsub thy tst pat
238 let val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True}))
239 in SOME (Rule.term_to_string''' thy prop, prop) end
241 let val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False}))
242 in SOME (Rule.term_to_string''' thy prop, prop) end
243 | eval_matchsub _ _ _ _ = NONE;
245 (*get the variables in an isabelle-term*)
246 (*("Vars" ,("Prog_Expr.Vars" , eval_var "#Vars_")):calc*)
247 fun eval_var (thmid:string) "Prog_Expr.Vars" (t as (Const _ $ arg)) thy =
249 val t' = ((TermC.list2isalist HOLogic.realT) o TermC.vars) t;
250 val thmId = thmid ^ Rule.term_to_string''' thy arg;
251 in SOME (thmId, HOLogic.Trueprop $ (TermC.mk_equality (t, t'))) end
252 | eval_var _ _ _ _ = NONE;
254 fun lhs (Const ("HOL.eq",_) $ l $ _) = l
255 | lhs t = error("lhs called with (" ^ Rule.term2str t ^ ")");
256 (*("lhs" ,("Prog_Expr.lhs" , eval_lhs "")):calc*)
257 fun eval_lhs _ "Prog_Expr.lhs"
258 (t as (Const ("Prog_Expr.lhs",_) $ (Const ("HOL.eq",_) $ l $ _))) _ =
259 SOME ((Rule.term2str t) ^ " = " ^ (Rule.term2str l),
260 HOLogic.Trueprop $ (TermC.mk_equality (t, l)))
261 | eval_lhs _ _ _ _ = NONE;
263 > val t = (Thm.term_of o the o (parse thy)) "lhs (1 * x ^^^ 2 = 0)";
264 > val SOME (id,t') = eval_lhs 0 0 t 0;
265 val id = "Prog_Expr.lhs (1 * x ^^^ 2 = 0) = 1 * x ^^^ 2" : string
267 val it = "Prog_Expr.lhs (1 * x ^^^ 2 = 0) = 1 * x ^^^ 2" : string
270 fun rhs (Const ("HOL.eq",_) $ _ $ r) = r
271 | rhs t = error("rhs called with (" ^ Rule.term2str t ^ ")");
272 (*("rhs" ,("Prog_Expr.rhs" , eval_rhs "")):calc*)
273 fun eval_rhs _ "Prog_Expr.rhs"
274 (t as (Const ("Prog_Expr.rhs",_) $ (Const ("HOL.eq",_) $ _ $ r))) _ =
275 SOME (Rule.term2str t ^ " = " ^ Rule.term2str r,
276 HOLogic.Trueprop $ (TermC.mk_equality (t, r)))
277 | eval_rhs _ _ _ _ = NONE;
278 (*\\------------------------- from Prog_Expr.thy-------------------------------------------------//*)
280 (*//------------------------- from Atools.thy------------------------------------------------\\*)
281 fun occurs_in v t = member op = (((map strip_thy) o TermC.ids2str) t) (Term.term_name v);
283 (*("occurs_in", ("Prog_Expr.occurs'_in", Prog_Expr.eval_occurs_in ""))*)
284 fun eval_occurs_in _ "Prog_Expr.occurs'_in"
285 (p as (Const ("Prog_Expr.occurs'_in",_) $ v $ t)) _ =
286 ((*tracing("@@@ eval_occurs_in: v= "^(Rule.term2str v));
287 tracing("@@@ eval_occurs_in: t= "^(Rule.term2str t));*)
289 then SOME ((Rule.term2str p) ^ " = True",
290 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
291 else SOME ((Rule.term2str p) ^ " = False",
292 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False}))))
293 | eval_occurs_in _ _ _ _ = NONE;
295 (*some of the (bound) variables (eg. in an eqsys) "vs" occur in term "t"*)
296 fun some_occur_in vs t =
297 let fun occurs_in' a b = occurs_in b a
298 in foldl or_ (false, map (occurs_in' t) vs) end;
300 (*("some_occur_in", ("Prog_Expr.some'_occur'_in",
301 eval_some_occur_in "#eval_some_occur_in_"))*)
302 fun eval_some_occur_in _ "Prog_Expr.some'_occur'_in"
303 (p as (Const ("Prog_Expr.some'_occur'_in",_)
305 if some_occur_in (TermC.isalist2list vs) t
306 then SOME ((Rule.term2str p) ^ " = True",
307 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
308 else SOME ((Rule.term2str p) ^ " = False",
309 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
310 | eval_some_occur_in _ _ _ _ = NONE;
312 (*("is_atom",("Prog_Expr.is'_atom", Prog_Expr.eval_is_atom "#is_atom_"))*)
313 fun eval_is_atom (thmid:string) "Prog_Expr.is'_atom"
314 (t as (Const _ $ arg)) _ =
316 Free (n,_) => SOME (TermC.mk_thmid thmid n "",
317 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
318 | _ => SOME (TermC.mk_thmid thmid "" "",
319 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False}))))
320 | eval_is_atom _ _ _ _ = NONE;
322 fun even i = (i div 2) * 2 = i;
323 (*("is_even",("Prog_Expr.is'_even", eval_is_even "#is_even_"))*)
324 fun eval_is_even (thmid:string) "Prog_Expr.is'_even"
325 (t as (Const _ $ arg)) _ =
328 (case TermC.int_of_str_opt n of
330 if even i then SOME (TermC.mk_thmid thmid n "",
331 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
332 else SOME (TermC.mk_thmid thmid "" "",
333 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
336 | eval_is_even _ _ _ _ = NONE;
338 (*evaluate 'is_const'*)
339 (*("is_const",("Prog_Expr.is'_const", Prog_Expr.eval_const "#is_const_"))*)
340 fun eval_const (thmid:string) _ (t as (Const _ $ arg)) _ =
343 SOME (TermC.mk_thmid thmid n1 "", HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
346 then SOME (TermC.mk_thmid thmid n1 "", HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
347 else SOME (TermC.mk_thmid thmid n1 "", HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
349 SOME (TermC.mk_thmid thmid (Rule.term2str arg) "", HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False}))))
350 | eval_const _ _ _ _ = NONE;
352 (*. evaluate binary, associative, commutative operators: *,+,^ .*)
353 (*("PLUS" ,("Groups.plus_class.plus" , (**)eval_binop "#add_")),
354 ("TIMES" ,("Groups.times_class.times" , (**)eval_binop "#mult_")),
355 ("POWER" ,("Prog_Expr.pow" , (**)eval_binop "#power_"))*)
357 (* val (thmid,op_,t as(Const (op0,t0) $ Free (n1,t1) $ Free(n2,t2)),thy) =
358 ("xxxxxx",op_,t,thy);
360 fun mk_thmid_f thmid ((v11, v12), (p11, p12)) ((v21, v22), (p21, p22)) = (* dropped *)
362 (string_of_int v11)^","^(string_of_int v12)^"), ("^
363 (string_of_int p11)^","^(string_of_int p12)^")) __ (("^
364 (string_of_int v21)^","^(string_of_int v22)^"), ("^
365 (string_of_int p21)^","^(string_of_int p22)^"))";
367 (*.evaluate < and <= for numerals.*)
368 (*("le" ,("Orderings.ord_class.less" , Prog_Expr.eval_equ "#less_")),
369 ("leq" ,("Orderings.ord_class.less_eq" , Prog_Expr.eval_equ "#less_equal_"))*)
370 fun eval_equ (thmid:string) (_(*op_*)) (t as
371 (Const (op0, _) $ Free (n1, _) $ Free(n2, _))) _ =
372 (case (TermC.int_of_str_opt n1, TermC.int_of_str_opt n2) of
373 (SOME n1', SOME n2') =>
374 if Calc.calc_equ (strip_thy op0) (n1', n2')
375 then SOME (TermC.mk_thmid thmid n1 n2,
376 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
377 else SOME (TermC.mk_thmid thmid n1 n2,
378 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
381 | eval_equ _ _ _ _ = NONE;
386 val it = "(?t = ?t) = True"
387 > val t = str2term "x = 0";
388 > val NONE = rewrite_ thy Rule.dummy_ord Rule.e_rls false reflI t;
390 > val t = str2term "1 = 0";
391 > val NONE = rewrite_ thy Rule.dummy_ord Rule.e_rls false reflI t;
392 ----------- thus needs Rule.Calc !
393 > val t = str2term "0 = 0";
394 > val SOME (t',_) = rewrite_ thy Rule.dummy_ord Rule.e_rls false reflI t;
398 val t = str2term "Not (x = 0)";
399 atomt t; Rule.term2str t;
405 val it = "x ~= 0" : string*)
407 (*.evaluate identity on the term-level, =!= ,i.e. without evaluation of
408 the arguments: thus special handling by 'fun eval_binop'*)
409 (*("ident" ,("Prog_Expr.ident", Prog_Expr.eval_ident "#ident_")):calc*)
410 fun eval_ident (thmid:string) "Prog_Expr.ident" (t as
411 (Const _(*op0, t0*) $ t1 $ t2 )) thy =
413 then SOME (TermC.mk_thmid thmid
414 ("(" ^ (Rule.term_to_string''' thy t1) ^ ")")
415 ("(" ^ (Rule.term_to_string''' thy t2) ^ ")"),
416 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
417 else SOME (TermC.mk_thmid thmid
418 ("(" ^ (Rule.term_to_string''' thy t1) ^ ")")
419 ("(" ^ (Rule.term_to_string''' thy t2) ^ ")"),
420 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
421 | eval_ident _ _ _ _ = NONE;
423 > val t = str2term "x =!= 0";
424 > val SOME (str, t') = eval_ident "ident_" "b" t thy;
426 val str = "ident_(x)_(0)" : string
427 val it = "(x =!= 0) = False" : string
428 > val t = str2term "1 =!= 0";
429 > val SOME (str, t') = eval_ident "ident_" "b" t thy;
431 val str = "ident_(1)_(0)" : string
432 val it = "(1 =!= 0) = False" : string
433 > val t = str2term "0 =!= 0";
434 > val SOME (str, t') = eval_ident "ident_" "b" t thy;
436 val str = "ident_(0)_(0)" : string
437 val it = "(0 =!= 0) = True" : string
440 (*.evaluate identity of terms, which stay ready for evaluation in turn;
441 thus returns False only for atoms.*)
442 (*("equal" ,("HOL.eq", Prog_Expr.eval_equal "#equal_")):calc*)
443 fun eval_equal (thmid : string) "HOL.eq" (t as (Const _(*op0,t0*) $ t1 $ t2 )) thy =
445 then SOME (TermC.mk_thmid thmid
446 ("(" ^ Rule.term_to_string''' thy t1 ^ ")")
447 ("(" ^ Rule.term_to_string''' thy t2 ^ ")"),
448 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
449 else (case (TermC.is_atom t1, TermC.is_atom t2) of
451 SOME (TermC.mk_thmid thmid
452 ("(" ^ Rule.term_to_string''' thy t1 ^ ")")
453 ("(" ^ Rule.term_to_string''' thy t2 ^ ")"),
454 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
455 | _ => NONE) (* NOT is_atom t1,t2 --> rew_sub *)
456 | eval_equal _ _ _ _ = NONE; (* error-exit *)
458 val t = str2term "x ~= 0";
459 val NONE = eval_equal "equal_" "b" t thy;
462 > val t = str2term "(x + 1) = (x + 1)";
463 > val SOME (str, t') = eval_equal "equal_" "b" t thy;
465 val str = "equal_(x + 1)_(x + 1)" : string
466 val it = "(x + 1 = x + 1) = True" : string
467 > val t = str2term "x = 0";
468 > val NONE = eval_equal "equal_" "b" t thy;
470 > val t = str2term "1 = 0";
471 > val SOME (str, t') = eval_equal "equal_" "b" t thy;
473 val str = "equal_(1)_(0)" : string
474 val it = "(1 = 0) = False" : string
475 > val t = str2term "0 = 0";
476 > val SOME (str, t') = eval_equal "equal_" "b" t thy;
478 val str = "equal_(0)_(0)" : string
479 val it = "(0 = 0) = True" : string
482 (*. evaluate HOL.divide .*)
483 (*("DIVIDE" ,("Rings.divide_class.divide" , eval_cancel "#divide_e"))*)
484 fun eval_cancel (thmid: string) "Rings.divide_class.divide" (t as
485 (Const (op0,t0) $ Free (n1, _) $ Free(n2, _))) _ =
486 (case (TermC.int_of_str_opt n1, TermC.int_of_str_opt n2) of
487 (SOME n1', SOME n2') =>
489 val sg = Calc.sign_mult n1' n2';
490 val (T1,T2,Trange) = TermC.dest_binop_typ t0;
491 val gcd' = Calc.gcd (abs n1') (abs n2');
493 then let val rhs = TermC.term_of_num Trange (sg * (abs n1') div gcd')
494 val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, rhs))
495 in SOME (TermC.mk_thmid thmid n1 n2, prop) end
496 else if 0 < n2' andalso gcd' = 1 then NONE
497 else let val rhs = TermC.mk_num_op_num T1 T2 (op0,t0) (sg * (abs n1') div gcd')
499 val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, rhs))
500 in SOME (TermC.mk_thmid thmid n1 n2, prop) end
502 | _ => ((*tracing"@@@ eval_cancel NONE";*)NONE))
503 | eval_cancel _ _ _ _ = NONE;
505 (* get the argument from a function-definition *)
506 (*("argument_in" ,("Prog_Expr.argument'_in", Prog_Expr.eval_argument_in "Prog_Expr.argument'_in"))*)
507 fun eval_argument_in _ "Prog_Expr.argument'_in"
508 (t as (Const ("Prog_Expr.argument'_in", _) $ (_(*f*) $ arg))) _ =
509 if is_Free arg (*could be something to be simplified before*)
511 SOME (Rule.term2str t ^ " = " ^ Rule.term2str arg,
512 HOLogic.Trueprop $ (TermC.mk_equality (t, arg)))
514 | eval_argument_in _ _ _ _ = NONE;
516 (* check if the function-identifier of the first argument matches
517 the function-identifier of the lhs of the second argument *)
518 (*("sameFunId" ,("Prog_Expr.sameFunId", eval_same_funid "Prog_Expr.sameFunId"))*)
519 fun eval_sameFunId _ "Prog_Expr.sameFunId"
520 (p as Const ("Prog_Expr.sameFunId",_) $ (f1 $ _) $ (Const ("HOL.eq", _) $ (f2 $ _) $ _)) _ =
522 then SOME ((Rule.term2str p) ^ " = True",
523 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
524 else SOME ((Rule.term2str p) ^ " = False",
525 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
526 | eval_sameFunId _ _ _ _ = NONE;
529 (*.from a list of fun-definitions "f x = ..." as 2nd argument
530 filter the elements with the same fun-identfier in "f y"
532 this is, because Isabelles filter takes more than 1 sec.*)
533 fun same_funid f1 (Const ("HOL.eq", _) $ (f2 $ _) $ _) = f1 = f2
534 | same_funid f1 t = error ("same_funid called with t = ("
535 ^Rule.term2str f1^") ("^Rule.term2str t^")");
536 (*("filter_sameFunId" ,("Prog_Expr.filter'_sameFunId",
537 eval_filter_sameFunId "Prog_Expr.filter'_sameFunId"))*)
538 fun eval_filter_sameFunId _ "Prog_Expr.filter'_sameFunId"
539 (p as Const ("Prog_Expr.filter'_sameFunId",_) $
541 let val fs' = ((TermC.list2isalist HOLogic.boolT) o
542 (filter (same_funid fid))) (TermC.isalist2list fs)
543 in SOME (Rule.term2str (TermC.mk_equality (p, fs')),
544 HOLogic.Trueprop $ (TermC.mk_equality (p, fs'))) end
545 | eval_filter_sameFunId _ _ _ _ = NONE;
547 (* make a list of terms to a sum *)
548 fun list2sum [] = error ("list2sum called with []")
552 fun sum su [s'] = Const ("Groups.plus_class.plus",
553 [HOLogic.realT,HOLogic.realT] ---> HOLogic.realT) $ su $ s'
554 | sum su (s'::ss') = sum (Const ("Groups.plus_class.plus",
555 [HOLogic.realT,HOLogic.realT] ---> HOLogic.realT) $ su $ s') ss'
556 | sum _ _ = raise ERROR "list2sum: pattern in fun.def is missing"
559 (* make a list of equalities to the sum of the lhs *)
560 (*("boollist2sum" ,("Prog_Expr.boollist2sum" , eval_boollist2sum "")):calc*)
561 fun eval_boollist2sum _ "Prog_Expr.boollist2sum"
562 (p as Const ("Prog_Expr.boollist2sum", _) $ (l as Const ("List.list.Cons", _) $ _ $ _)) _ =
564 val isal = TermC.isalist2list l
565 val lhss = map lhs isal
566 val sum = list2sum lhss
568 SOME ((Rule.term2str p) ^ " = " ^ (Rule.term2str sum),
569 HOLogic.Trueprop $ (TermC.mk_equality (p, sum)))
571 | eval_boollist2sum _ _ _ _ = NONE;
574 \<close> text \<open>
583 subsection \<open>extend rule-set for evaluating pre-conditions and program-expressions\<close>
585 val list_rls = Rule.append_rls "list_rls" list_rls [Rule.Calc ("Prog_Expr.rhs", Prog_Expr.eval_rhs "")];
590 subsection \<open>setup for rule-sets and ML-functions\<close>
591 setup \<open>KEStore_Elems.add_rlss [("list_rls", (Context.theory_name @{theory}, list_rls))]\<close>
592 setup \<open>KEStore_Elems.add_calcs
593 [("matches", ("Prog_Expr.matches", Prog_Expr.eval_matches "#matches_")),
594 ("matchsub", ("Prog_Expr.matchsub", Prog_Expr.eval_matchsub "#matchsub_")),
595 ("Vars", ("Prog_Expr.Vars", Prog_Expr.eval_var "#Vars_")),
596 ("lhs", ("Prog_Expr.lhs", Prog_Expr.eval_lhs "")),
597 ("rhs", ("Prog_Expr.rhs", Prog_Expr.eval_rhs ""))]\<close>
598 (*\\------------------------- from Tools .thy-------------------------------------------------//*)