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