1 (* Title: Specify/refine.sml
2 Author: Walther Neuper 110226
3 (c) due to copyright terms
5 Refine a problem by a search for a \<open>ML_structure Model_Pattern\<close>
6 better fitting the respective where_-condition.
8 The search on the tree given by @{term Know_Store.get_pbls} is costly such that
9 \<open>ML_structure Know_Store\<close> holds terms pre-parsed with a most generally type.
11 On transfer to a calculation these terms are strongly typed by Model_Pattern.adapt_to_type
12 (and users of this function in \<open>ML_structure Error_Pattern, MethodC, Problem\<close>)
13 according to the current context.
15 Note: From the children of eb89f586b0b2 onwards the old functions (\<open>term TermC.typ_a2real\<close> etc)
16 are adapted for "adapt_to_type on the fly" until further clarification.
18 timing (from evaluating imports to use-cases.sml) for Test_Isac_Short
19 before adapt_to_type: 01:05 sec:sec
21 timing for test/../refine.sml --- equation with CalcTree [ = 6 / 5] for timing: ...
22 before adapt_to_type: 1.5 sec
24 timing for test/../refine.sml --- refine ad-hoc equation for timing: ...
25 before adapt_to_type: 0.05 sec
29 signature REFINE_PROBLEM =
31 val problem: theory -> Problem.id -> I_Model.T -> (Problem.id * (I_Model.T * Pre_Conds.T)) option
33 val by_o_model : Proof.context -> O_Model.T -> Problem.id -> Problem.id option
34 val by_o_model' : Proof.context -> O_Model.T -> Problem.id -> Problem.id
35 val by_formalise: Proof.context -> Formalise.model -> Problem.id -> M_Match.T list
37 (*from isac_test for Minisubpbl*)
39 val find_match: theory -> Problem.id -> I_Model.T -> match_model_prec list ->
40 Problem.T Store.node -> match_model_prec list
41 val find_node_elem: (Probl_Def.T Store.node -> 'a) -> Store.key -> Store.key -> 'a
42 val check_match: Proof.context -> Problem.id -> O_Model.T -> Problem.T Store.node ->
44 val refins: Proof.context -> Problem.id -> O_Model.T -> Problem.T Store.node list ->
46 val match_found: match_model_prec list -> match_model_prec option
47 val find_matchs: theory -> Problem.id -> I_Model.T -> match_model_prec list -> Problem.T Store.node list -> match_model_prec list
55 structure Refine(**) : REFINE_PROBLEM(**) =
59 datatype match_model_prec =
60 Match_ of Problem.id * (( I_Model.T) * (Pre_Conds.T))
63 fun is_matches_ (Match_ _) = true
64 | is_matches_ _ = false;
65 fun match_found ms = ((find_first is_matches_) o rev) ms;
67 (* version for tactic Refine_Problem *)
69 fun find_match _ (pblRD: Problem.id) itms pbls (Store.Node (pI, [py], [])) =
71 val {thy, model, where_, where_rls, ...} = py
72 val ctxt = Proof_Context.init_global thy
73 val (b, where_') = M_Match.by_i_model ctxt itms (model, where_, where_rls);
76 then pbls @ [Match_ (rev (pblRD @ [pI]), (itms, where_'))]
77 else pbls @ [NoMatch_]
79 | find_match _ pblRD itms pbls (Store.Node (pI, [py], pys)) =
81 val {thy, model, where_, where_rls, ...} = py
82 val ctxt = Proof_Context.init_global thy
83 val (b, where_') = M_Match.by_i_model ctxt itms (model, where_, where_rls);
85 let val pbl = Match_ (rev (pblRD @ [pI]), (itms, where_'))
86 in find_matchs thy (pblRD @ [pI]) itms (pbls @ [pbl]) pys end
87 else (pbls @ [NoMatch_])
89 | find_match _ _ _ _ _ = raise ERROR "find_match: uncovered fun def."
90 and find_matchs _ _ _ pbls [] = pbls
91 | find_matchs thy pblRD itms pbls ((p as Store.Node _) :: pts) =
93 val pbls' = find_match thy pblRD itms pbls p
94 in case last_elem pbls' of
96 | NoMatch_ => find_matchs thy pblRD itms pbls' pts
99 fun problem thy pblID itms =
100 case match_found ((Store.apply (get_pbls ())) (find_match thy ((rev o tl) pblID) itms [])
101 pblID (rev pblID)) of
103 | SOME (Match_ (rfd as (pI', _))) => if pblID = pI' then NONE else SOME rfd
104 | _ => raise ERROR "Refine.problem: undef. args";
106 (* refine a problem; construct pblRD while scanning Problem.T Store.T*)
107 (*TODO: as \<open>check_match: 'a -> .. -> 'b option\<close> could be ignorant of Store.T structure.*)
108 fun check_match ctxt pblRD ori (Store.Node (pI, [py], [])) =
110 val {where_rls, model, where_, ...} = py: Problem.T
111 val model = map (Model_Pattern.adapt_to_type ctxt) model
112 val where_ = map (ParseC.adapt_term_to_type ctxt) where_
114 if M_Match.by_o_model ctxt ori (model, where_, where_rls)
115 then SOME (pblRD(**) @ [pI](**))
118 | check_match ctxt pblRD ori (Store.Node (pI, [py], pys)) =
120 val {where_rls, model, where_, ...} = py: Problem.T
121 val model = map (Model_Pattern.adapt_to_type ctxt) model
122 val where_ = map (ParseC.adapt_term_to_type ctxt) where_
124 if M_Match.by_o_model ctxt ori (model, where_, where_rls)
125 then (case refins ctxt (pblRD @ [pI]) ori pys of
126 SOME pblRD' => SOME pblRD'
127 | NONE => SOME (pblRD (**)@ [pI](**)))
130 | check_match _ _ _ _ = raise ERROR "check_match: uncovered fun def."
131 and refins _ _ _ [] = NONE
132 | refins ctxt pblRD ori ((p as Store.Node _) :: pts) =
133 (case check_match ctxt pblRD ori p of
134 SOME pblRD' => SOME pblRD'
135 | NONE => refins ctxt pblRD ori pts)
141 (* refine a problem; version providing output for math authors *)
142 (*val refin': Proof.context -> Problem.id -> Formalise.model -> M_Match.T list ->
143 Probl_Def.T Store.node -> M_Match.T list*)
144 fun refin' ctxt pblRD fmz pbls (Store.Node (pI, [py: Probl_Def.T], [])) =
146 val _ = (tracing o (curry op ^ "*** pass ") o strs2str) (pblRD @ [pI])
147 val {thy, model, where_, where_rls, ...} = py
148 val model = map (Model_Pattern.adapt_to_type ctxt) model
149 val where_ = map (ParseC.adapt_term_to_type ctxt) where_
150 val (oris, _) = O_Model.init thy fmz model; (*WN020803: oris might NOT be complete here*)
151 val (b, (itms, where_')) =
152 M_Match.data_by_o ctxt oris (model, where_, where_rls)
155 then pbls @ [M_Match.Matches (rev (pblRD @ [pI]), P_Model.from thy itms where_')]
156 else pbls @ [M_Match.NoMatch (rev (pblRD @ [pI]), P_Model.from thy itms where_')]
158 | refin' ctxt pblRD fmz pbls (Store.Node (pI, [py], pys)) =
160 val _ = (tracing o ((curry op ^)"*** pass ") o strs2str) (pblRD @ [pI])
161 val {thy, model, where_, where_rls, ...} = py
162 val model = map (Model_Pattern.adapt_to_type ctxt) model
163 val where_ = map (ParseC.adapt_term_to_type ctxt) where_
164 val (oris, _) = O_Model.init thy fmz model; (*WN020803: oris might NOT be complete here*)
165 val (b, (itms, where_')) =
166 M_Match.data_by_o ctxt oris (model, where_, where_rls)
170 let val pbl = M_Match.Matches (rev (pblRD @ [pI]), P_Model.from thy itms where_')
171 in refins' ctxt (pblRD @ [pI]) fmz (pbls @ [pbl]) pys end
172 else (pbls @ [M_Match.NoMatch (rev (pblRD @ [pI]), P_Model.from thy itms where_')])
174 | refin' _ _ _ _ _ = raise ERROR "refin': uncovered fun def."
175 and refins' _ _ _ pbls [] = pbls
176 | refins' ctxt pblRD fmz pbls ((p as Store.Node _) :: pts) =
178 val pbls' = refin' ctxt pblRD fmz pbls p
180 case last_elem pbls' of
181 M_Match.Matches _ => pbls'
182 | M_Match.NoMatch _ => refins' ctxt pblRD fmz pbls' pts
185 (* Store.apply scans Store.T only to the first hit *)
186 (*val find_node_elem: (Probl_Def.T Store.node -> 'a) -> Store.key -> Store.key -> 'a*)
187 fun find_node_elem x = Store.apply (get_pbls ()) x;
189 (*for tactic Refine_Tacitly; oris are already created wrt. some pbt; ctxt overrides thy in pbt*)
190 fun by_o_model ctxt oris pblID =
192 val opt = find_node_elem (check_match ctxt ((rev o tl) pblID) oris) pblID (rev pblID);
195 let val pblID': Problem.id = rev pblRD
196 in if pblID' = pblID then NONE else SOME pblID' end
199 fun by_o_model' ctxt oris pI = perhaps (by_o_model ctxt oris) pI;
201 (*specifically for tests*)
202 fun by_formalise ctxt fmz pblID =
203 find_node_elem (refin' ctxt ((rev o tl) pblID) fmz []) pblID (rev pblID);