src/Tools/isac/Specify/specify.sml
author wneuper <Walther.Neuper@jku.at>
Fri, 24 Nov 2023 15:34:07 +0100
changeset 60766 2e0603ca18a4
parent 60763 2121f1a39a64
child 60768 14da2230d5c3
permissions -rw-r--r--
followup 3: repair new fill_from_o, uncomment maximum of tests
     1 (* Title:  Specify/specify.sml
     2    Author: Walther Neuper
     3 *)
     4 signature SPECIFY =
     5 sig
     6   val find_next_step: Calc.T -> string * (Pos.pos_ * Tactic.input)
     7   val do_all: Calc.T -> Calc.T 
     8   val finish_phase: Calc.T -> Calc.T
     9 
    10 (*OLD* )
    11   val item_to_add: theory -> O_Model.T -> Model_Pattern.T -> I_Model.T ->
    12     (Model_Def.m_field * TermC.as_string) option
    13 ( *---*)
    14   val item_to_add: Proof.context -> O_Model.T -> I_Model.T_TEST -> 
    15     (Model_Def.m_field * TermC.as_string) option
    16 (*NEW*)
    17   val by_Add_: string -> TermC.as_string -> Calc.T -> string * Calc.state_post
    18 (*from isac_test for Minisubpbl*)
    19 (*OLD* )
    20   val for_problem: Proof.context -> O_Model.T -> References.T * References.T -> I_Model.T * I_Model.T ->
    21     string * (Pos.pos_ * Tactic.input)
    22 ( *---*)
    23   val for_problem: Proof.context -> O_Model.T -> References.T * References.T -> I_Model.T * I_Model.T_TEST ->
    24     string * (Pos.pos_ * Tactic.input)
    25 (*NEW*)
    26   val for_method: Proof.context -> O_Model.T -> References.T * References.T -> I_Model.T * I_Model.T ->
    27     string * (Pos.pos_ * Tactic.input)
    28   val select_inc_lists: I_Model.T_TEST -> I_Model.T_TEST
    29 
    30 \<^isac_test>\<open>
    31 (**)
    32 \<close>
    33 end
    34 
    35 (**)
    36 structure Specify(**): SPECIFY(**) =
    37 struct
    38 (**)
    39 
    40 fun select_inc_lists i_model =
    41   let
    42     (* filter problem with Const ("Input_Descript.solutions", _) *)
    43     val filt = (fn (_, _, _, _, (I_Model.Inc_TEST (descr, _::_) , _)) => Model_Def.is_list_descr descr
    44       | _ => false)
    45   in
    46     (filter filt i_model) @ (filter_out filt i_model)
    47   end
    48 
    49 fun item_to_add ctxt o_model i_model =
    50   let
    51     val max_vnt = last_elem (*this decides, for which variant initially help is given*)
    52       (Model_Def.max_variants o_model i_model)
    53     val o_vnts = filter (fn (_, vnts, _, _, _) => member op= vnts max_vnt) o_model
    54     val i_to_select = i_model
    55       |> filter_out (fn (_, vnts, _, _, (I_Model.Cor_TEST _, _)) => member op= vnts max_vnt | _ => false)
    56       |> select_inc_lists
    57   in
    58     if i_to_select = [] then NONE
    59     else
    60       case I_Model.fill_from_o o_vnts (hd i_to_select) of
    61         SOME (_, _, _, m_field, (feedb, _)) =>
    62           SOME (m_field, feedb |> I_Model.feedb_args_to_string ctxt)
    63       | NONE => raise ERROR "item_to_add: NONE not.impl."
    64   end
    65 
    66 
    67 (** find a next step in the specify-phase **)
    68 (*
    69   here should be mutual recursion between for_problem ctxt and for_method
    70 *)
    71 fun for_problem ctxt oris ((dI', pI', mI'), (dI, pI, mI)) (pbl, met) =
    72   let
    73     val cpI = if pI = Problem.id_empty then pI' else pI;
    74     val cmI = if mI = MethodC.id_empty then mI' else mI;
    75     val {model = pbt, where_rls, where_, ...} = Problem.from_store ctxt cpI;
    76     val {model = mpc, ...} = MethodC.from_store ctxt cmI
    77     val (preok, _) = Pre_Conds.check ctxt where_rls where_ (pbt, I_Model.OLD_to_TEST pbl);
    78   in
    79     if dI' = ThyC.id_empty andalso dI = ThyC.id_empty then
    80       ("dummy", (Pos.Pbl, Tactic.Specify_Theory dI'))
    81     else if pI' = Problem.id_empty andalso pI = Problem.id_empty then
    82         ("dummy", (Pos.Pbl, Tactic.Specify_Problem pI'))
    83     else
    84       case find_first (fn (_, _, _, _, feedb) => I_Model.is_error feedb) pbl of
    85         SOME (_, _, _, fd, itm_) =>
    86           ("dummy", (Pos.Pbl, P_Model.mk_delete (ThyC.get_theory ctxt
    87             (if dI = ThyC.id_empty then dI' else dI)) fd itm_))
    88       | NONE => 
    89 (*OLD* )
    90         (case item_to_add (ThyC.get_theory ctxt
    91             (if dI = ThyC.id_empty then dI' else dI)) oris pbt pbl of
    92 ( *---*)
    93         (case item_to_add ctxt oris (I_Model.OLD_to_TEST pbl) of
    94 (*NEW*)
    95            SOME (fd, ct') => ("dummy", (Pos.Pbl, P_Model.mk_additem fd ct'))
    96          | NONE => (*pbl-items complete*)        
    97            if not preok then ("dummy", (Pos.Pbl, Tactic.Refine_Problem pI'))
    98            (*this is an ERROR fall through ------^^^^^^^^^^^^^^^^^^^^^ for Pre_Conds, redesign ?*)
    99            else if dI = ThyC.id_empty then ("dummy", (Pos.Pbl, Tactic.Specify_Theory dI'))
   100            else if pI = Problem.id_empty then ("dummy", (Pos.Pbl, Tactic.Specify_Problem pI'))
   101            else if mI = MethodC.id_empty then ("dummy", (Pos.Pbl, Tactic.Specify_Method mI'))
   102            else
   103             case find_first (fn (_, _, _, _, feedb) => I_Model.is_error feedb) (I_Model.TEST_to_OLD met) of
   104               SOME (_, _, _, fd, itm_) =>
   105                  ("dummy", (Pos.Met, P_Model.mk_delete (ThyC.get_theory ctxt dI) fd itm_))
   106             | NONE => 
   107 (*OLD* )
   108               (case item_to_add (ThyC.get_theory ctxt dI) oris mpc met of
   109 ( *---*)
   110               (case item_to_add ctxt oris met of
   111 (*NEW*)
   112     	          SOME (fd, ct') =>
   113                    ("dummy", (Pos.Met, P_Model.mk_additem fd ct')) (*30.8.01: where_?!?*)
   114     		      | NONE => ("dummy", (Pos.Met, Tactic.Apply_Method mI))))
   115   end
   116 
   117 fun for_method ctxt oris ((dI', pI', mI'), (dI, pI, mI)) (_, met) =
   118   let
   119     val cmI = if mI = MethodC.id_empty then mI' else mI;
   120     val {model = mpc, where_rls, where_, ...} = MethodC.from_store ctxt cmI;
   121     val (preok, _) = Pre_Conds.check ctxt where_rls where_ (mpc, I_Model.OLD_to_TEST met);
   122   in
   123     (case find_first (fn (_, _, _, _, feedb) => I_Model.is_error feedb) met of
   124       SOME (_,_,_, fd, itm_) =>
   125         ("dummy", (Pos.Met, P_Model.mk_delete (ThyC.get_theory ctxt
   126             (if dI = ThyC.id_empty then dI' else dI)) fd itm_))
   127     | NONE => 
   128 (*OLD* )
   129       case item_to_add (ThyC.get_theory ctxt 
   130           (if dI = ThyC.id_empty then dI' else dI)) oris mpc (met) of
   131 ( *---*)
   132       case item_to_add ctxt oris (I_Model.OLD_to_TEST met) of
   133 (*NEW*)
   134         SOME (fd, ct') =>
   135           ("dummy", (Pos.Met, P_Model.mk_additem fd ct'))
   136       | NONE => 
   137         (if dI = ThyC.id_empty then ("dummy", (Pos.Met, Tactic.Specify_Theory dI'))
   138          else if pI = Problem.id_empty then ("dummy", (Pos.Met, Tactic.Specify_Problem pI'))
   139          else if not preok then ("dummy", (Pos.Met, Tactic.Specify_Method mI))
   140          else ("dummy", (Pos.Met, Tactic.Apply_Method mI))))
   141   end
   142 
   143 (*
   144   on finding a next step switching from problem to method or vice versa is possible,
   145   because the user is free to switch and edit the respective models independently.
   146 TODO: this free switch is NOT yet implemented; e.g. 
   147   preok is relevant for problem + method, i.e. in for_problem ctxt + for_method
   148 *)
   149 fun find_next_step (pt, pos as (_, p_)) =
   150   let
   151     val {meth = met, origin = origin as (oris, o_refs as (_, pI', mI'), _), probl = pbl,
   152       spec = refs, ...} = Calc.specify_data (pt, pos);
   153     val ctxt = Ctree.get_ctxt pt pos
   154   in
   155     if Ctree.just_created (pt, pos) andalso origin <> Ctree.e_origin then
   156       case mI' of
   157         ["no_met"] => ("ok", (Pos.Pbl, Tactic.Refine_Tacitly pI'))
   158       | _ => ("ok", (Pos.Pbl, Tactic.Model_Problem))
   159     else if p_ = Pos.Pbl then
   160       for_problem ctxt oris (o_refs, refs) (pbl, I_Model.OLD_to_TEST met)
   161     else
   162       for_method ctxt oris (o_refs, refs) (pbl, met)
   163   end
   164 
   165 
   166 (** tools **)
   167 
   168 fun by_Add_  m_field ct (pt, pos as (_, p_)) =
   169   let
   170     val (met, oris, (_, pI', mI'), pbl, (_, pI, mI), ctxt) = SpecificationC.get_data (pt, pos)
   171     val (i_model, m_patt) =
   172        if p_ = Pos.Met then
   173          (met,
   174            (if mI = MethodC.id_empty then mI' else mI) |> MethodC.from_store ctxt |> #model)
   175        else
   176          (pbl,
   177            (if pI = Problem.id_empty then pI' else pI) |> Problem.from_store ctxt |> #model)
   178     in
   179       case I_Model.check_single ctxt m_field oris i_model m_patt ct of
   180         I_Model.Add i_single => (*..union old input *)
   181 	        let
   182 	          val i_model' = I_Model.add_single (Proof_Context.theory_of ctxt) i_single i_model
   183             val tac' = I_Model.make_tactic m_field (ct, i_model')
   184 	          val  (_, _, _, pt') =  Specify_Step.add tac' (Istate_Def.Uistate, ctxt) (pt, pos)
   185 	        in
   186             ("ok", ([(Tactic.input_from_T ctxt tac', tac', (pos, (Istate_Def.Uistate, ctxt)))],
   187               [], (pt', pos)))
   188           end
   189       | I_Model.Err msg => (msg, ([], [], (pt, pos)))
   190     end
   191 
   192 (* complete _NON_empty calc-head for autocalc (sub-)pbl from oris
   193   + met from fmz; assumes pos on PblObj, meth = []                    *)
   194 fun finish_phase (pt, pos as (p, p_)) =
   195   let
   196     val _ = if p_ <> Pos.Pbl 
   197 	    then raise ERROR ("###finish_phase: only impl.for Pbl, called with " ^ Pos.pos'2str pos)
   198 	    else ()
   199 	  val (oris, ospec, probl, spec, meth) = case Ctree.get_obj I pt p of
   200 	    Ctree.PblObj {origin = (oris, ospec, _), probl, spec, meth, ...} => (oris, ospec, probl, spec, meth)
   201 	  | _ => raise ERROR "finish_phase: unvered case get_obj"
   202   	val (_, pI, mI) = References.select_input ospec spec
   203   	val ctxt = Ctree.get_ctxt pt pos
   204   	val (pits, mits) = I_Model.s_make_complete ctxt oris
   205   	 (I_Model.OLD_to_TEST probl, I_Model.OLD_to_TEST meth) (pI, mI)
   206   	val pt = Ctree.update_pblppc pt p (I_Model.TEST_to_OLD pits)
   207   	val pt = Ctree.update_metppc pt p (I_Model.TEST_to_OLD mits)
   208   in (pt, (p, Pos.Met)) end
   209 
   210 (*
   211   do all specification in one single step:
   212   bypass input of Problem.model and go immediately for MethodC: I_Model.complete'
   213 *)
   214 fun do_all (pt, (p, _)) =
   215   let
   216     val (itms, oris, probl, o_refs, spec, ctxt) = case Ctree.get_obj I pt p of
   217       Ctree.PblObj {meth = itms, origin = (oris, o_spec, _), probl, spec, ctxt, ...}
   218         => (itms, oris, probl, o_spec, spec, ctxt)
   219     | _ => raise ERROR "Specify.do_all: no PblObj"
   220     val (_, pbl_id', met_id') = References.select_input o_refs spec
   221     val (pbl_imod, met_imod) = I_Model.s_make_complete ctxt oris
   222       (I_Model.OLD_to_TEST probl, I_Model.OLD_to_TEST itms) (pbl_id', met_id')
   223     val (pt, _) = Ctree.cupdate_problem pt p
   224       (o_refs, I_Model.TEST_to_OLD pbl_imod, I_Model.TEST_to_OLD met_imod, ctxt)
   225   in
   226     (pt, (p, Pos.Met))
   227   end
   228 
   229 (**)end(**)