1 (* Title: Specify/input-calchead.sml
3 (c) due to copyright terms
5 This will be dropped at switch to Isabelle/PIDE .
8 signature PRESENTATION_SPECIFICATION =
11 (*/------- rename -------\*)
13 Find of TermC.as_string list
14 | Given of TermC.as_string list
15 | Relate of TermC.as_string list
16 type imodel = iitem list
17 type icalhd = Pos.pos' * TermC.as_string * imodel * Pos.pos_ * References.T
19 val input_icalhd : Ctree.ctree -> icalhd -> Ctree.ctree * SpecificationC.T
21 val appl_add': ThyC.id -> O_Model.T -> I_Model.T -> Model_Pattern.T ->
22 string * TermC.as_string -> I_Model.single
23 val appl_adds: ThyC.id -> O_Model.T -> I_Model.T -> Model_Pattern.T ->
24 (string * TermC.as_string) list -> I_Model.T
25 val eq7: ''a * ''b -> ''a * (''b * 'c) -> bool
26 val filter_sep: ('a -> bool) -> 'a list -> 'a list * 'a list
27 val fstr2itm_: theory -> (''a * (term * term)) list -> ''a * string ->
28 int list * bool * ''a * I_Model.feedback (*I_Model.single'*)
29 val imodel2fstr: iitem list -> (string * TermC.as_string) list
30 val is_Par: 'a * 'b * 'c * 'd * I_Model.feedback -> bool (*I_Model.T?*)
31 val is_e_ts: term list -> bool
32 val itms2fstr: Proof.context -> I_Model.single -> string * string
33 val par2fstr: I_Model.single -> string * TermC.as_string
34 val parsitm: theory -> I_Model.single -> I_Model.single
35 val unknown_expl: ThyC.id -> (''a * (term * term)) list -> (*Model_Pattern.T?*)
36 (''a * string) list ->
37 (int * int list * bool * ''a * Model_Def.i_model_feedback) list (*I_Model.T?*)
39 (*\------- rename -------/*)
43 structure P_Spec(**): PRESENTATION_SPECIFICATION(**) =
47 type record = {thy_id: ThyC.id, pbl_id: Problem.id, (* headline of Problem *)
48 givens: TermC.as_string list, wheres: TermC.as_string list, (* Model.T *)
49 find: TermC.as_string, relates: TermC.as_string list,
50 rthy_id: ThyC.id, rpbl_id: Problem.id, rmet_id: MethodC.id} (* References.T *)
52 fun flattup2 (a, (b ,c, d, e)) = (a, b, c, d, e)
54 (** handle an input P_Specific'action **)
57 Given of TermC.as_string list
58 (*Where is still not input*)
59 | Find of TermC.as_string list
60 | Relate of TermC.as_string list
62 type imodel = iitem list
65 Pos.pos' * (* the position in Ctree *)
66 TermC.as_string * (* the headline shown on Calc.T *)
67 imodel * (* the model *)
68 Pos.pos_ * (* model belongs to Problem or MethodC *)
69 References.T; (* into Know_Store *)
70 val empty = (Pos.e_pos', "", [Given [""]], Pos.Pbl, References.empty)
72 (* re-parse itms with a new thy and prepare for checking with ori list *)
73 fun parsitm thy (itm as (i, v, _, f, I_Model.Cor ((d, ts), _))) =
75 let val t = Input_Descript.join (d, ts)
76 val _ = (UnparseC.term_in_thy thy t)
77 (*t his ^^^^^^^^^^^^ should raise the exn on unability of re-parsing dts *)
81 (i, v, false, f, I_Model.Syn (UnparseC.term_in_thy thy TermC.empty (*t ..(t) has not been declared*))))
82 | parsitm _ (i, v, b, f, I_Model.Syn str) =
84 let val _ = Syntax.read_term\<^context> str
85 in (i, v, b ,f, I_Model.Par str) end\<close> of
87 | NONE => (i, v, b, f, I_Model.Syn str))
88 | parsitm _ (i, v, b, f, I_Model.Typ str) =
90 let val _ = Syntax.read_term\<^context> str
91 in (i, v, b, f, I_Model.Par str) end\<close> of
93 | NONE => (i, v, b, f, I_Model.Syn str))
94 | parsitm thy (itm as (i, v, _, f, I_Model.Inc ((d, ts), _))) =
96 let val t = Input_Descript.join (d,ts);
97 val _ = UnparseC.term_in_thy thy t;
98 (*this ^ should raise the exn on unability of re-parsing dts*)
101 | NONE => (i, v, false, f, I_Model.Syn (UnparseC.term_in_thy thy TermC.empty (*t ..(t) has not been declared*))))
102 | parsitm thy (itm as (i, v, _, f, I_Model.Sup (d, ts))) =
104 let val t = Input_Descript.join (d,ts);
105 val _ = UnparseC.term_in_thy thy t;
106 (*this ^ should raise the exn on unability of re-parsing dts*)
107 in itm end\<close> of
109 | NONE => (i, v, false, f, I_Model.Syn (UnparseC.term_in_thy thy TermC.empty (*t ..(t) has not been declared*))))
110 | parsitm thy (itm as (i, v, _, f, I_Model.Mis (d, t'))) =
113 val _ = UnparseC.term_in_thy thy t;
114 (*this ^ should raise the exn on unability of re-parsing dts*)
115 in itm end\<close> of
117 | NONE => (i, v, false, f, I_Model.Syn (UnparseC.term_in_thy thy TermC.empty (*t ..(t) has not been declared*))))
118 | parsitm thy (itm as (_, _, _, _, I_Model.Par _)) =
119 raise ERROR ("parsitm (" ^ I_Model.single_to_string (Proof_Context.init_global thy) itm ^
120 "): Par should be internal");
122 (*separate a list to a pair of elements that do NOT satisfy the predicate,
123 and of elements that satisfy the predicate, i.e. (false, true)*)
124 fun filter_sep pred xs =
127 | filt (a, b) (x :: xs) =
129 then filt (a, b @ [x]) xs
130 else filt (a @ [x], b) xs
131 in filt ([], []) xs end;
132 fun is_Par (_, _, _, _, I_Model.Par _) = true
135 fun is_e_ts [] = true
136 | is_e_ts [Const (\<^const_name>\<open>Nil\<close>, _)] = true
139 (* WN.9.11.03 copied from fun appl_add *)
140 fun appl_add' dI oris model pbt (sel, ct) =
142 val ctxt = ThyC.get_theory dI |> Proof_Context.init_global;
144 (*/------------ replace by ParseC.term_position ------------------\*)
145 case ParseC.term_opt ctxt ct of
146 (*\------------ replace by ParseC.term_position ------------------/*)
147 NONE => (0, [], false, sel, I_Model.Syn ct)
149 (case O_Model.contains ctxt sel oris t of
151 (case I_Model.is_notyet_input ctxt model all ori' pbt of
153 | (msg,_) => raise ERROR ("appl_add': " ^ msg))
154 | (_, (i, v, _, d, ts), _) =>
156 then (i, v, false, sel, I_Model.Inc ((d, ts), (TermC.empty, [])))
157 else (i, v, false, sel, I_Model.Sup (d, ts)))
160 (* generate preliminary itm_ from a string (with field "#Given" etc.) *)
161 fun eq7 (f, d) (f', (d', _)) = f = f' andalso d = d';
162 fun fstr2itm_ thy pbt (f, str) =
164 (*/------------ replace by ParseC.term_position ? ------------\*)
165 val topt = ParseC.term_opt (Proof_Context.init_global thy) str
166 (*\------------ replace by ParseC.term_position ? ------------/*)
169 NONE => ([], false, f, I_Model.Syn str)
172 val (d, ts) = Input_Descript.split ct
173 val popt = find_first (eq7 (f, d)) pbt
176 NONE => ([1](*??*), true(*??*), f, I_Model.Sup (d, ts))
177 | SOME (f, (d, id)) => ([1], true, f, I_Model.Cor ((d, ts), (id, ts)))
181 (*.input into empty PblObj, i.e. empty fmz+origin (unknown example).*)
182 fun unknown_expl dI pbt selcts =
184 val thy = Know_Store.get_via_last_thy dI
185 val its_ = map (fstr2itm_ thy pbt) selcts (*([1],true,"#Given",Cor (...))*)
186 val its = O_Model.add_enumerate its_
187 in map flattup2 its end
189 (* WN.11.03 for input_icalhd, ~ specify_additem for Add_Given/_Find/_Relation
190 appl_add': generate 1 item
191 appl_add' . contains: parse, get data from oris (vats, all (elems if list)..)
192 appl_add' . is_notyet_input: compare with items in model already input
193 insert_ppc': insert this 1 item*)
194 fun appl_adds dI [] _ pbt selcts = unknown_expl dI pbt selcts
195 (*already present itms in model are being overwritten*)
196 | appl_adds _ _ model _ [] = model
197 | appl_adds dI oris model pbt (selct :: ss) =
198 let val itm = appl_add' dI oris model pbt selct;
199 in appl_adds dI oris (I_Model.add itm model) pbt ss end
201 fun par2fstr (_, _, _, f, I_Model.Par s) = (f, s)
202 | par2fstr itm = raise ERROR ("par2fstr: called with " ^ I_Model.single_to_string (ThyC.id_to_ctxt "Isac_Knowledge") itm)
203 fun itms2fstr _ (_, _, _, f, I_Model.Cor ((d, ts), _)) = (f, Input_Descript.join''' (d, ts))
204 | itms2fstr _ (_, _, _, f, I_Model.Syn str) = (f, str)
205 | itms2fstr _ (_, _, _, f, I_Model.Typ str) = (f, str)
206 | itms2fstr _ (_, _, _, f, I_Model.Inc ((d, ts), _)) = (f, Input_Descript.join''' (d,ts))
207 | itms2fstr _ (_, _, _, f, I_Model.Sup (d, ts)) = (f, Input_Descript.join''' (d, ts))
208 | itms2fstr ctxt (_, _, _, f, I_Model.Mis (d, t)) = (f, UnparseC.term ctxt (d $ t))
209 | itms2fstr ctxt (itm as (_, _, _, _, I_Model.Par _)) =
210 raise ERROR ("parsitm (" ^ I_Model.single_to_string ctxt itm ^ "): Par should be internal");
212 fun imodel2fstr iitems =
215 | xxx is ((Given strs)::iis) = xxx (is @ (map (pair "#Given") strs)) iis
216 | xxx is ((Find strs)::iis) = xxx (is @ (map (pair "#Find") strs)) iis
217 | xxx is ((Relate strs)::iis) = xxx (is @ (map (pair "#Relate") strs)) iis
218 in xxx [] iitems end;
220 (* input a calchead, WN110505 "prep_oris -> (_, ctxt)" not handled properly *)
221 fun input_icalhd pt (((p, _), hdf, imodel, Pos.Pbl, spec as (dI, pI, mI)) : icalhd) =
223 val (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth) = case Ctree.get_obj I pt p of
224 Ctree.PblObj {fmz = fmz as (fmz_,_), origin = (oris, ospec, hdf'),
225 spec = sspec as (sdI, spI, smI), probl, meth, ctxt, ...}
226 => (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth)
227 | _ => raise ERROR "input_icalhd: uncovered case of get_obj I pt p"
228 val thy = Know_Store.get_via_last_thy dI
229 val ctxt = Proof_Context.init_global thy
230 in if CAS_Cmd.is_from hdf fmz
231 then the (CAS_Cmd.input (ParseC.term_opt ctxt hdf |> the))
233 let val (pos_, pits, mits) =
235 then let val its = map (parsitm (ThyC.get_theory dI)) probl;
236 val (its, trms) = filter_sep is_Par its;
237 val pbt = (#model o Problem.from_store ctxt)
238 (#2 (References.select_input ospec sspec))
239 in (Pos.Pbl, appl_adds dI oris its pbt (map par2fstr trms), meth) end
242 then if pI = snd3 ospec then (Pos.Pbl, probl, meth)
244 let val pbt = (#model o Problem.from_store ctxt) pI
245 val dI' = #1 (References.select_input ospec spec)
247 if pI = #2 ospec then oris
248 else O_Model.init thy fmz_ pbt |> #1;
249 in (Pos.Pbl, appl_adds dI' oris probl pbt
250 (map (itms2fstr ctxt) probl), meth) end
251 else if mI <> smI (*FIXME.WN0311: what if probl is incomplete?!*)
252 then let val met = (#model o MethodC.from_store ctxt) mI
253 val mits = I_Model.complete oris probl meth met
254 in if foldl and_ (true, map #3 mits)
255 then (Pos.Pbl, probl, mits) else (Pos.Met, probl, mits)
257 else (Pos.Pbl, appl_adds (#1 (References.select_input ospec spec)) oris [(*!!!*)]
258 ((#model o Problem.from_store ctxt)
259 (#2 (References.select_input ospec spec)))
260 (imodel2fstr imodel), meth)
261 val pt = Ctree.update_spec pt p spec;
265 val {where_rls, where_,...} = Problem.from_store ctxt
266 (#2 (References.select_input ospec spec))
267 val (_, where_) = Pre_Conds.check ctxt where_rls where_ pits 0
268 in (Ctree.update_pbl pt p pits,
269 (SpecificationC.is_complete' pits where_ spec, Pos.Pbl, hdf', pits, where_, spec))
273 val {where_rls,where_,...} = MethodC.from_store ctxt
274 (#3 (References.select_input ospec spec))
275 val (_, where_) = Pre_Conds.check ctxt where_rls where_ mits 0
276 in (Ctree.update_met pt p mits,
277 (SpecificationC.is_complete' mits where_ spec, Pos.Met, hdf', mits, where_, spec))
281 | input_icalhd _ (_, _, _, _(*Met*), _) = raise ERROR "input_icalhd Met not impl."