1 (* Title: Specify/p-spec.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 fstr2itm_: theory -> (''a * (term * term)) list -> ''a * string ->
27 int list * bool * ''a * I_Model.feedback (*I_Model.single'*)
28 val imodel2fstr: iitem list -> (string * TermC.as_string) list
29 val is_e_ts: term list -> bool
30 val itms2fstr: Proof.context -> I_Model.single -> string * string
31 val par2fstr: I_Model.single -> string * TermC.as_string
32 val unknown_expl: ThyC.id -> (''a * (term * term)) list -> (*Model_Pattern.T*)
33 (''a * string) list ->
34 (int * int list * bool * ''a * Model_Def.i_model_feedback) list (*I_Model.T*)
36 (*\------- rename -------/*)
40 structure P_Spec(**): PRESENTATION_SPECIFICATION(**) =
44 type record = {thy_id: ThyC.id, pbl_id: Problem.id, (* headline of Problem *)
45 givens: TermC.as_string list, wheres: TermC.as_string list, (* Model.T *)
46 find: TermC.as_string, relates: TermC.as_string list,
47 rthy_id: ThyC.id, rpbl_id: Problem.id, rmet_id: MethodC.id} (* References.T *)
49 fun flattup2 (a, (b ,c, d, e)) = (a, b, c, d, e)
51 (** handle an input P_Specific'action **)
54 Given of TermC.as_string list
55 (*Where is still not input*)
56 | Find of TermC.as_string list
57 | Relate of TermC.as_string list
59 type imodel = iitem list
62 Pos.pos' * (* the position in Ctree *)
63 TermC.as_string * (* the headline shown on Calc.T *)
64 imodel * (* the model *)
65 Pos.pos_ * (* model belongs to Problem or MethodC *)
66 References.T; (* into Know_Store *)
67 val empty = (Pos.e_pos', "", [Given [""]], Pos.Pbl, References.empty)
70 | is_e_ts [Const (\<^const_name>\<open>Nil\<close>, _)] = true
73 (* WN.9.11.03 copied from fun appl_add *)
74 fun appl_add' dI oris model pbt (sel, ct) =
76 val ctxt = Know_Store.get_via_last_thy dI |> Proof_Context.init_global;
78 (*/------------ replace by ParseC.term_position ------------------\*)
79 case ParseC.term_opt ctxt ct of
80 (*\------------ replace by ParseC.term_position ------------------/*)
81 NONE => (0, [], false, sel, I_Model.Syn ct)
83 (case O_Model.contains ctxt sel oris t of
85 (case I_Model.is_notyet_input ctxt model all ori' pbt of
87 | (msg,_) => raise ERROR ("appl_add': " ^ msg))
88 | (_, (i, v, _, d, ts), _) =>
90 then (i, v, false, sel, I_Model.Inc ((d, ts), (TermC.empty, [])))
91 else (i, v, false, sel, I_Model.Sup (d, ts)))
94 (* generate preliminary itm_ from a string (with field "#Given" etc.) *)
95 fun eq7 (f, d) (f', (d', _)) = f = f' andalso d = d';
96 fun fstr2itm_ thy pbt (f, str) =
98 (*/------------ replace by ParseC.term_position ? ------------\*)
99 val topt = ParseC.term_opt (Proof_Context.init_global thy) str
100 (*\------------ replace by ParseC.term_position ? ------------/*)
103 NONE => ([], false, f, I_Model.Syn str)
106 val (d, ts) = Input_Descript.split ct
107 val popt = find_first (eq7 (f, d)) pbt
110 NONE => ([1](*??*), true(*??*), f, I_Model.Sup (d, ts))
111 | SOME (f, (d, id)) => ([1], true, f, I_Model.Cor ((d, ts), (id, ts)))
115 (*.input into empty PblObj, i.e. empty fmz+origin (unknown example).*)
116 fun unknown_expl dI pbt selcts =
118 val thy = Know_Store.get_via_last_thy dI
119 val its_ = map (fstr2itm_ thy pbt) selcts (*([1],true,"#Given",Cor (...))*)
120 val its = O_Model.add_enumerate its_
121 in map flattup2 its end
123 fun eq_dsc ((_, _, _, _, itm_), (_, _, _, _, iitm_)) =
124 (I_Model.descriptor itm_ = I_Model.descriptor iitm_)
125 (* handles superfluous items carelessly *)
126 fun add itm itms = if member eq_dsc itms itm then itms else itms @ [itm] (* @ new itm *)
127 fun appl_adds dI [] _ pbt selcts = unknown_expl dI pbt selcts
128 (*already present itms in model are being overwritten*)
129 | appl_adds _ _ model _ [] = model
130 | appl_adds dI oris model pbt (selct :: ss) =
131 let val itm = appl_add' dI oris model pbt selct;
132 in appl_adds dI oris (add itm model) pbt ss end
134 fun par2fstr (_, _, _, f, I_Model.Par s) = (f, s)
135 | par2fstr itm = raise ERROR ("par2fstr: called with " ^
136 I_Model.single_to_string (ContextC.for_ERROR ()) itm)
137 fun itms2fstr _ (_, _, _, f, I_Model.Cor ((d, ts), _)) = (f, Input_Descript.join''' (d, ts))
138 | itms2fstr _ (_, _, _, f, I_Model.Syn str) = (f, str)
139 | itms2fstr _ (_, _, _, f, I_Model.Typ str) = (f, str)
140 | itms2fstr _ (_, _, _, f, I_Model.Inc ((d, ts), _)) = (f, Input_Descript.join''' (d,ts))
141 | itms2fstr _ (_, _, _, f, I_Model.Sup (d, ts)) = (f, Input_Descript.join''' (d, ts))
142 | itms2fstr ctxt (_, _, _, f, I_Model.Mis (d, t)) = (f, UnparseC.term ctxt (d $ t))
143 | itms2fstr ctxt (itm as (_, _, _, _, I_Model.Par _)) =
144 raise ERROR ("itms2fstr (" ^ I_Model.single_to_string ctxt itm ^ "): Par should be internal");
146 fun imodel2fstr iitems =
149 | xxx is ((Given strs)::iis) = xxx (is @ (map (pair "#Given") strs)) iis
150 | xxx is ((Find strs)::iis) = xxx (is @ (map (pair "#Find") strs)) iis
151 | xxx is ((Relate strs)::iis) = xxx (is @ (map (pair "#Relate") strs)) iis
152 in xxx [] iitems end;
154 (* input a calchead, WN110505 "prep_oris -> (_, ctxt)" not handled properly *)
155 fun input_icalhd pt (((p, _), hdf, imodel, Pos.Pbl, spec as (dI, pI, mI)) : icalhd) =
157 val (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth) = case Ctree.get_obj I pt p of
158 Ctree.PblObj {fmz = fmz as (fmz_,_), origin = (oris, ospec, hdf'),
159 spec = sspec as (sdI, spI, smI), probl, meth, ...}
160 => (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth)
161 | _ => raise ERROR "input_icalhd: uncovered case of get_obj I pt p"
162 val thy = Know_Store.get_via_last_thy dI
163 val ctxt = Proof_Context.init_global thy
164 in if CAS_Cmd.is_from hdf fmz
165 then the (CAS_Cmd.input (ParseC.term_opt ctxt hdf |> the))
167 let val (pos_, pits, mits) =
169 then let (* eliminated for PIDE turn 11: take Position into I_Model
170 val its = map (parsitm thy) probl;
171 val (its, trms) = filter_sep is_Par its;*)
172 val pbt = (#model o Problem.from_store ctxt)
173 (#2 (References.select_input ospec sspec))
174 in (Pos.Pbl, appl_adds dI oris [](*its*) pbt (map par2fstr [](*trms*)), meth) end
177 then if pI = snd3 ospec then (Pos.Pbl, probl, meth)
179 val pbt = (#model o Problem.from_store ctxt) pI
180 val dI' = #1 (References.select_input ospec spec)
182 if pI = #2 ospec then oris
183 else O_Model.init thy fmz_ pbt |> #1;
184 in (Pos.Pbl, appl_adds dI' oris probl pbt
185 (map (itms2fstr ctxt) probl), meth) end
186 else if mI <> smI (*FIXME.WN0311: what if probl is incomplete?!*)
188 val (_, mits) = I_Model.s_make_complete ctxt oris
189 (I_Model.OLD_to_TEST probl, I_Model.OLD_to_TEST meth) (pI, mI)
190 in if foldl and_ (true, map #3 mits)
191 then (Pos.Pbl, probl, I_Model.TEST_to_OLD mits)
192 else (Pos.Met, probl, I_Model.TEST_to_OLD mits)
194 else (Pos.Pbl, appl_adds (#1 (References.select_input ospec spec)) oris [(*!!!*)]
195 ((#model o Problem.from_store ctxt)
196 (#2 (References.select_input ospec spec)))
197 (imodel2fstr imodel), meth)
198 val pt = Ctree.update_spec pt p spec;
202 val {where_rls, where_, model, ...} = Problem.from_store ctxt
203 (#2 (References.select_input ospec spec))
204 val (_, where_) = Pre_Conds.check ctxt where_rls where_
205 (model, I_Model.OLD_to_TEST pits)
206 in (Ctree.update_pbl pt p pits,
207 (SpecificationC.is_complete' pits where_ spec, Pos.Pbl, hdf', pits, where_, spec))
211 val {where_rls,where_, model, ...} = MethodC.from_store ctxt
212 (#3 (References.select_input ospec spec))
213 val (_, where_) = Pre_Conds.check ctxt where_rls where_
214 (model, I_Model.OLD_to_TEST mits)
215 in (Ctree.update_met pt p mits,
216 (SpecificationC.is_complete' mits where_ spec, Pos.Met, hdf', mits, where_, spec))
220 | input_icalhd _ (_, _, _, _(*Met*), _) = raise ERROR "input_icalhd Met not impl."