src/Tools/isac/Specify/refine.sml
author wneuper <Walther.Neuper@jku.at>
Mon, 11 Dec 2023 17:26:30 +0100
changeset 60782 e797d1bdfe37
parent 60775 c31ae770d74c
permissions -rw-r--r--
eliminate the intermediate *_POS
     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   val problem: theory -> Problem.id -> I_Model.T -> (Problem.id * (I_Model.T * Pre_Conds.T)) option
    32 
    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
    36 
    37 (*from isac_test for Minisubpbl*)
    38   type match_model_prec
    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 ->
    43     Problem.id option
    44   val refins: Proof.context -> Problem.id -> O_Model.T -> Problem.T Store.node list ->
    45     Problem.id option
    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
    48 
    49 \<^isac_test>\<open>
    50 (**)
    51 \<close>
    52 end
    53 
    54 (**)
    55 structure Refine(**) : REFINE_PROBLEM(**) =
    56 struct
    57 (**)
    58 
    59 datatype match_model_prec = 
    60   Match_ of Problem.id * (( I_Model.T) * (Pre_Conds.T))
    61 | NoMatch_;
    62 
    63 fun is_matches_ (Match_ _) = true
    64   | is_matches_ _ = false;
    65 fun match_found ms = ((find_first is_matches_) o rev) ms;
    66 
    67 (* version for tactic Refine_Problem *)
    68 (*OLD*)
    69 fun find_match _ (pblRD: Problem.id) itms pbls (Store.Node (pI, [py], [])) =
    70     let
    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);
    74     in
    75       if b
    76       then pbls @ [Match_ (rev (pblRD @ [pI]), (itms, where_'))]
    77       else pbls @ [NoMatch_] 
    78     end
    79   | find_match _ pblRD itms pbls (Store.Node (pI, [py], pys)) =
    80     let
    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);
    84     in if b then
    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_])
    88     end              
    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) =
    92     let
    93       val pbls' = find_match thy pblRD itms pbls p
    94     in case last_elem pbls' of
    95       Match_ _ => pbls'
    96     | NoMatch_ => find_matchs thy pblRD itms pbls' pts
    97   end;
    98 
    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
   102 	  NONE => NONE
   103   | SOME (Match_ (rfd as (pI', _))) => if pblID = pI' then NONE else SOME rfd
   104   | _ => raise ERROR "Refine.problem: undef. args";
   105 
   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], [])) =
   109     let
   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_
   113     in
   114       if M_Match.by_o_model ctxt ori (model, where_, where_rls) 
   115       then SOME (pblRD(**) @ [pI](**))
   116       else NONE
   117     end
   118   | check_match ctxt pblRD ori (Store.Node (pI, [py], pys)) =
   119     let
   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_
   123     in
   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](**)))
   128       else NONE
   129     end
   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)
   136 
   137 \<^isac_test>\<open>
   138 (**)
   139 \<close>
   140 
   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], [])) =
   145     let
   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)
   153     in                                                  
   154       if b
   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_')]
   157     end
   158   | refin' ctxt pblRD fmz pbls (Store.Node (pI, [py], pys)) =
   159     let
   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)
   167     in
   168       if b 
   169       then
   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_')])
   173     end
   174   | refin' _ _ _ _ _ = raise ERROR "refin': uncovered fun def."
   175 and refins' _ _ _ pbls [] = pbls
   176   | refins' ctxt pblRD fmz pbls ((p as Store.Node _) :: pts) =
   177     let
   178       val pbls' = refin' ctxt pblRD fmz pbls p
   179     in
   180       case last_elem pbls' of
   181         M_Match.Matches _ => pbls'
   182       | M_Match.NoMatch _ => refins' ctxt pblRD fmz pbls' pts
   183     end;
   184 
   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;
   188 
   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 =
   191   let
   192     val opt = find_node_elem (check_match ctxt ((rev o tl) pblID) oris) pblID (rev pblID);
   193   in case opt of 
   194       SOME pblRD =>
   195         let val pblID': Problem.id = rev pblRD
   196 			  in if pblID' = pblID then NONE else SOME pblID' end
   197 	  | NONE => NONE
   198 	end;
   199 fun by_o_model' ctxt oris pI = perhaps (by_o_model ctxt oris) pI;
   200 
   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);
   204 
   205 \<^isac_test>\<open>
   206 (**)
   207 \<close>
   208 
   209 (**)end(**)