1 (* use"ME/generate.sml";
5 sig (*vvv request into signature is incremental with *.sml *)
6 (* for calchead.sml -------------------------------------------------------------- vvv *)
9 datatype pblmet = Method of metID | Problem of pblID | Upblmet
14 | PpcKF of pblmet * item ppc
15 | RefinedKF of pblID * (itm list * (bool * term) list)
16 val generate1 : theory -> tac_ -> istate * Proof.context ->
17 pos' -> ptree -> pos' * pos' list * mout * ptree (* for calchead.sml ^^^ *)
18 val init_istate : tac -> term -> istate (* for solve.sml *)
19 val init_pbl : (string * (term * 'a)) list -> itm list
20 val init_pbl' : (string * (term * term)) list -> itm list
21 val embed_deriv : taci list -> ptree * pos' -> pos' list * (ptree * pos') (* for inform.sml *)
22 val generate_hard : (* for solve.sml *)
23 theory -> tac_ -> pos' -> ptree -> pos' * pos' list * mout * ptree
24 val generate : (tac * tac_ * (pos' * (istate * Proof.context))) list ->
25 ptree * pos' list * pos' -> ptree * pos' list * pos' (* for mathengine.sml *)
26 val generate_inconsistent_rew : subs option * thm'' -> term -> istate * Proof.context ->
27 pos' -> ptree -> ptree * pos' (* for interface.sml *)
28 (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
29 ( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
33 structure Ctree(**): CALC_TREE(**) =
36 (* initialize istate for Detail_Set *)
37 fun init_istate (Rewrite_Set rls) t =
38 (case assoc_rls rls of
39 Rrls {scr = Rfuns {init_state = ii, ...}, ...} => RrlsState (ii t)
40 | Rls {scr = EmptyScr, ...} =>
41 error ("interSteps>..>init_istate: \"" ^ rls ^ "\" has EmptyScr." ^
42 "use prep_rls' for storing rule-sets !")
43 | Rls {scr = Prog s, ...} => (ScrState ([(one_scr_arg s, t)], [], NONE, e_term, Sundef, true))
44 | Seq {scr=EmptyScr,...} =>
45 error ("interSteps>..>init_istate: \"" ^ rls ^ "\" has EmptyScr." ^
46 "use prep_rls' for storing rule-sets !")
47 | Seq {scr = Prog s,...} => (ScrState ([(one_scr_arg s, t)], [], NONE, e_term, Sundef, true))
48 | _ => error "init_istate Rewrite_Set: uncovered case assoc_rls")
49 | init_istate (Rewrite_Set_Inst (subs, rls)) t =
51 val v = case subs2subst (assoc_thy "Isac") subs of
53 | _ => error "init_istate: uncovered case "
54 (*...we suppose the substitution of only _one_ bound variable*)
55 in case assoc_rls rls of
56 Rls {scr = EmptyScr, ...} =>
57 error ("interSteps>..>init_istate: \""^rls^"\" has EmptyScr." ^
58 "use prep_rls' for storing rule-sets !")
59 | Rls {scr = Prog s, ...} =>
60 let val (form, bdv) = two_scr_arg s
61 in (ScrState ([(form, t), (bdv, v)], [], NONE, e_term, Sundef,true))
63 | Seq {scr = EmptyScr, ...} =>
64 error ("interSteps>..>init_istate: \""^rls^"\" has EmptyScr." ^
65 "use prep_rls' for storing rule-sets !")
66 | Seq {scr = Prog s,...} =>
67 let val (form, bdv) = two_scr_arg s
68 in (ScrState ([(form, t), (bdv, v)],[], NONE, e_term, Sundef,true))
70 | _ => error "init_istate Rewrite_Set_Inst: uncovered case assoc_rls"
72 | init_istate tac _ = error ("init_istate: uncovered definition for " ^ tac2str tac)
74 (* a taci holds alle information required to build a node in the calc-tree;
75 a taci is assumed to be used efficiently such that the calc-tree
76 resulting from applying a taci need not be stored separately;
77 see "type calcstate" *)
78 (*TODO.WN0504 redesign ??? or redesign generate ?? see "fun generate"
79 TODO.WN0512 ? redesign this _list_:
80 # only used for [Apply_Method + (Take or Subproblem)], i.e. for initacs
81 # the latter problem may be resolved automatically if "fun autocalc" is
82 not any more used for the specify-phase and for changing the phases*)
84 (tac * (* for comparison with input tac *)
85 tac_ * (* for ptree generation *)
86 (pos' * (* after applying tac_, for ptree generation *)
87 (istate * Proof.context))) (* after applying tac_, for ptree generation *)
88 val e_taci = (Empty_Tac, Empty_Tac_, (e_pos', (e_istate, e_ctxt))): taci
89 fun taci2str ((tac, tac_, (pos', (istate, _))):taci) =
90 "( " ^ tac2str tac ^ ", " ^ tac_2str tac_ ^ ", ( " ^ pos'2str pos' ^ ", " ^
91 istate2str istate ^ " ))"
92 fun tacis2str tacis = (strs2str o (map (linefeed o taci2str))) tacis
94 datatype pblmet = (*%^%*)
96 | Problem of pblID (*%^%*)
97 | Method of metID; (*%^%*)
98 fun pblmet2str (Problem pblID) = "Problem " ^ strs2str pblID (*%^%*)
99 | pblmet2str (Method metID) = "Method " ^ metID2str metID (*%^%*)
100 | pblmet2str x = error ("pblmet2str: uncovered definition " ^ pblmet2str x)
102 (*3.5.00: TODO: foppFK eliminated in interface FE-KE !!!*)
103 datatype foppFK = (* in DG cases div 2 *)
104 EmptyFoppFK (*DG internal*)
106 | PpcFK of cterm' ppc
107 fun foppFK2str (FormFK ct') ="FormFK " ^ ct'
108 | foppFK2str (PpcFK ppc) ="PpcFK " ^ ppc2str ppc
109 | foppFK2str EmptyFoppFK ="EmptyFoppFK"
111 datatype nest = Open | Closed | Nundef;
112 fun nest2str Open = "Open"
113 | nest2str Closed = "Closed"
114 | nest2str Nundef = "Nundef"
117 datatype edit = EdUndef | Write | Protect;
118 (* bridge --> kernel *)
119 (* bridge <-> kernel *)
120 (* needed in dialog.sml *) (* bridge <-- kernel *)
121 fun edit2str EdUndef = "EdUndef"
122 | edit2str Write = "Write"
123 | edit2str Protect = "Protect";
125 datatype inout = (*FIXME.WN1105 drop this: was required for proto0 with dialog in sml*)
126 Error_ of string (*<--*)
127 | FormKF of cellID * edit * indent * nest * cterm' (*<--*)
128 | PpcKF of cellID * edit * indent * nest * (pblmet * item ppc) (*<--*)
129 | RefineKF of match list (*<--*)
130 | RefinedKF of (pblID * ((itm list) * ((bool * term) list))) (*<--*)
132 fun inout2str End_Proof = "End_Proof"
133 | inout2str (Error_ s) = "Error_ "^s
134 | inout2str (FormKF (cellID, edit, indent, nest, ct')) =
135 "FormKF ("^(string_of_int cellID)^","
136 ^(edit2str edit)^","^(string_of_int indent)^","
137 ^(nest2str nest)^",("
139 | inout2str (PpcKF (cellID, edit, indent, nest, (pm,itemppc))) =
140 "PpcKF ("^(string_of_int cellID)^","
141 ^(edit2str edit)^","^(string_of_int indent)^","
142 ^(nest2str nest)^",("
143 ^(pblmet2str pm)^","^(itemppc2str itemppc)^"))"
144 | inout2str (RefineKF ms) = "RefineKF "^(matchs2str ms)
145 | inout2str _ = error "inout2str: uncovered definition"
146 fun inouts2str ios = (strs2str' o (map inout2str)) ios
149 datatype mout = EmptyMout | Error' of inout | Form' of inout | Problems of inout
153 | PpcKF of (pblmet * item ppc)
154 | RefinedKF of pblID * (itm list * (bool * term) list)
158 fun mout2str (FormKF cterm') = "FormKF " ^ cterm'
159 | mout2str (PpcKF (pm, itemppc)) = "PpcKF (" ^ pblmet2str pm ^ "," ^ itemppc2str itemppc ^ ")"
160 | mout2str (RefinedKF (pblID, ls)) = "mout2str: RefinedKF not impl."
161 | mout2str (Error' str) = "Error' " ^ str
162 | mout2str (EmptyMout ) = "EmptyMout"
164 (* init pbl with ...,dsc,empty | [] *)
167 fun pbt2itm (f, (d, _)) = ((0, [], false, f, Inc ((d, []), (e_term, []))) : itm)
168 in map pbt2itm pbt end
170 (* take formal parameters from pbt, for transfer from pbl/met-hierarchy *)
173 fun pbt2itm (f, (d, t)) = ((0, [], false, f, Inc((d, [t]), (e_term, []))) : itm)
174 in map pbt2itm pbt end
176 (*generate 1 ppobj in ptree*)
177 (*TODO.WN0501: take calcstate as an argument (see embed_derive etc.)?specify?*)
178 fun generate1 thy (Add_Given' (_, itmlist)) _ (pos as (p, p_)) pt =
179 (pos: pos', [], PpcKF (Upblmet, itms2itemppc thy [][]),
181 Pbl => update_pbl pt p itmlist
182 | Met => update_met pt p itmlist
183 | _ => error ("uncovered case " ^ pos_2str p_))
184 (* WN110515 probably declare_constraints with input item (without dsc) --
185 -- important when specifying without formalisation *)
186 | generate1 thy (Add_Find' (_, itmlist)) _ (pos as (p, p_)) pt =
187 (pos, [], (PpcKF (Upblmet, itms2itemppc thy [] [])),
189 Pbl => update_pbl pt p itmlist
190 | Met => update_met pt p itmlist
191 | _ => error ("uncovered case " ^ pos_2str p_))
192 (*WN110515 probably declare_constraints with input item (without dsc)*)
193 | generate1 thy (Add_Relation' (_, itmlist)) _ (pos as (p, p_)) pt =
194 (pos, [], PpcKF (Upblmet, itms2itemppc thy [] []),
196 Pbl => update_pbl pt p itmlist
197 | Met => update_met pt p itmlist
198 | _ => error ("uncovered case " ^ pos_2str p_))
199 | generate1 thy (Specify_Theory' domID) _ (pos as (p,_)) pt =
200 (pos, [] , PpcKF (Upblmet, itms2itemppc thy [] []),
201 update_domID pt p domID)
202 | generate1 thy (Specify_Problem' (pI, (_, (itms, _)))) _ (p, _) pt =
204 val pt = update_pbl pt p itms
205 val pt = update_pblID pt p pI
207 ((p, Pbl), [], PpcKF (Upblmet, itms2itemppc thy [] []), pt)
209 | generate1 thy (Specify_Method' (mID, oris, itms)) _ (p, _) pt =
211 val pt = update_oris pt p oris
212 val pt = update_met pt p itms
213 val pt = update_metID pt p mID
215 ((p, Met), [], PpcKF (Upblmet, itms2itemppc thy [] []), pt)
217 | generate1 thy (Model_Problem' (_, itms, met)) _ (pos as (p,_)) pt =
219 val pt = update_pbl pt p itms
220 val pt = update_met pt p met
222 (pos, [], PpcKF (Upblmet, itms2itemppc thy [] []), pt)
224 | generate1 thy (Refine_Tacitly' (_, pIre, domID, metID, pbl)) _ (pos as (p, _)) pt =
226 val pt = update_pbl pt p pbl
227 val pt = update_orispec pt p (domID, pIre, metID)
229 (pos, [], PpcKF (Upblmet, itms2itemppc thy [] []), pt)
231 | generate1 thy (Refine_Problem' (pI, _)) _ (pos as (p, _)) pt =
233 val (dI, _, mI) = get_obj g_spec pt p
234 val pt = update_spec pt p (dI, pI, mI)
236 (pos, [], PpcKF (Upblmet, itms2itemppc thy [] []), pt)
238 | generate1 _ (Apply_Method' (_, topt, is, ctxt)) _ (pos as (p, _)) pt =
241 let val (pt, c) = cappend_form pt p (is, ctxt) t
242 in (pos, c, EmptyMout, pt) end
243 | NONE => (pos, [], EmptyMout, update_env pt p (SOME (is, ctxt))))
244 | generate1 _ (Take' t) l (p, _) pt = (* val (Take' t) = m; *)
247 let val (ps, p') = split_last p (* no connex to prev.ppobj *)
248 in if p' = 0 then ps @ [1] else p end
249 val (pt, c) = cappend_form pt p l t
251 ((p, Frm): pos', c, FormKF (term2str t), pt)
253 | generate1 _ (Begin_Trans' t) l (p, Frm) pt =
255 val (pt, c) = cappend_form pt p l t
256 val pt = update_branch pt p TransitiveB (*040312*)
257 (* replace the old PrfOjb ~~~~~ *)
258 val p = (lev_on o lev_dn (* starts with [...,0] *)) p
259 val (pt, c') = cappend_form pt p l t (*FIXME.0402 same istate ???*)
261 ((p, Frm), c @ c', FormKF (term2str t), pt)
263 | generate1 thy (Begin_Trans' t) l (p, Res) pt =
264 (*append after existing PrfObj vvvvvvvvvvvvv*)
265 generate1 thy (Begin_Trans' t) l (lev_on p, Frm) pt
266 | generate1 _ (End_Trans' tasm) l (p, _) pt =
269 val (pt, c) = append_result pt p' l tasm Complete
271 ((p', Res), c, FormKF (term2str t), pt)
273 | generate1 _ (Rewrite_Inst' (_, _, _, _, subs', thm', f, (f', asm))) (is, ctxt) (p, _) pt =
275 val (pt, c) = cappend_atomic pt p (is, insert_assumptions asm ctxt) f
276 (Rewrite_Inst (subst2subs subs', thm')) (f',asm) Complete;
277 val pt = update_branch pt p TransitiveB
279 ((p, Res), c, FormKF (term2str f'), pt)
281 | generate1 _ (Rewrite' (_, _, _, _, thm', f, (f', asm))) (is, ctxt) (p, _) pt =
283 val (pt, c) = cappend_atomic pt p (is, insert_assumptions asm ctxt) f
284 (Rewrite thm') (f', asm) Complete
285 val pt = update_branch pt p TransitiveB
287 ((p, Res), c, FormKF (term2str f'), pt)
289 | generate1 thy (Rewrite_Asm' all) l p pt = generate1 thy (Rewrite' all) l p pt
290 | generate1 _ (Rewrite_Set_Inst' (_, _, subs', rls', f, (f', asm))) (is, ctxt) (p, _) pt =
292 val (pt, c) = cappend_atomic pt p (is, insert_assumptions asm ctxt) f
293 (Rewrite_Set_Inst (subst2subs subs', id_rls rls')) (f', asm) Complete
294 val pt = update_branch pt p TransitiveB
296 ((p, Res), c, FormKF (term2str f'), pt)
298 | generate1 thy (Detail_Set_Inst' (_, _, subs, rls, f, (_, asm))) (is, ctxt) (p, _) pt =
300 val ctxt' = insert_assumptions asm ctxt
301 val (pt, _) = cappend_form pt p (is, ctxt') f
302 val pt = update_branch pt p TransitiveB
303 val is = init_istate (Rewrite_Set_Inst (subst2subs subs, id_rls rls)) f
304 val tac_ = Apply_Method' (e_metID, SOME t, is, ctxt')
305 val pos' = ((lev_on o lev_dn) p, Frm)
307 generate1 thy tac_ (is, ctxt') pos' pt (*implicit Take*)
309 | generate1 _ (Rewrite_Set' (_, _, rls', f, (f', asm))) (is, ctxt) (p, _) pt =
311 val (pt, c) = cappend_atomic pt p (is, insert_assumptions asm ctxt) f
312 (Rewrite_Set (id_rls rls')) (f',asm) Complete
313 val pt = update_branch pt p TransitiveB
315 ((p, Res), c, FormKF (term2str f'), pt)
317 | generate1 thy (Detail_Set' (_, _, rls, f, (_, asm))) (is, ctxt) (p, _) pt =
319 val ctxt' = insert_assumptions asm ctxt
320 val (pt, _) = cappend_form pt p (is, ctxt') f
321 val pt = update_branch pt p TransitiveB
322 val is = init_istate (Rewrite_Set (id_rls rls)) f
323 val tac_ = Apply_Method' (e_metID, SOME t, is, ctxt')
324 val pos' = ((lev_on o lev_dn) p, Frm)
326 generate1 thy tac_ (is, ctxt') pos' pt (*implicit Take*)
328 | generate1 _ (Check_Postcond' (_, (scval, asm))) l (p, _) pt =
330 val (pt, c) = append_result pt p l (scval, asm) Complete
332 ((p, Res), c, FormKF (term2str scval), pt)
334 | generate1 _ (Calculate' (_, op_, f, (f', _))) l (p, _) pt =
336 val (pt,c) = cappend_atomic pt p l f (Calculate op_) (f', []) Complete
338 ((p, Res), c, FormKF (term2str f'), pt)
340 | generate1 _ (Check_elementwise' (consts, pred, (f', asm))) l (p, _) pt =
342 val (pt,c) = cappend_atomic pt p l consts (Check_elementwise pred) (f',asm) Complete
344 ((p, Res), c, FormKF (term2str f'), pt)
346 | generate1 _ (Or_to_List' (ors, list)) l (p, _) pt =
348 val (pt,c) = cappend_atomic pt p l ors Or_to_List (list, []) Complete
350 ((p, Res), c, FormKF (term2str list), pt)
352 | generate1 _ (Substitute' (_, _, subte, t, t')) l (p, _) pt =
355 cappend_atomic pt p l t (Substitute (subte2sube subte)) (t',[]) Complete
356 in ((p, Res), c, FormKF (term2str t'), pt)
358 | generate1 _ (Tac_ (_, f, id, f')) l (p, _) pt =
360 val (pt, c) = cappend_atomic pt p l (str2term f) (Tac id) (str2term f', []) Complete
362 ((p,Res), c, FormKF f', pt)
364 | generate1 thy (Subproblem' ((domID, pblID, metID), oris, hdl, fmz_, ctxt, f)) l (p, _) pt =
366 val (pt, c) = cappend_problem pt p l (fmz_, (domID, pblID, metID)) (oris, (domID, pblID, metID), hdl)
367 val pt = update_ctxt pt p ctxt
368 val f = Syntax.string_of_term (thy2ctxt thy) f
370 ((p, Pbl), c, FormKF f, pt)
372 | generate1 _ m' _ _ _ = error ("generate1: not impl.for " ^ tac_2str m')
374 fun generate_inconsistent_rew (subs_opt, thm') f' (is, ctxt) (pos as (p,_)) pt =
376 val f = get_curr_formula (pt, pos)
377 val pos' as (p', _) = (lev_on p, Res)
378 val (pt, _) = case subs_opt of
379 NONE => cappend_atomic pt p' (is, insert_assumptions [] ctxt) f
380 (Rewrite thm') (f', []) Inconsistent
381 | SOME subs => cappend_atomic pt p' (is, insert_assumptions [] ctxt) f
382 (Rewrite_Inst (subs, thm')) (f', []) Inconsistent
383 val pt = update_branch pt p' TransitiveB
386 fun generate_hard thy m' (p,p_) pt =
389 Frm => p | Res => lev_on p
390 | _ => error ("generate_hard: call by " ^ pos'2str (p,p_))
391 in generate1 thy m' (e_istate, e_ctxt) (p,p_) pt end
393 (* tacis are in reverse order from nxt_solve_/specify_: last = fst to insert *)
394 fun generate ([]: taci list) ptp = ptp
395 | generate tacis (pt, c, _: pos'(*!dropped!WN0504redesign generate/tacis?*))=
397 val (tacis', (_, tac_, (p, is))) = split_last tacis
398 val (p',c',_,pt') = generate1 (assoc_thy "Isac") tac_ is p pt
400 generate tacis' (pt', c@c', p')
403 (* update pos in tacis for embedding by generate *)
404 fun insert_pos (_: pos) [] = []
405 | insert_pos (p: pos) (((tac, tac_, (_, ist)): taci) :: tacis) =
406 ((tac, tac_, ((p, Res), ist)): taci) :: (insert_pos (lev_on p) tacis)
408 fun res_from_taci (_, Rewrite' (_, _, _, _, _, _,(res, asm)), _) = (res, asm)
409 | res_from_taci (_, Rewrite_Set' (_, _, _, _, (res, asm)), _) = (res, asm)
410 | res_from_taci (_, tac_, _) = error ("res_from_taci: called with" ^ tac_2str tac_)
412 (* embed the tacis created by a '_deriv'ation; sys.form <> input.form
413 tacis are in order, thus are reverted for generate *)
414 fun embed_deriv (tacis: taci list) (pt, pos as (p, Frm): pos') =
415 (*inform at Frm: replace the whole PrfObj by a Transitive-ProfObj FIXME?0402
416 and transfer the istate (from _after_ compare_deriv) from Frm to Res*)
418 val (res, asm) = (res_from_taci o last_elem) tacis
419 val (ist, ctxt) = case get_obj g_loc pt p of
420 (SOME (ist, ctxt), _) => (ist, ctxt)
421 | (NONE, _) => error "embed_deriv Frm: uncovered case get_obj"
422 val form = get_obj g_form pt p
423 (*val p = lev_on p; ---------------only difference to (..,Res) below*)
424 val tacis = (Begin_Trans, Begin_Trans' form, (pos, (Uistate, ctxt))) ::
425 (insert_pos ((lev_on o lev_dn) p) tacis) @ [(End_Trans, End_Trans' (res, asm),
426 (pos_plus (length tacis) (lev_dn p, Res), (new_val res ist, ctxt)))]
427 val {nrls, ...} = get_met (get_obj g_metID pt (par_pblobj pt p))
428 val (pt, c, pos as (p, _)) = generate (rev tacis) (pt, [], (p, Res))
429 val pt = update_tac pt p (Derive (id_rls nrls))
430 val pt = update_branch pt p TransitiveB
431 in (c, (pt, pos: pos')) end
432 | embed_deriv tacis (pt, (p, Res)) =
433 (*inform at Res: append a Transitive-PrfObj FIXME?0402 other branch-types ?
434 and transfer the istate (from _after_ compare_deriv) from Res to new Res*)
435 let val (res, asm) = (res_from_taci o last_elem) tacis
436 val (ist, ctxt) = case get_obj g_loc pt p of
437 (_, SOME (ist, ctxt)) => (ist, ctxt)
438 | (_, NONE) => error "embed_deriv Frm: uncovered case get_obj"
439 val (f, _) = get_obj g_result pt p
440 val p = lev_on p(*---------------only difference to (..,Frm) above*);
441 val tacis = (Begin_Trans, Begin_Trans' f, ((p, Frm), (Uistate, ctxt))) ::
442 (insert_pos ((lev_on o lev_dn) p) tacis) @ [(End_Trans, End_Trans' (res, asm),
443 (pos_plus (length tacis) (lev_dn p, Res), (new_val res ist, ctxt)))];
444 val {nrls, ...} = get_met (get_obj g_metID pt (par_pblobj pt p))
445 val (pt, c, pos as (p, _)) = generate (rev tacis) (pt, [], (p, Res))
446 val pt = update_tac pt p (Derive (id_rls nrls))
447 val pt = update_branch pt p TransitiveB
448 in (c, (pt, pos)) end
449 | embed_deriv _ _ = error "embed_deriv: uncovered definition"