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: 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 dI (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 dI t)
77 (*t his ^^^^^^^^^^^^ should raise the exn on unability of re-parsing dts *)
80 | NONE => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t ..(t) has not been declared*))))
81 | parsitm dI (i, v, b, f, I_Model.Syn str) =
83 let val _ = TermC.parseNEW'' dI str
84 in (i, v, b ,f, I_Model.Par str) end\<close> of
86 | NONE => (i, v, b, f, I_Model.Syn str))
87 | parsitm dI (i, v, b, f, I_Model.Typ str) =
89 let val _ = TermC.parseNEW'' dI str
90 in (i, v, b, f, I_Model.Par str) end\<close> of
92 | NONE => (i, v, b, f, I_Model.Syn str))
93 | parsitm dI (itm as (i, v, _, f, I_Model.Inc ((d, ts), _))) =
95 let val t = Input_Descript.join (d,ts);
96 val _ = UnparseC.term_in_thy dI t;
97 (*this ^ should raise the exn on unability of re-parsing dts*)
100 | NONE => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t ..(t) has not been declared*))))
101 | parsitm dI (itm as (i, v, _, f, I_Model.Sup (d, ts))) =
103 let val t = Input_Descript.join (d,ts);
104 val _ = UnparseC.term_in_thy dI t;
105 (*this ^ should raise the exn on unability of re-parsing dts*)
106 in itm end\<close> of
108 | NONE => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t ..(t) has not been declared*))))
109 | parsitm dI (itm as (i, v, _, f, I_Model.Mis (d, t'))) =
112 val _ = UnparseC.term_in_thy dI t;
113 (*this ^ should raise the exn on unability of re-parsing dts*)
114 in itm end\<close> of
116 | NONE => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t ..(t) has not been declared*))))
117 | parsitm dI (itm as (_, _, _, _, I_Model.Par _)) =
118 raise ERROR ("parsitm (" ^ I_Model.single_to_string (Proof_Context.init_global dI) itm ^
119 "): Par should be internal");
121 (*separate a list to a pair of elements that do NOT satisfy the predicate,
122 and of elements that satisfy the predicate, i.e. (false, true)*)
123 fun filter_sep pred xs =
126 | filt (a, b) (x :: xs) =
128 then filt (a, b @ [x]) xs
129 else filt (a @ [x], b) xs
130 in filt ([], []) xs end;
131 fun is_Par (_, _, _, _, I_Model.Par _) = true
134 fun is_e_ts [] = true
135 | is_e_ts [Const (\<^const_name>\<open>Nil\<close>, _)] = true
138 (* WN.9.11.03 copied from fun appl_add *)
139 fun appl_add' dI oris ppc pbt (sel, ct) =
141 val ctxt = ThyC.get_theory dI |> Proof_Context.init_global;
143 case TermC.parseNEW ctxt ct of
144 NONE => (0, [], false, sel, I_Model.Syn ct)
146 (case O_Model.contains ctxt sel oris t of
148 (case I_Model.is_notyet_input ctxt ppc all ori' pbt of
150 | (msg,_) => raise ERROR ("appl_add': " ^ msg))
151 | (_, (i, v, _, d, ts), _) =>
153 then (i, v, false, sel, I_Model.Inc ((d, ts), (TermC.empty, [])))
154 else (i, v, false, sel, I_Model.Sup (d, ts)))
157 (* generate preliminary itm_ from a string (with field "#Given" etc.) *)
158 fun eq7 (f, d) (f', (d', _)) = f = f' andalso d = d';
159 fun fstr2itm_ thy pbt (f, str) =
161 val topt = TermC.parseNEW (Proof_Context.init_global thy) str
164 NONE => ([], false, f, I_Model.Syn str)
167 val (d, ts) = Input_Descript.split ct
168 val popt = find_first (eq7 (f, d)) pbt
171 NONE => ([1](*??*), true(*??*), f, I_Model.Sup (d, ts))
172 | SOME (f, (d, id)) => ([1], true, f, I_Model.Cor ((d, ts), (id, ts)))
176 (*.input into empty PblObj, i.e. empty fmz+origin (unknown example).*)
177 fun unknown_expl dI pbt selcts =
179 val thy = ThyC.get_theory dI
180 val its_ = map (fstr2itm_ thy pbt) selcts (*([1],true,"#Given",Cor (...))*)
181 val its = O_Model.add_enumerate its_
182 in map flattup2 its end
184 (* WN.11.03 for input_icalhd, ~ specify_additem for Add_Given/_Find/_Relation
185 appl_add': generate 1 item
186 appl_add' . contains: parse, get data from oris (vats, all (elems if list)..)
187 appl_add' . is_notyet_input: compare with items in model already input
188 insert_ppc': insert this 1 item*)
189 fun appl_adds dI [] _ pbt selcts = unknown_expl dI pbt selcts
190 (*already present itms in model are being overwritten*)
191 | appl_adds _ _ ppc _ [] = ppc
192 | appl_adds dI oris ppc pbt (selct :: ss) =
193 let val itm = appl_add' dI oris ppc pbt selct;
194 in appl_adds dI oris (I_Model.add itm ppc) pbt ss end
196 fun par2fstr (_, _, _, f, I_Model.Par s) = (f, s)
197 | par2fstr itm = raise ERROR ("par2fstr: called with " ^ I_Model.single_to_string (ThyC.id_to_ctxt "Isac_Knowledge") itm)
198 fun itms2fstr (_, _, _, f, I_Model.Cor ((d, ts), _)) = (f, Input_Descript.join''' (d, ts))
199 | itms2fstr (_, _, _, f, I_Model.Syn str) = (f, str)
200 | itms2fstr (_, _, _, f, I_Model.Typ str) = (f, str)
201 | itms2fstr (_, _, _, f, I_Model.Inc ((d, ts), _)) = (f, Input_Descript.join''' (d,ts))
202 | itms2fstr (_, _, _, f, I_Model.Sup (d, ts)) = (f, Input_Descript.join''' (d, ts))
203 | itms2fstr (_, _, _, f, I_Model.Mis (d, t)) = (f, UnparseC.term (d $ t))
204 | itms2fstr (itm as (_, _, _, _, I_Model.Par _)) =
205 raise ERROR ("parsitm (" ^ I_Model.single_to_string (ThyC.id_to_ctxt "Isac_Knowledge") itm ^ "): Par should be internal");
207 fun imodel2fstr iitems =
210 | xxx is ((Given strs)::iis) = xxx (is @ (map (pair "#Given") strs)) iis
211 | xxx is ((Find strs)::iis) = xxx (is @ (map (pair "#Find") strs)) iis
212 | xxx is ((Relate strs)::iis) = xxx (is @ (map (pair "#Relate") strs)) iis
213 in xxx [] iitems end;
215 (* input a calchead, WN110505 "prep_oris -> (_, ctxt)" not handled properly *)
216 fun input_icalhd pt (((p, _), hdf, imodel, Pos.Pbl, spec as (dI, pI, mI)) : icalhd) =
218 val (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth) = case Ctree.get_obj I pt p of
219 Ctree.PblObj {fmz = fmz as (fmz_,_), origin = (oris, ospec, hdf'),
220 spec = sspec as (sdI, spI, smI), probl, meth, ...}
221 => (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth)
222 | _ => raise ERROR "input_icalhd: uncovered case of get_obj I pt p"
223 in if CAS_Cmd.is_from hdf fmz then the (CAS_Cmd.input (TermC.str2term hdf))
224 else (*hacked WN0602 ~~~ ~~~~~~~~~, ..dropped !*)
225 let val (pos_, pits, mits) =
227 then let val its = map (parsitm (ThyC.get_theory dI)) probl;
228 val (its, trms) = filter_sep is_Par its;
229 val pbt = (#ppc o Problem.from_store) (#2 (References.select ospec sspec))
230 in (Pos.Pbl, appl_adds dI oris its pbt (map par2fstr trms), meth) end
233 then if pI = snd3 ospec then (Pos.Pbl, probl, meth)
235 let val pbt = (#ppc o Problem.from_store) pI
236 val dI' = #1 (References.select ospec spec)
238 if pI = #2 ospec then oris
239 else O_Model.init (ThyC.get_theory "Isac_Knowledge") fmz_ pbt(* |> #1*);
240 in (Pos.Pbl, appl_adds dI' oris probl pbt
241 (map itms2fstr probl), meth) end
242 else if mI <> smI (*FIXME.WN0311: what if probl is incomplete?!*)
243 then let val met = (#ppc o MethodC.from_store) mI
244 val mits = I_Model.complete oris probl meth met
245 in if foldl and_ (true, map #3 mits)
246 then (Pos.Pbl, probl, mits) else (Pos.Met, probl, mits)
248 else (Pos.Pbl, appl_adds (#1 (References.select ospec spec)) oris [(*!!!*)]
249 ((#ppc o Problem.from_store) (#2 (References.select ospec spec)))
250 (imodel2fstr imodel), meth)
251 val pt = Ctree.update_spec pt p spec;
253 then let val {prls,where_,...} = Problem.from_store (#2 (References.select ospec spec))
254 val (_, pre) = Pre_Conds.check prls where_ pits 0
255 in (Ctree.update_pbl pt p pits,
256 (SpecificationC.complete pits pre spec, Pos.Pbl, hdf', pits, pre, spec): SpecificationC.T)
258 else let val {prls,pre,...} = MethodC.from_store (#3 (References.select ospec spec))
259 val (_, pre) = Pre_Conds.check prls pre mits 0
260 in (Ctree.update_met pt p mits,
261 (SpecificationC.complete mits pre spec, Pos.Met, hdf', mits, pre, spec) : SpecificationC.T)
265 | input_icalhd _ (_, _, _, _(*Met*), _) = raise ERROR "input_icalhd Met not impl."