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.
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>
19 pow :: "[real, real] => real" (infixr "^^^" 80)
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 numeral constant ?*)
35 is'_const :: "real => bool" ("_ is'_const" 10)
36 (*is_const rename to is_num FIXXXME.WN.16.5.03 *)
37 is'_atom :: "real => bool" ("_ is'_atom" 10)
38 is'_even :: "real => bool" ("_ is'_even" 10)
40 (* identity on term level*)
41 ident :: "['a, 'a] => bool" ("(_ =!=/ _)" [51, 51] 50)
42 argument'_in :: "real => real" ("argument'_in _" 10)
43 sameFunId :: "[real, bool] => bool" (* "same'_funid _ _" 10
44 WN0609 changed the id, because ".. _ _" inhibits currying *)
45 filter'_sameFunId:: "[real, bool list] => bool list"
46 ("filter'_sameFunId _ _" 10)
47 boollist2sum :: "bool list => real"
48 lastI :: "'a list \<Rightarrow> 'a"
50 subsection \<open>ML code for functions in pre-conditions and program-expressions\<close>
54 (*//------------------------- from Tools .thy-------------------------------------------------\\*)
56 val UniversalList: term
59 val eval_lhs: 'a -> string -> term -> 'b -> (string * term) option
60 val eval_matches: string -> string -> term -> theory -> (string * term) option
61 val matchsub: theory -> term -> term -> bool
62 val eval_matchsub: string -> string -> term -> theory -> (string * term) option
64 val eval_rhs: 'a -> string -> term -> 'b -> (string * term) option
65 val eval_var: string -> string -> term -> theory -> (string * term) option
67 val or2list: term -> term
68 (*\\------------------------- from Tools .thy-------------------------------------------------//*)
69 (*//------------------------- from Atools .thy------------------------------------------------\\*)
70 val occurs_in: term -> term -> bool
71 val eval_occurs_in: 'a -> string -> term -> 'b -> (string * term) option
72 val some_occur_in: term list -> term -> bool
73 val eval_some_occur_in: 'a -> string -> term -> 'b -> (string * term) option
74 val eval_is_atom: string -> string -> term -> 'a -> (string * term) option
76 val eval_is_even: string -> string -> term -> 'a -> (string * term) option
77 val eval_const: string -> string -> term -> 'a -> (string * term) option
78 val eval_equ: string -> string -> term -> 'a -> (string * term) option
79 val eval_ident: string -> string -> term -> theory -> (string * term) option
80 val eval_equal: string -> string -> term -> theory -> (string * term) option
81 val eval_cancel: string -> string -> term -> 'a -> (string * term) option
82 val eval_argument_in: string -> string -> term -> 'a -> (string * term) option
83 val same_funid: term -> term -> bool
84 val eval_sameFunId: string -> string -> term -> 'a -> (string * term) option
85 val eval_filter_sameFunId: string -> string -> term -> 'a -> (string * term) option
86 val list2sum: term list -> term
87 val eval_boollist2sum: string -> string -> term -> 'a -> (string * term) option
89 val mk_thmid_f: string -> (int * int) * (int * int) -> (int * int) * (int * int) -> string
90 (*\\------------------------- from Atools.thy------------------------------------------------//*)
94 structure Prog_Expr(**): PROG_EXPR =(**)
97 (*//------------------------- from Tools .thy-------------------------------------------------\\*)
98 (* auxiliary functions for scripts WN.9.00*)
99 (*11.02: for equation solving only*)
100 val UniversalList = (Thm.term_of o the o (TermC.parse @{theory})) "UniversalList";
101 val EmptyList = (Thm.term_of o the o (TermC.parse @{theory})) "[]::bool list";
103 (*+ for Or_to_List +*)
104 fun or2list (Const ("HOL.True",_)) = (tracing"### or2list True";UniversalList)
105 | or2list (Const ("HOL.False",_)) = (tracing"### or2list False";EmptyList)
106 | or2list (t as Const ("HOL.eq",_) $ _ $ _) = TermC.list2isalist HOLogic.boolT [t]
109 fun get ls (Const ("HOL.disj",_) $ o1 $ o2) =
111 Const ("HOL.disj",_) $ _ $ _ => get (ls @ [o1]) o2
112 | _ => ls @ [o1, o2])
113 | get _ t = raise TERM ("or2list: missing pattern in fun.def", [t])
114 in (((TermC.list2isalist HOLogic.boolT) o (get [])) ors) end
115 (*>val t = @{term True};
116 > val t' = or2list t;
118 "Prog_Expr.UniversalList"
119 > val t = @{term False};
120 > val t' = or2list t;
123 > val t=(Thm.term_of o the o (parse thy)) "x=3";
124 > val t' = or2list t;
127 > val t=(Thm.term_of o the o (parse thy))"(x=3) | (x=-3) | (x=0)";
128 > val t' = or2list t;
130 "[x = #3, x = #-3, x = #0]" : string *)
133 (** evaluation on the meta-level **)
135 (*. evaluate the predicate matches (match on whole term only) .*)
136 (*("matches",("Prog_Expr.matches", eval_matches "#matches_")):calc*)
137 fun eval_matches (_:string) "Prog_Expr.matches"
138 (t as Const ("Prog_Expr.matches",_) $ pat $ tst) thy =
139 if TermC.matches thy tst pat
142 val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True}))
143 in SOME (Rule.term_to_string''' thy prop, prop) end
146 val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False}))
147 in SOME (Rule.term_to_string''' thy prop, prop) end
148 | eval_matches _ _ _ _ = NONE;
150 > val t = (Thm.term_of o the o (parse thy))
151 "matches (?x = 0) (1 * x ^^^ 2 = 0)";
152 > eval_matches "/thmid/" "/op_/" t thy;
155 ("matches (x = 0) (1 * x ^^^ 2 = 0) = False",
156 Const (#,#) $ (# $ # $ Const #)) : (string * term) option
158 > val t = (Thm.term_of o the o (parse thy))
159 "matches (?a = #0) (#1 * x ^^^ #2 = #0)";
160 > eval_matches "/thmid/" "/op_/" t thy;
163 ("matches (?a = #0) (#1 * x ^^^ #2 = #0) = True",
164 Const (#,#) $ (# $ # $ Const #)) : (string * term) option
166 > val t = (Thm.term_of o the o (parse thy))
167 "matches (?a * x = #0) (#1 * x ^^^ #2 = #0)";
168 > eval_matches "/thmid/" "/op_/" t thy;
171 ("matches (?a * x = #0) (#1 * x ^^^ #2 = #0) = False",
172 Const (#,#) $ (# $ # $ Const #)) : (string * term) option
174 > val t = (Thm.term_of o the o (parse thy))
175 "matches (?a * x ^^^ #2 = #0) (#1 * x ^^^ #2 = #0)";
176 > eval_matches "/thmid/" "/op_/" t thy;
179 ("matches (?a * x ^^^ #2 = #0) (#1 * x ^^^ #2 = #0) = True",
180 Const (#,#) $ (# $ # $ Const #)) : (string * term) option
181 ----- before ?patterns ---:
182 > val t = (Thm.term_of o the o (parse thy))
183 "matches (a * b^^^#2 = c) (#3 * x^^^#2 = #1)";
184 > eval_matches "/thmid/" "/op_/" t thy;
186 ("matches (a * b ^^^ #2 = c) (#3 * x ^^^ #2 = #1) = True",
187 Const ("HOL.Trueprop","bool => prop") $ (Const # $ (# $ #) $ Const (#,#)))
188 : (string * term) option
190 > val t = (Thm.term_of o the o (parse thy))
191 "matches (a * b^^^#2 = c) (#3 * x^^^#2222 = #1)";
192 > eval_matches "/thmid/" "/op_/" t thy;
193 SOME ("matches (a * b ^^^ #2 = c) (#3 * x ^^^ #2222 = #1) = False",
194 Const ("HOL.Trueprop","bool => prop") $ (Const # $ (# $ #) $ Const (#,#)))
196 > val t = (Thm.term_of o the o (parse thy))
197 "matches (a = b) (x + #1 + #-1 * #2 = #0)";
198 > eval_matches "/thmid/" "/op_/" t thy;
199 SOME ("matches (a = b) (x + #1 + #-1 * #2 = #0) = True",Const # $ (# $ #))
202 (*.does a pattern match some subterm ?.*)
203 fun matchsub thy t pat =
205 fun matchs (t as Const _) = TermC.matches thy t pat
206 | matchs (t as Free _) = TermC.matches thy t pat
207 | matchs (t as Var _) = TermC.matches thy t pat
208 | matchs (Bound _) = false
209 | matchs (t as Abs (_, _, body)) =
210 if TermC.matches thy t pat then true else TermC.matches thy body pat
211 | matchs (t as f1 $ f2) =
212 if TermC.matches thy t pat then true
213 else if matchs f1 then true else matchs f2
216 (*("matchsub",("Prog_Expr.matchsub", eval_matchsub "#matchsub_")):calc*)
217 fun eval_matchsub (_:string) "Prog_Expr.matchsub"
218 (t as Const ("Prog_Expr.matchsub",_) $ pat $ tst) thy =
219 if matchsub thy tst pat
221 let val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True}))
222 in SOME (Rule.term_to_string''' thy prop, prop) end
224 let val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False}))
225 in SOME (Rule.term_to_string''' thy prop, prop) end
226 | eval_matchsub _ _ _ _ = NONE;
228 (*get the variables in an isabelle-term*)
229 (*("Vars" ,("Prog_Expr.Vars" , eval_var "#Vars_")):calc*)
230 fun eval_var (thmid:string) "Prog_Expr.Vars" (t as (Const _ $ arg)) thy =
232 val t' = ((TermC.list2isalist HOLogic.realT) o TermC.vars) t;
233 val thmId = thmid ^ Rule.term_to_string''' thy arg;
234 in SOME (thmId, HOLogic.Trueprop $ (TermC.mk_equality (t, t'))) end
235 | eval_var _ _ _ _ = NONE;
237 fun lhs (Const ("HOL.eq",_) $ l $ _) = l
238 | lhs t = error("lhs called with (" ^ Rule.term2str t ^ ")");
239 (*("lhs" ,("Prog_Expr.lhs" , eval_lhs "")):calc*)
240 fun eval_lhs _ "Prog_Expr.lhs"
241 (t as (Const ("Prog_Expr.lhs",_) $ (Const ("HOL.eq",_) $ l $ _))) _ =
242 SOME ((Rule.term2str t) ^ " = " ^ (Rule.term2str l),
243 HOLogic.Trueprop $ (TermC.mk_equality (t, l)))
244 | eval_lhs _ _ _ _ = NONE;
246 > val t = (Thm.term_of o the o (parse thy)) "lhs (1 * x ^^^ 2 = 0)";
247 > val SOME (id,t') = eval_lhs 0 0 t 0;
248 val id = "Prog_Expr.lhs (1 * x ^^^ 2 = 0) = 1 * x ^^^ 2" : string
250 val it = "Prog_Expr.lhs (1 * x ^^^ 2 = 0) = 1 * x ^^^ 2" : string
253 fun rhs (Const ("HOL.eq",_) $ _ $ r) = r
254 | rhs t = error("rhs called with (" ^ Rule.term2str t ^ ")");
255 (*("rhs" ,("Prog_Expr.rhs" , eval_rhs "")):calc*)
256 fun eval_rhs _ "Prog_Expr.rhs"
257 (t as (Const ("Prog_Expr.rhs",_) $ (Const ("HOL.eq",_) $ _ $ r))) _ =
258 SOME (Rule.term2str t ^ " = " ^ Rule.term2str r,
259 HOLogic.Trueprop $ (TermC.mk_equality (t, r)))
260 | eval_rhs _ _ _ _ = NONE;
261 (*\\------------------------- from Prog_Expr.thy-------------------------------------------------//*)
263 (*//------------------------- from Atools.thy------------------------------------------------\\*)
264 fun occurs_in v t = member op = (((map strip_thy) o TermC.ids2str) t) (Term.term_name v);
266 (*("occurs_in", ("Prog_Expr.occurs'_in", Prog_Expr.eval_occurs_in ""))*)
267 fun eval_occurs_in _ "Prog_Expr.occurs'_in"
268 (p as (Const ("Prog_Expr.occurs'_in",_) $ v $ t)) _ =
269 ((*tracing("@@@ eval_occurs_in: v= "^(Rule.term2str v));
270 tracing("@@@ eval_occurs_in: t= "^(Rule.term2str t));*)
272 then SOME ((Rule.term2str p) ^ " = True",
273 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
274 else SOME ((Rule.term2str p) ^ " = False",
275 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False}))))
276 | eval_occurs_in _ _ _ _ = NONE;
278 (*some of the (bound) variables (eg. in an eqsys) "vs" occur in term "t"*)
279 fun some_occur_in vs t =
280 let fun occurs_in' a b = occurs_in b a
281 in foldl or_ (false, map (occurs_in' t) vs) end;
283 (*("some_occur_in", ("Prog_Expr.some'_occur'_in",
284 eval_some_occur_in "#eval_some_occur_in_"))*)
285 fun eval_some_occur_in _ "Prog_Expr.some'_occur'_in"
286 (p as (Const ("Prog_Expr.some'_occur'_in",_)
288 if some_occur_in (TermC.isalist2list vs) 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_some_occur_in _ _ _ _ = NONE;
295 (*("is_atom",("Prog_Expr.is'_atom", Prog_Expr.eval_is_atom "#is_atom_"))*)
296 fun eval_is_atom (thmid:string) "Prog_Expr.is'_atom"
297 (t as (Const _ $ arg)) _ =
299 Free (n,_) => SOME (TermC.mk_thmid thmid n "",
300 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
301 | _ => SOME (TermC.mk_thmid thmid "" "",
302 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False}))))
303 | eval_is_atom _ _ _ _ = NONE;
305 fun even i = (i div 2) * 2 = i;
306 (*("is_even",("Prog_Expr.is'_even", eval_is_even "#is_even_"))*)
307 fun eval_is_even (thmid:string) "Prog_Expr.is'_even"
308 (t as (Const _ $ arg)) _ =
311 (case TermC.int_of_str_opt n of
313 if even i then SOME (TermC.mk_thmid thmid n "",
314 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
315 else SOME (TermC.mk_thmid thmid "" "",
316 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
319 | eval_is_even _ _ _ _ = NONE;
321 (*evaluate 'is_const'*)
322 (*("is_const",("Prog_Expr.is'_const", Prog_Expr.eval_const "#is_const_"))*)
323 fun eval_const (thmid:string) _ (t as (Const _ $ arg)) _ =
326 SOME (TermC.mk_thmid thmid n1 "", HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
329 then SOME (TermC.mk_thmid thmid n1 "", HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
330 else SOME (TermC.mk_thmid thmid n1 "", HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
332 SOME (TermC.mk_thmid thmid (Rule.term2str arg) "", HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False}))))
333 | eval_const _ _ _ _ = NONE;
335 (*. evaluate binary, associative, commutative operators: *,+,^ .*)
336 (*("PLUS" ,("Groups.plus_class.plus" , (**)eval_binop "#add_")),
337 ("TIMES" ,("Groups.times_class.times" , (**)eval_binop "#mult_")),
338 ("POWER" ,("Prog_Expr.pow" , (**)eval_binop "#power_"))*)
340 (* val (thmid,op_,t as(Const (op0,t0) $ Free (n1,t1) $ Free(n2,t2)),thy) =
341 ("xxxxxx",op_,t,thy);
343 fun mk_thmid_f thmid ((v11, v12), (p11, p12)) ((v21, v22), (p21, p22)) = (* dropped *)
345 (string_of_int v11)^","^(string_of_int v12)^"), ("^
346 (string_of_int p11)^","^(string_of_int p12)^")) __ (("^
347 (string_of_int v21)^","^(string_of_int v22)^"), ("^
348 (string_of_int p21)^","^(string_of_int p22)^"))";
350 (*.evaluate < and <= for numerals.*)
351 (*("le" ,("Orderings.ord_class.less" , Prog_Expr.eval_equ "#less_")),
352 ("leq" ,("Orderings.ord_class.less_eq" , Prog_Expr.eval_equ "#less_equal_"))*)
353 fun eval_equ (thmid:string) (_(*op_*)) (t as
354 (Const (op0, _) $ Free (n1, _) $ Free(n2, _))) _ =
355 (case (TermC.int_of_str_opt n1, TermC.int_of_str_opt n2) of
356 (SOME n1', SOME n2') =>
357 if Calc.calc_equ (strip_thy op0) (n1', n2')
358 then SOME (TermC.mk_thmid thmid n1 n2,
359 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
360 else SOME (TermC.mk_thmid thmid n1 n2,
361 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
364 | eval_equ _ _ _ _ = NONE;
369 val it = "(?t = ?t) = True"
370 > val t = str2term "x = 0";
371 > val NONE = rewrite_ thy Rule.dummy_ord Rule.e_rls false reflI t;
373 > val t = str2term "1 = 0";
374 > val NONE = rewrite_ thy Rule.dummy_ord Rule.e_rls false reflI t;
375 ----------- thus needs Rule.Calc !
376 > val t = str2term "0 = 0";
377 > val SOME (t',_) = rewrite_ thy Rule.dummy_ord Rule.e_rls false reflI t;
381 val t = str2term "Not (x = 0)";
382 atomt t; Rule.term2str t;
388 val it = "x ~= 0" : string*)
390 (*.evaluate identity on the term-level, =!= ,i.e. without evaluation of
391 the arguments: thus special handling by 'fun eval_binop'*)
392 (*("ident" ,("Prog_Expr.ident", Prog_Expr.eval_ident "#ident_")):calc*)
393 fun eval_ident (thmid:string) "Prog_Expr.ident" (t as
394 (Const _(*op0, t0*) $ t1 $ t2 )) thy =
396 then SOME (TermC.mk_thmid thmid
397 ("(" ^ (Rule.term_to_string''' thy t1) ^ ")")
398 ("(" ^ (Rule.term_to_string''' thy t2) ^ ")"),
399 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
400 else SOME (TermC.mk_thmid thmid
401 ("(" ^ (Rule.term_to_string''' thy t1) ^ ")")
402 ("(" ^ (Rule.term_to_string''' thy t2) ^ ")"),
403 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
404 | eval_ident _ _ _ _ = NONE;
406 > val t = str2term "x =!= 0";
407 > val SOME (str, t') = eval_ident "ident_" "b" t thy;
409 val str = "ident_(x)_(0)" : string
410 val it = "(x =!= 0) = False" : string
411 > val t = str2term "1 =!= 0";
412 > val SOME (str, t') = eval_ident "ident_" "b" t thy;
414 val str = "ident_(1)_(0)" : string
415 val it = "(1 =!= 0) = False" : string
416 > val t = str2term "0 =!= 0";
417 > val SOME (str, t') = eval_ident "ident_" "b" t thy;
419 val str = "ident_(0)_(0)" : string
420 val it = "(0 =!= 0) = True" : string
423 (*.evaluate identity of terms, which stay ready for evaluation in turn;
424 thus returns False only for atoms.*)
425 (*("equal" ,("HOL.eq", Prog_Expr.eval_equal "#equal_")):calc*)
426 fun eval_equal (thmid : string) "HOL.eq" (t as (Const _(*op0,t0*) $ t1 $ t2 )) thy =
428 then SOME (TermC.mk_thmid thmid
429 ("(" ^ Rule.term_to_string''' thy t1 ^ ")")
430 ("(" ^ Rule.term_to_string''' thy t2 ^ ")"),
431 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
432 else (case (TermC.is_atom t1, TermC.is_atom t2) of
434 SOME (TermC.mk_thmid thmid
435 ("(" ^ Rule.term_to_string''' thy t1 ^ ")")
436 ("(" ^ Rule.term_to_string''' thy t2 ^ ")"),
437 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
438 | _ => NONE) (* NOT is_atom t1,t2 --> rew_sub *)
439 | eval_equal _ _ _ _ = NONE; (* error-exit *)
441 val t = str2term "x ~= 0";
442 val NONE = eval_equal "equal_" "b" t thy;
445 > val t = str2term "(x + 1) = (x + 1)";
446 > val SOME (str, t') = eval_equal "equal_" "b" t thy;
448 val str = "equal_(x + 1)_(x + 1)" : string
449 val it = "(x + 1 = x + 1) = True" : string
450 > val t = str2term "x = 0";
451 > val NONE = eval_equal "equal_" "b" t thy;
453 > val t = str2term "1 = 0";
454 > val SOME (str, t') = eval_equal "equal_" "b" t thy;
456 val str = "equal_(1)_(0)" : string
457 val it = "(1 = 0) = False" : string
458 > val t = str2term "0 = 0";
459 > val SOME (str, t') = eval_equal "equal_" "b" t thy;
461 val str = "equal_(0)_(0)" : string
462 val it = "(0 = 0) = True" : string
465 (*. evaluate HOL.divide .*)
466 (*("DIVIDE" ,("Rings.divide_class.divide" , eval_cancel "#divide_e"))*)
467 fun eval_cancel (thmid: string) "Rings.divide_class.divide" (t as
468 (Const (op0,t0) $ Free (n1, _) $ Free(n2, _))) _ =
469 (case (TermC.int_of_str_opt n1, TermC.int_of_str_opt n2) of
470 (SOME n1', SOME n2') =>
472 val sg = Calc.sign_mult n1' n2';
473 val (T1,T2,Trange) = TermC.dest_binop_typ t0;
474 val gcd' = Calc.gcd (abs n1') (abs n2');
476 then let val rhs = TermC.term_of_num Trange (sg * (abs n1') div gcd')
477 val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, rhs))
478 in SOME (TermC.mk_thmid thmid n1 n2, prop) end
479 else if 0 < n2' andalso gcd' = 1 then NONE
480 else let val rhs = TermC.mk_num_op_num T1 T2 (op0,t0) (sg * (abs n1') div gcd')
482 val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, rhs))
483 in SOME (TermC.mk_thmid thmid n1 n2, prop) end
485 | _ => ((*tracing"@@@ eval_cancel NONE";*)NONE))
486 | eval_cancel _ _ _ _ = NONE;
488 (* get the argument from a function-definition *)
489 (*("argument_in" ,("Prog_Expr.argument'_in", Prog_Expr.eval_argument_in "Prog_Expr.argument'_in"))*)
490 fun eval_argument_in _ "Prog_Expr.argument'_in"
491 (t as (Const ("Prog_Expr.argument'_in", _) $ (_(*f*) $ arg))) _ =
492 if is_Free arg (*could be something to be simplified before*)
494 SOME (Rule.term2str t ^ " = " ^ Rule.term2str arg,
495 HOLogic.Trueprop $ (TermC.mk_equality (t, arg)))
497 | eval_argument_in _ _ _ _ = NONE;
499 (* check if the function-identifier of the first argument matches
500 the function-identifier of the lhs of the second argument *)
501 (*("sameFunId" ,("Prog_Expr.sameFunId", eval_same_funid "Prog_Expr.sameFunId"))*)
502 fun eval_sameFunId _ "Prog_Expr.sameFunId"
503 (p as Const ("Prog_Expr.sameFunId",_) $ (f1 $ _) $ (Const ("HOL.eq", _) $ (f2 $ _) $ _)) _ =
505 then SOME ((Rule.term2str p) ^ " = True",
506 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
507 else SOME ((Rule.term2str p) ^ " = False",
508 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
509 | eval_sameFunId _ _ _ _ = NONE;
512 (*.from a list of fun-definitions "f x = ..." as 2nd argument
513 filter the elements with the same fun-identfier in "f y"
515 this is, because Isabelles filter takes more than 1 sec.*)
516 fun same_funid f1 (Const ("HOL.eq", _) $ (f2 $ _) $ _) = f1 = f2
517 | same_funid f1 t = error ("same_funid called with t = ("
518 ^Rule.term2str f1^") ("^Rule.term2str t^")");
519 (*("filter_sameFunId" ,("Prog_Expr.filter'_sameFunId",
520 eval_filter_sameFunId "Prog_Expr.filter'_sameFunId"))*)
521 fun eval_filter_sameFunId _ "Prog_Expr.filter'_sameFunId"
522 (p as Const ("Prog_Expr.filter'_sameFunId",_) $
524 let val fs' = ((TermC.list2isalist HOLogic.boolT) o
525 (filter (same_funid fid))) (TermC.isalist2list fs)
526 in SOME (Rule.term2str (TermC.mk_equality (p, fs')),
527 HOLogic.Trueprop $ (TermC.mk_equality (p, fs'))) end
528 | eval_filter_sameFunId _ _ _ _ = NONE;
530 (* make a list of terms to a sum *)
531 fun list2sum [] = error ("list2sum called with []")
535 fun sum su [s'] = Const ("Groups.plus_class.plus",
536 [HOLogic.realT,HOLogic.realT] ---> HOLogic.realT) $ su $ s'
537 | sum su (s'::ss') = sum (Const ("Groups.plus_class.plus",
538 [HOLogic.realT,HOLogic.realT] ---> HOLogic.realT) $ su $ s') ss'
539 | sum _ _ = raise ERROR "list2sum: pattern in fun.def is missing"
542 (* make a list of equalities to the sum of the lhs *)
543 (*("boollist2sum" ,("Prog_Expr.boollist2sum" , eval_boollist2sum "")):calc*)
544 fun eval_boollist2sum _ "Prog_Expr.boollist2sum"
545 (p as Const ("Prog_Expr.boollist2sum", _) $ (l as Const ("List.list.Cons", _) $ _ $ _)) _ =
547 val isal = TermC.isalist2list l
548 val lhss = map lhs isal
549 val sum = list2sum lhss
551 SOME ((Rule.term2str p) ^ " = " ^ (Rule.term2str sum),
552 HOLogic.Trueprop $ (TermC.mk_equality (p, sum)))
554 | eval_boollist2sum _ _ _ _ = NONE;
557 \<close> text \<open>
563 subsection \<open>extend rule-set for evaluating pre-conditions and program-expressions\<close>
565 val list_rls = Rule.append_rls "list_rls" list_rls [Rule.Calc ("Prog_Expr.rhs", Prog_Expr.eval_rhs "")];
570 subsection \<open>setup for rule-sets and ML-functions\<close>
571 setup \<open>KEStore_Elems.add_rlss [("list_rls", (Context.theory_name @{theory}, list_rls))]\<close>
572 setup \<open>KEStore_Elems.add_calcs
573 [("matches", ("Prog_Expr.matches", Prog_Expr.eval_matches "#matches_")),
574 ("matchsub", ("Prog_Expr.matchsub", Prog_Expr.eval_matchsub "#matchsub_")),
575 ("Vars", ("Prog_Expr.Vars", Prog_Expr.eval_var "#Vars_")),
576 ("lhs", ("Prog_Expr.lhs", Prog_Expr.eval_lhs "")),
577 ("rhs", ("Prog_Expr.rhs", Prog_Expr.eval_rhs ""))]\<close>
578 (*\\------------------------- from Tools .thy-------------------------------------------------//*)