1 (* Types and tools for 'modeling' und 'specifying' to be used in
2 modspec.sml. The types are separated from calchead.sml into this file,
3 because some of them are stored in the calc-tree, and thus are required
5 author: Walther Neuper, Mathias Lehnfeld
6 (c) due to copyright terms
8 12345678901234567890123456789012345678901234567890123456789012345678901234567890
9 10 20 30 40 50 60 70 80
12 signature SPECIFY_TOOLS =
24 val item2str : item -> string
26 val itm2str_ : Proof.context -> itm -> string
29 Cor of (term * term list) * (term * term list)
30 | Inc of (term * term list) * (term * term list)
33 | Sup of term * term list
36 val itm_2str : itm_ -> string
37 val itm_2str_ : Proof.context -> itm_ -> string
38 val itms2str_ : Proof.context -> itm list -> string
41 {Find: string list, With: string list, Given: string list,
42 Where: string list, Relate: string list} -> string
45 Matches of pblID * item ppc
46 | NoMatch of pblID * item ppc
47 val match2str : match -> string
50 Match_ of pblID * (itm list * (bool * term) list)
52 val matchs2str : match list -> string
54 val ori2str : ori -> string
55 val oris2str : ori list -> string
57 val preori2str : preori -> string
58 val preoris2str : preori list -> string
60 (* val penv2str_ : Proof.context -> penv -> string *)
62 (*----------------------------------------------------------------------*)
63 val all_ts_in : itm_ list -> term list
67 term list -> itm list -> (bool * term) list
71 itm list -> 'a -> (bool * term) list
72 (* val chkpre2item : rls -> term -> bool * item *)
73 val pres2str : (bool * term) list -> string
74 (* val evalprecond : rls -> term -> bool * term *)
75 (* val cnt : itm list -> int -> int * int *)
76 val comp_dts : term * term list -> term
77 val comp_dts' : term * term list -> term
78 val comp_dts'' : term * term list -> string
79 val comp_ts : term * term list -> term
80 val d_in : itm_ -> term
81 val de_item : item -> cterm'
82 val declare_constraints : string -> Proof.context -> Proof.context
83 val declare_constraints' : term list -> Proof.context -> Proof.context
84 val dest_list : term * term list -> term list (* for testing *)
85 val dest_list' : term -> term list
86 val dts2str : term * term list -> string
88 (* val e_listBool : term *)
89 (* val e_listReal : term *)
92 val empty_ppc : item ppc
93 (* val empty_ppc_ct' : cterm' ppc *)
94 (* val getval : term * term list -> term * term *)
100 itm list -> 'b -> term * (bool * term) list*)
101 (* val init_item : string -> item *)
102 (* val is_matches : match -> bool *)
103 (* val is_matches_ : match_ -> bool *)
104 val is_var : term -> bool
106 string ppc -> item ppc *)
107 val itemppc2str : item ppc -> string
108 (* val matches_pblID : match -> pblID *)
109 val max2 : ('a * int) list -> 'a * int
110 val max_vt : itm list -> int
111 val mk_e : itm_ -> (term * term) list
112 val mk_en : int -> itm -> (term * term) list
113 val mk_env : itm list -> (term * term) list
114 val mkval : 'a -> term list -> term
115 val mkval' : term list -> term
116 (* val pblID_of_match : match -> pblID *)
117 val pbl_ids : Proof.context -> term -> term -> term list
118 val pbl_ids' : term -> term list -> term list
119 (* val pen2str : theory -> term * term list -> string *)
120 val penvval_in : itm_ -> term list
121 val refined : match list -> pblID
123 match_ list -> match_ option
124 (* val refined_IDitms :
125 match list -> match option *)
126 val split_dts : term -> term * term list
127 val split_dts' : term * term -> term list
128 (* val take_apart : term -> term list *)
129 (* val take_apart_inv : term list -> term *)
130 val ts_in : itm_ -> term list
131 (* val unique : term *)
132 val untouched : itm list -> bool
135 (''a * (''b * term list) list) list ->
137 ''b * term -> ''a -> ''a * (''b * term list) list
142 term -> term -> term -> envv
145 (''a * term list) list ->
146 term -> ''a * term -> (''a * term list) list
150 (vats * term * term * term) list ->
152 val vts_cnt : int list -> itm list -> (int * int) list
153 val vts_in : itm list -> int list
154 (* val w_itms2str_ : Proof.context -> itm list -> unit *)
155 val get_assumptions : Proof.context -> term list
156 val insert_assumptions : term list -> Proof.context -> Proof.context
157 val transfer_asms_from_to : Proof.context -> Proof.context -> Proof.context
158 val from_subpbl_to_caller : Proof.context -> term -> Proof.context -> Proof.context
162 (*----------------------------------------------------------*)
163 structure SpecifyTools : SPECIFY_TOOLS =
165 (*----------------------------------------------------------*)
166 val script_parse = the o (@{theory Script} |> thy2ctxt |> parseNEW);
167 val e_listReal = script_parse "[]::(real list)";
168 val e_listBool = script_parse "[]::(bool list)";
170 (*.take list-term apart w.r.t. handling elementwise input.*)
172 let val elems = isalist2list t
173 in map ((list2isalist (type_of (hd elems))) o single) elems end;
174 (*val t = str2term "[a, b]";
175 > val ts = take_apart t; tracing (terms2str ts);
178 > t = (take_apart_inv o take_apart) t;
180 fun take_apart_inv ts =
181 let val elems = (flat o (map isalist2list)) ts;
182 in list2isalist (type_of (hd elems)) elems end;
183 (*val ts = [str2term "[a]", str2term "[b]"];
184 > val t = take_apart_inv ts; term2str t;
187 ts = (take_apart o take_apart_inv) ts;
193 (*.revert split_dts only for ts; compare comp_dts.*)
194 fun comp_ts (d, ts) =
196 then if is_list (hd ts)
198 then (hd ts) (*e.g. someList [1,3,2]*)
199 else (take_apart_inv ts)
200 (* SML[ [a], [b] ]SML --> [a,b] *)
201 else (hd ts) (*a variable or metavariable for a list*)
204 WN050903 we do NOT know which is from subtheory, description or term;
205 typecheck thus may lead to TYPE-error 'unknown constant';
206 solution: typecheck with (Thy_Info.get_theory "Isac"); i.e. arg 'thy' superfluous*)
207 (*fun comp_dts thy (d,[]) =
208 Thm.global_cterm_of (*(sign_of o assoc_thy) "Isac"*)
209 (Thy_Info.get_theory "Isac")
210 (*comp_dts:FIXXME stay with term for efficiency !!!*)
211 (if is_reall_dsc d then (d $ e_listReal)
212 else if is_booll_dsc d then (d $ e_listBool)
215 (Thm.global_cterm_of (*(sign_of o assoc_thy) "Isac"*)
216 (Thy_Info.get_theory "Isac")
217 (*comp_dts:FIXXME stay with term for efficiency !!*)
218 (d $ (comp_ts (d, ts)))
219 handle _ => error ("comp_dts: "^(term2str d)^
220 " $ "^(term2str (hd ts))));*)
221 fun comp_dts (d,[]) =
222 (if is_reall_dsc d then (d $ e_listReal)
223 else if is_booll_dsc d then (d $ e_listBool)
226 (d $ (comp_ts (d, ts)))
227 handle _ => error ("comp_dts: "^(term2str d)^
228 " $ "^(term2str (hd ts)));
230 fun comp_dts' (d,[]) =
231 if is_reall_dsc d then (d $ e_listReal)
232 else if is_booll_dsc d then (d $ e_listBool)
234 | comp_dts' (d,ts) = (d $ (comp_ts (d, ts)))
235 handle _ => error ("comp_dts': "^(term2str d)^
236 " $ "^(term2str (hd ts)));
237 (*val t = str2term "maximum A";
238 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
239 val it = "maximum A" : cterm
240 > val t = str2term "fixedValues [r=Arbfix]";
241 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
242 "fixedValues [r = Arbfix]"
243 > val t = str2term "valuesFor [a]";
244 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
246 > val t = str2term "valuesFor [a,b]";
247 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
249 > val t = str2term "relations [A=a*b, (a/2)^^^2 + (b/2)^^^2 = r^^^2]";
250 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
251 relations [A = a * b, (a / 2) ^^^ 2 + (b / 2) ^^^ 2 = r ^^^ 2]"
252 > val t = str2term "boundVariable a";
253 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
255 > val t = str2term "interval {x::real. 0 <= x & x <= 2*r}";
256 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
257 "interval {x. 0 <= x & x <= 2 * r}"
259 > val t = str2term "equality (sqrt(9+4*x)=sqrt x + sqrt(5+x))";
260 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
261 "equality (sqrt (9 + 4 * x) = sqrt x + sqrt (5 + x))"
262 > val t = str2term "solveFor x";
263 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
265 > val t = str2term "errorBound (eps=0)";
266 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
267 "errorBound (eps = 0)"
268 > val t = str2term "solutions L";
269 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
273 > val t = (term_of o the o (parse thy)) "testdscforlist [#1]";
274 > val (d,ts) = split_dts t;
275 > comp_dts thy (d,ts);
276 val it = "testdscforlist [#1]" : cterm
278 > val t = (term_of o the o (parse thy)) "(A::real)";
279 > val (d,ts) = split_dts t;
280 val d = Const ("empty","empty") : term
281 val ts = [Free ("A","RealDef.real")] : term list
282 > val t = (term_of o the o (parse thy)) "[R=(R::real)]";
283 > val (d,ts) = split_dts t;
284 val d = Const ("empty","empty") : term
285 val ts = [Const # $ Free # $ Free (#,#)] : term list
286 > val t = (term_of o the o (parse thy)) "[#1,#2]";
287 > val (d,ts) = split_dts t;
288 val ts = [Free ("#1","'a"),Free ("#2","'a")] : NOT WANTED
291 (*for input_icalhd 11.03*)
292 fun comp_dts'' (d,[]) =
293 if is_reall_dsc d then term2str (d $ e_listReal)
294 else if is_booll_dsc d then term2str (d $ e_listBool)
296 | comp_dts'' (d,ts) = term2str (d $ (comp_ts (d, ts)))
297 handle _ => error ("comp_dts'': "^(term2str d)^
298 " $ "^(term2str (hd ts)));
305 (* this may decompose an object-language isa-list;
306 use only, if description is not available, eg. not input ?WN:14.5.03 ??!?*)
307 fun dest_list' t = if is_list t then isalist2list t else [t];
309 (*fun is_metavar (Free (str, _)) =
310 if (last_elem o Symbol.explode) str = "_" then true else false
311 | is_metavar _ = false;*)
312 fun is_var (Free _) = true
315 (*.special handling for lists. ?WN:14.5.03 ??!?*)
316 fun dest_list (d,ts) =
318 if is_list_dsc d andalso not (is_unl d)
319 andalso not (is_var t) (*..for pbt*)
320 then isalist2list t else [t]
321 in (flat o (map dest)) ts end;
324 (*.decompose an input into description, terms (ev. elems of lists),
325 and the value for the problem-environment; inv to comp_dts .*)
326 (*WN.8.6.03: corrected with minimal effort,
327 fn : theory -> term ->
329 term list * lists decomposed for elementwise input
330 term list pbl_ids not _HERE_: dont know which list-elems input*)
331 fun split_dts (t as d $ arg) =
333 if is_list_dsc d andalso is_list arg andalso is_unl d |> not then
334 (d, take_apart arg) else
336 (e_term, dest_list' t)
338 let val t' as (h, _) = strip_comb t;
340 (h, dest_list t') else
341 (e_term, dest_list' t)
344 (* tests see fun comp_dts
346 > val t = str2term "someList []";
347 > val (_,ts) = split_dts thy t; tracing (terms2str ts);
349 > val t = str2term "valuesFor []";
350 > val (_,ts) = split_dts thy t; tracing (terms2str ts);
353 (*.version returning ts only.*)
354 fun split_dts' (d, arg) =
356 then if is_list_dsc d
359 then ([arg]) (*e.g. someList [1,3,2]*)
360 else (take_apart arg)(*[a,b] --> SML[ [a], [b] ]SML*)
361 else ([arg]) (*a variable or metavariable for a list*)
363 else (dest_list' arg(*9.01 ???*))
364 | split_dts' (d, t) = (*either dsc or term; 14.5.03 only copied*)
365 let val (h,argl) = strip_comb t
366 in if (not o is_dsc) h then (dest_list' t)
367 else (dest_list (h,argl))
374 (*27.8.01: problem-environment
375 WN.6.5.03: FIXXME reconsider if penv is worth the effort --
376 -- just rerun a whole expl with num/var may show the same ?!
377 WN.9.5.03: penv-concept stalled, immediately generate script env !
378 but [#0, epsilon] only outcommented for eventual reconsideration
380 type penv = (term (*err_*)
381 * (term list) (*[#0, epsilon] 9.5.03 outcommented*)
383 fun pen2str ctxt (t, ts) =
384 pair2str (term_to_string' ctxt t,
385 (strs2str' o map (term_to_string' ctxt)) ts);
386 fun penv2str_ thy (penv:penv) = (strs2str' o (map (pen2str thy))) penv;
389 9.5.03: still unused, but left for eventual future development*)
390 type envv = (int * penv) list; (*over variants*)
392 (*. 14.9.01: not used after putting penv-values into itm_
393 make the result of split_* a value of problem-environment .*)
394 fun mkval dsc [] = error "mkval called with []"
396 | mkval dsc ts = list2isalist ((type_of o hd) ts) ts;
398 fun mkval' x = mkval e_term x;
402 (*. get the constant value from a penv .*)
403 fun getval (id, values) =
405 [] => error ("penv_value: no values in '" ^ term2str id)
407 | (v1::v2::_) => (case v1 of
408 Const ("Script.Arbfix",_) => (id, v2)
411 val e_ = (term_of o the o (parse thy)) "e_::bool";
412 val ev = (term_of o the o (parse thy)) "#4 + #3 * x^^^#2 = #0";
413 val v_ = (term_of o the o (parse thy)) "v_";
414 val vv = (term_of o the o (parse thy)) "x";
415 val r_ = (term_of o the o (parse thy)) "err_::bool";
416 val rv1 = (term_of o the o (parse thy)) "#0";
417 val rv2 = (term_of o the o (parse thy)) "eps";
419 val penv = [(e_,[ev]),(v_,[vv]),(r_,[rv2,rv2])]:penv;
421 [(Free ("e_","bool"),
422 Const (#,#) $ (# $ # $ (# $ #)) $ Free ("#0","RealDef.real")),
423 (Free ("v_","RealDef.real"),Free ("x","RealDef.real")),
424 (Free ("err_","bool"),Free ("#0","RealDef.real"))] : (term * term) list
428 (*==========================================================================
429 23.3.02 TODO: ideas on redesign of type itm_,type item,type ori,type item ppc
431 (1.1) untouched: for modeling only dsc displayed(impossible after match_itms)
432 =(presently) Mis (? should be Inc initially, and Mis after match_itms?)
433 (1.2) Syn,Typ,Sup: not related to oris
434 Syn, Typ (presently) should be accepted in appl_add (instead Error')
435 Sup (presently) should be accepted in appl_add (instead Error')
436 _could_ be w.r.t current vat (and then _is_ related to vat
437 Mis should _not_ be made Inc ((presently, by appl_add & match_itms)
438 - dsc in itm_ is timeconsuming -- keep id for respective queries ?
439 - order of items in ppc should be stable w.r.t order of itms
441 - stepwise input of itms --- match_itms (in one go) ..not coordinated
443 - match_itms / match_itms_oris ..2 versions ?!
444 (fast, for refine / slow, for modeling)
446 - clarify: efficiency <--> simplicity !!!
447 ?: shift dsc itm_ -> itm | discard int in ori,itm | take int instead dsc
448 | take int for perserving order of item ppc in itms
449 | make all(!?) handling of itms stable against reordering(?)
450 | field in ori ?? (not from fmz!) -- meant for efficiency (not doc!???)
451 -"- "#undef" ?= not touched ?= (id,..)
452 -----------------------------------------------------------------
454 def: type pbt = (field, (dsc, pid)) *** design considerations ***
456 (1) fmz + pbt -> oris
457 (2) input + oris -> itm
458 (3) match_itms : schnell(?) f"ur refine
459 match_itms_oris : r"uckmeldung f"ur item ppc
461 (1.1) in oris fehlt daher pid: (i,v,f,d,ts,pid)
462 ---------- ^^^^^ --- dh. pbt meist als argument zu viel !!!
464 (3.1) abwarten, wie das matchen mehr unterschiedlicher pbt's sich macht;
465 wenn Problem pbt v"ollig neue, dann w"are eigentlich n"otig ????:
466 (a) (_,_,d1,ts,_):ori + pbt -> (i,vt,d2,ts,pid) dh.vt neu ????
468 ==========================================================================*)
470 (*the internal representation of a models' item
472 4.9.01: not consistent:
473 after Init_Proof 'Inc', but after copy_probl 'Mis' - for same situation
474 (involves 'is_error');
475 bool in itm really necessary ???*)
477 Cor of (term * (* description *)
478 (term list)) * (* for list: elem-wise input *)
479 (*split_dts <-> comp_dts*)
480 (term * (term list)) (* elem of penv *)
481 (*9.5.03: ---- is already for script -- penv delayed to future*)
484 | Inc of (term * (term list)) * (term * (term list)) (*lists,
485 + init_pbl WN.11.03 FIXXME: empty penv .. bad
486 init_pbl should return Mis !!!*)
487 | Sup of (term * (term list)) (* user-input not found in pbt(+?oris?11.03)*)
488 | Mis of (term * term) (* after re-specification pbt-item not found
489 in pbl: only dsc, pid_*)
490 | Par of cterm'; (*internal state from fun parsitm*)
492 type vats = int list; (*variants in formalizations*)
494 (*.data-type for working on pbl/met-ppc:
495 in pbl initially holds descriptions (only) for user guidance.*)
497 int * (* id =0 .. untouched - descript (only) from init
498 23.3.02: seems to correspond to ori (fun insert_ppc)
499 <> maintain order in item ppc?*)
500 vats * (* variants - copy from ori *)
501 bool * (* input on this item is not/complete *)
502 string * (* #Given | #Find | #Relate *)
504 (* use"ME/sequent.sml";
506 val e_itm = (0,[],false,"e_itm",Syn"e_itm"):itm;
507 (*in CalcTree/Subproblem an 'untouched' model is created
508 FIXME.WN.9.03 model should be filled to 'untouched' by Model/Refine_Problem*)
509 fun untouched (itms: itm list) =
510 foldl and_ (true ,map ((curry op= 0) o #1) itms);
515 > untouched [e_itm, (1,[],false,"e_itm",Syn "e_itm")];
516 val it = false : bool*)
522 (* find most frequent variant v in itms *)
524 fun vts_in itms = (distinct o flat o (map #2)) (itms:itm list);
526 fun cnt itms v = (v,(length o (filter (curry op= v)) o
527 flat o (map #2)) (itms:itm list));
528 fun vts_cnt vts itms = map (cnt itms) vts;
529 fun max2 [] = error "max2 of []"
531 let fun mx (a,x) [] = (a,x)
532 | mx (a,x) ((b,y)::ys) =
533 if x < y then mx (b,y) ys else mx (a,x) ys;
536 (*. find the variant with most items already input .*)
538 let val vts = (vts_cnt (vts_in itms)) itms;
539 in if vts = [] then 0 else (fst o max2) vts end;
542 (* TODO ev. make more efficient by avoiding flat *)
543 fun mk_e (Cor (_, iv)) = [getval iv]
546 | mk_e (Inc (_, iv)) = [getval iv]
549 fun mk_en vt ((i,vts,b,f,itm_):itm) =
550 if member op = vts vt then mk_e itm_ else [];
551 (*. extract the environment from an item list;
552 takes the variant with most items .*)
554 let val vt = max_vt itms
555 in (flat o (map (mk_en vt))) itms end;
559 (*. example as provided by an author, complete w.r.t. pbt specified
560 not touched by any user action .*)
561 type ori = (int * (* id: 10.3.00ff impl. only <>0 .. touched
562 21.3.02: insert_ppc needs it ! ?:purpose maintain
563 order in item ppc ???*)
564 vats * (* variants 21.3.02: related to pbt..discard ?*)
565 string * (* #Given | #Find | #Relate 21.3.02: discard ?*)
566 term * (* description *)
567 term list (* isalist2list t | [t] *)
569 val e_ori_ = (0,[],"",e_term,[e_term]):ori;
570 val e_ori = (0,[],"",e_term,[e_term]):ori;
572 fun ori2str ((i,vs,fi,t,ts):ori) =
573 "("^(string_of_int i)^", "^((strs2str o (map string_of_int)) vs)^", "^fi^","^
574 (term2str t)^", "^((strs2str o (map term2str)) ts)^")";
576 let (*val s = !show_types
577 val _ = show_types:= true*)
578 val str = (strs2str' o (map (linefeed o ori2str)))
579 (*val _ = show_types:= s*)
582 (*.an or without leading integer.*)
583 type preori = (vats *
587 fun preori2str ((vs,fi,t,ts):preori) =
588 "("^((strs2str o (map string_of_int)) vs)^", "^fi^", "^
589 (term2str t)^", "^((strs2str o (map term2str)) ts)^")";
590 val preoris2str = (strs2str' o (map (linefeed o preori2str)));
592 (*. given the input value (from split_dts)
593 make the value in a problem-env according to description-type .*)
594 (*28.8.01: .nam and .una impl. properly, others copied .. TODO*)
595 fun pbl_ids ctxt (Const(_,Type("fun",[_,Type("Tools.nam",_)]))) v =
597 then [v] (*eg. [r=Arbfix]*)
598 else (case v of (*eg. eps=#0*)
599 (Const ("HOL.eq",_) $ l $ r) => [r,l]
600 | _ => error ("pbl_ids Tools.nam: no equality " ^ term_to_string' ctxt v))
601 | pbl_ids ctxt (Const(_,Type("fun",[_,Type("Tools.una",_)]))) v = [v]
602 | pbl_ids ctxt (Const(_,Type("fun",[_,Type("Tools.unl",_)]))) v = [v]
603 | pbl_ids ctxt (Const(_,Type("fun",[_,Type("Tools.str",_)]))) v = [v]
604 | pbl_ids ctxt (Const(_,Type("fun",[_,Type("Tools.toreal",_)]))) v = [v]
605 | pbl_ids ctxt (Const(_,Type("fun",[_,Type("Tools.toreall",_)])))v = [v]
606 | pbl_ids ctxt (Const(_,Type("fun",[_,Type("Tools.unknown",_)])))v = [v]
608 error ("pbl_ids: not implemented for " ^ term2str v);
610 val t as t1 $ t2 = str2term "antiDerivativeName M_b";
613 val t = (term_of o the o (parse thy)) "fixedValues [r=Arbfix]";
614 val (d,argl) = strip_comb t;
615 is_dsc d; (*see split_dts*)
620 [Const ("List.list.Cons","[bool, bool List.list] => bool List.list") $
621 (Const # $ Free # $ Const (#,#)) $ Const ("List.list.Nil","bool List..
623 val (dsc,vl) = (split_dts o term_of o the o (parse thy)) "solveFor x";
624 val dsc = Const ("Descript.solveFor","RealDef.real => Tools.una") : term
625 val vl = Free ("x","RealDef.real") : term
627 val (dsc,id) = (split_did o term_of o the o (parse thy)) "solveFor v_";
629 val it = [Free ("x","RealDef.real")] : term list
631 val (dsc,vl) = (split_dts o term_of o the o(parse thy))
632 "errorBound (eps=#0)";
633 val (dsc,id) = (split_did o term_of o the o(parse thy)) "errorBound err_";
635 val it = [Free ("#0","RealDef.real"),Free ("eps","RealDef.real")] : term list *)
637 (*. given an already input itm, ((14.9.01: no difference to pbl_ids jet!!))
638 make the value in a problem-env according to description-type .*)
639 (*28.8.01: .nam and .una impl. properly, others copied .. TODO*)
640 fun pbl_ids' (Const(_,Type("fun",[_,Type("Tools.nam",_)]))) vs =
642 [] => error ("pbl_ids' Tools.nam called with []")
643 | [t] => (case t of (*eg. eps=#0*)
644 (Const ("HOL.eq",_) $ l $ r) => [r,l]
646 error ("pbl_ids' Tools.nam: no equality " ^ term2str t))
647 | vs' => vs (*14.9.01: ???TODO *))
648 | pbl_ids' (Const(_,Type("fun",[_,Type("Tools.una",_)]))) vs = vs
649 | pbl_ids' (Const(_,Type("fun",[_,Type("Tools.unl",_)]))) vs = vs
650 | pbl_ids' (Const(_,Type("fun",[_,Type("Tools.str",_)]))) vs = vs
651 | pbl_ids' (Const(_,Type("fun",[_,Type("Tools.toreal",_)]))) vs = vs
652 | pbl_ids' (Const(_,Type("fun",[_,Type("Tools.toreall",_)])))vs = vs
653 | pbl_ids' (Const(_,Type("fun",[_,Type("Tools.unknown",_)])))vs = vs
655 error ("pbl_ids': not implemented for " ^ terms2str vs);
656 (*9.5.03 penv postponed: pbl_ids'*)
657 fun pbl_ids' d vs = [comp_ts (d, vs)];
660 (*14.9.01: not used after putting values for penv into itm_
661 WN.5.5.03: used in upd .. upd_envv*)
662 fun upd_penv ctxt penv dsc (id, vl) =
663 (tracing"### upd_penv used !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
664 tracing"### upd_penv used !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
665 tracing"### upd_penv used !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
666 overwrite (penv, (id, pbl_ids ctxt dsc vl))
670 val (dsc,vl) = (split_did o term_of o the o (parse thy)) "solveFor x";
671 val (dsc,id) = (split_did o term_of o the o (parse thy)) "solveFor v_";
672 val penv = upd_penv thy penv dsc (id, vl);
673 [(Free ("v_","RealDef.real"),
674 [Const (#,#) $ Free (#,#) $ Free ("#0","RealDef.real")])]
675 : (term * term list) list
677 val (dsc,vl) = (split_did o term_of o the o(parse thy))"errorBound (eps=#0)";
678 val (dsc,id) = (split_did o term_of o the o(parse thy))"errorBound err_";
679 upd_penv thy penv dsc (id, vl);
680 [(Free ("v_","RealDef.real"),
681 [Const (#,#) $ Free (#,#) $ Free ("#0","RealDef.real")]),
682 (Free ("err_","bool"),
683 [Free ("#0","RealDef.real"),Free ("eps","RealDef.real")])]
684 : (term * term list) list ^.........!!!!
687 (*WN.9.5.03: not reconsidered; looks strange !!!*)
688 fun upd thy envv dsc (id, vl) i =
689 let val penv = case assoc (envv, i) of
692 val penv' = upd_penv thy penv dsc (id, vl);
696 val envv = [(1,[]:penv),(2,[]:penv),(3,[]:penv)]:envv;
697 val (dsc,vl) = (split_did o term_of o the o(parse thy))"boundVariable b";
698 val (dsc,id) = (split_did o term_of o the o(parse thy))"boundVariable v_";
699 upd thy envv dsc (id, vl) i;
700 val it = (2,[(Free ("v_","RealDef.real"),[Free ("b","RealDef.real")])])
701 : int * (term * term list) list*)
704 (*14.9.01: not used after putting pre-penv into itm_*)
705 fun upd_envv thy (envv:envv) (vats:vats) dsc id vl =
706 let val vats = if length vats = 0
707 then (*unknown id to _all_ variants*)
708 if length envv = 0 then [1]
709 else (intsto o length) envv
711 fun isin vats (i,_) = member op = vats i;
712 val envs_notin_vat = filter_out (isin vats) envv;
713 in ((map (upd thy envv dsc (id, vl)) vats) @ envs_notin_vat):envv end;
715 val envv = [(1,[]:penv),(2,[]:penv),(3,[]:penv)]:envv;
718 val (dsc,vl) = (split_did o term_of o the o(parse thy))"boundVariable b";
719 val (dsc,id) = (split_did o term_of o the o(parse thy))"boundVariable v_";
720 val envv = upd_envv thy envv vats dsc id vl;
721 val envv = [(2,[(Free ("v_","RealDef.real"),[Free ("b","RealDef.real")])])]
722 : (int * (term * term list) list) list
725 val (dsc,vl) = (split_did o term_of o the o(parse thy))"maximum A";
726 val (dsc,id) = (split_did o term_of o the o(parse thy))"maximum m_";
727 upd_envv thy envv vats dsc id vl;
728 [(1,[(Free ("m_","bool"),[Free ("A","bool")])]),
730 [(Free ("v_","RealDef.real"),[Free ("b","RealDef.real")]),
731 (Free ("m_","bool"),[Free ("A","bool")])]),
732 (3,[(Free ("m_","bool"),[Free ("A","bool")])])]
733 : (int * (term * term list) list) list
737 val (d,ts) = (split_dts o term_of o the o (parse thy))
738 "fixedValues [r=Arbfix]";
739 val (_,id) = (split_did o term_of o the o (parse thy))"fixedValues fix_";
741 val env = upd_envv thy env vats d id (mkval ts);
744 (*. update envv by folding from a list of arguments .*)
745 fun upds_envv thy envv [] = envv
746 | upds_envv thy envv ((vs, dsc, id, vl)::ps) =
747 upds_envv thy (upd_envv thy envv vs dsc id vl) ps;
748 (* eval test-maximum.sml until Specify_Method ...
749 val PblObj{probl=(_,pbl),origin=(_,(_,_,mI),_),...} = get_obj I pt [];
750 val met = (#ppc o get_met) mI;
753 val eargs = flat eargs;
754 val (vs, dsc, id, vl) = hd eargs;
755 val envv = upds_envv thy envv [(vs, dsc, id, vl)];
757 val (vs, dsc, id, vl) = hd (tl eargs);
758 val envv = upds_envv thy envv [(vs, dsc, id, vl)];
760 val (vs, dsc, id, vl) = hd (tl (tl eargs));
761 val envv = upds_envv thy envv [(vs, dsc, id, vl)];
763 val (vs, dsc, id, vl) = hd (tl (tl (tl eargs)));
764 val envv = upds_envv thy envv [(vs, dsc, id, vl)];
766 [(Free ("fix_","bool List.list"),[Const (#,#),Free (#,#)]),
767 (Free ("m_","bool"),[Free (#,#)]),
768 (Free ("vs_","bool List.list"),[# $ # $ Const #]),
769 (Free ("rs_","bool List.list"),[# $ # $ (# $ #)])]),
771 [(Free ("fix_","bool List.list"),[Const (#,#),Free (#,#)]),
772 (Free ("m_","bool"),[Free (#,#)]),
773 (Free ("vs_","bool List.list"),[# $ # $ Const #]),
774 (Free ("rs_","bool List.list"),[# $ # $ (# $ #)])]),
776 [(Free ("fix_","bool List.list"),[Const (#,#),Free (#,#)]),
777 (Free ("m_","bool"),[Free (#,#)]),
778 (Free ("vs_","bool List.list"),[# $ # $ Const #])])] : envv *)
780 (*for _output_ of the items of a Model*)
782 Correct of cterm' (*labels a correct formula (type cterm')*)
783 | SyntaxE of string (**)
784 | TypeE of string (**)
785 | False of cterm' (*WN050618 notexistent in itm_: only used in Where*)
786 | Incompl of cterm' (**)
787 | Superfl of string (**)
789 fun item2str (Correct s) ="Correct " ^ s
790 | item2str (SyntaxE s) ="SyntaxE " ^ s
791 | item2str (TypeE s) ="TypeE " ^ s
792 | item2str (False s) ="False " ^ s
793 | item2str (Incompl s) ="Incompl " ^ s
794 | item2str (Superfl s) ="Superfl " ^ s
795 | item2str (Missing s) ="Missing " ^ s;
796 (*make string for error-msgs*)
797 fun itm_2str_ ctxt (Cor ((d,ts), penv)) =
798 "Cor " ^ term_to_string' ctxt (comp_dts (d,ts)) ^ " ," ^ pen2str ctxt penv
799 | itm_2str_ ctxt (Syn c) = "Syn " ^ c
800 | itm_2str_ ctxt (Typ c) = "Typ " ^ c
801 | itm_2str_ ctxt (Inc ((d,ts), penv)) =
802 "Inc " ^ term_to_string' ctxt (comp_dts (d,ts)) ^ " ," ^ pen2str ctxt penv
803 | itm_2str_ ctxt (Sup (d,ts)) =
804 "Sup " ^ term_to_string' ctxt (comp_dts (d,ts))
805 | itm_2str_ ctxt (Mis (d,pid)) =
806 "Mis "^ term_to_string' ctxt d ^ " " ^ term_to_string' ctxt pid
807 | itm_2str_ ctxt (Par s) = "Trm "^s;
808 fun itm_2str t = itm_2str_ (thy2ctxt' "Isac") t;
809 fun itm2str_ ctxt ((i,is,b,s,itm_):itm) =
810 "("^(string_of_int i)^" ,"^(ints2str' is)^" ,"^(bool2str b)^" ,"^
811 s^" ,"^(itm_2str_ ctxt itm_)^")";
812 fun itms2str_ ctxt itms = strs2str' (map (linefeed o (itm2str_ ctxt)) itms);
813 fun w_itms2str_ ctxt itms = tracing (itms2str_ ctxt itms);
815 fun init_item str = SyntaxE str;
826 fun ppc2str {Given=Given,Where=Where,Find=Find,With=With,Relate=Relate}=
827 ("{Given =" ^ (strs2str Given ) ^
828 ",Where=" ^ (strs2str Where) ^
829 ",Find =" ^ (strs2str Find ) ^
830 ",With =" ^ (strs2str With ) ^
831 ",Relate=" ^ (strs2str Relate) ^ "}");
836 fun item_ppc ({Given = gi,Where= wh,
837 Find = fi,With = wi,Relate= re}: string ppc) =
838 {Given = map init_item gi,Where= map init_item wh,
839 Find = map init_item fi,With = map init_item wi,
840 Relate= map init_item re}:item ppc;
841 fun itemppc2str ({Given=Given,Where=Where,
842 Find=Find,With=With,Relate=Relate}:item ppc)=
843 ("{Given =" ^ ((strs2str' o (map item2str)) Given ) ^
844 ",Where=" ^ ((strs2str' o (map item2str)) Where) ^
845 ",Find =" ^ ((strs2str' o (map item2str)) Find ) ^
846 ",With =" ^ ((strs2str' o (map item2str)) With ) ^
847 ",Relate=" ^ ((strs2str' o (map item2str)) Relate) ^ "}");
849 fun de_item (Correct x) = x
850 | de_item (SyntaxE x) = x
851 | de_item (TypeE x) = x
852 | de_item (False x) = x
853 | de_item (Incompl x) = x
854 | de_item (Superfl x) = x
855 | de_item (Missing x) = x;
856 val empty_ppc ={Given = [],
860 Relate= []}:item ppc;
861 val empty_ppc_ct' ={Given = [],
865 Relate= []}:cterm' ppc;
869 Matches of pblID * item ppc
870 | NoMatch of pblID * item ppc;
871 fun match2str (Matches (pI, ppc)) =
872 "Matches ("^(strs2str pI)^", "^(itemppc2str ppc)^")"
873 | match2str(NoMatch (pI, ppc)) =
874 "NoMatch ("^(strs2str pI)^", "^(itemppc2str ppc)^")";
875 fun matchs2str ms = (strs2str o (map match2str)) ms;
876 fun pblID_of_match (Matches (pI,_)) = pI
877 | pblID_of_match (NoMatch (pI,_)) = pI;
879 (*10.03 for Refine_Problem*)
881 Match_ of pblID * ((itm list) * ((bool * term) list))
884 (*. the refined pbt is the last_element Matches in the list .*)
885 fun is_matches (Matches _) = true
886 | is_matches _ = false;
887 fun matches_pblID (Matches (pI,_)) = pI;
888 fun refined ms = ((matches_pblID o the o (find_first is_matches) o rev) ms)
889 handle _ => []:pblID;
890 fun refined_IDitms ms = ((find_first is_matches) o rev) ms;
892 (*. the refined pbt is the last_element Matches in the list,
893 for Refine_Problem, tryrefine .*)
894 fun is_matches_ (Match_ _) = true
895 | is_matches_ _ = false;
896 fun refined_ ms = ((find_first is_matches_) o rev) ms;
899 fun ts_in (Cor ((_,ts),_)) = ts
900 | ts_in (Syn (c)) = []
901 | ts_in (Typ (c)) = []
902 | ts_in (Inc ((_,ts),_)) = ts
903 | ts_in (Sup (_,ts)) = ts
904 | ts_in (Mis _) = [];
906 fun all_ts_in itm_s = (flat o (map ts_in)) itm_s;
907 val unique = (term_of o the o (parse (Thy_Info.get_theory "Real"))) "UnIqE_tErM";
908 fun d_in (Cor ((d,_),_)) = d
909 | d_in (Syn (c)) = (tracing("*** d_in: Syn ("^c^")"); unique)
910 | d_in (Typ (c)) = (tracing("*** d_in: Typ ("^c^")"); unique)
911 | d_in (Inc ((d,_),_)) = d
912 | d_in (Sup (d,_)) = d
913 | d_in (Mis (d,_)) = d;
915 fun dts2str (d,ts) = pair2str (term2str d, terms2str ts);
916 fun penvval_in (Cor ((d,_),(_,ts))) = [comp_ts (d,ts)]
917 | penvval_in (Syn (c)) = (tracing("*** penvval_in: Syn ("^c^")"); [])
918 | penvval_in (Typ (c)) = (tracing("*** penvval_in: Typ ("^c^")"); [])
919 | penvval_in (Inc (_,(_,ts))) = ts
920 | penvval_in (Sup dts) = (tracing("*** penvval_in: Sup "^(dts2str dts)); [])
921 | penvval_in (Mis (d,t)) = (tracing("*** penvval_in: Mis "^
922 (pair2str(term2str d, term2str t))); []);
925 (*. check a predicate labelled with indication of incomplete substitution;
926 rls -> (*for eval_true*)
927 bool * (*have _all_ variables(Free) from the model-pattern
928 been substituted by a value from the pattern's environment ?*)
929 term (*the precondition*)
931 bool * (*has the precondition evaluated to true*)
932 term (*the precondition (for map)*)
934 fun evalprecond prls (false, pre) =
935 (*NOT ALL Free's have been substituted, eg. because of incomplete model*)
937 | evalprecond prls (true, pre) =
938 (* val (prls, pre) = (prls, hd pres');
939 val (prls, pre) = (prls, hd (tl pres'));
941 if eval_true (assoc_thy "Isac") (*for Pattern.match *)
942 [pre] prls (*pre parsed, prls.thy*)
946 fun pre2str (b, t) = pair2str(bool2str b, term2str t);
947 fun pres2str pres = strs2str' (map (linefeed o pre2str) pres);
949 (* check preconditions, return true if all true *)
950 fun check_preconds' _ [] _ _ = [] (*empty preconditions are true*)
951 | check_preconds' prls pres pbl _(*FIXME.WN0308 mvat re-introduce*) =
952 let val env = mk_env pbl;
953 val pres' = map (subst_atomic_all env) pres;
954 in map (evalprecond prls) pres' end;
956 fun check_preconds thy prls pres pbl =
957 check_preconds' prls pres pbl (max_vt pbl);
959 fun declare_constraints' ts ctxt =
960 fold Variable.declare_constraints (flat (map vars ts)) ctxt;
961 (*WN110613 fun declare_constraints' shall replace fun declare_constraints*)
962 fun declare_constraints t ctxt =
964 fun get_vars ((v,T)::vs) = (case raw_explode v |> Library.read_int of
965 (_, _::_) => (Free (v,T)::get_vars vs)
966 | (_, [] ) => get_vars vs) (*filter out nums as long as
967 we have Free ("123",_)*)
969 val ts = Term.add_frees (Syntax.read_term ctxt t) [] |> get_vars;
970 in fold Variable.declare_constraints ts ctxt end;
972 structure Context_Data = Proof_Data
975 fun get_assumptions ctxt = Context_Data.get ctxt
976 fun insert_assumptions asms = Context_Data.map (fn xs => distinct (asms @ xs))
978 (* transfer assumptions from one to another ctxt.
979 does NOT respect scope: in a calculation identifiers are unique.
980 but environments are scoped as usual in Luacs-interpretation.
981 WN110520 redo (1) take declare_constraints (2) with combinators*)
982 fun transfer_asms_from_to from_ctxt to_ctxt =
984 val to_vars = get_assumptions to_ctxt |> map vars |> flat
985 fun transfer [] to_ctxt = to_ctxt
986 | transfer (from_asm::fas) to_ctxt =
987 if inter op = (vars from_asm) to_vars = []
988 then transfer fas to_ctxt
989 else transfer fas (insert_assumptions [from_asm] to_ctxt)
990 in transfer (get_assumptions from_ctxt) to_ctxt end
992 (* exported from a subproblem to the context of the calling method:
993 # 'scrval': the result of script interpretation and
994 # those assumptions in the subproblem wich contain a variable known
995 in the calling method. *)
996 fun from_subpbl_to_caller sub_ctxt scrval caller_ctxt =
997 let val caller_ctxt = (scrval |> dest_list' |> insert_assumptions) caller_ctxt
998 in transfer_asms_from_to sub_ctxt caller_ctxt end;
1000 (*----------------------------------------------------------*)
1003 (*----------------------------------------------------------*)