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 =
10 (*/------- rename -------\*)
12 Find of TermC.as_string list
13 | Given of TermC.as_string list
14 | Relate of TermC.as_string list
15 type imodel = iitem list
16 type icalhd = Pos.pos' * TermC.as_string * imodel * Pos.pos_ * References.T
18 val input_icalhd : Ctree.ctree -> icalhd -> Ctree.ctree * SpecificationC.T
19 (* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
21 (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
22 val appl_add': ThyC.id -> O_Model.T -> I_Model.T -> Model_Pattern.T ->
23 string * TermC.as_string -> I_Model.single
24 val appl_adds: ThyC.id -> O_Model.T -> I_Model.T -> Model_Pattern.T ->
25 (string * TermC.as_string) list -> I_Model.T
26 val eq7: ''a * ''b -> ''a * (''b * 'c) -> bool
27 val filter_sep: ('a -> bool) -> 'a list -> 'a list * 'a list
28 val fstr2itm_: theory -> (''a * (term * term)) list -> ''a * string ->
29 int list * bool * ''a * I_Model.feedback (*I_Model.single'*)
30 val imodel2fstr: iitem list -> (string * TermC.as_string) list
31 val is_Par: 'a * 'b * 'c * 'd * I_Model.feedback -> bool (*I_Model.T?*)
32 val is_e_ts: term list -> bool
33 val itms2fstr: I_Model.single -> string * string
34 val par2fstr: I_Model.single -> string * TermC.as_string
35 val parsitm: theory -> I_Model.single -> I_Model.single
36 val unknown_expl: ThyC.id -> (''a * (term * term)) list -> (*Model_Pattern.T?*)
37 (''a * string) list ->
38 (int * int list * bool * ''a * Model_Def.i_model_feedback) list (*I_Model.T?*)
39 ( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
40 (*\------- rename -------/*)
44 structure P_Spec(**): PRESENTATION_SPECIFICATION(**) =
48 fun flattup2 (a, (b ,c, d, e)) = (a, b, c, d, e)
50 (** handle an input P_Specific'action **)
53 Given of TermC.as_string list
54 (*Where is still not input*)
55 | Find of TermC.as_string list
56 | Relate of TermC.as_string list
58 type imodel = iitem list
61 Pos.pos' * (* the position in Ctree *)
62 TermC.as_string * (* the headline shown on Calc.T *)
63 imodel * (* the model *)
64 Pos.pos_ * (* model belongs to Problem or Method *)
65 References.T; (* into Know_Store *)
66 val empty = (Pos.e_pos', "", [Given [""]], Pos.Pbl, References.empty)
68 (* re-parse itms with a new thy and prepare for checking with ori list *)
69 fun parsitm dI (itm as (i, v, _, f, I_Model.Cor ((d, ts), _))) =
70 (let val t = Input_Descript.join (d, ts)
71 val _ = (UnparseC.term_in_thy dI t)
72 (*t his ^^^^^^^^^^^^ should raise the exn on unability of re-parsing dts *)
74 handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t ..(t) has not been declared*))))
75 | parsitm dI (i, v, b, f, I_Model.Syn str) =
76 (let val _ = (Thm.term_of o the o (TermC.parse dI)) str
77 in (i, v, b ,f, I_Model.Par str) end
78 handle _ => (i, v, b, f, I_Model.Syn str))
79 | parsitm dI (i, v, b, f, I_Model.Typ str) =
80 (let val _ = (Thm.term_of o the o (TermC.parse dI)) str
81 in (i, v, b, f, I_Model.Par str) end
82 handle _ => (i, v, b, f, I_Model.Syn str))
83 | parsitm dI (itm as (i, v, _, f, I_Model.Inc ((d, ts), _))) =
84 (let val t = Input_Descript.join (d,ts);
85 val _ = UnparseC.term_in_thy dI t;
86 (*this ^ should raise the exn on unability of re-parsing dts*)
88 handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t ..(t) has not been declared*))))
89 | parsitm dI (itm as (i, v, _, f, I_Model.Sup (d, ts))) =
90 (let val t = Input_Descript.join (d,ts);
91 val _ = UnparseC.term_in_thy dI t;
92 (*this ^ should raise the exn on unability of re-parsing dts*)
94 handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t ..(t) has not been declared*))))
95 | parsitm dI (itm as (i, v, _, f, I_Model.Mis (d, t'))) =
97 val _ = UnparseC.term_in_thy dI t;
98 (*this ^ should raise the exn on unability of re-parsing dts*)
100 handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t ..(t) has not been declared*))))
101 | parsitm dI (itm as (_, _, _, _, I_Model.Par _)) =
102 raise ERROR ("parsitm (" ^ I_Model.single_to_string (ThyC.to_ctxt dI) itm ^ "): Par should be internal");
104 (*separate a list to a pair of elements that do NOT satisfy the predicate,
105 and of elements that satisfy the predicate, i.e. (false, true)*)
106 fun filter_sep pred xs =
109 | filt (a, b) (x :: xs) =
111 then filt (a, b @ [x]) xs
112 else filt (a @ [x], b) xs
113 in filt ([], []) xs end;
114 fun is_Par (_, _, _, _, I_Model.Par _) = true
117 fun is_e_ts [] = true
118 | is_e_ts [Const ("List.list.Nil", _)] = true
121 (* WN.9.11.03 copied from fun appl_add *)
122 fun appl_add' dI oris ppc pbt (sel, ct) =
124 val ctxt = ThyC.get_theory dI |> ThyC.to_ctxt;
126 case TermC.parseNEW ctxt ct of
127 NONE => (0, [], false, sel, I_Model.Syn ct)
129 (case O_Model.is_known ctxt sel oris t of
131 (case I_Model.is_notyet_input ctxt ppc all ori' pbt of
133 | (msg,_) => raise ERROR ("appl_add': " ^ msg))
134 | (_, (i, v, _, d, ts), _) =>
136 then (i, v, false, sel, I_Model.Inc ((d, ts), (TermC.empty, [])))
137 else (i, v, false, sel, I_Model.Sup (d, ts)))
140 (* generate preliminary itm_ from a strin (with field "#Given" etc.) *)
141 fun eq7 (f, d) (f', (d', _)) = f = f' andalso d = d';
142 fun fstr2itm_ thy pbt (f, str) =
144 val topt = TermC.parse thy str
147 NONE => ([], false, f, I_Model.Syn str)
150 val (d, ts) = (Input_Descript.split o Thm.term_of) ct
151 val popt = find_first (eq7 (f, d)) pbt
154 NONE => ([1](*??*), true(*??*), f, I_Model.Sup (d, ts))
155 | SOME (f, (d, id)) => ([1], true, f, I_Model.Cor ((d, ts), (id, ts)))
159 (*.input into empty PblObj, i.e. empty fmz+origin (unknown example).*)
160 fun unknown_expl dI pbt selcts =
162 val thy = ThyC.get_theory dI
163 val its_ = map (fstr2itm_ thy pbt) selcts (*([1],true,"#Given",Cor (...))*)
164 val its = O_Model.add_id its_
165 in map flattup2 its end
167 (* WN.11.03 for input_icalhd, ~ specify_additem for Add_Given/_Find/_Relation
168 appl_add': generate 1 item
169 appl_add' . is_known: parse, get data from oris (vats, all (elems if list)..)
170 appl_add' . is_notyet_input: compare with items in model already input
171 insert_ppc': insert this 1 item*)
172 fun appl_adds dI [] _ pbt selcts = unknown_expl dI pbt selcts
173 (*already present itms in model are being overwritten*)
174 | appl_adds _ _ ppc _ [] = ppc
175 | appl_adds dI oris ppc pbt (selct :: ss) =
176 let val itm = appl_add' dI oris ppc pbt selct;
177 in appl_adds dI oris (I_Model.add itm ppc) pbt ss end
179 fun par2fstr (_, _, _, f, I_Model.Par s) = (f, s)
180 | par2fstr itm = raise ERROR ("par2fstr: called with " ^ I_Model.single_to_string (ThyC.id_to_ctxt "Isac_Knowledge") itm)
181 fun itms2fstr (_, _, _, f, I_Model.Cor ((d, ts), _)) = (f, Input_Descript.join''' (d, ts))
182 | itms2fstr (_, _, _, f, I_Model.Syn str) = (f, str)
183 | itms2fstr (_, _, _, f, I_Model.Typ str) = (f, str)
184 | itms2fstr (_, _, _, f, I_Model.Inc ((d, ts), _)) = (f, Input_Descript.join''' (d,ts))
185 | itms2fstr (_, _, _, f, I_Model.Sup (d, ts)) = (f, Input_Descript.join''' (d, ts))
186 | itms2fstr (_, _, _, f, I_Model.Mis (d, t)) = (f, UnparseC.term (d $ t))
187 | itms2fstr (itm as (_, _, _, _, I_Model.Par _)) =
188 raise ERROR ("parsitm (" ^ I_Model.single_to_string (ThyC.id_to_ctxt "Isac_Knowledge") itm ^ "): Par should be internal");
190 fun imodel2fstr iitems =
193 | xxx is ((Given strs)::iis) = xxx (is @ (map (pair "#Given") strs)) iis
194 | xxx is ((Find strs)::iis) = xxx (is @ (map (pair "#Find") strs)) iis
195 | xxx is ((Relate strs)::iis) = xxx (is @ (map (pair "#Relate") strs)) iis
196 in xxx [] iitems end;
198 (* input a calchead, WN110505 "prep_oris -> (_, ctxt)" not handled properly *)
199 fun input_icalhd pt (((p, _), hdf, imodel, Pos.Pbl, spec as (dI, pI, mI)) : icalhd) =
201 val (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth) = case Ctree.get_obj I pt p of
202 Ctree.PblObj {fmz = fmz as (fmz_,_), origin = (oris, ospec, hdf'),
203 spec = sspec as (sdI, spI, smI), probl, meth, ...}
204 => (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth)
205 | _ => raise ERROR "input_icalhd: uncovered case of get_obj I pt p"
206 in if CAS_Cmd.is_from hdf fmz then the (CAS_Cmd.input (TermC.str2term hdf))
207 else (*hacked WN0602 ~~~ ~~~~~~~~~, ..dropped !*)
208 let val (pos_, pits, mits) =
210 then let val its = map (parsitm (ThyC.get_theory dI)) probl;
211 val (its, trms) = filter_sep is_Par its;
212 val pbt = (#ppc o Problem.from_store) (#2 (References.select ospec sspec))
213 in (Pos.Pbl, appl_adds dI oris its pbt (map par2fstr trms), meth) end
216 then if pI = snd3 ospec then (Pos.Pbl, probl, meth)
218 let val pbt = (#ppc o Problem.from_store) pI
219 val dI' = #1 (References.select ospec spec)
220 val oris = if pI = #2 ospec then oris
221 else O_Model.init fmz_(ThyC.get_theory"Isac_Knowledge") pbt(* |> #1*);
222 in (Pos.Pbl, appl_adds dI' oris probl pbt
223 (map itms2fstr probl), meth) end
224 else if mI <> smI (*FIXME.WN0311: what if probl is incomplete?!*)
225 then let val met = (#ppc o Method.from_store) mI
226 val mits = I_Model.complete oris probl meth met
227 in if foldl and_ (true, map #3 mits)
228 then (Pos.Pbl, probl, mits) else (Pos.Met, probl, mits)
230 else (Pos.Pbl, appl_adds (#1 (References.select ospec spec)) oris [(*!!!*)]
231 ((#ppc o Problem.from_store) (#2 (References.select ospec spec)))
232 (imodel2fstr imodel), meth)
233 val pt = Ctree.update_spec pt p spec;
235 then let val {prls,where_,...} = Problem.from_store (#2 (References.select ospec spec))
236 val (_, pre) = Pre_Conds.check prls where_ pits 0
237 in (Ctree.update_pbl pt p pits,
238 (SpecificationC.complete pits pre spec, Pos.Pbl, hdf', pits, pre, spec): SpecificationC.T)
240 else let val {prls,pre,...} = Method.from_store (#3 (References.select ospec spec))
241 val (_, pre) = Pre_Conds.check prls pre mits 0
242 in (Ctree.update_met pt p mits,
243 (SpecificationC.complete mits pre spec, Pos.Met, hdf', mits, pre, spec) : SpecificationC.T)
247 | input_icalhd _ (_, _, _, _(*Met*), _) = raise ERROR "input_icalhd Met not impl."