1 (* elements of calculations.
2 they are partially held in association lists as ref's for
3 switching language levels (meta-string, object-values).
4 in order to keep these ref's during re-evaluation of code,
5 they are defined here at the beginning of the code.
6 Author: Walther Neuper 2003
7 (c) copyright due to lincense terms
14 val linefeed = (curry op^) "\n";
15 type authors = string list;
18 val empty_cterm' = "empty_cterm'";
20 type thmID = string; (* identifier for a thm (the shortest possible identifier) *)
21 type thmDeriv = string; (* WN120524 deprecated
22 thyID ^"."^ xxx ^"."^ thmID, see fun thmID_of_derivation_name
23 WN120524: dont use Thm.derivation_name, this is destroyed by num_str;
24 Thm.get_name_hint survives num_str and seems perfectly reliable *)
26 type thm' = thmID * cterm';(*WN060610 deprecated in favour of thm''*)
27 type thm'' = thmID * term;
30 (*.a 'guh'='globally unique handle' is a string unique for each element
31 of isac's KEStore and persistent over time
32 (in particular under shifts within the respective hierarchy);
34 # guh NOT resistant agains shifts from one thy to another
35 (which is the price for Isabelle's design: thy's overwrite ids of subthy's)
36 # requirement for matchTheory: induce guh from tac + current thy
37 (see 'fun thy_containing_thm', 'fun thy_containing_rls' etc.)
38 TODO: introduce to pbl, met.*)
40 val e_guh = "e_guh":guh;
44 (* eval function calling sml code during rewriting.
45 Unifying "type cal" and "type calc" would make Lucas-Interpretation more efficient,
46 see "fun rule2stac": instead of
47 Calc: calID * eval_fn -> rule
49 Calc: prog_calcID * (calID * eval_fn)) -> rule*)
50 type eval_fn = (string -> term -> theory -> (string * term) option);
51 fun e_evalfn (_:'a) (_:term) (_:theory) = NONE:(string * term) option;
52 (*. op in isa-term 'Const(op,_)' .*)
56 type cal = (calID * eval_fn);
57 (*. fun calculate_ fetches the evaluation-function via this list. *)
58 type prog_calcID = string;
59 type calc = (prog_calcID * cal);
61 prog_calcID * (* a simple identifier used in programs *)
62 (calID * (* a long identifier used in Const *)
63 eval_fn) (* an ML function *)
64 fun calc_eq ((pi1, (ci1, _)) : calc_elem, (pi2, (ci2, _)) : calc_elem) =
66 then if ci1 = ci2 then true else error ("calc_eq: " ^ ci1 ^ " <> " ^ ci2)
70 type subs' = (cterm' * cterm') list; (*16.11.00 for FE-KE*)
71 type subst = (term * term) list; (*here for ets2str*)
72 val e_subst = []:(term * term) list;
74 (*TODO.WN060610 make use of "type rew_ord" total*)
75 type rew_ord' = string;
76 val e_rew_ord' = "e_rew_ord" : rew_ord';
77 type rew_ord_ = subst -> Term.term * Term.term -> bool;
78 fun dummy_ord (_:subst) (_:term,_:term) = true;
79 val e_rew_ord_ = dummy_ord;
80 type rew_ord = rew_ord' * rew_ord_;
81 val e_rew_ord = dummy_ord; (* TODO.WN071231 clarify identifiers..e_rew_ordX*)
82 val e_rew_ordX = (e_rew_ord', e_rew_ord_) : rew_ord;
84 (* error patterns and fill patterns *)
85 type errpatID = string
87 errpatID (* one identifier for a list of patterns
88 DESIGN ?TODO: errpatID list for hierarchy of errpats ? *)
89 * term list (* error patterns *)
90 * thm list (* thms related to error patterns; note that respective lhs
91 do not match (which reflects student's error).
92 fillpatterns are stored with these thms. *)
94 (* for (at least) 2 kinds of access:
95 (1) given an errpatID, find the respective fillpats (e.g. in fun find_fill_pats)
96 (2) given a thm, find respective fillpats *)
97 type fillpatID = string
99 fillpatID (* DESIGN ?TODO: give an order w.r.t difficulty ? *)
100 * term (* the pattern with fill-in gaps *)
101 * errpatID; (* which the fillpat would be a help for
102 DESIGN ?TODO: list for several patterns ? *)
105 Erule (*.the empty rule .*)
106 | Thm of (string * Basic_Thm.thm)(*.a theorem, ie (identifier, Thm.thm).*)
107 | Calc of string * (*.sml-code manipulating a (sub)term .*)
108 (string -> term -> theory -> (string * term) option)
109 | Cal1 of string * (*.sml-code applied only to whole term
110 or left/right-hand-side of eqality .*)
111 (string -> term -> theory -> (string * term) option)
112 | Rls_ of rls (*.ie. rule sets may be nested.*)
115 | Prog of term (* for met *)
116 | Rfuns of (* for Rrls, usage see rational.sml ----- reverse rewrite ----- *)
117 {init_state : (* initialise for reverse rewriting by the Interpreter *)
118 term -> (* for this the rrlsstate is initialised: *)
119 term * (* the current formula: goes locate_gen -> next_tac via istate *)
120 term * (* the final formula *)
121 rule list (* of reverse rewrite set (#1#) *)
122 list * (* may be serveral, eg. in norm_rational *)
123 ( rule * (* Thm (+ Thm generated from Calc) resulting in ... *)
124 (term * (* ... rewrite with ... *)
125 term list)) (* ... assumptions *)
126 list, (* derivation from given term to normalform
127 in reverse order with sym_thm;
128 (#1#) could be extracted from here #1 *)
129 normal_form: (* the function which drives the Rrls ##############################*)
130 term -> (term * term list) option,
131 locate_rule: (* checks a rule R for being a cancel-rule, and if it is,
132 then return the list of rules (+ the terms they are rewriting to)
133 which need to be applied before R should be applied.
134 precondition: the rule is applicable to the argument-term. *)
135 rule list list -> (* the reverse rule list *)
136 term -> (* ... to which the rule shall be applied *)
137 rule -> (* ... to be applied to term *)
138 ( rule * (* value: a rule rewriting to ... *)
139 (term * (* ... the resulting term ... *)
140 term list)) (* ... with the assumptions ( //#0) *)
141 list, (* there may be several such rules; the list is empty,
142 if the rule has nothing to do with e.g. cancelation *)
143 next_rule: (* for a given term return the next rules to be done for cancelling *)
144 rule list list->(* the reverse rule list *)
145 term -> (* the term for which ... *)
146 rule option, (* ... this rule is appropriate for cancellation;
147 there may be no such rule (if the term is eg.canceled already*)
148 attach_form: (* checks an input term TI, if it may belong to e.g. a current
149 cancellation, by trying to derive it from the given term TG.
152 term -> (* TG, the last one agreed upon by user + math-eng *)
153 term -> (* TI, the next one input by the user *)
154 ( rule * (* the rule to be applied in order to reach TI *)
155 (term * (* ... obtained by applying the rule ... *)
156 term list)) (* ... and the respective assumptions *)
157 list} (* there may be several such rules; the list is empty, if the
158 users term does not belong to e.g. a cancellation of the term
161 Erls (*for init e_rls*)
163 | Rls of (*a confluent and terminating ruleset, in general *)
164 {id : string, (*for trace_rewrite:=true *)
165 preconds : term list, (*unused WN020820 *)
166 (*WN060616 for efficiency...
167 bdvs : false, (*set in prep_rls for get_bdvs *)*)
168 rew_ord : rew_ord, (*for rules*)
169 erls : rls, (*for the conditions in rules *)
170 srls : rls, (*for evaluation of list_fns in script *)
171 calc : calc list, (*for Calculate in scr, set by prep_rls *)
173 errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy*)
174 scr : scr} (*Prog term: generating intermed.steps *)
175 | Seq of (*a sequence of rules to be tried only once *)
176 {id : string, (*for trace_rewrite:=true *)
177 preconds : term list, (*unused 20.8.02 *)
178 (*WN060616 for efficiency...
179 bdvs : false, (*set in prep_rls for get_bdvs *)*)
180 rew_ord : rew_ord, (*for rules *)
181 erls : rls, (*for the conditions in rules *)
182 srls : rls, (*for evaluation of list_fns in script *)
183 calc : calc list, (*for Calculate in scr, set by prep_rls *)
185 errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy*)
186 scr : scr} (*Prog term (how to restrict type ???)*)
188 (*Rrls call SML-code and simulate an rls
189 difference: there is always _ONE_ redex rewritten in 1 call,
190 thus wrap Rrls by: Rls (Rls_ ...)*)
191 | Rrls of (* SML-functions within rewriting; step-wise execution provided;
193 difference: there is always _ONE_ redex rewritten in 1 call,
194 thus wrap Rrls by: Rls (Rls_ ...) *)
195 {id : string, (* for trace_rewrite := true *)
196 prepat : (term list *(* preconds, eval with subst from pattern;
197 if [@{term True}], match decides alone *)
198 term ) (* pattern matched with current (sub)term *)
199 list, (* meta-conjunction is or *)
200 rew_ord : rew_ord, (* for rules *)
201 erls : rls, (* for the conditions in rules and preconds *)
202 calc : calc list, (* for Calculate in scr, set automatic.in prep_rls *)
203 errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy*)
204 scr : scr}; (* Rfuns {...} (how to restrict type ???) *)
206 fun thy2ctxt' thy' = Proof_Context.init_global (Thy_Info.get_theory thy');(*FIXXXME thy-ctxt*)
207 fun thy2ctxt thy = Proof_Context.init_global thy;(*FIXXXME thy-ctxt*)
209 fun Isac _ = Proof_Context.theory_of (thy2ctxt' "Isac"); (*@{theory "Isac"}*)
211 val e_rule = Thm ("refl", @{thm refl});
212 fun id_of_thm (Thm (id, _)) = id
213 | id_of_thm _ = error "error id_of_thm";
214 fun thm_of_thm (Thm (_, thm)) = thm
215 | thm_of_thm _ = error "error thm_of_thm";
216 fun rep_thm_G' (Thm (thmid, thm)) = (thmid, thm);
218 fun thmID_of_derivation_name dn = last_elem (space_explode "." dn);
219 fun thyID_of_derivation_name dn = hd (space_explode "." dn);
221 fun eq_thmI ((thmid1 : thmID, _ : thm), (thmid2 : thmID, _ : thm)) =
222 (strip_thy thmid1) = (strip_thy thmid2);
223 (*WN120201 weakened*)
224 fun eq_thmI ((thmid1 : thmID, _ : thm), (thmid2 : thmID, _)) = thmid1 = thmid2;
225 (*version typed weaker WN100910*)
226 fun eq_thmI' ((thmid1, _), (thmid2, _)) =
227 (thmID_of_derivation_name thmid1) = (thmID_of_derivation_name thmid2);
229 val string_of_thm = Thm.get_name_hint; (*FIXME.2009*)
230 fun thm'_of_thm thm =
231 ((thmID_of_derivation_name o Thm.get_name_hint) thm, ""): thm'
233 (*check for [.] as caused by "fun assoc_thm'"*)
234 fun string_of_thmI thm =
235 let val ct' = (de_quote o string_of_thm) thm
236 val (a, b) = split_nlast (5, Symbol.explode ct')
238 [" ", " ","[", ".", "]"] => implode a
242 (*.id requested for all, Rls,Seq,Rrls.*)
243 fun id_rls Erls = "e_rls" (*WN060714 quick and dirty: recursive defs!*)
244 | id_rls (Rls {id,...}) = id
245 | id_rls (Seq {id,...}) = id
246 | id_rls (Rrls {id,...}) = id;
247 val rls2str = id_rls;
248 fun id_rule (Thm (id, _)) = id
249 | id_rule (Calc (id, _)) = id
250 | id_rule (Rls_ rls) = id_rls rls;
252 fun get_rules (Rls {rules,...}) = rules
253 | get_rules (Seq {rules,...}) = rules
254 | get_rules (Rrls _) = [];
256 fun rule2str Erule = "Erule"
257 | rule2str (Thm (str, thm)) = "Thm (\""^str^"\","^(string_of_thmI thm)^")"
258 | rule2str (Calc (str,f)) = "Calc (\""^str^"\",fn)"
259 | rule2str (Cal1 (str,f)) = "Cal1 (\""^str^"\",fn)"
260 | rule2str (Rls_ rls) = "Rls_ (\""^id_rls rls^"\")";
261 fun rule2str' Erule = "Erule"
262 | rule2str' (Thm (str, thm)) = "Thm (\""^str^"\",\"\")"
263 | rule2str' (Calc (str,f)) = "Calc (\""^str^"\",fn)"
264 | rule2str' (Cal1 (str,f)) = "Cal1 (\""^str^"\",fn)"
265 | rule2str' (Rls_ rls) = "Rls_ (\""^id_rls rls^"\")";
267 (*WN080102 compare eq_rule ?!?*)
268 fun eqrule (Thm (id1,_), Thm (id2,_)) = id1 = id2
269 | eqrule (Calc (id1,_), Calc (id2,_)) = id1 = id2
270 | eqrule (Cal1 (id1,_), Cal1 (id2,_)) = id1 = id2
271 | eqrule (Rls_ _, Rls_ _) = false (*{id=id1}{id=id2} = id1 = id2 FIXXME*)
274 type rrlsstate = (* state for reverse rewriting, comments see type rule and scr | Rfuns *)
275 (term * term * rule list list * (rule * (term * term list)) list);
277 val e_type = Type("empty",[]);
278 val a_type = TFree("'a",[]);
279 val e_term = Const("empty",e_type);
280 val a_term = Free("empty",a_type);
281 val e_rrlsstate = (e_term,e_term,[[e_rule]],[(e_rule,(e_term,[]))]):rrlsstate;
283 val e_term = Const("empty", Type("'a", []));
284 val e_scr = Prog e_term;
287 there are two kinds of theorems ...
288 (1) known by isabelle
289 (2) not known, eg. calc_thm, instantiated rls
290 the latter have a thmid "#..."
291 and thus outside isa we ALWAYS transport both (thmid,string_of_thmI)
292 and have a special assoc_thm / assoc_rls in this interface *)
293 type theory' = string; (* = domID ^".thy" WN.101011 ABOLISH !*)
294 type domID = string; (* domID ^".thy" = theory' WN.101011 replace by thyID*)
295 type thyID = string; (*WN.3.11.03 TODO: replace domID with thyID*)
297 fun string_of_thy thy = Context.theory_name thy: theory';
298 val theory2domID = string_of_thy;
299 val theory2thyID = (get_thy o string_of_thy) : theory -> thyID;
300 val theory2theory' = string_of_thy;
301 val theory2str = string_of_thy; (*WN050903 ..most consistent naming*)
302 val theory2str' = implode o (drop_last_n 4) o Symbol.explode o string_of_thy;
303 (* fun theory'2theory = fun thyID2thy ... see fun assoc_thy (...Thy_Info.get_theory string);
304 al it = "Isac" : string
307 fun thyID2theory' (thyID:thyID) = thyID;
309 let val ss = Symbol.explode thyID
310 val ext = implode (takelast (4, ss))
311 in if ext = ".thy" then thyID : theory' (*disarm abuse of thyID*)
315 (* thyID2theory' "Isac" (*ok*);
316 val it = "Isac" : theory'
317 > thyID2theory' "Isac" (*abuse, goes ok...*);
318 val it = "Isac" : theory'
321 fun theory'2thyID (theory':theory') = theory';
323 let val ss = Symbol.explode theory'
324 val ext = implode (takelast (4, ss))
325 in if ext = ".thy" then ((implode o (drop_last_n 4)) ss) : thyID
326 else theory' (*disarm abuse of theory'*)
329 (* theory'2thyID "Isac";
330 val it = "Isac" : thyID
331 > theory'2thyID "Isac";
332 val it = "Isac" : thyID*)
335 (*. WN0509 discussion:
336 #############################################################################
337 # How to manage theorys in subproblems wrt. the requirement, #
338 # that scripts should be re-usable ? #
339 #############################################################################
341 eg. 'Script Solve_rat_equation' calls 'SubProblem (RatEq',..'
342 which would not allow to 'solve (y'' = -M_b / EI, M_b)' by this script
343 because Biegelinie.thy is subthy of RatEq.thy and thus Biegelinie.M_b
344 is unknown in RatEq.thy and M_b cannot be parsed into the scripts guard
347 Preliminary solution:
348 # the thy in 'SubProblem (thy', pbl, arglist)' is not taken automatically,
349 # instead the 'maxthy (rootthy pt) thy' is taken for each subpbl
350 # however, a thy specified by the user in the rootpbl may lead to
351 errors in far-off subpbls (which are not yet reported properly !!!)
352 and interactively specifiying thys in subpbl is not very relevant.
354 Other solutions possible:
355 # always parse and type-check with Thy_Info.get_theory "Isac"
356 (rejected tue to the vague idea eg. to re-use equations for R in C etc.)
357 # regard the subthy-relation in specifying thys of subpbls
358 # specifically handle 'SubProblem (undefined, pbl, arglist)'
361 (*WN0509 TODO "ProtoPure" ... would be more consistent
362 with assoc_thy <--> theory2theory' +FIXME assoc_thy "e_domID" -> Script.thy*)
363 val e_domID = "e_domID":domID;
365 (*the key into the hierarchy ob theory elements*)
366 type theID = string list;
367 val e_theID = ["e_theID"];
368 val theID2str = strs2str;
369 (*theID eg. is ["IsacKnowledge", "Test", "Rulesets", "ac_plus_times"]*)
370 fun theID2thyID (theID:theID) =
371 if length theID >= 3 then (last_elem o (drop_last_n 2)) theID : thyID
372 else error ("theID2thyID called with "^ theID2str theID);
374 (*the key into the hierarchy ob problems*)
375 type pblID = string list; (* domID::...*)
376 val e_pblID = ["e_pblID"]:pblID;
377 val pblID2str = strs2str;
379 (*the key into the hierarchy ob methods*)
380 type metID = string list;
381 val e_metID = ["e_metID"]:metID;
382 val metID2str = strs2str;
384 (*either theID or pblID or metID*)
385 type kestoreID = string list;
386 val e_kestoreID = ["e_kestoreID"];
387 val kestoreID2str = strs2str;
389 (*for distinction of contexts WN130621: disambiguate with Isabelle's Context !*)
390 datatype ketype = Exp_ | Thy_ | Pbl_ | Met_;
391 fun ketype2str Exp_ = "Exp_"
392 | ketype2str Thy_ = "Thy_"
393 | ketype2str Pbl_ = "Pbl_"
394 | ketype2str Met_ = "Met_";
395 fun ketype2str' Exp_ = "Example"
396 | ketype2str' Thy_ = "Theory"
397 | ketype2str' Pbl_ = "Problem"
398 | ketype2str' Met_ = "Method";
400 (*see 'How to manage theorys in subproblems' at 'type thyID'*)
401 val theory' = Unsynchronized.ref ([]:(theory' * theory) list);
403 (* theories for html representation: Isabelle, Knowledge, ProgLang *)
404 val isabthys = Unsynchronized.ref ([] : theory list);
405 val knowthys = Unsynchronized.ref ([] : theory list);
406 val progthys = Unsynchronized.ref ([] : theory list);
408 (*rewrite orders, also stored in 'type met' and type 'and rls'
409 The association list is required for 'rewrite.."rew_ord"..'
410 WN0509 tests not well-organized: see smltest/Knowledge/termorder.sml*)
413 ([]:(rew_ord' * (*the key for the association list *)
414 (subst (*the bound variables - they get high order*)
415 -> (term * term) (*(t1, t2) to be compared *)
416 -> bool)) (*if t1 <= t2 then true else false *)
417 list); (*association list *)
419 rew_ord' := overwritel (!rew_ord', [("e_rew_ord", e_rew_ord),
420 ("dummy_ord", dummy_ord)]);
423 (*FIXME.040207 calclist': used by prep_rls, NOT in met*)
424 val calclist' = Unsynchronized.ref ([]: calc list);
426 (* A tree for storing data defined in different theories
427 for access from the Interpreter and from dialogue authoring
428 using a string list as key.
429 'a is for pbt | met | thydata; after WN030424 naming became inappropriate *)
431 Ptyp of string * (* element of the key *)
432 'a list * (* several pbts with different domIDs/thy TODO: select by subthy (isaref.p.69)
433 presently only _ONE_ elem FOR ALL KINDS OF CONTENT pbt | met | thydata *)
434 ('a ptyp) list;(* the children nodes *)
436 (*.datatype for collecting thydata for hierarchy.*)
437 (*WN060720 more consistent naming would be 'type thyelem' or 'thelem'*)
438 (*WN0606 Htxt contains html which does not belong to the sml-kernel*)
439 datatype thydata = Html of {guh: guh,
440 coursedesign: authors,
441 mathauthors: authors,
442 html: string} (*html; for demos before database*)
444 coursedesign: authors,
445 mathauthors: authors,
446 fillpats: fillpat list,
449 coursedesign: authors,
450 mathauthors: authors,
451 thy_rls: (thyID * rls)}
453 coursedesign: authors,
454 mathauthors: authors,
457 coursedesign: authors,
458 mathauthors: authors,
459 ord: (subst -> (term * term) -> bool)};
460 val e_thydata = Html {guh="e_guh", coursedesign=[], mathauthors=[], html=""};
462 type thehier = (thydata ptyp) list;
463 val thehier = Unsynchronized.ref ([] : thehier); (*WN101011 make argument, del*)
465 (* an association list, gets the value once in Isac.ML.
466 stores Isabelle's thms as terms for compatibility with Theory.axioms_of.
467 WN1-1-28 make this data arguments and del ref ?*)
468 val isab_thm_thy = Unsynchronized.ref ([] : (thmDeriv * term) list);
469 val isabthys = Unsynchronized.ref ([] : theory list);
471 val first_ProgLang_thy = Unsynchronized.ref (@{theory Pure});
472 val first_Knowledge_thy = Unsynchronized.ref (@{theory Pure});
476 type filename = string;
478 (*val xxx = fn: a b => (a,b); ??? fun-definition ???*)
480 fun ii (_:term) = e_rrlsstate;
481 fun no (_:term) = SOME (e_term,[e_term]);
482 fun lo (_:rule list list) (_:term) (_:rule) = [(e_rule,(e_term,[e_term]))];
483 fun ne (_:rule list list) (_:term) = SOME e_rule;
484 fun fo (_:rule list list) (_:term) (_:term) = [(e_rule,(e_term,[e_term]))];
486 val e_rfuns = Rfuns {init_state=ii,normal_form=no,locate_rule=lo,
487 next_rule=ne,attach_form=fo};
491 Rls {id = "e_rls", preconds = [], rew_ord = ("dummy_ord", dummy_ord), erls = Erls,
492 srls = Erls, calc = [], rules = [], errpatts = [], scr = EmptyScr}: rls;
494 Rrls {id = "e_rrls", prepat = [], rew_ord = ("dummy_ord", dummy_ord), erls = Erls,
495 calc = [], errpatts = [], scr=e_rfuns}:rls;
497 fun rep_rls (Rls {id,preconds,rew_ord,erls,srls,calc,errpatts,rules,scr}) =
498 {id=id,preconds=preconds,rew_ord=rew_ord,erls=erls,srls=srls,calc=calc,
499 (*asm_thm=asm_thm,*)rules=rules,scr=scr}
500 | rep_rls (Seq {id,preconds,rew_ord,erls,srls,calc,errpatts,rules,scr}) =
501 {id=id,preconds=preconds,rew_ord=rew_ord,erls=erls,srls=srls,calc=calc,
502 (*asm_thm=asm_thm,*)rules=rules,scr=scr}
503 | rep_rls Erls = rep_rls e_rls
504 | rep_rls (Rrls {id,...}) = rep_rls e_rls
505 (*error("rep_rls doesn't take apart reverse-rewrite-rule-sets: "^id)*);
506 (*| rep_rls (Seq {id,...}) =
507 error("rep_rls doesn't take apart reverse-rewrite-rule-sets: "^id);
509 fun rep_rrls (Rrls {id, calc, erls, prepat, rew_ord, errpatts,
510 scr = Rfuns {attach_form, init_state, locate_rule, next_rule, normal_form}}) =
511 {id=id, calc=calc, erls=erls, prepat=prepat,
512 rew_ord=rew_ord, errpatts=errpatts, attach_form=attach_form, init_state=init_state,
513 locate_rule=locate_rule, next_rule=next_rule, normal_form=normal_form}
514 | rep_rrls (Rls {id,...}) =
515 error ("rep_rrls doesn't take apart (normal) rule-sets: "^id)
516 | rep_rrls (Seq {id,...}) =
517 error ("rep_rrls doesn't take apart (normal) rule-sets: "^id);
519 fun append_rls id (Rls {id=_,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
520 rules =rs, errpatts=errpatts, scr=sc}) r =
521 (Rls{id=id,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
522 rules = rs @ r, errpatts=errpatts, scr=sc}:rls)
523 | append_rls id (Seq {id=_,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
524 rules =rs, errpatts=errpatts, scr=sc}) r =
525 (Seq{id=id,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
526 rules = rs @ r, errpatts=errpatts, scr=sc}:rls)
527 | append_rls id (Rrls _) _ =
528 error ("append_rls: not for reverse-rewrite-rule-set "^id);
530 (*. are _atomic_ rules equal ?.*)
531 (*WN080102 compare eqrule ?!?*)
532 fun eq_rule (Thm (thm1,_), Thm (thm2,_)) = thm1 = thm2
533 | eq_rule (Calc (id1,_), Calc (id2,_)) = id1 = id2
534 | eq_rule (Rls_ rls1, Rls_ rls2) = id_rls rls1 = id_rls rls2
535 (*id_rls checks for Rls, Seq, Rrls*)
538 fun merge_ids rls1 rls2 =
540 val id1 = (#id o rep_rls) rls1
541 val id2 = (#id o rep_rls) rls2
543 if id1 = id2 then id1
544 else "merged_" ^ id1 ^ "_" ^ id2
546 fun merge_rls _ Erls rls = rls
547 | merge_rls _ rls Erls = rls
548 | merge_rls _ (Rrls x) _ = Rrls x (* required for merging Theory_Data *)
549 | merge_rls _ _ (Rrls x) = Rrls x
551 (Rls {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
552 rules = rs1, errpatts = eps1, scr = sc1, ...})
553 (Rls {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
554 rules = rs2, errpatts = eps2, ...})
556 (Rls {id = id, rew_ord = ro1, scr = sc1,
557 preconds = union (op =) pc1 pc2,
558 erls = merge_rls (merge_ids er1 er2) er1 er2,
559 srls = merge_rls (merge_ids sr1 sr2) sr1 sr2,
560 calc = union calc_eq ca1 ca2,
561 rules = union eq_rule rs1 rs2,
562 errpatts = union (op =) eps1 eps2} : rls)
564 (Seq {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
565 rules = rs1, errpatts = eps1, scr = sc1, ...})
566 (Seq {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
567 rules = rs2, errpatts = eps2, ...})
569 (Seq {id = id, rew_ord = ro1, scr = sc1,
570 preconds = union (op =) pc1 pc2,
571 erls = merge_rls (merge_ids er1 er2) er1 er2,
572 srls = merge_rls (merge_ids sr1 sr2) sr1 sr2,
573 calc = union calc_eq ca1 ca2,
574 rules = union eq_rule rs1 rs2,
575 errpatts = union (op =) eps1 eps2} : rls)
576 | merge_rls id _ _ = error ("merge_rls: \"" ^ id ^
577 "\"; not for reverse-rewrite-rule-sets and not for mixed Rls -- Seq");
579 fun remove_rls id (Rls {id=_,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
580 rules=rs, errpatts=eps, scr=sc}) r =
581 (Rls{id=id,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
582 rules = gen_rems eq_rule (rs, r),
583 errpatts = eps(*gen_rems op= (eps, TODO)*),
585 | remove_rls id (Seq {id=_,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
586 rules =rs, errpatts=eps, scr=sc}) r =
587 (Seq{id=id,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
588 rules = gen_rems eq_rule (rs, r),
589 errpatts = eps(*gen_rems op= (eps, TODO)*),
591 | remove_rls id (Rrls _) _ = error
592 ("remove_rls: not for reverse-rewrite-rule-set "^id);
594 (* datastructure for KEStore_Elems, intermediate for thehier *)
596 (rls' * (* identifier unique within Isac *)
597 (theory' * (* just for assignment in thehier, not appropriate for parsing etc *)
598 rls)) (* ((#id o rep_rls) rls) = rls' by coding discipline *)
599 fun rls_eq ((id1, (_, _)), (id2, (_, _))) = id1 = id2
601 fun insert_merge_rls (re as (id, (thyID, r1)) : rlss_elem) ys =
602 case get_index (fn y => if curry rls_eq re y then SOME y else NONE) ys of
604 | SOME (i, (_, (_, r2))) =>
606 val r12 = merge_rls id r1 r2
607 in list_update ys i (id, (thyID, r12)) end
609 fun merge_rlss (s1, s2) = fold insert_merge_rls s1 s2;
612 fun memrls r (Rls {rules,...}) = gen_mem eqrule (r, rules)
613 | memrls r (Seq {rules,...}) = gen_mem eqrule (r, rules)
614 | memrls r _ = error ("memrls: incomplete impl. r= "^(rule2str r));
616 fun assoc' ([], key) = error ("ME_Isa: '"^key^"' not known")
617 | assoc' ((keyi, xi) :: pairs, key) =
618 if key = keyi then SOME xi else assoc' (pairs, key);
620 (*100818 fun assoc_thy (thy:theory') = ((the o assoc')(!theory',thy))
621 handle _ => error ("ME_Isa: thy '"^thy^"' not in system");*)
622 fun assoc_thy (thy:theory') =
623 if thy = "e_domID" then (Thy_Info.get_theory "Script") (*lower bound of Knowledge*)
624 else (Thy_Info.get_theory thy)
625 handle _ => error ("ME_Isa: thy '" ^ thy ^ "' not in system");
627 (*.overwrite an element in an association list and pair it with a thyID
628 in order to create the thy_hierarchy;
629 overwrites existing rls' even if they are defined in a different thy;
630 this is related to assoc_rls, TODO.WN060120: assoc_rew_ord, assoc_calc;.*)
631 (*WN060120 ...these are NOT compatible to "fun assoc_thm'" in that
632 they do NOT handle overlays by re-using an identifier in different thys;
633 "thyID.rlsID" would be a good solution, if the "." would be possible
635 actually a hack to get alltogether run again with minimal effort*)
636 fun insthy thy' (rls', rls) = (rls', (thy', rls));
637 fun overwritelthy thy (al, bl:(rls' * rls) list) =
638 let val bl' = map (insthy ((get_thy o theory2theory') thy)) bl
639 in overwritel (al, bl') end;
641 fun assoc_rew_ord ro = ((the o assoc') (!rew_ord',ro))
642 handle _ => error ("ME_Isa: rew_ord '"^ro^"' not in system");
643 (*get the string for stac from rule*)
645 fun term_to_string' ctxt t =
647 val ctxt' = Config.put show_markup false ctxt
648 in Print_Mode.setmp [] (Syntax.string_of_term ctxt') t end;
649 fun term_to_string'' (thyID : thyID) t =
651 val ctxt' = Config.put show_markup false (Proof_Context.init_global (Thy_Info.get_theory thyID))
652 in Print_Mode.setmp [] (Syntax.string_of_term ctxt') t end;
653 fun term_to_string''' thy t =
655 val ctxt' = Config.put show_markup false (Proof_Context.init_global thy)
656 in Print_Mode.setmp [] (Syntax.string_of_term ctxt') t end;
658 fun term_to_string t = term_to_string'' "Isac" t;
659 fun term2str t = term_to_string'' "Isac" t; (*legacy*)
660 fun terms2str ts = (strs2str o (map term2str )) ts;
661 (*terms2str [t1,t2] = "[\"1 + 2\",\"abc\"]";*)
662 fun terms2str' ts = (strs2str' o (map term2str )) ts;
663 (*terms2str' [t1,t2] = "[1 + 2,abc]";*)
664 fun terms2strs ts = (map term2str) ts;
665 (*terms2strs [t1,t2] = ["1 + 2", "abc"];*)
667 fun termopt2str (SOME t) = "SOME " ^ term2str t
668 | termopt2str NONE = "NONE";
670 fun type_to_string' ctxt t =
672 val ctxt' = Config.put show_markup false ctxt
673 in Print_Mode.setmp [] (Syntax.string_of_typ ctxt') t end;
674 fun type_to_string'' (thyID : thyID) t =
676 val ctxt' = Config.put show_markup false (Proof_Context.init_global (Thy_Info.get_theory thyID))
677 in Print_Mode.setmp [] (Syntax.string_of_typ ctxt') t end;
678 fun type_to_string''' thy t =
680 val ctxt' = Config.put show_markup false (Proof_Context.init_global thy)
681 in Print_Mode.setmp [] (Syntax.string_of_typ ctxt') t end;
683 fun type2str typ = type_to_string'' "Isac" typ; (*legacy*)
684 val string_of_typ = type2str; (*legacy*)
685 fun string_of_typ_thy thy typ = type_to_string'' thy typ; (*legacy*)
687 fun subst2str (s:subst) =
689 (map (linefeed o pair2str o
691 (apfst term2str)))) s;
692 fun subst2str' (s:subst) =
696 (apfst term2str)))) s;
697 (*> subst2str' [(str2term "bdv", str2term "x"),
698 (str2term "bdv_2", str2term "y")];
699 val it = "[(bdv, x)]" : string
701 val env2str = subst2str;
705 fun scr2str (Prog s) = "Prog " ^ term2str s
706 | scr2str (Rfuns _) = "Rfuns";
709 fun maxthy thy1 thy2 = if Theory.subthy (thy1, thy2) then thy2 else thy1;
712 (*.trace internal steps of isac's rewriter*)
713 val trace_rewrite = Unsynchronized.ref false;
714 (*.depth of recursion in traces of the rewriter, if trace_rewrite:=true.*)
715 val depth = Unsynchronized.ref 99999;
716 (*.no of rewrites exceeding this int -> NO rewrite.*)
717 (*WN060829 still unused...*)
718 val lim_rewrite = Unsynchronized.ref 99999;
719 (*.no of derivation-elements exceeding this int -> SOME derivation-elements.*)
720 val lim_deriv = Unsynchronized.ref 100;
721 (*.switch for checking guhs unique before storing a pbl or met;
722 set true at startup (done at begin of ROOT.ML)
723 set false for editing IsacKnowledge (done at end of ROOT.ML).*)
724 val check_guhs_unique = Unsynchronized.ref true;
727 datatype lrd = (*elements of a path (=loc_) into an Isabelle term*)
729 | R (*go right at $*)
730 | D; (*go down at Abs*)
731 type loc_ = lrd list;
735 fun loc_2str (k:loc_) = (strs2str' o (map ldr2str)) k;