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>
19 pow :: "[real, real] => real" (infixr "\<up>" 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 eval_lhs: 'a -> string -> term -> 'b -> (string * term) option
57 val eval_matches: string -> string -> term -> theory -> (string * term) option
58 val matchsub: theory -> term -> term -> bool
59 val eval_matchsub: string -> string -> term -> theory -> (string * term) option
61 val eval_rhs: 'a -> string -> term -> 'b -> (string * term) option
62 val eval_var: string -> string -> term -> theory -> (string * term) option
64 val or2list: term -> term
65 (*\\------------------------- from Tools .thy-------------------------------------------------//*)
66 (*//------------------------- from Atools .thy------------------------------------------------\\*)
67 val occurs_in: term -> term -> bool
68 val eval_occurs_in: 'a -> string -> term -> 'b -> (string * term) option
69 val some_occur_in: term list -> term -> bool
70 val eval_some_occur_in: 'a -> string -> term -> 'b -> (string * term) option
71 val eval_is_atom: string -> string -> term -> 'a -> (string * term) option
73 val eval_is_even: string -> string -> term -> 'a -> (string * term) option
74 val eval_const: string -> string -> term -> 'a -> (string * term) option
75 val eval_equ: string -> string -> term -> 'a -> (string * term) option
76 val eval_ident: string -> string -> term -> theory -> (string * term) option
77 val eval_equal: string -> string -> term -> theory -> (string * term) option
78 val eval_cancel: string -> string -> term -> 'a -> (string * term) option
79 val eval_argument_in: string -> string -> term -> 'a -> (string * term) option
80 val same_funid: term -> term -> bool
81 val eval_sameFunId: string -> string -> term -> 'a -> (string * term) option
82 val eval_filter_sameFunId: string -> string -> term -> 'a -> (string * term) option
83 val list2sum: term list -> term
84 val eval_boollist2sum: string -> string -> term -> 'a -> (string * term) option
86 val mk_thmid_f: string -> (int * int) * (int * int) -> (int * int) * (int * int) -> string
90 structure Prog_Expr(**): PROG_EXPR =(**)
94 (*+ for Or_to_List +*)
95 fun or2list (Const ("HOL.True",_)) = ((*tracing"### or2list True";*) UniversalList)
96 | or2list (Const ("HOL.False",_)) = ((*tracing"### or2list False";*) EmptyList)
97 | or2list (t as Const ("HOL.eq",_) $ _ $ _) = TermC.list2isalist HOLogic.boolT [t]
100 fun get ls (Const ("HOL.disj",_) $ o1 $ o2) =
102 Const ("HOL.disj",_) $ _ $ _ => get (ls @ [o1]) o2
103 | _ => ls @ [o1, o2])
104 | get _ t = raise TERM ("or2list: missing pattern in fun.def", [t])
105 in (((TermC.list2isalist HOLogic.boolT) o (get [])) ors) end
106 (*>val t = @{term True};
107 > val t' = or2list t;
109 "ListC.UniversalList"
110 > val t = @{term False};
111 > val t' = or2list t;
114 > val t=(Thm.term_of o the o (parse thy)) "x=3";
115 > val t' = or2list t;
118 > val t=(Thm.term_of o the o (parse thy))"(x=3) | (x=-3) | (x=0)";
119 > val t' = or2list t;
121 "[x = #3, x = #-3, x = #0]" : string *)
124 (** evaluation on the meta-level **)
126 (*. evaluate the predicate matches (match on whole term only) .*)
127 (*("matches",("Prog_Expr.matches", eval_matches "#matches_")):calc*)
128 fun eval_matches (_:string) "Prog_Expr.matches" (t as Const ("Prog_Expr.matches",_) $ pat $ tst) thy =
129 if TermC.matches thy tst pat
132 val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True}))
133 in SOME (UnparseC.term_in_thy thy prop, prop) end
136 val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False}))
137 in SOME (UnparseC.term_in_thy thy prop, prop) end
138 | eval_matches _ _ _ _ = NONE;
140 > val t = (Thm.term_of o the o (parse thy))
141 "matches (?x = 0) (1 * x \<up> 2 = 0)";
142 > eval_matches "/thmid/" "/op_/" t thy;
145 ("matches (x = 0) (1 * x \<up> 2 = 0) = False",
146 Const (#,#) $ (# $ # $ Const #)) : (string * term) option
148 > val t = (Thm.term_of o the o (parse thy))
149 "matches (?a = #0) (#1 * x \<up> #2 = #0)";
150 > eval_matches "/thmid/" "/op_/" t thy;
153 ("matches (?a = #0) (#1 * x \<up> #2 = #0) = True",
154 Const (#,#) $ (# $ # $ Const #)) : (string * term) option
156 > val t = (Thm.term_of o the o (parse thy))
157 "matches (?a * x = #0) (#1 * x \<up> #2 = #0)";
158 > eval_matches "/thmid/" "/op_/" t thy;
161 ("matches (?a * x = #0) (#1 * x \<up> #2 = #0) = False",
162 Const (#,#) $ (# $ # $ Const #)) : (string * term) option
164 > val t = (Thm.term_of o the o (parse thy))
165 "matches (?a * x \<up> #2 = #0) (#1 * x \<up> #2 = #0)";
166 > eval_matches "/thmid/" "/op_/" t thy;
169 ("matches (?a * x \<up> #2 = #0) (#1 * x \<up> #2 = #0) = True",
170 Const (#,#) $ (# $ # $ Const #)) : (string * term) option
171 ----- before ?patterns ---:
172 > val t = (Thm.term_of o the o (parse thy))
173 "matches (a * b \<up> #2 = c) (#3 * x \<up> #2 = #1)";
174 > eval_matches "/thmid/" "/op_/" t thy;
176 ("matches (a * b \<up> #2 = c) (#3 * x \<up> #2 = #1) = True",
177 Const ("HOL.Trueprop", "bool => prop") $ (Const # $ (# $ #) $ Const (#,#)))
178 : (string * term) option
180 > val t = (Thm.term_of o the o (parse thy))
181 "matches (a * b \<up> #2 = c) (#3 * x \<up> #2222 = #1)";
182 > eval_matches "/thmid/" "/op_/" t thy;
183 SOME ("matches (a * b \<up> #2 = c) (#3 * x \<up> #2222 = #1) = False",
184 Const ("HOL.Trueprop", "bool => prop") $ (Const # $ (# $ #) $ Const (#,#)))
186 > val t = (Thm.term_of o the o (parse thy))
187 "matches (a = b) (x + #1 + #-1 * #2 = #0)";
188 > eval_matches "/thmid/" "/op_/" t thy;
189 SOME ("matches (a = b) (x + #1 + #-1 * #2 = #0) = True",Const # $ (# $ #))
192 (*.does a pattern match some subterm ?.*)
193 fun matchsub thy t pat =
195 fun matchs (t as Const _) = TermC.matches thy t pat
196 | matchs (t as Free _) = TermC.matches thy t pat
197 | matchs (t as Var _) = TermC.matches thy t pat
198 | matchs (Bound _) = false
199 | matchs (t as Abs (_, _, body)) =
200 if TermC.matches thy t pat then true else TermC.matches thy body pat
201 | matchs (t as f1 $ f2) =
202 if TermC.matches thy t pat then true
203 else if matchs f1 then true else matchs f2
206 (*("matchsub",("Prog_Expr.matchsub", eval_matchsub "#matchsub_")):calc*)
207 fun eval_matchsub (_:string) "Prog_Expr.matchsub"
208 (t as Const ("Prog_Expr.matchsub",_) $ pat $ tst) thy =
209 if matchsub thy tst pat
211 let val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True}))
212 in SOME (UnparseC.term_in_thy thy prop, prop) end
214 let val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False}))
215 in SOME (UnparseC.term_in_thy thy prop, prop) end
216 | eval_matchsub _ _ _ _ = NONE;
218 (*get the variables in an isabelle-term*)
219 (*("Vars" ,("Prog_Expr.Vars" , eval_var "#Vars_")):calc*)
220 fun eval_var (thmid:string) "Prog_Expr.Vars" (t as (Const _ $ arg)) thy =
222 val t' = ((TermC.list2isalist HOLogic.realT) o TermC.vars) t;
223 val thmId = thmid ^ UnparseC.term_in_thy thy arg;
224 in SOME (thmId, HOLogic.Trueprop $ (TermC.mk_equality (t, t'))) end
225 | eval_var _ _ _ _ = NONE;
227 (* refer to Thm.lhs_of *)
228 fun lhs (Const ("HOL.eq",_) $ l $ _) = l
229 | lhs t = raise ERROR("lhs called with (" ^ UnparseC.term t ^ ")");
230 (*("lhs" ,("Prog_Expr.lhs" , eval_lhs "")):calc*)
231 fun eval_lhs _ "Prog_Expr.lhs" (t as (Const ("Prog_Expr.lhs",_) $ (Const ("HOL.eq",_) $ l $ _))) _ =
232 SOME ((UnparseC.term t) ^ " = " ^ (UnparseC.term l),
233 HOLogic.Trueprop $ (TermC.mk_equality (t, l)))
234 | eval_lhs _ _ _ _ = NONE;
236 > val t = (Thm.term_of o the o (parse thy)) "lhs (1 * x \<up> 2 = 0)";
237 > val SOME (id,t') = eval_lhs 0 0 t 0;
238 val id = "Prog_Expr.lhs (1 * x \<up> 2 = 0) = 1 * x \<up> 2" : string
240 val it = "Prog_Expr.lhs (1 * x \<up> 2 = 0) = 1 * x \<up> 2" : string
243 fun rhs (Const ("HOL.eq",_) $ _ $ r) = r
244 | rhs t = raise ERROR("rhs called with (" ^ UnparseC.term t ^ ")");
245 (*("rhs" ,("Prog_Expr.rhs" , eval_rhs "")):calc*)
246 fun eval_rhs _ "Prog_Expr.rhs" (t as (Const ("Prog_Expr.rhs",_) $ (Const ("HOL.eq",_) $ _ $ r))) _ =
247 SOME (UnparseC.term t ^ " = " ^ UnparseC.term r,
248 HOLogic.Trueprop $ (TermC.mk_equality (t, r)))
249 | eval_rhs _ _ _ _ = NONE;
250 (*\\------------------------- from Prog_Expr.thy-------------------------------------------------//*)
252 (*//------------------------- from Atools.thy------------------------------------------------\\*)
253 fun occurs_in v t = member op = (((map strip_thy) o TermC.ids2str) t) (Term.term_name v);
255 (*("occurs_in", ("Prog_Expr.occurs'_in", Prog_Expr.eval_occurs_in ""))*)
256 fun eval_occurs_in _ "Prog_Expr.occurs'_in" (p as (Const ("Prog_Expr.occurs'_in",_) $ v $ t)) _ =
257 ((*tracing("#>@ eval_occurs_in: v= "^(UnparseC.term v));
258 tracing("#>@ eval_occurs_in: t= "^(UnparseC.term t));*)
260 then SOME ((UnparseC.term p) ^ " = True",
261 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
262 else SOME ((UnparseC.term p) ^ " = False",
263 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False}))))
264 | eval_occurs_in _ _ _ _ = NONE;
266 (*some of the (bound) variables (eg. in an eqsys) "vs" occur in term "t"*)
267 fun some_occur_in vs t =
268 let fun occurs_in' a b = occurs_in b a
269 in foldl or_ (false, map (occurs_in' t) vs) end;
271 (*("some_occur_in", ("Prog_Expr.some'_occur'_in",
272 eval_some_occur_in "#eval_some_occur_in_"))*)
273 fun eval_some_occur_in _ "Prog_Expr.some'_occur'_in"
274 (p as (Const ("Prog_Expr.some'_occur'_in",_) $ vs $ t)) _ =
275 if some_occur_in (TermC.isalist2list vs) t
276 then SOME ((UnparseC.term p) ^ " = True",
277 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
278 else SOME ((UnparseC.term p) ^ " = False",
279 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
280 | eval_some_occur_in _ _ _ _ = NONE;
282 (*("is_atom",("Prog_Expr.is'_atom", Prog_Expr.eval_is_atom "#is_atom_"))*)
283 fun eval_is_atom (thmid:string) "Prog_Expr.is'_atom"
284 (t as (Const _ $ arg)) _ =
286 Free (n,_) => SOME (TermC.mk_thmid thmid n "",
287 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
288 | _ => SOME (TermC.mk_thmid thmid "" "",
289 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False}))))
290 | eval_is_atom _ _ _ _ = NONE;
292 fun even i = (i div 2) * 2 = i;
293 (*("is_even",("Prog_Expr.is'_even", eval_is_even "#is_even_"))*)
294 fun eval_is_even (thmid:string) "Prog_Expr.is'_even"
295 (t as (Const _ $ arg)) _ =
298 (case ThmC_Def.int_opt_of_string n of
300 if even i then SOME (TermC.mk_thmid thmid n "",
301 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
302 else SOME (TermC.mk_thmid thmid "" "",
303 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
306 | eval_is_even _ _ _ _ = NONE;
308 (*evaluate 'is_const'*)
309 (*("is_const",("Prog_Expr.is'_const", Prog_Expr.eval_const "#is_const_"))*)
310 fun eval_const (thmid:string) _ (t as (Const _ $ arg)) _ =
313 SOME (TermC.mk_thmid thmid n1 "", HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
316 then SOME (TermC.mk_thmid thmid n1 "", HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
317 else SOME (TermC.mk_thmid thmid n1 "", HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
319 SOME (TermC.mk_thmid thmid (UnparseC.term arg) "", HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False}))))
320 | eval_const _ _ _ _ = NONE;
322 (*. evaluate binary, associative, commutative operators: *,+,^ .*)
323 (*("PLUS" ,("Groups.plus_class.plus" , (**)eval_binop "#add_")),
324 ("TIMES" ,("Groups.times_class.times" , (**)eval_binop "#mult_")),
325 ("POWER" ,("Prog_Expr.pow" , (**)eval_binop "#power_"))*)
327 (* val (thmid,op_,t as(Const (op0,t0) $ Free (n1,t1) $ Free(n2,t2)),thy) =
328 ("xxxxxx",op_,t,thy);
330 fun mk_thmid_f thmid ((v11, v12), (p11, p12)) ((v21, v22), (p21, p22)) = (* dropped *)
332 (string_of_int v11)^", "^(string_of_int v12)^"), ("^
333 (string_of_int p11)^", "^(string_of_int p12)^")) __ (("^
334 (string_of_int v21)^", "^(string_of_int v22)^"), ("^
335 (string_of_int p21)^", "^(string_of_int p22)^"))";
337 (*.evaluate < and <= for numerals.*)
338 (*("le" ,("Orderings.ord_class.less" , Prog_Expr.eval_equ "#less_")),
339 ("leq" ,("Orderings.ord_class.less_eq" , Prog_Expr.eval_equ "#less_equal_"))*)
340 fun eval_equ (thmid:string) (_(*op_*)) (t as
341 (Const (op0, _) $ Free (n1, _) $ Free(n2, _))) _ =
342 (case (ThmC_Def.int_opt_of_string n1, ThmC_Def.int_opt_of_string n2) of
343 (SOME n1', SOME n2') =>
344 if Eval.calc_equ (strip_thy op0) (n1', n2')
345 then SOME (TermC.mk_thmid thmid n1 n2,
346 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
347 else SOME (TermC.mk_thmid thmid n1 n2,
348 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
351 | eval_equ _ _ _ _ = NONE;
356 val it = "(?t = ?t) = True"
357 > val t = str2term "x = 0";
358 > val NONE = rewrite_ thy Rewrite_Ord.dummy_ord Rule_Set.empty false reflI t;
360 > val t = str2term "1 = 0";
361 > val NONE = rewrite_ thy Rewrite_Ord.dummy_ord Rule_Set.empty false reflI t;
362 ----------- thus needs Rule.Eval !
363 > val t = str2term "0 = 0";
364 > val SOME (t',_) = rewrite_ thy Rewrite_Ord.dummy_ord Rule_Set.empty false reflI t;
368 val t = str2term "Not (x = 0)";
369 atomt t; UnparseC.term t;
375 val it = "x ~= 0" : string*)
377 (*.evaluate identity on the term-level, =!= ,i.e. without evaluation of
378 the arguments: thus special handling by 'fun eval_binop'*)
379 (*("ident" ,("Prog_Expr.ident", Prog_Expr.eval_ident "#ident_")):calc*)
380 fun eval_ident (thmid:string) "Prog_Expr.ident" (t as
381 (Const _(*op0, t0*) $ t1 $ t2 )) thy =
383 then SOME (TermC.mk_thmid thmid
384 ("(" ^ (UnparseC.term_in_thy thy t1) ^ ")")
385 ("(" ^ (UnparseC.term_in_thy thy t2) ^ ")"),
386 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
387 else SOME (TermC.mk_thmid thmid
388 ("(" ^ (UnparseC.term_in_thy thy t1) ^ ")")
389 ("(" ^ (UnparseC.term_in_thy thy t2) ^ ")"),
390 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
391 | eval_ident _ _ _ _ = NONE;
393 > val t = str2term "x =!= 0";
394 > val SOME (str, t') = eval_ident "ident_" "b" t thy;
396 val str = "ident_(x)_(0)" : string
397 val it = "(x =!= 0) = False" : string
398 > val t = str2term "1 =!= 0";
399 > val SOME (str, t') = eval_ident "ident_" "b" t thy;
401 val str = "ident_(1)_(0)" : string
402 val it = "(1 =!= 0) = False" : string
403 > val t = str2term "0 =!= 0";
404 > val SOME (str, t') = eval_ident "ident_" "b" t thy;
406 val str = "ident_(0)_(0)" : string
407 val it = "(0 =!= 0) = True" : string
411 (* evaluate identity of terms, which stay ready for further evaluation;
412 thus returns False only for atoms *)
413 (*("equal" ,("HOL.eq", Prog_Expr.eval_equal "#equal_")):calc*)
414 fun eval_equal (thmid : string) "HOL.eq" (t as (Const _(*op0,t0*) $ t1 $ t2 )) thy =
417 SOME (TermC.mk_thmid thmid
418 ("(" ^ UnparseC.term_in_thy thy t1 ^ ")") ("(" ^ UnparseC.term_in_thy thy t2 ^ ")"),
419 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
421 (case (TermC.is_atom t1, TermC.is_atom t2) of
423 if TermC.variable_constant_pair (t1, t2)
425 else SOME (TermC.mk_thmid thmid
426 ("(" ^ UnparseC.term_in_thy thy t1 ^ ")") ("(" ^ UnparseC.term_in_thy thy t2 ^ ")"),
427 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
428 | _ => NONE) (* NOT is_atom t1,t2 --> rew_sub *)
429 | eval_equal _ _ _ _ = NONE; (* error-exit *)
431 (*. evaluate HOL.divide .*)
432 (*("DIVIDE" ,("Rings.divide_class.divide" , eval_cancel "#divide_e"))*)
433 fun eval_cancel (thmid: string) "Rings.divide_class.divide" (t as
434 (Const (op0,t0) $ Free (n1, _) $ Free(n2, _))) _ =
435 (case (ThmC_Def.int_opt_of_string n1, ThmC_Def.int_opt_of_string n2) of
436 (SOME n1', SOME n2') =>
438 val sg = Eval.sign_mult n1' n2';
439 val (T1,T2,Trange) = TermC.dest_binop_typ t0;
440 val gcd' = Eval.gcd (abs n1') (abs n2');
442 then let val rhs = TermC.term_of_num Trange (sg * (abs n1') div gcd')
443 val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, rhs))
444 in SOME (TermC.mk_thmid thmid n1 n2, prop) end
445 else if 0 < n2' andalso gcd' = 1 then NONE
446 else let val rhs = TermC.mk_num_op_num T1 T2 (op0,t0) (sg * (abs n1') div gcd')
448 val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, rhs))
449 in SOME (TermC.mk_thmid thmid n1 n2, prop) end
451 | _ => ((*tracing"#>@ eval_cancel NONE";*)NONE))
452 | eval_cancel _ _ _ _ = NONE;
454 (* get the argument from a function-definition *)
455 (*("argument_in" ,("Prog_Expr.argument'_in", Prog_Expr.eval_argument_in "Prog_Expr.argument'_in"))*)
456 fun eval_argument_in _ "Prog_Expr.argument'_in"
457 (t as (Const ("Prog_Expr.argument'_in", _) $ (_(*f*) $ arg))) _ =
458 if is_Free arg (*could be something to be simplified before*)
460 SOME (UnparseC.term t ^ " = " ^ UnparseC.term arg,
461 HOLogic.Trueprop $ (TermC.mk_equality (t, arg)))
463 | eval_argument_in _ _ _ _ = NONE;
465 (* check if the function-identifier of the first argument matches
466 the function-identifier of the lhs of the second argument *)
467 (*("sameFunId" ,("Prog_Expr.sameFunId", eval_same_funid "Prog_Expr.sameFunId"))*)
468 fun eval_sameFunId _ "Prog_Expr.sameFunId"
469 (p as Const ("Prog_Expr.sameFunId",_) $ (f1 $ _) $ (Const ("HOL.eq", _) $ (f2 $ _) $ _)) _ =
471 then SOME ((UnparseC.term p) ^ " = True",
472 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
473 else SOME ((UnparseC.term p) ^ " = False",
474 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
475 | eval_sameFunId _ _ _ _ = NONE;
478 (*.from a list of fun-definitions "f x = ..." as 2nd argument
479 filter the elements with the same fun-identfier in "f y"
481 this is, because Isabelles filter takes more than 1 sec.*)
482 fun same_funid f1 (Const ("HOL.eq", _) $ (f2 $ _) $ _) = f1 = f2
483 | same_funid f1 t = raise ERROR ("same_funid called with t = ("
484 ^ UnparseC.term f1 ^ ") (" ^ UnparseC.term t ^ ")");
485 (*("filter_sameFunId" ,("Prog_Expr.filter'_sameFunId",
486 eval_filter_sameFunId "Prog_Expr.filter'_sameFunId"))*)
487 fun eval_filter_sameFunId _ "Prog_Expr.filter'_sameFunId"
488 (p as Const ("Prog_Expr.filter'_sameFunId",_) $
490 let val fs' = ((TermC.list2isalist HOLogic.boolT) o
491 (filter (same_funid fid))) (TermC.isalist2list fs)
492 in SOME (UnparseC.term (TermC.mk_equality (p, fs')),
493 HOLogic.Trueprop $ (TermC.mk_equality (p, fs'))) end
494 | eval_filter_sameFunId _ _ _ _ = NONE;
496 (* make a list of terms to a sum *)
497 fun list2sum [] = raise ERROR ("list2sum called with []")
501 fun sum su [s'] = Const ("Groups.plus_class.plus",
502 [HOLogic.realT,HOLogic.realT] ---> HOLogic.realT) $ su $ s'
503 | sum su (s'::ss') = sum (Const ("Groups.plus_class.plus",
504 [HOLogic.realT,HOLogic.realT] ---> HOLogic.realT) $ su $ s') ss'
505 | sum _ _ = raise ERROR "list2sum: pattern in fun.def is missing"
508 (* make a list of equalities to the sum of the lhs *)
509 (*("boollist2sum" ,("Prog_Expr.boollist2sum" , eval_boollist2sum "")):calc*)
510 fun eval_boollist2sum _ "Prog_Expr.boollist2sum"
511 (p as Const ("Prog_Expr.boollist2sum", _) $ (l as Const ("List.list.Cons", _) $ _ $ _)) _ =
513 val isal = TermC.isalist2list l
514 val lhss = map lhs isal
515 val sum = list2sum lhss
517 SOME ((UnparseC.term p) ^ " = " ^ (UnparseC.term sum),
518 HOLogic.Trueprop $ (TermC.mk_equality (p, sum)))
520 | eval_boollist2sum _ _ _ _ = NONE;
523 \<close> text \<open>
529 subsection \<open>extend rule-set for evaluating pre-conditions and program-expressions\<close>
531 val prog_expr = Rule_Set.append_rules "prog_expr" prog_expr [Rule.Eval ("Prog_Expr.rhs", Prog_Expr.eval_rhs "")];
536 subsection \<open>setup for rule-sets and ML-functions\<close>
537 setup \<open>KEStore_Elems.add_rlss [("prog_expr", (Context.theory_name @{theory}, prog_expr))]\<close>
538 setup \<open>KEStore_Elems.add_calcs
539 [("matches", ("Prog_Expr.matches", Prog_Expr.eval_matches "#matches_")),
540 ("matchsub", ("Prog_Expr.matchsub", Prog_Expr.eval_matchsub "#matchsub_")),
541 ("Vars", ("Prog_Expr.Vars", Prog_Expr.eval_var "#Vars_")),
542 ("lhs", ("Prog_Expr.lhs", Prog_Expr.eval_lhs "")),
543 ("rhs", ("Prog_Expr.rhs", Prog_Expr.eval_rhs ""))]\<close>
544 (*\\------------------------- from Tools .thy-------------------------------------------------//*)