src/Tools/isac/Specify/specify.sml
author wneuper <Walther.Neuper@jku.at>
Wed, 25 Oct 2023 12:34:12 +0200
changeset 60760 3b173806efe2
parent 60757 9f4d7a352426
child 60763 2121f1a39a64
permissions -rw-r--r--
prepare 12: M_Model.match_itms_oris takes Position.T and new max_mariants

some tests are outcommented until <broken elementwise input to lists> is repaired alltogether
     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   val item_to_add: theory -> O_Model.T -> Model_Pattern.T -> I_Model.T ->
    11     (Model_Def.m_field * TermC.as_string) option
    12   val by_Add_: string -> TermC.as_string -> Calc.T -> string * Calc.state_post
    13 (*from isac_test for Minisubpbl*)
    14   val for_problem: Proof.context -> O_Model.T -> References.T * References.T -> I_Model.T * I_Model.T ->
    15     string * (Pos.pos_ * Tactic.input)
    16   val for_method: Proof.context -> O_Model.T -> References.T * References.T -> I_Model.T * I_Model.T ->
    17     string * (Pos.pos_ * Tactic.input)
    18 
    19 \<^isac_test>\<open>
    20 (**)
    21 \<close>
    22 end
    23 
    24 (**)
    25 structure Specify(**): SPECIFY(**) =
    26 struct
    27 (**)
    28 
    29 (*
    30   select an item in oris, notyet input in itms 
    31   (precondition: in itms are only I_Model.Cor, I_Model.Sup, I_Model.Inc)
    32 args of item_to_add
    33   thy : for?
    34   oris: from formalization 'type fmz', structured for efficient access 
    35   pbt : the problem-pattern to be matched with oris in order to get itms
    36   itms: already input items
    37 *)
    38 fun item_to_add thy [] pbt itms = (*root (only) ori...fmz=[]*)
    39     let
    40       fun test_d d (i, _, _, _, itm_) = (d = (I_Model.descriptor itm_)) andalso i <> 0
    41       fun is_elem itms (_, (d, _)) = 
    42         case find_first (test_d d) itms of SOME _ => true | NONE => false
    43     in
    44       case filter_out (is_elem itms) pbt of
    45         (f, (d, _)) :: _ => SOME (f, ((UnparseC.term_in_thy thy) o Input_Descript.join) (d, []))
    46       | _ => NONE
    47     end
    48     (* m_field is in ------vvvv *)
    49   | item_to_add thy oris _ itms =
    50     let
    51       fun testr_vt v ori = member op= (#2 (ori : O_Model.single)) v andalso (#3 ori) <> "#undef"
    52       fun testi_vt v itm = member op= (#2 (itm : I_Model.single)) v
    53       fun test_id ids r = member op= ids (#1 (r : O_Model.single))
    54       fun test_subset itm (_, _, _, d, ts) = 
    55         (I_Model.descriptor (#5 (itm: I_Model.single))) = d andalso subset op = (I_Model.o_model_values (#5 itm), ts)
    56       fun false_and_not_Sup (_, _, false, _, I_Model.Sup _) = false
    57         | false_and_not_Sup (_, _, false, _, _) = true
    58         | false_and_not_Sup _ = false
    59       val v = if itms = [] then 1 else Pre_Conds.max_variant itms
    60       val vors = if v = 0 then oris else filter (testr_vt v) oris
    61       val vits =
    62         if v = 0
    63         then itms                                 (* because of dsc without dat *)
    64   	    else filter (testi_vt v) itms;                             (* itms..vat *)
    65       val icl = filter false_and_not_Sup vits;                    (* incomplete *)
    66     in
    67       if icl = [] then
    68         case filter_out (test_id (map #1 vits)) vors of
    69           [] => NONE
    70         | miss => SOME (O_Model.get_field_term thy (hd miss))
    71       else
    72         case find_first (test_subset (hd icl)) vors of
    73           NONE => raise ERROR "item_to_add: types or dsc DO NOT MATCH BETWEEN fmz --- pbt"
    74         | SOME ori => SOME (I_Model.get_field_term thy ori (hd icl))
    75     end
    76 
    77 
    78 (** find a next step in the specify-phase **)
    79 (*
    80   here should be mutual recursion between for_problem ctxt and for_method
    81 *)
    82 fun for_problem ctxt oris ((dI', pI', mI'), (dI, pI, mI)) (pbl, met) =
    83   let
    84     val cpI = if pI = Problem.id_empty then pI' else pI;
    85     val cmI = if mI = MethodC.id_empty then mI' else mI;
    86     val {model = pbt, where_rls, where_, ...} = Problem.from_store ctxt cpI;
    87     val {model = mpc, ...} = MethodC.from_store ctxt cmI
    88     val (preok, _) = Pre_Conds.check ctxt where_rls where_ (pbt, I_Model.OLD_to_TEST pbl);
    89   in
    90     if dI' = ThyC.id_empty andalso dI = ThyC.id_empty then
    91       ("dummy", (Pos.Pbl, Tactic.Specify_Theory dI'))
    92     else if pI' = Problem.id_empty andalso pI = Problem.id_empty then
    93         ("dummy", (Pos.Pbl, Tactic.Specify_Problem pI'))
    94     else
    95       case find_first (I_Model.is_error o #5) pbl of
    96         SOME (_, _, _, fd, itm_) =>
    97           ("dummy", (Pos.Pbl, P_Model.mk_delete (ThyC.get_theory ctxt
    98             (if dI = ThyC.id_empty then dI' else dI)) fd itm_))
    99       | NONE => 
   100         (case item_to_add (ThyC.get_theory ctxt
   101             (if dI = ThyC.id_empty then dI' else dI)) oris pbt pbl of
   102            SOME (fd, ct') => ("dummy", (Pos.Pbl, P_Model.mk_additem fd ct'))
   103          | NONE => (*pbl-items complete*)        
   104            if not preok then ("dummy", (Pos.Pbl, Tactic.Refine_Problem pI'))
   105            (*this is an ERROR fall through ------^^^^^^^^^^^^^^^^^^^^^ for Pre_Conds, redesign ?*)
   106            else if dI = ThyC.id_empty then ("dummy", (Pos.Pbl, Tactic.Specify_Theory dI'))
   107            else if pI = Problem.id_empty then ("dummy", (Pos.Pbl, Tactic.Specify_Problem pI'))
   108            else if mI = MethodC.id_empty then ("dummy", (Pos.Pbl, Tactic.Specify_Method mI'))
   109            else
   110             case find_first (I_Model.is_error o #5) met of
   111               SOME (_, _, _, fd, itm_) =>
   112                  ("dummy", (Pos.Met, P_Model.mk_delete (ThyC.get_theory ctxt dI) fd itm_))
   113             | NONE => 
   114               (case item_to_add (ThyC.get_theory ctxt dI) oris mpc met of
   115     	          SOME (fd, ct') =>
   116                    ("dummy", (Pos.Met, P_Model.mk_additem fd ct')) (*30.8.01: where_?!?*)
   117     		      | NONE => ("dummy", (Pos.Met, Tactic.Apply_Method mI))))
   118   end
   119 
   120 fun for_method ctxt oris ((dI', pI', mI'), (dI, pI, mI)) (_, met) =
   121   let
   122     val cmI = if mI = MethodC.id_empty then mI' else mI;
   123     val {model = mpc, where_rls, where_, ...} = MethodC.from_store ctxt cmI;
   124     val (preok, _) = Pre_Conds.check ctxt where_rls where_ (mpc, I_Model.OLD_to_TEST met);
   125   in
   126     (case find_first (I_Model.is_error o #5) met of
   127       SOME (_,_,_, fd, itm_) =>
   128         ("dummy", (Pos.Met, P_Model.mk_delete (ThyC.get_theory ctxt
   129             (if dI = ThyC.id_empty then dI' else dI)) fd itm_))
   130     | NONE => 
   131       case item_to_add (ThyC.get_theory ctxt 
   132           (if dI = ThyC.id_empty then dI' else dI)) oris mpc met of
   133         SOME (fd, ct') =>
   134           ("dummy", (Pos.Met, P_Model.mk_additem fd ct')) (*->->*)
   135       | NONE => 
   136         (if dI = ThyC.id_empty then ("dummy", (Pos.Met, Tactic.Specify_Theory dI'))
   137          else if pI = Problem.id_empty then ("dummy", (Pos.Met, Tactic.Specify_Problem pI'))
   138          else if not preok then ("dummy", (Pos.Met, Tactic.Specify_Method mI))
   139          else ("dummy", (Pos.Met, Tactic.Apply_Method mI))))
   140   end
   141 
   142 (*
   143   on finding a next step switching from problem to method or vice versa is possible,
   144   because the user is free to switch and edit the respective models independently.
   145 TODO: this free switch is NOT yet implemented; e.g. 
   146   preok is relevant for problem + method, i.e. in for_problem ctxt + for_method
   147 *)
   148 fun find_next_step (pt, pos as (_, p_)) =
   149   let
   150     val {meth = met, origin = origin as (oris, o_refs as (_, pI', mI'), _), probl = pbl,
   151       spec = refs, ...} = Calc.specify_data (pt, pos);
   152     val ctxt = Ctree.get_ctxt pt pos
   153     in
   154       if Ctree.just_created (pt, pos) andalso origin <> Ctree.e_origin then
   155         case mI' of
   156           ["no_met"] => ("ok", (Pos.Pbl, Tactic.Refine_Tacitly pI'))
   157         | _ => ("ok", (Pos.Pbl, Tactic.Model_Problem))
   158       else if p_ = Pos.Pbl then
   159         for_problem ctxt oris (o_refs, refs) (pbl, met)
   160       else
   161         for_method ctxt oris (o_refs, refs) (pbl, met)
   162     end
   163 
   164 
   165 (** tools **)
   166 
   167 fun by_Add_  m_field ct (pt, pos as (_, p_)) =
   168   let
   169     val (met, oris, (_, pI', mI'), pbl, (_, pI, mI), ctxt) = SpecificationC.get_data (pt, pos)
   170     val (i_model, m_patt) =
   171        if p_ = Pos.Met then
   172          (met,
   173            (if mI = MethodC.id_empty then mI' else mI) |> MethodC.from_store ctxt |> #model)
   174        else
   175          (pbl,
   176            (if pI = Problem.id_empty then pI' else pI) |> Problem.from_store ctxt |> #model)
   177     in
   178       case I_Model.check_single ctxt m_field oris i_model m_patt ct of
   179         I_Model.Add i_single => (*..union old input *)
   180 	        let
   181 	          val i_model' = I_Model.add_single (Proof_Context.theory_of ctxt) i_single i_model
   182             val tac' = I_Model.make_tactic m_field (ct, i_model')
   183 	          val  (_, _, _, pt') =  Specify_Step.add tac' (Istate_Def.Uistate, ctxt) (pt, pos)
   184 	        in
   185             ("ok", ([(Tactic.input_from_T ctxt tac', tac', (pos, (Istate_Def.Uistate, ctxt)))],
   186               [], (pt', pos)))
   187           end
   188       | I_Model.Err msg => (msg, ([], [], (pt, pos)))
   189     end
   190 
   191 (* complete _NON_empty calc-head for autocalc (sub-)pbl from oris
   192   + met from fmz; assumes pos on PblObj, meth = []                    *)
   193 fun finish_phase (pt, pos as (p, p_)) =
   194   let
   195     val _ = if p_ <> Pos.Pbl 
   196 	    then raise ERROR ("###finish_phase: only impl.for Pbl, called with " ^ Pos.pos'2str pos)
   197 	    else ()
   198 	  val (oris, ospec, probl, spec, meth) = case Ctree.get_obj I pt p of
   199 	    Ctree.PblObj {origin = (oris, ospec, _), probl, spec, meth, ...} => (oris, ospec, probl, spec, meth)
   200 	  | _ => raise ERROR "finish_phase: unvered case get_obj"
   201   	val (_, pI, mI) = References.select_input ospec spec
   202   	val ctxt = Ctree.get_ctxt pt pos
   203   	val (pits, mits) = I_Model.s_make_complete ctxt oris
   204   	 (I_Model.OLD_to_TEST probl, I_Model.OLD_to_TEST meth) (pI, mI)
   205   	val pt = Ctree.update_pblppc pt p (I_Model.TEST_to_OLD pits)
   206   	val pt = Ctree.update_metppc pt p (I_Model.TEST_to_OLD mits)
   207   in (pt, (p, Pos.Met)) end
   208 
   209 (*
   210   do all specification in one single step:
   211   bypass input of Problem.model and go immediately for MethodC: I_Model.complete'
   212 *)
   213 fun do_all (pt, (p, _)) =
   214   let
   215     val (itms, oris, probl, o_refs, spec, ctxt) = case Ctree.get_obj I pt p of
   216       Ctree.PblObj {meth = itms, origin = (oris, o_spec, _), probl, spec, ctxt, ...}
   217         => (itms, oris, probl, o_spec, spec, ctxt)
   218     | _ => raise ERROR "Specify.do_all: no PblObj"
   219     val (_, pbl_id', met_id') = References.select_input o_refs spec
   220     val (pbl_imod, met_imod) = I_Model.s_make_complete ctxt oris
   221       (I_Model.OLD_to_TEST probl, I_Model.OLD_to_TEST itms) (pbl_id', met_id')
   222     val (pt, _) = Ctree.cupdate_problem pt p
   223       (o_refs, I_Model.TEST_to_OLD pbl_imod, I_Model.TEST_to_OLD met_imod, ctxt)
   224   in
   225     (pt, (p, Pos.Met))
   226   end
   227 
   228 (**)end(**)