Walther@60605
|
1 |
(* Title: Specify/pre-conds.sml
|
walther@59960
|
2 |
Author: Walther Neuper 110226
|
Walther@60705
|
3 |
(c) due to copyrigh,t terms
|
walther@59960
|
4 |
*)
|
walther@59960
|
5 |
|
walther@59960
|
6 |
signature PRE_CONDITIONS =
|
walther@59960
|
7 |
sig
|
walther@59963
|
8 |
type T
|
Walther@60605
|
9 |
type unchecked
|
Walther@60712
|
10 |
(*RENA unchecked_pos*)
|
Walther@60705
|
11 |
type unchecked_TEST
|
Walther@60705
|
12 |
type checked
|
Walther@60712
|
13 |
(*RENA checked_pos*)
|
Walther@60705
|
14 |
type checked_TEST
|
Walther@60705
|
15 |
|
Walther@60705
|
16 |
type env_subst
|
Walther@60705
|
17 |
type env_eval
|
Walther@60556
|
18 |
type input
|
Walther@60705
|
19 |
|
Walther@60673
|
20 |
val to_string : Proof.context -> T -> string
|
walther@59967
|
21 |
|
Walther@60705
|
22 |
val max_variant: Model_Def.i_model -> int
|
Walther@60705
|
23 |
val environment: Model_Def.i_model -> Env.T
|
Walther@60705
|
24 |
|
Walther@60727
|
25 |
(* WRONG ENVS *)
|
Walther@60706
|
26 |
val check: Proof.context -> Rule_Set.T -> unchecked -> Model_Def.i_model -> 'a -> checked
|
Walther@60727
|
27 |
(* WRONG ENVS *)
|
Walther@60710
|
28 |
val check_from_store: Proof.context -> Rule_Set.T -> unchecked -> env_subst -> env_eval -> checked
|
Walther@60727
|
29 |
(* vvv-replaces-^^^*)
|
Walther@60727
|
30 |
val check_envs_TEST: Proof.context -> Rule_Def.rule_set -> term list ->
|
Walther@60727
|
31 |
Env.T * (env_subst * env_eval) -> checked
|
Walther@60727
|
32 |
(* WRONG ENVS *)
|
Walther@60710
|
33 |
val check_TEST : Proof.context -> Rule_Set.T -> unchecked_TEST -> env_eval -> checked_TEST
|
Walther@60710
|
34 |
|
Walther@60710
|
35 |
(*this makes the OLD tests run AFTER introduction of I_Model.T_TEST..*)
|
Walther@60710
|
36 |
val check_OLD: Proof.context -> Rule_Set.T -> unchecked ->
|
Walther@60706
|
37 |
Model_Pattern.T * Model_Def.i_model_TEST -> checked
|
Walther@60706
|
38 |
|
Walther@60590
|
39 |
val eval: Proof.context -> Rule_Set.T -> bool * term -> bool * term
|
Walther@60706
|
40 |
val feedback_TEST_to_string: Model_Def.i_model_feedback_TEST -> string
|
Walther@60590
|
41 |
|
Walther@60705
|
42 |
val check_variant_envs: Proof.context -> Rule_Set.T -> unchecked ->
|
Walther@60705
|
43 |
env_subst -> env_eval -> checked
|
Walther@60710
|
44 |
|
Walther@60715
|
45 |
(*val sep_variants_envs replaced by of_max_variant TODO
|
Walther@60715
|
46 |
for that purpose compare with sep_variants_envs_OLD TODO*)
|
Walther@60706
|
47 |
val sep_variants_envs: Model_Pattern.T -> Model_Def.i_model_TEST ->
|
Walther@60706
|
48 |
((Model_Def.i_model_TEST * env_subst * env_eval) * Model_Def.variant) list
|
Walther@60710
|
49 |
val sep_variants_envs_OLD: Model_Pattern.T -> Model_Def.i_model_TEST ->
|
Walther@60706
|
50 |
env_subst * env_eval;
|
Walther@60715
|
51 |
|
Walther@60728
|
52 |
(*T_TESTold* )
|
Walther@60710
|
53 |
val of_max_variant: Model_Pattern.T -> Model_Def.i_model_TEST ->
|
Walther@60710
|
54 |
Model_Def.i_model_TEST * env_subst * env_eval
|
Walther@60728
|
55 |
( *T_TEST*)
|
Walther@60729
|
56 |
val of_max_variant: Model_Pattern.T -> Model_Def.i_model_TEST ->
|
Walther@60722
|
57 |
Model_Def.i_model_TEST * Env.T * (env_subst * env_eval)
|
Walther@60728
|
58 |
(*T_TESTnew*)
|
Walther@60706
|
59 |
|
Walther@60705
|
60 |
(*from isac_test for Minisubpbl*)
|
Walther@60729
|
61 |
val handle_lists: term -> Model_Def.descriptor * term list -> Env.T;
|
Walther@60710
|
62 |
val discern_feedback_subst: Model_Def.descriptor -> Model_Def.i_model_feedback_TEST -> Env.T;
|
Walther@60710
|
63 |
val discern_type_subst: term * term -> term list -> Env.T;
|
Walther@60710
|
64 |
val discern_type_eval: Model_Def.descriptor -> term list -> env_eval
|
Walther@60710
|
65 |
val discern_atoms: term list -> Env.T;
|
Walther@60712
|
66 |
|
Walther@60728
|
67 |
val make_env_model: (Model_Pattern.single * Model_Def.i_model_single_TEST) list -> Env.T
|
Walther@60728
|
68 |
|
Walther@60705
|
69 |
val mk_env: Model_Def.i_model_feedback_TEST -> Env.T
|
Walther@60705
|
70 |
val mk_env_subst: (Model_Pattern.single * Model_Def.i_model_single_TEST) list -> env_subst
|
Walther@60710
|
71 |
val mk_env_subst_DEL: (Model_Pattern.single * Model_Def.i_model_single_TEST) list -> env_subst
|
Walther@60705
|
72 |
val mk_env_eval: (Model_Pattern.single * Model_Def.i_model_single_TEST) list -> env_eval
|
Walther@60710
|
73 |
val mk_env_eval_DEL: Model_Def.i_model_single_TEST list -> env_eval
|
Walther@60706
|
74 |
val arrange_args2: 'a list -> 'b list -> 'c list -> int * 'd list -> (('a * 'b * 'c) * 'd) list
|
Walther@60706
|
75 |
val filter_variants: (Model_Pattern.single * Model_Def.i_model_single_TEST) list ->
|
Walther@60706
|
76 |
Model_Def.variant -> (Model_Pattern.single * Model_Def.i_model_single_TEST) list
|
Walther@60706
|
77 |
val get_equal_descr: Model_Def.i_model_TEST -> Model_Pattern.single ->
|
Walther@60706
|
78 |
(Model_Pattern.single * Model_Def.i_model_single_TEST) list
|
Walther@60706
|
79 |
val unchecked_OLD_to_TEST: term list -> (term * Position.T) list
|
Walther@60705
|
80 |
|
Walther@60710
|
81 |
val filter_variants': Model_Def.i_model_TEST -> Model_Def.variant -> Model_Def.i_model_TEST
|
Walther@60710
|
82 |
val arrange_args1: int list -> int * Model_Def.variant list -> (int * Model_Def.variant) list
|
Walther@60710
|
83 |
val cnt_corrects: Model_Def.i_model_TEST -> int
|
Walther@60710
|
84 |
val handle_descr_ts: term -> term list -> Env.T
|
Walther@60710
|
85 |
val distinguish_atoms: term list -> Env.T
|
Walther@60712
|
86 |
(*REN type_repair_env: Env.T -> Env.T*)
|
Walther@60710
|
87 |
val repair_env: Env.T -> Env.T
|
Walther@60710
|
88 |
val all_atoms: term list -> bool
|
Walther@60710
|
89 |
val all_lhs_atoms: term list -> bool
|
Walther@60728
|
90 |
(*from isac_test for Minisubpbl*)
|
Walther@60728
|
91 |
val mk_env_model: term -> Model_Def.i_model_feedback_TEST -> Env.T
|
Walther@60728
|
92 |
val make_envs_preconds: (Model_Pattern.single * Model_Def.i_model_single_TEST) list ->
|
Walther@60728
|
93 |
((term * term) * (term * term)) list
|
Walther@60728
|
94 |
val switch_type_TEST: Model_Def.descriptor -> term list -> Model_Def.descriptor;
|
Walther@60728
|
95 |
val discern_feedback: term -> Model_Def.i_model_feedback_TEST ->
|
Walther@60728
|
96 |
((term * term) * (term * term)) list
|
Walther@60728
|
97 |
val discern_typ: term -> Model_Pattern.descriptor * term list ->
|
Walther@60728
|
98 |
((term * term) * (term * term)) list
|
Walther@60710
|
99 |
|
Walther@60722
|
100 |
\<^isac_test>\<open>
|
Walther@60722
|
101 |
val switch_type: Model_Pattern.descriptor -> typ -> Model_Pattern.descriptor
|
Walther@60705
|
102 |
|
wenzelm@60223
|
103 |
\<close>
|
walther@59960
|
104 |
end
|
Walther@60722
|
105 |
|
walther@59965
|
106 |
(**)
|
walther@59960
|
107 |
structure Pre_Conds(**) : PRE_CONDITIONS(**) =
|
walther@59960
|
108 |
struct
|
walther@59965
|
109 |
(**)
|
Walther@60706
|
110 |
open Model_Def
|
walther@59960
|
111 |
|
Walther@60673
|
112 |
type T = Pre_Conds_Def.T; (*= (bool * term) list;*)
|
Walther@60605
|
113 |
type unchecked = term list
|
Walther@60705
|
114 |
type unchecked_TEST = (term * Position.T) list
|
Walther@60705
|
115 |
type checked = bool * (bool * term) list
|
Walther@60705
|
116 |
type checked_TEST = bool * (bool * (term * Position.T)) list
|
Walther@60705
|
117 |
|
Walther@60715
|
118 |
(*
|
Walther@60715
|
119 |
we have three kinds of Env.T in the specification-phase:
|
Walther@60715
|
120 |
(*1*) Env.T produced by I_Model.of_max_variant and required to create O_Model.T, I_Model.T from
|
Walther@60715
|
121 |
Model_Pattern.T.
|
Walther@60715
|
122 |
Env.T / (Constants, fixes) in Model_Pattern.T
|
Walther@60715
|
123 |
e.g.[(fixes, [r = 7])] |
|
Walther@60722
|
124 |
> (Constants, [<r = 7>]) in O/I_Model.T *)
|
Walther@60715
|
125 |
|
Walther@60715
|
126 |
(*2*) type env_subst = Env.T (* / 0 < fixes in Problem.{where_, ...}
|
Walther@60715
|
127 |
eg. [(fixes, r)] |
|
Walther@60715
|
128 |
> 0 < r *)
|
Walther@60715
|
129 |
(*3*) type env_eval = Env.T (* |
|
Walther@60715
|
130 |
eg. [(r, 7)] |
|
Walther@60715
|
131 |
> 0 < 7 \<longrightarrow> true
|
Walther@60715
|
132 |
|
Walther@60715
|
133 |
(*1*) is required to produce (*2*) and (*3*); thus respective code is shifted to Pre_Conds.
|
Walther@60715
|
134 |
(*2*) and (*3*) is produced from I_Model.of_max_variant by restricting to "#Given".
|
Walther@60715
|
135 |
|
Walther@60715
|
136 |
There is a typing problem, probably to be solved by a language for Specification in the future:
|
Walther@60715
|
137 |
term <fixes> in (*1*) has type "bool list"
|
Walther@60715
|
138 |
term <fixes> in (*2*) has type "real". So for the transition from (*1*) to (*2..3*)
|
Walther@60715
|
139 |
fun switch_type is required.
|
Walther@60715
|
140 |
The transition requires better modelling by a novel language for Specification.
|
Walther@60715
|
141 |
*)
|
Walther@60715
|
142 |
|
Walther@60605
|
143 |
type input = TermC.as_string list;
|
walther@59963
|
144 |
|
Walther@60675
|
145 |
fun to_str ctxt (b, t) = pair2str (bool2str b, UnparseC.term ctxt t);
|
Walther@60673
|
146 |
fun to_string ctxt pres = strs2str' (map (linefeed o (to_str ctxt)) pres);
|
walther@59960
|
147 |
|
Walther@60590
|
148 |
fun eval _ _ (false, where_) = (false, where_) (*NOT ALL Free's have been substituted*)
|
Walther@60590
|
149 |
| eval ctxt where_rls (true, where_) =
|
Walther@60590
|
150 |
if Rewrite.eval_true ctxt [where_] where_rls
|
Walther@60706
|
151 |
then (true, where_)
|
Walther@60706
|
152 |
else (false, where_);
|
walther@59960
|
153 |
|
Walther@60712
|
154 |
(** old code before I_Model.T_TEST **)
|
Walther@60712
|
155 |
|
Walther@60705
|
156 |
(* find most frequent variant v in itms *)
|
Walther@60705
|
157 |
(* ATTENTION
|
Walther@60705
|
158 |
* spurios old code
|
Walther@60705
|
159 |
* misses variants with equal number of items, etc
|
Walther@60705
|
160 |
*)
|
Walther@60705
|
161 |
fun variants itms = ((distinct op =) o flat o (map (fn (_, vrs, _, _, _) => vrs))) itms;
|
Walther@60705
|
162 |
|
Walther@60705
|
163 |
(*version without typing problem*)
|
Walther@60705
|
164 |
fun cnt itms v = (v, (length o (filter (curry op = v)) o flat o (map (fn (_, vrs, _, _, _) => vrs))) itms);
|
Walther@60705
|
165 |
fun count_variants vts itms = map (cnt itms) vts;
|
Walther@60705
|
166 |
|
Walther@60705
|
167 |
fun max2 [] = raise ERROR "max2 of []"
|
Walther@60705
|
168 |
| max2 (y :: ys) =
|
Walther@60705
|
169 |
let
|
Walther@60705
|
170 |
fun mx (a, x) [] = (a, x)
|
Walther@60705
|
171 |
| mx (a, x) ((b, y) :: ys) = if x < y then mx (b, y) ys else mx (a, x) ys;
|
Walther@60705
|
172 |
in mx y ys end;
|
Walther@60705
|
173 |
|
Walther@60705
|
174 |
(* get the constant value from a penv *)
|
Walther@60705
|
175 |
fun getval (id, values) =
|
Walther@60705
|
176 |
case values of
|
Walther@60705
|
177 |
[] => raise ERROR ("penv_value: no values in '" ^ UnparseC.term (ContextC.for_ERROR ()) id)
|
Walther@60705
|
178 |
| [v] => (id, v)
|
Walther@60705
|
179 |
| (v1 :: v2 :: _) => (case v1 of
|
Walther@60705
|
180 |
Const (\<^const_name>\<open>Program.Arbfix\<close>, _) => (id, v2)
|
Walther@60705
|
181 |
| _ => (id, v1));
|
Walther@60705
|
182 |
|
Walther@60705
|
183 |
(* find the variant with most items already input *)
|
Walther@60705
|
184 |
fun max_variant itms =
|
Walther@60705
|
185 |
let val vts = (count_variants (variants itms)) itms;
|
Walther@60705
|
186 |
in if vts = [] then 0 else (fst o max2) vts end;
|
Walther@60705
|
187 |
|
Walther@60706
|
188 |
fun mk_e (Cor (_, iv)) = [getval iv]
|
Walther@60706
|
189 |
| mk_e (Syn _) = []
|
Walther@60706
|
190 |
| mk_e (Typ _) = []
|
Walther@60706
|
191 |
| mk_e (Inc (_, iv)) = [getval iv]
|
Walther@60706
|
192 |
| mk_e (Sup _) = []
|
Walther@60706
|
193 |
| mk_e (Mis _) = []
|
Walther@60705
|
194 |
| mk_e _ = raise ERROR "mk_e: uncovered case in fun.def.";
|
Walther@60705
|
195 |
fun mk_en vt (_, vts, _, _, itm_) = if member op = vts vt then mk_e itm_ else [];
|
Walther@60705
|
196 |
|
Walther@60705
|
197 |
(* extract the environment from an item list; takes the variant with most items *)
|
Walther@60705
|
198 |
fun environment itms =
|
Walther@60705
|
199 |
let val vt = max_variant itms
|
Walther@60712
|
200 |
in (flat o (map (mk_en vt))) itms end;
|
Walther@60705
|
201 |
|
Walther@60706
|
202 |
|
Walther@60712
|
203 |
(** new code for I_Model.T_TEST **)
|
Walther@60712
|
204 |
|
Walther@60706
|
205 |
fun pen2str ctxt (t, ts) =
|
Walther@60706
|
206 |
pair2str (UnparseC.term ctxt t, (strs2str' o map (UnparseC.term ctxt)) ts);
|
Walther@60706
|
207 |
fun feedback_to_string_TEST ctxt (Cor_TEST ((d, ts), penv)) =
|
Walther@60706
|
208 |
"Cor_TEST " ^ UnparseC.term ctxt (Input_Descript.join (d, ts)) ^ " ," ^ pen2str ctxt penv
|
Walther@60706
|
209 |
| feedback_to_string_TEST _ (Syn_TEST c) =
|
Walther@60706
|
210 |
"Syn_TEST " ^ c
|
Walther@60714
|
211 |
| feedback_to_string_TEST ctxt (Inc_TEST ((d, []), _)) =
|
Walther@60714
|
212 |
"Inc_TEST " ^ UnparseC.term ctxt (Input_Descript.join (d, [])) ^ " " ^
|
Walther@60717
|
213 |
Model_Pattern.empty_for d
|
Walther@60714
|
214 |
| feedback_to_string_TEST ctxt (Inc_TEST ((d, ts), penv)) =
|
Walther@60706
|
215 |
"Inc_TEST " ^ UnparseC.term ctxt (Input_Descript.join (d, ts)) ^ " ," ^ pen2str ctxt penv
|
Walther@60706
|
216 |
| feedback_to_string_TEST ctxt (Sup_TEST (d, ts)) =
|
Walther@60706
|
217 |
"Sup_TEST " ^ UnparseC.term ctxt (Input_Descript.join (d, ts))
|
Walther@60706
|
218 |
fun feedback_TEST_to_string t = feedback_to_string_TEST (ContextC.for_ERROR ()) t;
|
Walther@60705
|
219 |
|
Walther@60705
|
220 |
|
Walther@60710
|
221 |
(*** make environments for substituting model_pattern+program and for evaluating pre-conditions ***)
|
Walther@60705
|
222 |
|
Walther@60712
|
223 |
(** OLD make environment for substituting model_pattern+program and pre-conditions **)
|
Walther@60710
|
224 |
|
Walther@60712
|
225 |
(*val all_atoms_OLD: term list -> bool*)
|
Walther@60712
|
226 |
fun all_atoms_OLD ts =
|
Walther@60705
|
227 |
fold (curry and_) (map (fn t => TermC.is_atom (TermC.lhs t)) ts) true
|
Walther@60705
|
228 |
fun switch_type (Free (id, _)) T = Free (id, T)
|
Walther@60705
|
229 |
| switch_type t _ = raise TERM ("switch_type not for", [t])
|
Walther@60712
|
230 |
|
Walther@60705
|
231 |
(*
|
Walther@60705
|
232 |
this repair is spurious and indicates the need for considering a separate language for Model-ing.
|
Walther@60705
|
233 |
E.g. "fixes" is used with 2 different types "bool list" and "real" in the problem-definition
|
Walther@60705
|
234 |
problem pbl_opti_univ : "univariate_calculus/Optimisation" = ..
|
Walther@60705
|
235 |
Given: "Constants fixes"
|
Walther@60705
|
236 |
Where: "0 < fixes"
|
Walther@60705
|
237 |
...
|
Walther@60705
|
238 |
(1) Given: Const ("Input_Descript.Constants", "bool list \<Rightarrow> nam") $ Free ("fixes", "bool list")
|
Walther@60705
|
239 |
(2) Where: Const ("Orderings.ord_class.less", "real \<Rightarrow> real \<Rightarrow> bool") $
|
Walther@60705
|
240 |
Const ("Groups.zero_class.zero", "real") $ Free ("fixes", "real")
|
Walther@60705
|
241 |
|
Walther@60705
|
242 |
The intermediately simplistic solution is to build a pair of environments, type env_pair,
|
Walther@60705
|
243 |
|
Walther@60705
|
244 |
The simplistic design works for the instantiation of the demo-example
|
Walther@60705
|
245 |
"Constants [r = (7::real)]"
|
Walther@60705
|
246 |
but it could be extended to instantiations like
|
Walther@60705
|
247 |
"Constants [a = (1::real), b = (2::real)]"
|
Walther@60705
|
248 |
with the same Where: "0 < fixes" in the problem-definition
|
Walther@60705
|
249 |
if indexing / deletion of found elements from the association list is considered.
|
Walther@60705
|
250 |
|
Walther@60705
|
251 |
For the purpose of future pimprovements a pair of environments, type env_pair, is built;
|
Walther@60705
|
252 |
where as key of the "outer" environment Free ("fixes", "real" kept to future improvements
|
Walther@60705
|
253 |
(by indexing "fixes_1", "fixes_2" in case the are more than one element in
|
Walther@60705
|
254 |
"Input_Descript.Constants", "bool list..)
|
Walther@60705
|
255 |
*)
|
Walther@60705
|
256 |
fun repair_env env =
|
Walther@60705
|
257 |
map (fn (id_inst, val_) => (switch_type id_inst (type_of val_), val_)) env
|
Walther@60710
|
258 |
|
Walther@60705
|
259 |
(* make an Env.T for Prec_Conds.eval *)
|
Walther@60705
|
260 |
fun mk_env (Model_Def.Cor_TEST ((descr, [ts]), _)) =
|
Walther@60705
|
261 |
let
|
Walther@60706
|
262 |
(*val ts = TermC.isalist2list ts*)
|
Walther@60706
|
263 |
val ts = if TermC.is_list ts then TermC.isalist2list ts else [ts]
|
Walther@60705
|
264 |
in
|
Walther@60705
|
265 |
(*compare Model_Pattern.empty_for*)
|
Walther@60705
|
266 |
case type_of descr of
|
Walther@60705
|
267 |
(Type ("fun", [Type ("List.list", [Type ("HOL.bool", [])]), _])) (*SideConditions \<longrightarrow> (sideconds, [(u / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2])*)
|
Walther@60712
|
268 |
=> if all_atoms_OLD ts then map (fn t => (TermC.lhs t, TermC.rhs t)) ts else []
|
Walther@60705
|
269 |
| (Type ("fun", [Type ("List.list", [Type ("Real.real", [])]), _])) (*AdditionalValues [u, v]*)
|
Walther@60705
|
270 |
=> []
|
Walther@60705
|
271 |
| (Type ("HOL.bool", [])) (*Precision (\<epsilon> = 0.01)*)
|
Walther@60712
|
272 |
=> if all_atoms_OLD ts then map (fn t => (TermC.lhs t, TermC.rhs t)) ts else []
|
Walther@60706
|
273 |
| _ (*Maximum A*) => [(hd ts, TermC.empty (*dummy rhs*))]
|
Walther@60705
|
274 |
end
|
Walther@60705
|
275 |
| mk_env (Model_Def.Inc_TEST ((descr, [ts]), _)) =
|
Walther@60705
|
276 |
let
|
Walther@60706
|
277 |
(*val ts = TermC.isalist2list ts*)
|
Walther@60706
|
278 |
val ts = if TermC.is_list ts then TermC.isalist2list ts else [ts]
|
Walther@60705
|
279 |
in
|
Walther@60705
|
280 |
(*compare Model_Pattern.empty_for*)
|
Walther@60705
|
281 |
case type_of descr of
|
Walther@60705
|
282 |
(Type ("fun", [Type ("List.list", [Type ("HOL.bool", [])]), _])) (*SideConditions \<longrightarrow> (sideconds, [(u / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2])*)
|
Walther@60712
|
283 |
=> if all_atoms_OLD ts then map (fn t => (TermC.lhs t, TermC.rhs t)) ts else []
|
Walther@60705
|
284 |
| (Type ("fun", [Type ("List.list", [Type ("Real.real", [])]), _])) (*AdditionalValues [u, v]*)
|
Walther@60705
|
285 |
=> []
|
Walther@60705
|
286 |
| (Type ("HOL.bool", [])) (*Precision (\<epsilon> = 0.01)*)
|
Walther@60712
|
287 |
=> if all_atoms_OLD ts then map (fn t => (TermC.lhs t, TermC.rhs t)) ts else []
|
Walther@60706
|
288 |
| _ (*Maximum A*) => [(hd ts, TermC.empty (*dummy rhs*))]
|
Walther@60705
|
289 |
end
|
Walther@60706
|
290 |
| mk_env (Model_Def.Sup_TEST (descr, [ts])) =
|
Walther@60705
|
291 |
let
|
Walther@60706
|
292 |
(*val ts = TermC.isalist2list ts*)
|
Walther@60706
|
293 |
val ts = if TermC.is_list ts then TermC.isalist2list ts else [ts]
|
Walther@60705
|
294 |
in
|
Walther@60705
|
295 |
(*compare Model_Pattern.empty_for*)
|
Walther@60705
|
296 |
case type_of descr of
|
Walther@60705
|
297 |
(Type ("fun", [Type ("List.list", [Type ("HOL.bool", [])]), _])) (*SideConditions \<longrightarrow> (sideconds, [(u / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2])*)
|
Walther@60712
|
298 |
=> if all_atoms_OLD ts then map (fn t => (TermC.lhs t, TermC.rhs t)) ts else []
|
Walther@60705
|
299 |
| (Type ("fun", [Type ("List.list", [Type ("Real.real", [])]), _])) (*AdditionalValues [u, v]*)
|
Walther@60705
|
300 |
=> []
|
Walther@60705
|
301 |
| (Type ("HOL.bool", [])) (*Precision (\<epsilon> = 0.01)*)
|
Walther@60712
|
302 |
=> if all_atoms_OLD ts then map (fn t => (TermC.lhs t, TermC.rhs t)) ts else []
|
Walther@60706
|
303 |
| _ (*Maximum A*) => [(hd ts, TermC.empty (*dummy rhs*))]
|
Walther@60705
|
304 |
end
|
Walther@60706
|
305 |
| mk_env feedb =
|
Walther@60706
|
306 |
raise ERROR ("mk_env not reasonable for " ^ quote (feedback_TEST_to_string feedb))
|
Walther@60705
|
307 |
fun mk_env_subst equal_descr_pairs =
|
Walther@60705
|
308 |
map (fn ((_, (_, id)), (_, _, _, _, (feedb, _)))
|
Walther@60706
|
309 |
=> (mk_env feedb) |> map fst (*dummy rhs dropped*)|> map (pair id)) equal_descr_pairs
|
Walther@60705
|
310 |
|> flat
|
Walther@60705
|
311 |
|> repair_env
|
Walther@60710
|
312 |
(*fun mk_env_eval unused? DEL!*)
|
Walther@60705
|
313 |
fun mk_env_eval equal_descr_pairs =
|
Walther@60705
|
314 |
map (fn (_, (_, _, _, _, (feedb, _))) => mk_env feedb) equal_descr_pairs
|
Walther@60705
|
315 |
|> flat
|
Walther@60706
|
316 |
|> filter_out (fn (_, b) => b = TermC.empty) (*drop pairs with dummy rhs*)
|
Walther@60710
|
317 |
|
Walther@60712
|
318 |
(* all_atoms: term list -> bool*)
|
Walther@60710
|
319 |
fun all_atoms ts =
|
Walther@60710
|
320 |
fold (curry and_) (map (fn t => TermC.is_atom t) ts) true
|
Walther@60712
|
321 |
(* all_lhs_atoms: term list -> bool*)
|
Walther@60710
|
322 |
fun all_lhs_atoms ts = fold (curry and_) (map (fn t =>
|
Walther@60729
|
323 |
if can TermC.lhs t andalso not (TermC.is_num (TermC.lhs t))
|
Walther@60729
|
324 |
then TermC.is_atom (TermC.lhs t)
|
Walther@60729
|
325 |
else false) ts) true
|
Walther@60729
|
326 |
(*T_TESTold* )
|
Walther@60729
|
327 |
fun all_lhs_atoms ts = fold (curry and_) (map (fn t =>
|
Walther@60710
|
328 |
if can TermC.lhs t then TermC.is_atom (TermC.lhs t) else false) ts) true
|
Walther@60729
|
329 |
( *T_TEST*)
|
Walther@60712
|
330 |
(* distinguish_atoms: term list -> Env.T*)
|
Walther@60729
|
331 |
(*T_TESTnew*)
|
Walther@60710
|
332 |
fun distinguish_atoms (*[(u / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2], \<epsilon> = 0.01, [L]*) ts =
|
Walther@60710
|
333 |
if all_atoms ts
|
Walther@60710
|
334 |
then map (rpair TermC.empty (*dummy rhs*)) ts
|
Walther@60710
|
335 |
else if all_lhs_atoms ts
|
Walther@60710
|
336 |
then map (fn t => (TermC.lhs t, TermC.rhs t)) ts
|
Walther@60710
|
337 |
else []
|
Walther@60712
|
338 |
(* handle_descr_ts: term -> term list -> (term * term) list*)
|
Walther@60710
|
339 |
fun handle_descr_ts descr ts =
|
Walther@60710
|
340 |
(*compare Model_Pattern.empty_for*)
|
Walther@60710
|
341 |
case type_of descr of
|
Walther@60710
|
342 |
(Type ("fun", [Type ("List.list", [Type ("HOL.bool", [])]), _])) (*(SideConds [(u / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2]), [L]*)
|
Walther@60710
|
343 |
=> distinguish_atoms ts
|
Walther@60710
|
344 |
| (Type ("fun", [Type ("List.list", [Type ("Real.real", [])]), _])) (*AdditionalValues [u, v]*)
|
Walther@60710
|
345 |
=> if TermC.is_list (hd ts)
|
Walther@60710
|
346 |
then [(descr, ts |> map TermC.the_single |> TermC.list2isalist HOLogic.realT)]
|
Walther@60710
|
347 |
else []
|
Walther@60710
|
348 |
| (Type ("HOL.bool", [])) (*Precision (\<epsilon> = 0.01)*)
|
Walther@60710
|
349 |
=> distinguish_atoms ts
|
Walther@60710
|
350 |
| _ (*Maximum A*) => [(hd ts, TermC.empty (*dummy rhs*))]
|
Walther@60710
|
351 |
|
Walther@60712
|
352 |
|
Walther@60712
|
353 |
(** NEW make environment for substituting program and pre-conditions **)
|
Walther@60710
|
354 |
|
Walther@60710
|
355 |
fun switch_type (Free (id, _)) T = Free (id, T)
|
Walther@60710
|
356 |
| switch_type t _ = raise TERM ("switch_type not for", [t])
|
Walther@60710
|
357 |
|
Walther@60710
|
358 |
fun repair_env env =
|
Walther@60710
|
359 |
map (fn (id_inst, val_) => (switch_type id_inst (type_of val_), val_)) env
|
Walther@60710
|
360 |
|
Walther@60710
|
361 |
(* discern_type_subst: term * term -> term list -> Env.T*)
|
Walther@60710
|
362 |
fun discern_type_subst (descr, id) ts =
|
Walther@60710
|
363 |
(*..compare Model_Pattern.empty_for*)
|
Walther@60710
|
364 |
case type_of descr of
|
Walther@60710
|
365 |
(Type ("fun", [Type ("List.list", [Type ("HOL.bool", [])]), _])) (*(SideConds [(u / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2]), [L]*)
|
Walther@60712
|
366 |
=> if TermC.is_list (hd ts)
|
Walther@60712
|
367 |
then [(id, ts |> map TermC.the_single |> TermC.list2isalist HOLogic.boolT)]
|
Walther@60712
|
368 |
else if length ts = 1
|
Walther@60712
|
369 |
then [(id, Library.the_single ts)]
|
Walther@60710
|
370 |
else raise error "discern_type_subst list bool DESIGN ERROR"
|
Walther@60710
|
371 |
| (Type ("fun", [Type ("List.list", [Type ("Real.real", [])]), _])) (*AdditionalValues [u, v]*)
|
Walther@60712
|
372 |
=> if TermC.is_list (hd ts)
|
Walther@60712
|
373 |
then [(id, ts |> map TermC.the_single |> TermC.list2isalist HOLogic.realT)]
|
Walther@60712
|
374 |
else if length ts = 1
|
Walther@60712
|
375 |
then [(id, Library.the_single ts)]
|
Walther@60710
|
376 |
else raise error "discern_type_subst list bool DESIGN ERROR"
|
Walther@60712
|
377 |
| _ (*Maximum A*) => [(id, Library.the_single ts)] (*if length ts = 1 !*)
|
Walther@60710
|
378 |
|
Walther@60712
|
379 |
(* discern_feedback_subst: Model_Def.i_model_feedback_TEST -> Env.T*)
|
Walther@60710
|
380 |
fun discern_feedback_subst id (Model_Def.Cor_TEST ((descr, [ts]), _)) =
|
Walther@60712
|
381 |
discern_type_subst (descr, id) [ts]
|
Walther@60710
|
382 |
| discern_feedback_subst id (Model_Def.Inc_TEST ((descr, [ts]), _)) =
|
Walther@60712
|
383 |
discern_type_subst (descr, id) [ts]
|
Walther@60712
|
384 |
| discern_feedback_subst id (Model_Def.Sup_TEST (descr, [ts])) =
|
Walther@60712
|
385 |
discern_type_subst (descr, id) [ts]
|
Walther@60710
|
386 |
| discern_feedback_subst id (Model_Def.Cor_TEST ((descr, (*[*)ts(*]*)), _)) =
|
Walther@60712
|
387 |
discern_type_subst (descr, id) ts
|
Walther@60712
|
388 |
| discern_feedback_subst _ feedb =
|
Walther@60712
|
389 |
raise ERROR ("discern_feedback_subst not reasonable for " ^ quote (feedback_TEST_to_string feedb))
|
Walther@60710
|
390 |
|
Walther@60710
|
391 |
fun mk_env_subst_DEL equal_descr_pairs =
|
Walther@60710
|
392 |
map (fn ((_, (_, id)), (_, _, _, _, (feedb, _)))
|
Walther@60710
|
393 |
=> (discern_feedback_subst id feedb)) equal_descr_pairs
|
Walther@60710
|
394 |
|> flat
|
Walther@60710
|
395 |
|> repair_env
|
Walther@60710
|
396 |
|
Walther@60710
|
397 |
|
Walther@60712
|
398 |
(** NEW make environment for evaluating pre-conditions **)
|
Walther@60712
|
399 |
|
Walther@60712
|
400 |
(* all_atoms: term list -> bool*)
|
Walther@60710
|
401 |
fun all_atoms ts =
|
Walther@60710
|
402 |
fold (curry and_) (map (fn t => TermC.is_atom t) ts) true
|
Walther@60712
|
403 |
(* discern_atoms: term list -> (term * term) list*)
|
Walther@60710
|
404 |
fun discern_atoms (*[(u / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2], \<epsilon> = 0.01, [L]*) ts =
|
Walther@60710
|
405 |
if all_atoms ts
|
Walther@60710
|
406 |
then map (rpair TermC.empty (*dummy rhs*)) ts
|
Walther@60710
|
407 |
else if all_lhs_atoms ts
|
Walther@60710
|
408 |
then map (fn t => (TermC.lhs t, TermC.rhs t)) ts
|
Walther@60710
|
409 |
else []
|
Walther@60710
|
410 |
|
Walther@60710
|
411 |
fun discern_type_eval descr ts =
|
Walther@60710
|
412 |
(*..compare Model_Pattern.empty_for*)
|
Walther@60710
|
413 |
case type_of descr of
|
Walther@60710
|
414 |
(Type ("fun", [Type ("List.list", [Type ("HOL.bool", [])]), _])) (*(SideConds [(u / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2]), [L]*)
|
Walther@60710
|
415 |
=> discern_atoms ts
|
Walther@60710
|
416 |
| (Type ("fun", [Type ("List.list", [Type ("Real.real", [])]), _])) (*AdditionalValues [u, v]*)
|
Walther@60710
|
417 |
=> discern_atoms ts
|
Walther@60710
|
418 |
| _ (*Maximum A*) => discern_atoms ts
|
Walther@60710
|
419 |
|
Walther@60712
|
420 |
fun discern_feedback_eval (Model_Def.Cor_TEST ((descr, [ts]), _)) = discern_type_eval descr [ts]
|
Walther@60712
|
421 |
| discern_feedback_eval (Model_Def.Inc_TEST ((descr, [ts]), _)) = discern_type_eval descr [ts]
|
Walther@60712
|
422 |
| discern_feedback_eval (Model_Def.Sup_TEST (descr, [ts])) = discern_type_eval descr [ts]
|
Walther@60712
|
423 |
| discern_feedback_eval (Model_Def.Cor_TEST ((descr, ts), _)) = discern_type_eval descr ts
|
Walther@60712
|
424 |
| discern_feedback_eval feedb =
|
Walther@60710
|
425 |
raise ERROR ("discern_feedback_eval not defined for " ^ quote (feedback_TEST_to_string feedb))
|
Walther@60710
|
426 |
|
Walther@60710
|
427 |
fun mk_env_eval_DEL i_singles =
|
Walther@60710
|
428 |
map (fn (_, _, _, _, (feedb, _)) => discern_feedback_eval feedb) i_singles
|
Walther@60710
|
429 |
|> flat
|
Walther@60710
|
430 |
|> filter_out (fn (_, b) => b = TermC.empty) (*drop pairs with dummy rhs*)
|
Walther@60710
|
431 |
|
Walther@60710
|
432 |
|
Walther@60712
|
433 |
(*** old check_variant_envs, sep_variants_envs/_OLD, of_max_variant, check/_TEST/_OLD ***)
|
Walther@60710
|
434 |
|
Walther@60705
|
435 |
fun check_variant_envs ctxt where_rls pres env_subst env_eval =
|
Walther@60705
|
436 |
let
|
Walther@60705
|
437 |
val pres_subst = map (TermC.subst_atomic_all env_subst) pres;
|
Walther@60705
|
438 |
val full_subst = map (fn (bool, t) => (bool, subst_atomic env_eval t)) pres_subst
|
Walther@60705
|
439 |
val evals = map (eval ctxt where_rls) full_subst
|
Walther@60705
|
440 |
in
|
Walther@60705
|
441 |
(foldl and_ (true, map fst evals), pres_subst)
|
Walther@60705
|
442 |
end
|
Walther@60705
|
443 |
|
walther@59965
|
444 |
|
Walther@60710
|
445 |
(* check_variant_envs for PIDE *)
|
Walther@60706
|
446 |
|
Walther@60706
|
447 |
fun filter_variants pairs i =
|
Walther@60706
|
448 |
filter (fn (_, (_, variants, _, _, _)) => member op= variants i) pairs
|
Walther@60712
|
449 |
|
Walther@60706
|
450 |
(*get descriptor of those items which can contribute to Subst.T and Env.T *)
|
Walther@60712
|
451 |
(* get_dscr: feedback_TEST -> descriptor option*)
|
Walther@60710
|
452 |
fun get_dscr' (Cor_TEST ((descr, _), _)) = SOME descr
|
Walther@60710
|
453 |
| get_dscr' (Inc_TEST ((descr, _), _)) = SOME descr
|
Walther@60710
|
454 |
| get_dscr' (Sup_TEST (descr, _)) = SOME descr
|
Walther@60710
|
455 |
| get_dscr' _
|
Walther@60706
|
456 |
(*other feedback cannot contribute to I_Model.mk_env_TEST and Pre_Conds.mk_env_TEST*) = NONE
|
Walther@60712
|
457 |
(* get_descr: I_Model.single_TEST -> descriptor option*)
|
Walther@60706
|
458 |
fun get_descr (_, _, _, _, (feedb, _)) =
|
Walther@60710
|
459 |
case get_dscr' feedb of NONE => NONE | some_descr => some_descr
|
Walther@60712
|
460 |
(* get_equal_descr: I_Model.T_TEST -> Model_Pattern.single ->
|
Walther@60706
|
461 |
(Model_Pattern.single * I_Model.single_TEST) list*)
|
Walther@60706
|
462 |
fun get_equal_descr i_model (m_patt_single as (_, (descr, _))) =
|
Walther@60706
|
463 |
let
|
Walther@60706
|
464 |
val equal_variants =
|
Walther@60706
|
465 |
filter (fn i_single => case get_descr i_single of
|
Walther@60706
|
466 |
NONE => false (*--------vvvvv*)
|
Walther@60706
|
467 |
| SOME descr' => descr' = descr) (*probl_POS*) i_model
|
Walther@60706
|
468 |
in
|
Walther@60706
|
469 |
(map (pair m_patt_single) equal_variants)
|
Walther@60706
|
470 |
end
|
Walther@60706
|
471 |
fun arrange_args2 [] _ _ _(*all have equal length*) = []
|
Walther@60706
|
472 |
| arrange_args2 (b :: bs) (c :: cs) (d :: ds) (cnt, all) =
|
Walther@60706
|
473 |
((b, c, d), nth cnt all) :: (arrange_args2 bs cs ds (cnt + 1, all))
|
Walther@60706
|
474 |
| arrange_args2 _ _ _ _ = raise ERROR "I_Model.arrange_args2 UnequalLengths"
|
Walther@60706
|
475 |
fun sep_variants_envs model_patt i_model =
|
Walther@60706
|
476 |
let
|
Walther@60706
|
477 |
val equal_descr_pairs =
|
Walther@60706
|
478 |
map (get_equal_descr i_model) model_patt
|
Walther@60706
|
479 |
|> flat
|
Walther@60706
|
480 |
val all_variants =
|
Walther@60706
|
481 |
map (fn (_, (_, variants, _, _, _)) => variants) equal_descr_pairs
|
Walther@60706
|
482 |
|> flat
|
Walther@60706
|
483 |
|> distinct op =
|
Walther@60706
|
484 |
val variants_separated = map (filter_variants equal_descr_pairs) all_variants
|
Walther@60712
|
485 |
val i_models = map (map snd) variants_separated
|
Walther@60706
|
486 |
val restricted_to_given = map (filter (fn ((m_field, (_, _)), (_, _, _, _, (_, _))) =>
|
Walther@60706
|
487 |
m_field = "#Given")) variants_separated
|
Walther@60706
|
488 |
val envs_subst = map mk_env_subst restricted_to_given
|
Walther@60706
|
489 |
val envs_eval = map mk_env_eval restricted_to_given
|
Walther@60706
|
490 |
in
|
Walther@60706
|
491 |
arrange_args2 i_models envs_subst envs_eval (1, all_variants)
|
Walther@60706
|
492 |
end
|
Walther@60712
|
493 |
|
Walther@60706
|
494 |
(* smash all environments into one; this works only for old test before Maximum-example *)
|
Walther@60706
|
495 |
fun sep_variants_envs_OLD model_patt i_model =
|
Walther@60706
|
496 |
let
|
Walther@60706
|
497 |
val equal_descr_pairs =
|
Walther@60706
|
498 |
map (get_equal_descr i_model) model_patt
|
Walther@60706
|
499 |
|> flat
|
Walther@60706
|
500 |
val all_variants =
|
Walther@60706
|
501 |
map (fn (_, (_, variants, _, _, _)) => variants) equal_descr_pairs
|
Walther@60706
|
502 |
|> flat
|
Walther@60706
|
503 |
|> distinct op =
|
Walther@60706
|
504 |
val variants_separated = map (filter_variants equal_descr_pairs) all_variants
|
Walther@60706
|
505 |
val restricted_to_given = map (filter (fn ((m_field, (_, _)), (_, _, _, _, (_, _))) =>
|
Walther@60706
|
506 |
m_field = "#Given")) variants_separated
|
Walther@60706
|
507 |
val envs_subst = map mk_env_subst restricted_to_given
|
Walther@60706
|
508 |
val envs_eval = map mk_env_eval restricted_to_given
|
Walther@60706
|
509 |
in
|
Walther@60706
|
510 |
(flat envs_subst, flat envs_eval)
|
Walther@60706
|
511 |
end
|
Walther@60706
|
512 |
|
Walther@60712
|
513 |
(** of_max_variant **)
|
Walther@60721
|
514 |
|
Walther@60729
|
515 |
(*T_TESTold* )
|
Walther@60726
|
516 |
fun handle_lists id (descr, ts) =
|
Walther@60729
|
517 |
if Model_Pattern.is_list_descr descr
|
Walther@60729
|
518 |
then if length ts > 1 (*list with more than 1 element needs to collect by [.., .., ..]*)
|
Walther@60729
|
519 |
then [(id, TermC.list2isalist (Model_Pattern.typ_of_element descr) ts)] (*AdditValues [u, v]*)
|
Walther@60729
|
520 |
else if TermC.is_atom (Library.the_single ts)
|
Walther@60729
|
521 |
then [(id, Library.the_single ts)] (* solutions L, ...*)
|
Walther@60729
|
522 |
else [(id, TermC.list2isalist (Model_Pattern.typ_of_element descr) ts)](*Constants [r = 7]*)
|
Walther@60729
|
523 |
else [(id, Library.the_single ts)] (* solveFor x, Extremum (A = ...)*)
|
Walther@60729
|
524 |
( *T_TEST***)
|
Walther@60729
|
525 |
fun handle_lists id (descr, ts) =
|
Walther@60729
|
526 |
if Model_Pattern.is_list_descr descr
|
Walther@60729
|
527 |
then if length ts > 1 (*list with more than 1 element needs to collect by [.., .., ..]*)
|
Walther@60729
|
528 |
then if TermC.is_list (hd ts) (*---broken elementwise input to lists---*)
|
Walther@60729
|
529 |
then [(id, TermC.list2isalist (Model_Pattern.typ_of_element descr) (map TermC.the_single ts))]
|
Walther@60729
|
530 |
else [(id, TermC.list2isalist (Model_Pattern.typ_of_element descr) ts)] (*AdditValues [u, v]*)
|
Walther@60729
|
531 |
else if TermC.is_atom (Library.the_single ts)
|
Walther@60729
|
532 |
then [(id, Library.the_single ts)] (* solutions L, ...*)
|
Walther@60729
|
533 |
else [(id, TermC.list2isalist (Model_Pattern.typ_of_element descr) ts)](*Constants [r = 7]*)
|
Walther@60729
|
534 |
else [(id, Library.the_single ts)] (* solveFor x, Extremum (A = ...)*)
|
Walther@60729
|
535 |
(*T_TESTnew*)
|
Walther@60726
|
536 |
|
Walther@60722
|
537 |
fun mk_env_model _ (Model_Def.Cor_TEST ((_, []), _)) = []
|
Walther@60722
|
538 |
| mk_env_model id (Model_Def.Cor_TEST ((descr, ts), _)) =
|
Walther@60728
|
539 |
(*T_TEST.150* )
|
Walther@60722
|
540 |
if Model_Pattern.is_list_descr descr
|
Walther@60721
|
541 |
then [(id, TermC.list2isalist (Model_Pattern.typ_of_element descr) ts)]
|
Walther@60721
|
542 |
else [(id, hd ts)]
|
Walther@60728
|
543 |
( *T_TEST*)
|
Walther@60726
|
544 |
handle_lists id (descr, ts)
|
Walther@60728
|
545 |
(*T_TEST.100*)
|
Walther@60722
|
546 |
| mk_env_model _ (Model_Def.Syn_TEST _) = [] (*TODO handle correct list elements*)
|
Walther@60722
|
547 |
| mk_env_model _ (Model_Def.Inc_TEST ((_, []), _)) = []
|
Walther@60722
|
548 |
| mk_env_model id (Model_Def.Inc_TEST ((descr, ts), _)) =
|
Walther@60728
|
549 |
(*T_TEST.150* )
|
Walther@60722
|
550 |
if Model_Pattern.is_list_descr descr
|
Walther@60721
|
551 |
then [(id, TermC.list2isalist (Model_Pattern.typ_of_element descr) ts)]
|
Walther@60721
|
552 |
else [(id, hd ts)]
|
Walther@60728
|
553 |
( *T_TEST*)
|
Walther@60726
|
554 |
handle_lists id (descr, ts)
|
Walther@60728
|
555 |
(*T_TEST.100*)
|
Walther@60722
|
556 |
| mk_env_model _ (Model_Def.Sup_TEST _) = []
|
Walther@60722
|
557 |
fun make_env_model equal_descr_pairs =
|
Walther@60721
|
558 |
map (fn ((_, (_, id)), (_, _, _, _, (feedb, _)))
|
Walther@60722
|
559 |
=> (mk_env_model id feedb)) equal_descr_pairs
|
Walther@60721
|
560 |
|> flat
|
Walther@60722
|
561 |
|
Walther@60722
|
562 |
fun switch_type_TEST descr [] = descr
|
Walther@60722
|
563 |
| switch_type_TEST (Free (id_string, _)) ts =
|
Walther@60724
|
564 |
Free (id_string, ts |> hd |> TermC.lhs |> type_of)
|
Walther@60728
|
565 |
| switch_type_TEST descr _ = raise ERROR ("switch_type_TEST undefined argument " ^
|
Walther@60722
|
566 |
quote (UnparseC.term (ContextC.for_ERROR ()) descr))
|
Walther@60729
|
567 |
(*T_TESTold* )
|
Walther@60722
|
568 |
fun discern_typ _ (_, []) = []
|
Walther@60722
|
569 |
| discern_typ id (descr, ts) =
|
Walther@60728
|
570 |
(*TODO.md "review (descriptor, ts)" REMOVE--------------------------------------\*)
|
Walther@60723
|
571 |
let
|
Walther@60728
|
572 |
val ts = if Model_Pattern.is_list_descr descr andalso TermC.is_list (hd ts)
|
Walther@60723
|
573 |
then TermC.isalist2list (hd ts)
|
Walther@60723
|
574 |
else ts
|
Walther@60723
|
575 |
in
|
Walther@60728
|
576 |
(*TODO.md "review (descriptor, ts)" REMOVE--------------------------------------/*)
|
Walther@60722
|
577 |
if Model_Pattern.typ_of_element descr = HOLogic.boolT
|
Walther@60722
|
578 |
andalso ts |> map TermC.lhs |> all_atoms
|
Walther@60722
|
579 |
then
|
Walther@60722
|
580 |
if length ts > 1
|
Walther@60722
|
581 |
then raise ERROR "model items of type 'bool' in lists with 'lengt ts > 1' NOT YET IMPLEMENTED"
|
Walther@60722
|
582 |
else [((switch_type_TEST id ts, TermC.lhs (hd ts)),
|
Walther@60722
|
583 |
(TermC.lhs (hd ts), TermC.rhs (hd ts)))]
|
Walther@60722
|
584 |
else []
|
Walther@60728
|
585 |
(*TODO.md "review (descriptor, ts)" REMOVE--------------------------------------\*)
|
Walther@60723
|
586 |
end
|
Walther@60728
|
587 |
(*TODO.md "review (descriptor, ts)" REMOVE--------------------------------------/*)
|
Walther@60729
|
588 |
( *T_TEST*)
|
Walther@60729
|
589 |
fun discern_typ _ (_, []) = []
|
Walther@60729
|
590 |
| discern_typ id (descr, ts) =
|
Walther@60729
|
591 |
(*TODO.md "review (descriptor, ts)" REMOVE--------------------------------------\*)
|
Walther@60729
|
592 |
let
|
Walther@60729
|
593 |
val ts = if Model_Pattern.is_list_descr descr
|
Walther@60729
|
594 |
then if TermC.is_list (hd ts)
|
Walther@60729
|
595 |
then ts |> map TermC.isalist2list |> flat
|
Walther@60729
|
596 |
else ts
|
Walther@60729
|
597 |
else ts
|
Walther@60729
|
598 |
in
|
Walther@60729
|
599 |
(*TODO.md "review (descriptor, ts)" REMOVE--------------------------------------/*)
|
Walther@60729
|
600 |
if Model_Pattern.typ_of_element descr = HOLogic.boolT andalso all_lhs_atoms ts
|
Walther@60729
|
601 |
then
|
Walther@60729
|
602 |
if length ts > 1
|
Walther@60729
|
603 |
then (writeln "model items of type 'bool' in lists with 'lengt ts > 1' NOT YET IMPLEMENTED";
|
Walther@60729
|
604 |
[])
|
Walther@60729
|
605 |
else [((switch_type_TEST id ts, TermC.lhs (hd ts)),
|
Walther@60729
|
606 |
(TermC.lhs (hd ts), TermC.rhs (hd ts)))]
|
Walther@60729
|
607 |
else []
|
Walther@60729
|
608 |
(*TODO.md "review (descriptor, ts)" REMOVE--------------------------------------\*)
|
Walther@60729
|
609 |
end
|
Walther@60729
|
610 |
(*TODO.md "review (descriptor, ts)" REMOVE--------------------------------------/*)
|
Walther@60729
|
611 |
(*T_TESTnew*)
|
Walther@60729
|
612 |
|
Walther@60722
|
613 |
fun discern_feedback id (Model_Def.Cor_TEST ((descr, ts), _)) = discern_typ id (descr, ts)
|
Walther@60722
|
614 |
| discern_feedback _ (Model_Def.Syn_TEST _) = [] (*TODO: handle correct elements*)
|
Walther@60722
|
615 |
| discern_feedback id (Model_Def.Inc_TEST ((descr, ts), _)) = discern_typ id (descr, ts)
|
Walther@60722
|
616 |
| discern_feedback _ (Model_Def.Sup_TEST _) = []
|
Walther@60728
|
617 |
fun make_envs_preconds equal_givens =
|
Walther@60722
|
618 |
map (fn ((_, (_, id)), (_, _, _, _, (feedb, _))) => discern_feedback id feedb) equal_givens
|
Walther@60722
|
619 |
|> flat
|
Walther@60721
|
620 |
|
Walther@60712
|
621 |
(* filter_variants': Model_Def.i_model_TEST -> Model_Def.variant -> Model_Def.i_model_TEST*)
|
Walther@60710
|
622 |
fun filter_variants' i_singles n =
|
Walther@60710
|
623 |
filter (fn (_, variants, _, _, _) => member op= variants n) i_singles
|
Walther@60710
|
624 |
(*arrange_args1: int list -> int * Model_Def.variant list -> (int * Model_Def.variant) list*)
|
Walther@60710
|
625 |
fun arrange_args1 [] _ = []
|
Walther@60710
|
626 |
| arrange_args1 (sum :: ss) (cnt, all) = (sum, nth cnt all) :: (arrange_args1 ss (cnt + 1, all))
|
Walther@60710
|
627 |
(*cnt_corrects: I_Model.T_TEST -> int*)
|
Walther@60710
|
628 |
fun cnt_corrects i_model =
|
Walther@60710
|
629 |
fold (curry op +) (map (fn (_, _, _, _, (Cor_TEST _, _)) => 1 | _ => 0) i_model) 0;
|
Walther@60723
|
630 |
|
Walther@60728
|
631 |
(*T_TESTold* )
|
Walther@60710
|
632 |
fun of_max_variant model_patt i_model =
|
Walther@60728
|
633 |
( *T_TEST*)
|
Walther@60723
|
634 |
fun of_max_variant _ [] = ([], [], ([], []))
|
Walther@60723
|
635 |
| of_max_variant model_patt i_model =
|
Walther@60728
|
636 |
(*T_TESTnew*)
|
Walther@60723
|
637 |
let
|
Walther@60710
|
638 |
val all_variants =
|
Walther@60710
|
639 |
map (fn (_, variants, _, _, _) => variants) i_model
|
Walther@60710
|
640 |
|> flat
|
Walther@60710
|
641 |
|> distinct op =
|
Walther@60710
|
642 |
val variants_separated = map (filter_variants' i_model) all_variants
|
Walther@60710
|
643 |
val sums_corr = map (cnt_corrects) variants_separated
|
Walther@60710
|
644 |
val sum_variant_s = arrange_args1 sums_corr (1, all_variants)
|
Walther@60710
|
645 |
val (_, max_variant) = hd (*..crude decision, up to improvement *)
|
Walther@60710
|
646 |
(sort (fn ((i, _), (j, _)) => int_ord (i, j)) sum_variant_s)
|
Walther@60710
|
647 |
val i_model_max =
|
Walther@60710
|
648 |
filter (fn (_, variants, _ , _ ,_) => member (op =) variants max_variant) i_model
|
Walther@60721
|
649 |
val equal_descr_pairs = map (get_equal_descr i_model) model_patt |> flat
|
Walther@60728
|
650 |
(*T_TESTold* )
|
Walther@60712
|
651 |
val env_subst = mk_env_subst_DEL equal_descr_pairs
|
Walther@60712
|
652 |
val env_eval = mk_env_eval_DEL i_model_max
|
Walther@60710
|
653 |
in
|
Walther@60712
|
654 |
(i_model_max, env_subst, env_eval)
|
Walther@60710
|
655 |
end
|
Walther@60728
|
656 |
( *T_TEST*)
|
Walther@60722
|
657 |
val env_model = make_env_model equal_descr_pairs
|
Walther@60722
|
658 |
val equal_givens = filter (fn ((m_field, _), _) => m_field = "#Given") equal_descr_pairs
|
Walther@60722
|
659 |
val subst_eval_list = make_envs_preconds equal_givens
|
Walther@60722
|
660 |
val (env_subst, env_eval) = split_list subst_eval_list
|
Walther@60721
|
661 |
in
|
Walther@60722
|
662 |
(i_model_max, env_model, (env_subst, env_eval))
|
Walther@60721
|
663 |
end
|
Walther@60728
|
664 |
(*T_TESTnew*)
|
Walther@60712
|
665 |
|
Walther@60712
|
666 |
|
Walther@60712
|
667 |
(** check pre-conditions **)
|
Walther@60710
|
668 |
|
Walther@60727
|
669 |
(* WRONG ENVS expects the precondition from Problem, ie. needs substitution *)
|
Walther@60706
|
670 |
fun check _ _ [] _ _ = (true, [])
|
Walther@60706
|
671 |
| check ctxt where_rls pres pbl (_ (*I_Model.variants UNNECESSARY: I_Model.T filtered for*)) =
|
Walther@60706
|
672 |
let
|
Walther@60706
|
673 |
val env = environment pbl;
|
Walther@60706
|
674 |
val pres' = map (TermC.subst_atomic_all env) pres;
|
Walther@60706
|
675 |
val evals = map (eval ctxt where_rls) pres';
|
Walther@60706
|
676 |
in (foldl and_ (true, map fst evals), evals) end;
|
Walther@60727
|
677 |
(* WRONG ENVS expects the precondition from PIDE, ie. already substituted *)
|
Walther@60706
|
678 |
fun check_TEST _ _ [] _ = (true, [])
|
Walther@60706
|
679 |
| check_TEST ctxt where_rls preconds env_eval =
|
Walther@60706
|
680 |
let
|
Walther@60706
|
681 |
val full_subst =
|
Walther@60706
|
682 |
map (fn (t, pos) => (subst_atomic env_eval t, pos)) preconds
|
Walther@60706
|
683 |
val evals =
|
Walther@60706
|
684 |
map (fn (term, pos) => (Rewrite.eval_true ctxt [term] where_rls, (term, pos))) full_subst
|
Walther@60706
|
685 |
in
|
Walther@60706
|
686 |
(foldl and_ (true, map fst evals), (map fst evals) ~~ preconds)
|
Walther@60706
|
687 |
end;
|
Walther@60727
|
688 |
(* WRONG ENVS *)
|
Walther@60710
|
689 |
fun check_from_store ctxt where_rls pres env_subst env_eval =
|
Walther@60710
|
690 |
let
|
Walther@60710
|
691 |
val pres_subst = map (TermC.subst_atomic_all env_subst) pres;
|
Walther@60710
|
692 |
val full_subst = map (fn (bool, t) => (bool, subst_atomic env_eval t)) pres_subst
|
Walther@60710
|
693 |
val evals = map (eval ctxt where_rls) full_subst
|
Walther@60710
|
694 |
in
|
Walther@60710
|
695 |
(foldl and_ (true, map fst evals), pres_subst)
|
Walther@60710
|
696 |
end
|
Walther@60727
|
697 |
(* vvv-replaces-^^^*)
|
Walther@60727
|
698 |
fun check_envs_TEST ctxt where_rls pres (env_model, (env_subst, env_eval)) =
|
Walther@60727
|
699 |
let
|
Walther@60727
|
700 |
val pres_subst = map (TermC.subst_atomic_all env_subst) pres;
|
Walther@60727
|
701 |
val pres_subst_other = map (TermC.subst_atomic_all env_model) (map #2 pres_subst);
|
Walther@60727
|
702 |
val full_subst = if env_eval = [] then pres_subst_other
|
Walther@60727
|
703 |
else map (TermC.subst_atomic_all env_eval) (map #2 pres_subst_other)
|
Walther@60727
|
704 |
val evals = map (eval ctxt where_rls) full_subst
|
Walther@60727
|
705 |
in
|
Walther@60727
|
706 |
(foldl and_ (true, map fst evals), pres_subst_other)
|
Walther@60727
|
707 |
end
|
Walther@60710
|
708 |
|
Walther@60726
|
709 |
(* expects the precondition from Problem, ie. needs substitution by env_model*)
|
Walther@60706
|
710 |
fun check_OLD _ _ [] _ = (true, [])
|
Walther@60729
|
711 |
| check_OLD ctxt where_rls where_ (model_patt, i_model) =
|
Walther@60706
|
712 |
let
|
Walther@60728
|
713 |
(*T_TESTold* )
|
Walther@60706
|
714 |
val (env_subst, env_eval) = sep_variants_envs_OLD model_patt i_model
|
Walther@60706
|
715 |
val pres_subst = map (TermC.subst_atomic_all env_subst) pres;
|
Walther@60706
|
716 |
val full_subst = map (fn (bool, t) => (bool, subst_atomic env_eval t)) pres_subst
|
Walther@60727
|
717 |
val evals = map (eval ctxt where_rls) full_subst
|
Walther@60727
|
718 |
in
|
Walther@60727
|
719 |
(foldl and_ (true, map fst evals), pres_subst)
|
Walther@60727
|
720 |
end;
|
Walther@60728
|
721 |
( *T_TEST*)
|
Walther@60726
|
722 |
val (_, env_model, (env_subst, env_eval)) = of_max_variant model_patt i_model
|
Walther@60729
|
723 |
val pres_subst = map (TermC.subst_atomic_all env_subst) where_;
|
Walther@60726
|
724 |
val pres_subst_other = map (TermC.subst_atomic_all env_model) (map #2 pres_subst);
|
Walther@60726
|
725 |
val full_subst = if env_eval = [] then pres_subst_other
|
Walther@60726
|
726 |
else map (TermC.subst_atomic_all env_eval) (map #2 pres_subst_other)
|
Walther@60706
|
727 |
val evals = map (eval ctxt where_rls) full_subst
|
Walther@60706
|
728 |
in
|
Walther@60726
|
729 |
(foldl and_ (true, map fst evals), pres_subst_other)
|
Walther@60706
|
730 |
end;
|
Walther@60728
|
731 |
(*T_TESTnew*)
|
Walther@60706
|
732 |
|
Walther@60706
|
733 |
fun unchecked_OLD_to_TEST pres = map (fn t => (t, Position.none)) pres
|
Walther@60706
|
734 |
|
walther@59965
|
735 |
(**)end(**)
|