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