src/Tools/isac/Specify/refine.sml
author wneuper <Walther.Neuper@jku.at>
Tue, 15 Aug 2023 12:22:49 +0200
changeset 60729 43d11e7742e1
parent 60706 632abf0c253c
child 60736 7297c166991e
permissions -rw-r--r--
prepare 13: Testi_Isac_Short without errors
     1 (* Title:  Specify/refine.sml
     2    Author: Walther Neuper 110226
     3    (c) due to copyright terms
     4 
     5 Refine a problem by a search for a \<open>ML_structure Model_Pattern\<close> 
     6 better fitting the respective where_-condition.
     7 
     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. 
    10 
    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.
    14 
    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.
    17 
    18 timing (from evaluating imports to use-cases.sml) for Test_Isac_Short
    19   before adapt_to_type: 01:05 sec:sec
    20   after  adapt_to_type: 
    21 timing for test/../refine.sml --- equation with CalcTree [ = 6 / 5] for timing: ...
    22   before adapt_to_type: 1.5 sec
    23   after  adapt_to_type: 
    24 timing for test/../refine.sml --- refine ad-hoc equation for timing: ...
    25   before adapt_to_type: 0.05 sec
    26   after  adapt_to_type: 
    27 *)                          
    28 
    29 signature REFINE_PROBLEM =
    30 sig
    31 (**)
    32   val problem: theory -> Problem.id -> I_Model.T -> (Problem.id * (I_Model.T * Pre_Conds.T)) option
    33 
    34 (**)
    35   val refine_ori : Proof.context -> O_Model.T -> Problem.id -> Problem.id option
    36 (**)
    37   val refine_ori' : Proof.context -> O_Model.T -> Problem.id -> Problem.id
    38 (*from isac_test for Minisubpbl*)
    39 (**)
    40   datatype match_ = Match_ of Problem.id * (( I_Model.T) * (Pre_Conds.T)) | NoMatch_;
    41 (**)
    42   val refin'': theory -> Problem.id -> I_Model.T -> match_ list -> Problem.T Store.node -> match_ list
    43 
    44 (*from isac_test for Minisubpbl*)
    45   val app_ptyp: (Probl_Def.T Store.node -> 'a) -> Store.key -> Store.key -> 'a
    46 (*val refin: Problem.id -> O_Model.T -> Problem.T Store.node -> Problem.id option*)
    47   val refin: Proof.context -> Problem.id -> O_Model.T -> Problem.T Store.node -> Problem.id option
    48 
    49 \<^isac_test>\<open>
    50 (*val test : Formalise.model -> Problem.id -> M_Match.T list*)
    51 (*val refine : Formalise.model -> Problem.id -> M_Match.T list*)
    52   val xxxxx: Proof.context -> Formalise.model -> Problem.id -> M_Match.T list
    53 (**)
    54   val refined_: match_ list -> match_ option
    55 (**)
    56   val refins'': theory -> Problem.id -> I_Model.T -> match_ list -> Problem.T Store.node list -> match_ list
    57 (**)
    58 \<close>
    59 end
    60 
    61 (**)
    62 structure Refine(**) : REFINE_PROBLEM(**) =
    63 struct
    64 (**)
    65 
    66 datatype match_ = 
    67   Match_ of Problem.id * (( I_Model.T) * (Pre_Conds.T))
    68 | NoMatch_;
    69 
    70 fun is_matches_ (Match_ _) = true
    71   | is_matches_ _ = false;
    72 
    73 fun refined_ ms = ((find_first is_matches_) o rev) ms;
    74 
    75 fun eq1 d (_, (d', _)) = (d = d');
    76 
    77 (*  chk_: theory -> ('a * (term * term)) list -> I_Model.single -> I_Model.single*)
    78 fun chk_ (_: theory) pbt (i, vats, b, f, I_Model.Cor ((d, vs), _)) =
    79       (case find_first (eq1 d) pbt of 
    80         SOME (_, (_, id)) => (i, vats, b, f, I_Model.Cor ((d, vs), (id, [Input_Descript.join'''' (d, vs)])))
    81       | NONE =>  (i, vats, false, f, I_Model.Sup (d, vs)))
    82   | chk_ _ pbt (i, vats, b, f, I_Model.Inc ((d, vs), _)) =
    83       (case find_first (eq1 d) pbt of 
    84         SOME (_, (_, id)) => (i, vats, b, f, I_Model.Cor ((d, vs), (id, [Input_Descript.join'''' (d, vs)])))
    85       | NONE => (i, vats, false, f, I_Model.Sup (d, vs)))
    86   | chk_ _ _ (itm as (_, _, _, _, I_Model.Syn _)) = itm
    87   | chk_ _ _ (itm as (_, _, _, _, I_Model.Typ _)) = itm
    88   | chk_ _ pbt (i, vats, b, f, I_Model.Sup (d, vs)) =
    89       (case find_first (eq1 d) pbt of 
    90         SOME (_, (_, id)) => (i, vats, b, f, I_Model.Cor ((d,vs), (id, [Input_Descript.join'''' (d, vs)])))
    91       | NONE => (i, vats, false, f, I_Model.Sup (d, vs)))
    92   | chk_ _ pbt (i, vats, _, f, I_Model.Mis (d, vs)) =
    93       (case find_first (eq1 d) pbt of
    94         SOME _ =>
    95           raise ERROR "chk_: ((i,vats,b,f,I_Model.Cor ((d,vs),(id, Input_Descript.join'''' d vs))):itm)"
    96       | NONE => (i, vats, false, f, I_Model.Sup (d, [vs])))
    97   | chk_ _ _ _ = raise ERROR "chk_: uncovered fun def.";
    98 
    99 fun eq2 (_, (d, _)) (_, _, _, _, itm_) = d = I_Model.descriptor itm_;
   100 fun eq0 (0, _, _, _, _) = true
   101   | eq0 _ = false;
   102 fun max_i i [] = i
   103   | max_i i ((id, _, _, _, _) :: is) = if i > id then max_i i is else max_i id is;
   104 fun max_id [] = 0
   105   | max_id ((id, _, _, _, _) :: is) = max_i id is;
   106 fun add_idvat itms _ _ [] = itms
   107   | add_idvat itms i mvat ((_, _, b, f, itm_) :: its) =
   108     add_idvat (itms @ [(i, [], b, f, itm_)]) (i + 1) mvat its;
   109 
   110 (* find elements of pbt not contained in itms;
   111    if such one is untouched, return this one, otherwise create new itm *)
   112 fun chk_m itms untouched (p as (f, (d, id))) = 
   113   case find_first (eq2 p) itms of
   114 	  SOME _ => []
   115   | NONE =>
   116       (case find_first (eq2 p) untouched of
   117         SOME itm => [itm]
   118       | NONE => [(0, [], false, f, I_Model.Mis (d, id))]);
   119 
   120 fun chk_mis mvat itms untouched pbt = 
   121     let val mis = (flat o (map (chk_m itms untouched))) pbt; 
   122         val mid = max_id itms;
   123     in add_idvat [] (mid + 1) mvat mis end;
   124 
   125 (* check a problem (ie. itm list) for matching a problemtype, 
   126    takes the Pre_Conds.max_variant for concluding completeness (could be another!) *)
   127 (*T_TESTold* )
   128 fun match_itms thy itms (pbt, where_, where_rls) = 
   129   let
   130     fun okv mvat (_, vats, b, _, _) = member op = vats mvat andalso b;
   131     val itms' = map (chk_ thy pbt) itms; (* all found are #3 true *)
   132     val mvat = Pre_Conds.max_variant itms';
   133 	  val itms'' = filter (okv mvat) itms';
   134 	  val untouched = filter eq0 itms; (* i.e. dsc only (from init)*)
   135 	  val mis = chk_mis mvat itms'' untouched pbt;
   136 	  val (pb, where_')  = Pre_Conds.check (Proof_Context.init_global thy) where_rls where_ itms'' mvat
   137   in (length mis = 0 andalso pb, (itms'@ mis, where_')) end;
   138 ( *T_TEST**)
   139 fun match_itms thy itms (pbt, where_, where_rls) = 
   140   let
   141     fun okv mvat (_, vats, b, _, _) = member op = vats mvat andalso b;
   142     val itms' = map (chk_ thy pbt) itms; (* all found are #3 true *)
   143     val mvat = Pre_Conds.max_variant itms';
   144 	  val itms'' = filter (okv mvat) itms';
   145 	  val untouched = filter eq0 itms; (* i.e. dsc only (from init)*)
   146 	  val mis = chk_mis mvat itms'' untouched pbt;
   147 	  val (pb, where_')  = Pre_Conds.check_OLD (Proof_Context.init_global thy) where_rls where_
   148 	    (pbt, I_Model.OLD_to_TEST itms'')
   149   in (length mis = 0 andalso pb, (itms'@ mis, where_')) end;
   150 (*T_TESTnew*)
   151 
   152 (* refine a problem; version for tactic Refine_Problem *)
   153 fun refin'' _ (pblRD: Problem.id) itms pbls (Store.Node (pI, [py], [])) =
   154     let
   155 	    val {thy, model, where_, where_rls, ...} = py
   156 	    (*TODO val where_ = map TermC.adapt_to_type where_ ...  adapt to current ctxt*)
   157 	    val (b, (itms', where_')) = match_itms thy itms (model, where_, where_rls);
   158     in
   159       if b
   160       then pbls @ [Match_ (rev (pblRD @ [pI]), (itms', where_'))]
   161       else pbls @ [NoMatch_] 
   162     end
   163   | refin'' _ pblRD itms pbls (Store.Node (pI, [py], pys)) =
   164     let
   165       val {thy, model, where_, where_rls, ...} = py 
   166       val (b, (itms', where_')) = match_itms thy itms (model, where_, where_rls);
   167     in if b 
   168        then let val pbl = Match_ (rev (pblRD @ [pI]), (itms', where_'))
   169 	    in refins'' thy (pblRD @ [pI]) itms (pbls @ [pbl]) pys end
   170        else (pbls @ [NoMatch_])
   171     end              
   172   | refin'' _ _ _ _ _ = raise ERROR "refin'': uncovered fun def."
   173 and refins'' _ _ _ pbls [] = pbls
   174   | refins'' thy pblRD itms pbls ((p as Store.Node _) :: pts) =
   175     let
   176       val pbls' = refin'' thy pblRD itms pbls p
   177     in case last_elem pbls' of
   178       Match_ _ => pbls'
   179     | NoMatch_ => refins'' thy pblRD itms pbls' pts
   180   end;
   181 
   182 fun problem thy pblID itms =
   183   case refined_ ((Store.apply (get_pbls ())) (refin'' thy ((rev o tl) pblID) itms [])
   184       pblID (rev pblID)) of
   185 	  NONE => NONE
   186   | SOME (Match_ (rfd as (pI', _))) => if pblID = pI' then NONE else SOME rfd;
   187 
   188 (* 
   189   refine a problem; construct pblRD while scanning Problem.T Store.T
   190 TODO: as \<open>refin: 'a -> .. -> 'b option\<close> could be ignorant of Store.T structure.
   191 *)
   192 fun refin ctxt pblRD ori (Store.Node (pI, [py], [])) =
   193     let
   194 val _ = writeln ("refin 1: " ^ strs2str pblRD)
   195       val {where_rls, model, where_, ...} = py: Problem.T
   196       val model = map (Model_Pattern.adapt_to_type ctxt) model
   197       val where_ = map (ParseC.adapt_term_to_type ctxt) where_
   198     in
   199       if M_Match.match_oris ctxt where_rls ori (model, where_) 
   200       then SOME (pblRD(**) @ [pI](**))
   201       else NONE
   202     end
   203   | refin ctxt pblRD ori (Store.Node (pI, [py], pys)) =
   204     let
   205 val _ = writeln ("refin 2: " ^ strs2str pblRD)
   206       val {where_rls, model, where_, ...} = py: Problem.T
   207       val model = map (Model_Pattern.adapt_to_type ctxt) model
   208       val where_ = map (ParseC.adapt_term_to_type ctxt) where_
   209     in
   210       if M_Match.match_oris ctxt where_rls ori (model, where_) 
   211       then (case refins ctxt (pblRD @ [pI]) ori pys of
   212 	        SOME pblRD' => SOME pblRD'
   213 	      | NONE => SOME (pblRD (**)@ [pI](**)))
   214       else NONE
   215     end
   216   | refin _ _ _ _ = raise ERROR "refin: uncovered fun def."
   217 and refins _ _ _ [] = NONE
   218   | refins ctxt pblRD ori ((p as Store.Node _) :: pts) =
   219 (writeln ("refins: " ^ strs2str pblRD);
   220     (case refin ctxt pblRD ori p of
   221       SOME pblRD' => SOME (pblRD')
   222     | NONE => refins ctxt pblRD ori pts)
   223 );
   224 
   225 \<^isac_test>\<open>
   226 (* refine a problem; version providing output for math authors *)
   227 (*val refin': Proof.context -> Problem.id -> Formalise.model -> M_Match.T list -> 
   228     Probl_Def.T Store.node -> M_Match.T list*)
   229 fun refin' ctxt pblRD fmz pbls (Store.Node (pI, [py: Probl_Def.T], [])) =
   230     let
   231       val _ = (tracing o (curry op ^ "*** pass ") o strs2str) (pblRD @ [pI])
   232       val {thy, model, where_, where_rls, ...} = py 
   233       val model = map (Model_Pattern.adapt_to_type ctxt) model
   234       val where_ = map (ParseC.adapt_term_to_type ctxt) where_
   235       val (oris, _) = O_Model.init thy fmz model; (*WN020803: oris might NOT be complete here*)
   236       val (b, (itms, where_')) =
   237         M_Match.match_oris' (Proof_Context.theory_of ctxt) oris (model, where_, where_rls)
   238     in                                                  
   239       if b
   240       then pbls @ [M_Match.Matches (rev (pblRD @ [pI]), P_Model.from thy itms where_')]
   241       else pbls @ [M_Match.NoMatch (rev (pblRD @ [pI]), P_Model.from thy itms where_')]
   242     end
   243   | refin' ctxt pblRD fmz pbls (Store.Node (pI, [py], pys)) =
   244     let
   245       val _ = (tracing o ((curry op ^)"*** pass ") o strs2str) (pblRD @ [pI])
   246       val {thy, model, where_, where_rls, ...} = py 
   247       val model = map (Model_Pattern.adapt_to_type ctxt) model
   248       val where_ = map (ParseC.adapt_term_to_type ctxt) where_
   249       val (oris, _) = O_Model.init thy fmz model; (*WN020803: oris might NOT be complete here*)
   250       val (b, (itms, where_')) =
   251         M_Match.match_oris' (Proof_Context.theory_of ctxt) oris (model, where_, where_rls)
   252     in
   253       if b 
   254       then
   255         let val pbl = M_Match.Matches (rev (pblRD @ [pI]), P_Model.from thy itms where_')
   256 	      in refins' ctxt (pblRD @ [pI]) fmz (pbls @ [pbl]) pys end
   257       else (pbls @ [M_Match.NoMatch (rev (pblRD @ [pI]), P_Model.from thy itms where_')])
   258     end
   259   | refin' _ _ _ _ _ = raise ERROR "refin': uncovered fun def."
   260 and refins' _ _ _ pbls [] = pbls
   261   | refins' ctxt pblRD fmz pbls ((p as Store.Node _) :: pts) =
   262     let
   263       val pbls' = refin' ctxt pblRD fmz pbls p
   264     in
   265       case last_elem pbls' of
   266         M_Match.Matches _ => pbls'
   267       | M_Match.NoMatch _ => refins' ctxt pblRD fmz pbls' pts
   268     end;
   269 \<close>
   270 
   271 (*
   272   TODO: rename \<rightarrow> apply_to_node
   273   apply a fun to a ptyps node.
   274   val app_ptyp: (Probl_Def.T Store.node -> 'a) -> Store.key -> Store.key -> 'a
   275 TODO: Store.apply scans Store.T only to the first hit; see Store.apply.
   276 *)
   277 fun app_ptyp x = Store.apply (get_pbls ()) x;
   278 
   279 (* TODO rename \<rightarrow> by_oris
   280    for tactic Refine_Tacitly
   281    oris are already created wrt. some pbt; ctxt overrides thy in pbt  *)
   282 fun refine_ori ctxt oris pblID =
   283   let
   284     val opt = app_ptyp (refin ctxt ((rev o tl) pblID) oris) pblID (rev pblID);
   285   in case opt of 
   286       SOME pblRD =>
   287         let val pblID': Problem.id = rev pblRD
   288 			  in if pblID' = pblID then NONE else SOME pblID' end
   289 	  | NONE => NONE
   290 	end;
   291 fun refine_ori' ctxt oris pI = perhaps (refine_ori ctxt oris) pI;
   292 
   293 \<^isac_test>\<open>
   294 fun xxxxx ctxt fmz pblID =
   295   app_ptyp (refin' ctxt ((rev o tl) pblID) fmz []) pblID (rev pblID);
   296 \<close>
   297 
   298 (**)end(**)