walther@59822
|
1 |
(* Title: Specify/input-calchead.sml
|
walther@59822
|
2 |
Author: Walther Neuper
|
walther@59822
|
3 |
(c) due to copyright terms
|
walther@59960
|
4 |
|
walther@60111
|
5 |
This will be dropped at switch to Isabelle/PIDE .
|
walther@59960
|
6 |
*)
|
walther@59822
|
7 |
|
walther@59984
|
8 |
signature PRESENTATION_SPECIFICATION =
|
walther@59822
|
9 |
sig
|
walther@60132
|
10 |
type record
|
walther@59995
|
11 |
(*/------- rename -------\*)
|
walther@59960
|
12 |
datatype iitem =
|
walther@59960
|
13 |
Find of TermC.as_string list
|
walther@59960
|
14 |
| Given of TermC.as_string list
|
walther@59960
|
15 |
| Relate of TermC.as_string list
|
walther@59822
|
16 |
type imodel = iitem list
|
walther@59976
|
17 |
type icalhd = Pos.pos' * TermC.as_string * imodel * Pos.pos_ * References.T
|
walther@59995
|
18 |
val empty: icalhd
|
walther@60097
|
19 |
val input_icalhd : Ctree.ctree -> icalhd -> Ctree.ctree * SpecificationC.T
|
walther@59822
|
20 |
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
|
walther@59822
|
21 |
(* NONE *)
|
walther@59886
|
22 |
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
|
walther@59977
|
23 |
val appl_add': ThyC.id -> O_Model.T -> I_Model.T -> Model_Pattern.T ->
|
walther@59977
|
24 |
string * TermC.as_string -> I_Model.single
|
walther@59977
|
25 |
val appl_adds: ThyC.id -> O_Model.T -> I_Model.T -> Model_Pattern.T ->
|
walther@59977
|
26 |
(string * TermC.as_string) list -> I_Model.T
|
walther@59977
|
27 |
val eq7: ''a * ''b -> ''a * (''b * 'c) -> bool
|
walther@59977
|
28 |
val filter_sep: ('a -> bool) -> 'a list -> 'a list * 'a list
|
walther@59977
|
29 |
val fstr2itm_: theory -> (''a * (term * term)) list -> ''a * string ->
|
walther@59977
|
30 |
int list * bool * ''a * I_Model.feedback (*I_Model.single'*)
|
walther@59977
|
31 |
val imodel2fstr: iitem list -> (string * TermC.as_string) list
|
walther@59977
|
32 |
val is_Par: 'a * 'b * 'c * 'd * I_Model.feedback -> bool (*I_Model.T?*)
|
walther@59977
|
33 |
val is_e_ts: term list -> bool
|
walther@59977
|
34 |
val itms2fstr: I_Model.single -> string * string
|
walther@59977
|
35 |
val par2fstr: I_Model.single -> string * TermC.as_string
|
walther@59977
|
36 |
val parsitm: theory -> I_Model.single -> I_Model.single
|
walther@59977
|
37 |
val unknown_expl: ThyC.id -> (''a * (term * term)) list -> (*Model_Pattern.T?*)
|
walther@59977
|
38 |
(''a * string) list ->
|
walther@59977
|
39 |
(int * int list * bool * ''a * Model_Def.i_model_feedback) list (*I_Model.T?*)
|
walther@59886
|
40 |
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
|
walther@59995
|
41 |
(*\------- rename -------/*)
|
walther@59822
|
42 |
end
|
walther@59822
|
43 |
|
walther@59977
|
44 |
(**)
|
walther@59984
|
45 |
structure P_Spec(**): PRESENTATION_SPECIFICATION(**) =
|
walther@59822
|
46 |
struct
|
walther@59977
|
47 |
(**)
|
walther@59822
|
48 |
|
walther@60132
|
49 |
type record = {thy_id: ThyC.id, pbl_id: Problem.id, (* headline of Problem *)
|
walther@60132
|
50 |
givens: TermC.as_string list, wheres: TermC.as_string list, (* Model.T *)
|
walther@60132
|
51 |
find: TermC.as_string, relates: TermC.as_string list,
|
walther@60154
|
52 |
rthy_id: ThyC.id, rpbl_id: Problem.id, rmet_id: MethodC.id} (* References.T *)
|
walther@60132
|
53 |
|
walther@59822
|
54 |
fun flattup2 (a, (b ,c, d, e)) = (a, b, c, d, e)
|
walther@59822
|
55 |
|
walther@59982
|
56 |
(** handle an input P_Specific'action **)
|
walther@59822
|
57 |
|
walther@59822
|
58 |
datatype iitem =
|
walther@59865
|
59 |
Given of TermC.as_string list
|
walther@59977
|
60 |
(*Where is still not input*)
|
walther@59865
|
61 |
| Find of TermC.as_string list
|
walther@59865
|
62 |
| Relate of TermC.as_string list
|
walther@59822
|
63 |
|
walther@59822
|
64 |
type imodel = iitem list
|
walther@59822
|
65 |
|
walther@59822
|
66 |
type icalhd =
|
walther@59977
|
67 |
Pos.pos' * (* the position in Ctree *)
|
walther@59977
|
68 |
TermC.as_string * (* the headline shown on Calc.T *)
|
walther@59977
|
69 |
imodel * (* the model *)
|
walther@60154
|
70 |
Pos.pos_ * (* model belongs to Problem or MethodC *)
|
walther@59977
|
71 |
References.T; (* into Know_Store *)
|
walther@59995
|
72 |
val empty = (Pos.e_pos', "", [Given [""]], Pos.Pbl, References.empty)
|
walther@59822
|
73 |
|
walther@59822
|
74 |
(* re-parse itms with a new thy and prepare for checking with ori list *)
|
walther@59943
|
75 |
fun parsitm dI (itm as (i, v, _, f, I_Model.Cor ((d, ts), _))) =
|
walther@59953
|
76 |
(let val t = Input_Descript.join (d, ts)
|
walther@59870
|
77 |
val _ = (UnparseC.term_in_thy dI t)
|
walther@59822
|
78 |
(*t his ^^^^^^^^^^^^ should raise the exn on unability of re-parsing dts *)
|
walther@59822
|
79 |
in itm end
|
walther@59943
|
80 |
handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t ..(t) has not been declared*))))
|
walther@59943
|
81 |
| parsitm dI (i, v, b, f, I_Model.Syn str) =
|
walther@59822
|
82 |
(let val _ = (Thm.term_of o the o (TermC.parse dI)) str
|
walther@59943
|
83 |
in (i, v, b ,f, I_Model.Par str) end
|
walther@59943
|
84 |
handle _ => (i, v, b, f, I_Model.Syn str))
|
walther@59943
|
85 |
| parsitm dI (i, v, b, f, I_Model.Typ str) =
|
walther@59822
|
86 |
(let val _ = (Thm.term_of o the o (TermC.parse dI)) str
|
walther@59943
|
87 |
in (i, v, b, f, I_Model.Par str) end
|
walther@59943
|
88 |
handle _ => (i, v, b, f, I_Model.Syn str))
|
walther@59943
|
89 |
| parsitm dI (itm as (i, v, _, f, I_Model.Inc ((d, ts), _))) =
|
walther@59953
|
90 |
(let val t = Input_Descript.join (d,ts);
|
walther@59870
|
91 |
val _ = UnparseC.term_in_thy dI t;
|
walther@59822
|
92 |
(*this ^ should raise the exn on unability of re-parsing dts*)
|
walther@59822
|
93 |
in itm end
|
walther@59943
|
94 |
handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t ..(t) has not been declared*))))
|
walther@59943
|
95 |
| parsitm dI (itm as (i, v, _, f, I_Model.Sup (d, ts))) =
|
walther@59953
|
96 |
(let val t = Input_Descript.join (d,ts);
|
walther@59870
|
97 |
val _ = UnparseC.term_in_thy dI t;
|
walther@59822
|
98 |
(*this ^ should raise the exn on unability of re-parsing dts*)
|
walther@59822
|
99 |
in itm end
|
walther@59943
|
100 |
handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t ..(t) has not been declared*))))
|
walther@59943
|
101 |
| parsitm dI (itm as (i, v, _, f, I_Model.Mis (d, t'))) =
|
walther@59822
|
102 |
(let val t = d $ t';
|
walther@59870
|
103 |
val _ = UnparseC.term_in_thy dI t;
|
walther@59822
|
104 |
(*this ^ should raise the exn on unability of re-parsing dts*)
|
walther@59822
|
105 |
in itm end
|
walther@59943
|
106 |
handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t ..(t) has not been declared*))))
|
walther@59943
|
107 |
| parsitm dI (itm as (_, _, _, _, I_Model.Par _)) =
|
walther@59962
|
108 |
raise ERROR ("parsitm (" ^ I_Model.single_to_string (ThyC.to_ctxt dI) itm ^ "): Par should be internal");
|
walther@59822
|
109 |
|
walther@59822
|
110 |
(*separate a list to a pair of elements that do NOT satisfy the predicate,
|
walther@59822
|
111 |
and of elements that satisfy the predicate, i.e. (false, true)*)
|
walther@59822
|
112 |
fun filter_sep pred xs =
|
walther@59822
|
113 |
let
|
walther@59822
|
114 |
fun filt ab [] = ab
|
walther@59822
|
115 |
| filt (a, b) (x :: xs) =
|
walther@59822
|
116 |
if pred x
|
walther@59822
|
117 |
then filt (a, b @ [x]) xs
|
walther@59822
|
118 |
else filt (a @ [x], b) xs
|
walther@59822
|
119 |
in filt ([], []) xs end;
|
walther@59943
|
120 |
fun is_Par (_, _, _, _, I_Model.Par _) = true
|
walther@59822
|
121 |
| is_Par _ = false;
|
walther@59822
|
122 |
|
walther@59822
|
123 |
fun is_e_ts [] = true
|
walther@59822
|
124 |
| is_e_ts [Const ("List.list.Nil", _)] = true
|
walther@59822
|
125 |
| is_e_ts _ = false;
|
walther@59822
|
126 |
|
walther@59822
|
127 |
(* WN.9.11.03 copied from fun appl_add *)
|
walther@59822
|
128 |
fun appl_add' dI oris ppc pbt (sel, ct) =
|
walther@59822
|
129 |
let
|
walther@59881
|
130 |
val ctxt = ThyC.get_theory dI |> ThyC.to_ctxt;
|
walther@59822
|
131 |
in
|
walther@59822
|
132 |
case TermC.parseNEW ctxt ct of
|
walther@59943
|
133 |
NONE => (0, [], false, sel, I_Model.Syn ct)
|
walther@59822
|
134 |
| SOME t =>
|
walther@60000
|
135 |
(case O_Model.is_known ctxt sel oris t of
|
walther@59822
|
136 |
("", ori', all) =>
|
walther@59956
|
137 |
(case I_Model.is_notyet_input ctxt ppc all ori' pbt of
|
walther@59822
|
138 |
("",itm) => itm
|
walther@59962
|
139 |
| (msg,_) => raise ERROR ("appl_add': " ^ msg))
|
walther@59822
|
140 |
| (_, (i, v, _, d, ts), _) =>
|
walther@59822
|
141 |
if is_e_ts ts
|
walther@59943
|
142 |
then (i, v, false, sel, I_Model.Inc ((d, ts), (TermC.empty, [])))
|
walther@59943
|
143 |
else (i, v, false, sel, I_Model.Sup (d, ts)))
|
walther@59822
|
144 |
end
|
walther@59822
|
145 |
|
walther@59822
|
146 |
(* generate preliminary itm_ from a strin (with field "#Given" etc.) *)
|
walther@59822
|
147 |
fun eq7 (f, d) (f', (d', _)) = f = f' andalso d = d';
|
walther@59822
|
148 |
fun fstr2itm_ thy pbt (f, str) =
|
walther@59822
|
149 |
let
|
walther@59822
|
150 |
val topt = TermC.parse thy str
|
walther@59822
|
151 |
in
|
walther@59822
|
152 |
case topt of
|
walther@59943
|
153 |
NONE => ([], false, f, I_Model.Syn str)
|
walther@59822
|
154 |
| SOME ct =>
|
walther@59822
|
155 |
let
|
walther@59953
|
156 |
val (d, ts) = (Input_Descript.split o Thm.term_of) ct
|
walther@59822
|
157 |
val popt = find_first (eq7 (f, d)) pbt
|
walther@59822
|
158 |
in
|
walther@59822
|
159 |
case popt of
|
walther@59943
|
160 |
NONE => ([1](*??*), true(*??*), f, I_Model.Sup (d, ts))
|
walther@59943
|
161 |
| SOME (f, (d, id)) => ([1], true, f, I_Model.Cor ((d, ts), (id, ts)))
|
walther@59822
|
162 |
end
|
walther@59822
|
163 |
end
|
walther@59822
|
164 |
|
walther@59822
|
165 |
(*.input into empty PblObj, i.e. empty fmz+origin (unknown example).*)
|
walther@59822
|
166 |
fun unknown_expl dI pbt selcts =
|
walther@59822
|
167 |
let
|
walther@59881
|
168 |
val thy = ThyC.get_theory dI
|
walther@59822
|
169 |
val its_ = map (fstr2itm_ thy pbt) selcts (*([1],true,"#Given",Cor (...))*)
|
walther@59947
|
170 |
val its = O_Model.add_id its_
|
walther@59822
|
171 |
in map flattup2 its end
|
walther@59822
|
172 |
|
walther@59822
|
173 |
(* WN.11.03 for input_icalhd, ~ specify_additem for Add_Given/_Find/_Relation
|
walther@59822
|
174 |
appl_add': generate 1 item
|
walther@59822
|
175 |
appl_add' . is_known: parse, get data from oris (vats, all (elems if list)..)
|
walther@59822
|
176 |
appl_add' . is_notyet_input: compare with items in model already input
|
walther@59822
|
177 |
insert_ppc': insert this 1 item*)
|
walther@59822
|
178 |
fun appl_adds dI [] _ pbt selcts = unknown_expl dI pbt selcts
|
walther@59822
|
179 |
(*already present itms in model are being overwritten*)
|
walther@59822
|
180 |
| appl_adds _ _ ppc _ [] = ppc
|
walther@59822
|
181 |
| appl_adds dI oris ppc pbt (selct :: ss) =
|
walther@59822
|
182 |
let val itm = appl_add' dI oris ppc pbt selct;
|
walther@59988
|
183 |
in appl_adds dI oris (I_Model.add itm ppc) pbt ss end
|
walther@59822
|
184 |
|
walther@59943
|
185 |
fun par2fstr (_, _, _, f, I_Model.Par s) = (f, s)
|
walther@59962
|
186 |
| par2fstr itm = raise ERROR ("par2fstr: called with " ^ I_Model.single_to_string (ThyC.id_to_ctxt "Isac_Knowledge") itm)
|
walther@59953
|
187 |
fun itms2fstr (_, _, _, f, I_Model.Cor ((d, ts), _)) = (f, Input_Descript.join''' (d, ts))
|
walther@59943
|
188 |
| itms2fstr (_, _, _, f, I_Model.Syn str) = (f, str)
|
walther@59943
|
189 |
| itms2fstr (_, _, _, f, I_Model.Typ str) = (f, str)
|
walther@59953
|
190 |
| itms2fstr (_, _, _, f, I_Model.Inc ((d, ts), _)) = (f, Input_Descript.join''' (d,ts))
|
walther@59953
|
191 |
| itms2fstr (_, _, _, f, I_Model.Sup (d, ts)) = (f, Input_Descript.join''' (d, ts))
|
walther@59943
|
192 |
| itms2fstr (_, _, _, f, I_Model.Mis (d, t)) = (f, UnparseC.term (d $ t))
|
walther@59943
|
193 |
| itms2fstr (itm as (_, _, _, _, I_Model.Par _)) =
|
walther@59962
|
194 |
raise ERROR ("parsitm (" ^ I_Model.single_to_string (ThyC.id_to_ctxt "Isac_Knowledge") itm ^ "): Par should be internal");
|
walther@59822
|
195 |
|
walther@59822
|
196 |
fun imodel2fstr iitems =
|
walther@59822
|
197 |
let
|
walther@59822
|
198 |
fun xxx is [] = is
|
walther@59822
|
199 |
| xxx is ((Given strs)::iis) = xxx (is @ (map (pair "#Given") strs)) iis
|
walther@59822
|
200 |
| xxx is ((Find strs)::iis) = xxx (is @ (map (pair "#Find") strs)) iis
|
walther@59822
|
201 |
| xxx is ((Relate strs)::iis) = xxx (is @ (map (pair "#Relate") strs)) iis
|
walther@59822
|
202 |
in xxx [] iitems end;
|
walther@59822
|
203 |
|
walther@59822
|
204 |
(* input a calchead, WN110505 "prep_oris -> (_, ctxt)" not handled properly *)
|
walther@59822
|
205 |
fun input_icalhd pt (((p, _), hdf, imodel, Pos.Pbl, spec as (dI, pI, mI)) : icalhd) =
|
walther@59822
|
206 |
let
|
walther@59822
|
207 |
val (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth) = case Ctree.get_obj I pt p of
|
walther@59822
|
208 |
Ctree.PblObj {fmz = fmz as (fmz_,_), origin = (oris, ospec, hdf'),
|
walther@59822
|
209 |
spec = sspec as (sdI, spI, smI), probl, meth, ...}
|
walther@59822
|
210 |
=> (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth)
|
walther@59962
|
211 |
| _ => raise ERROR "input_icalhd: uncovered case of get_obj I pt p"
|
walther@59982
|
212 |
in if CAS_Cmd.is_from hdf fmz then the (CAS_Cmd.input (TermC.str2term hdf))
|
walther@59822
|
213 |
else (*hacked WN0602 ~~~ ~~~~~~~~~, ..dropped !*)
|
walther@59822
|
214 |
let val (pos_, pits, mits) =
|
walther@59822
|
215 |
if dI <> sdI
|
walther@59881
|
216 |
then let val its = map (parsitm (ThyC.get_theory dI)) probl;
|
walther@59822
|
217 |
val (its, trms) = filter_sep is_Par its;
|
walther@59983
|
218 |
val pbt = (#ppc o Problem.from_store) (#2 (References.select ospec sspec))
|
walther@59822
|
219 |
in (Pos.Pbl, appl_adds dI oris its pbt (map par2fstr trms), meth) end
|
walther@59822
|
220 |
else
|
walther@59822
|
221 |
if pI <> spI
|
walther@59822
|
222 |
then if pI = snd3 ospec then (Pos.Pbl, probl, meth)
|
walther@59822
|
223 |
else
|
walther@59970
|
224 |
let val pbt = (#ppc o Problem.from_store) pI
|
walther@59983
|
225 |
val dI' = #1 (References.select ospec spec)
|
walther@59822
|
226 |
val oris = if pI = #2 ospec then oris
|
walther@59952
|
227 |
else O_Model.init fmz_(ThyC.get_theory"Isac_Knowledge") pbt(* |> #1*);
|
walther@59822
|
228 |
in (Pos.Pbl, appl_adds dI' oris probl pbt
|
walther@59822
|
229 |
(map itms2fstr probl), meth) end
|
walther@59822
|
230 |
else if mI <> smI (*FIXME.WN0311: what if probl is incomplete?!*)
|
walther@60154
|
231 |
then let val met = (#ppc o MethodC.from_store) mI
|
walther@59988
|
232 |
val mits = I_Model.complete oris probl meth met
|
walther@59822
|
233 |
in if foldl and_ (true, map #3 mits)
|
walther@59822
|
234 |
then (Pos.Pbl, probl, mits) else (Pos.Met, probl, mits)
|
walther@59822
|
235 |
end
|
walther@59983
|
236 |
else (Pos.Pbl, appl_adds (#1 (References.select ospec spec)) oris [(*!!!*)]
|
walther@59983
|
237 |
((#ppc o Problem.from_store) (#2 (References.select ospec spec)))
|
walther@59822
|
238 |
(imodel2fstr imodel), meth)
|
walther@59822
|
239 |
val pt = Ctree.update_spec pt p spec;
|
walther@59822
|
240 |
in if pos_ = Pos.Pbl
|
walther@59983
|
241 |
then let val {prls,where_,...} = Problem.from_store (#2 (References.select ospec spec))
|
walther@60018
|
242 |
val (_, pre) = Pre_Conds.check prls where_ pits 0
|
walther@59822
|
243 |
in (Ctree.update_pbl pt p pits,
|
walther@60097
|
244 |
(SpecificationC.complete pits pre spec, Pos.Pbl, hdf', pits, pre, spec): SpecificationC.T)
|
walther@59822
|
245 |
end
|
walther@60154
|
246 |
else let val {prls,pre,...} = MethodC.from_store (#3 (References.select ospec spec))
|
walther@60018
|
247 |
val (_, pre) = Pre_Conds.check prls pre mits 0
|
walther@59822
|
248 |
in (Ctree.update_met pt p mits,
|
walther@60097
|
249 |
(SpecificationC.complete mits pre spec, Pos.Met, hdf', mits, pre, spec) : SpecificationC.T)
|
walther@59822
|
250 |
end
|
walther@59822
|
251 |
end
|
walther@59822
|
252 |
end
|
walther@59962
|
253 |
| input_icalhd _ (_, _, _, _(*Met*), _) = raise ERROR "input_icalhd Met not impl."
|
walther@59822
|
254 |
|
walther@60132
|
255 |
(**)end (**)
|