src/Tools/isac/Specify/specify-step.sml
author Walther Neuper <walther.neuper@jku.at>
Mon, 01 Jun 2020 11:49:37 +0200
changeset 60014 11b5b8b81876
parent 60009 34b5f67da5c9
child 60154 2ab0d1523731
permissions -rw-r--r--
unify code
     1 (* Title:  Specify/specify-step.sml
     2    Author: Walther Neuper
     3    (c) due to copyright terms
     4                                                                           
     5 Code for the specify-phase in analogy to structure Solve_Step for the solve-phase.
     6 *)
     7 
     8 signature SPECIFY_STEP =
     9 sig
    10   val check: Tactic.input -> Calc.T -> Applicable.T
    11   val add: Tactic.T -> Istate_Def.T * Proof.context -> Calc.T -> Test_Out.T
    12 
    13   val complete_for: Method.id -> Calc.T -> O_Model.T * Proof.context * I_Model.T
    14 (* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
    15   (*NONE*)                                                     
    16 (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
    17   (*NONE*)                                                     
    18 ( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
    19 end
    20 
    21 (**)
    22 structure Specify_Step(**): SPECIFY_STEP(**) =
    23 struct
    24 (**)
    25 
    26 fun complete_for mID (ctree, pos) =
    27   let
    28     val {origin = (o_model, o_refs, _), spec = refs, probl = i_prob, ctxt,
    29        ...} = Calc.specify_data (ctree, pos);
    30     val (dI, _, _) = References.select o_refs refs;
    31     val {ppc = m_patt, pre, prls, ...} = Method.from_store mID
    32     val {origin = (root_model, _, _), ...} = Calc.specify_data (ctree, ([], Pos.Und))
    33     val (o_model', ctxt') = O_Model.complete_for m_patt root_model (o_model, ctxt)
    34     val thy = ThyC.get_theory dI
    35     val (_, (i_model, _)) = M_Match.match_itms_oris thy i_prob (m_patt, pre, prls) o_model';
    36   in
    37     (o_model', ctxt', i_model)
    38   end
    39 
    40 (*
    41   check tactics (input by the user, mostly) for applicability
    42   and determine as much of the result of the tactic as possible initially.
    43 *)
    44 fun check (Tactic.Add_Find ct') _ = Applicable.Yes (Tactic.Add_Find' (ct', [(*filled later*)]))
    45     (*Add_.. should reject (dsc //) (see fmz=[] in sqrt*)
    46   | check (Tactic.Add_Given ct') _ = Applicable.Yes (Tactic.Add_Given' (ct', [(*filled later*)]))
    47   | check (Tactic.Add_Relation ct') _ = Applicable.Yes (Tactic.Add_Relation' (ct', [(*filled later*)]))
    48     (*required for corner cases, e.g. test setup in inssort.sml*)
    49   | check (Tactic.Del_Find ct') _ = Applicable.Yes (Tactic.Del_Find' ct')
    50   | check (Tactic.Del_Given ct') _ = Applicable.Yes (Tactic.Del_Given' ct')
    51   | check (Tactic.Del_Relation ct') _ = Applicable.Yes (Tactic.Del_Relation' ct')
    52   | check Tactic.Model_Problem (pt, (p, _)) =
    53       let 
    54         val pI' = case Ctree.get_obj I pt p of
    55           Ctree.PblObj {origin = (_, (_, pI', _),_), ...} => pI'
    56         | _ => raise ERROR "Specify_Step.check Model_Problem: uncovered case Ctree.get_obj"
    57 	      val {ppc, ...} = Problem.from_store pI'
    58 	      val pbl = I_Model.init ppc
    59       in Applicable.Yes (Tactic.Model_Problem' (pI', pbl, [])) end
    60   | check (Tactic.Refine_Problem pI) (pt, (p, _)) =
    61       let
    62         val (dI, dI', itms) = case Ctree.get_obj I pt p of
    63             Ctree.PblObj {origin= (_, (dI, _, _), _), spec= (dI', _, _), probl = itms, ...}
    64               => (dI, dI', itms)
    65           | _ => raise ERROR "Specify_Step.check Refine_Problem: uncovered case Ctree.get_obj"
    66       	val thy = if dI' = ThyC.id_empty then dI else dI';
    67       in
    68         case Refine.problem (ThyC.get_theory thy) pI itms of
    69           NONE => Applicable.No (Tactic.input_to_string (Tactic.Refine_Problem pI) ^ " not applicable")
    70 	      | SOME (rf as (pI', _)) =>
    71       	   if pI' = pI
    72       	   then Applicable.No (Tactic.input_to_string (Tactic.Refine_Problem pI) ^ " not applicable")
    73       	   else Applicable.Yes (Tactic.Refine_Problem' rf)
    74     end
    75   | check (Tactic.Refine_Tacitly pI) (pt, (p, _)) =
    76      let 
    77        val oris = case Ctree.get_obj I pt p of
    78          Ctree.PblObj {origin = (oris, _, _), ...} => oris
    79        | _ => raise ERROR "Specify_Step.check Refine_Tacitly: uncovered case Ctree.get_obj"
    80      in
    81        case Refine.refine_ori oris pI of
    82 	       SOME pblID => 
    83 	         Applicable.Yes (Tactic.Refine_Tacitly' (pI, pblID, ThyC.id_empty, Method.id_empty, [(*filled later*)]))
    84 	     | NONE => Applicable.No (Tactic.input_to_string (Tactic.Refine_Tacitly pI) ^ " not applicable")
    85      end
    86   | check (Tactic.Specify_Method mID) (ctree, pos) =
    87       let
    88         val (o_model, _, i_model) = complete_for mID (ctree, pos)
    89       in
    90         Applicable.Yes (Tactic.Specify_Method' (mID, o_model, i_model))
    91       end
    92   | check (Tactic.Specify_Problem pID) (pt, (p, _)) =
    93       let
    94 		    val (oris, dI, pI, dI', pI', itms) = case Ctree.get_obj I pt p of
    95 		      Ctree.PblObj {origin = (oris, (dI, pI, _), _), spec = (dI', pI', _), probl = itms, ...}
    96 		        => (oris, dI, pI, dI', pI', itms)
    97         | _ => raise ERROR "Specify_Step.check Specify_Problem: uncovered case Ctree.get_obj"
    98 	      val thy = ThyC.get_theory (if dI' = ThyC.id_empty then dI else dI');
    99         val {ppc, where_, prls, ...} = Problem.from_store pID;
   100         val pbl = if pI' = Problem.id_empty andalso pI = Problem.id_empty
   101           then (false, (I_Model.init ppc, []))
   102           else M_Match.match_itms_oris thy itms (ppc, where_, prls) oris;
   103        in
   104          Applicable.Yes (Tactic.Specify_Problem' (pID, pbl))
   105        end
   106   | check (Tactic.Specify_Theory dI)_ = Applicable.Yes (Tactic.Specify_Theory' dI)
   107   | check Tactic.Empty_Tac _ = Applicable.No "Empty_Tac is not applicable"
   108   | check tac (_, pos) =
   109       raise ERROR ("Specify_Step.check called for " ^ Tactic.input_to_string tac ^ " at" ^ Pos.pos'2str pos);
   110 
   111 (* exceed line length, because result type will change *)
   112 fun add (Tactic.Model_Problem' (_, itms, met)) (_, ctxt) (pt, pos as (p, _)) =
   113       let
   114         val pt = Ctree.update_pbl pt p itms
   115   	    val pt = Ctree.update_met pt p met
   116       in
   117         (pos, [], Test_Out.PpcKF (Test_Out.Upblmet, P_Model.from (Proof_Context.theory_of ctxt) [] []), pt)
   118       end
   119   | add (Tactic.Add_Given' (_, itmlist)) (_, ctxt) (pt, pos as (p, p_)) = 
   120       (pos, [], Test_Out.PpcKF (Test_Out.Upblmet,P_Model.from (Proof_Context.theory_of ctxt) [][]),
   121          case p_ of
   122            Pos.Pbl => Ctree.update_pbl pt p itmlist
   123   	     | Pos.Met => Ctree.update_met pt p itmlist
   124          | _ => raise ERROR ("uncovered case " ^ Pos.pos_2str p_))
   125   | add (Tactic.Add_Find' (_, itmlist)) (_, ctxt) (pt, pos as (p, p_)) = 
   126       (pos, [], (Test_Out.PpcKF (Test_Out.Upblmet, P_Model.from (Proof_Context.theory_of ctxt) [] [])),
   127          case p_ of
   128            Pos.Pbl => Ctree.update_pbl pt p itmlist
   129   	     | Pos.Met => Ctree.update_met pt p itmlist
   130          | _ => raise ERROR ("uncovered case " ^ Pos.pos_2str p_))
   131   | add (Tactic.Add_Relation' (_, itmlist)) (_, ctxt) (pt, pos as (p, p_)) = 
   132       (pos, [],  Test_Out.PpcKF (Test_Out.Upblmet, P_Model.from (Proof_Context.theory_of ctxt) [] []),
   133          case p_ of
   134            Pos.Pbl => Ctree.update_pbl pt p itmlist
   135   	     | Pos.Met => Ctree.update_met pt p itmlist
   136          | _ => raise ERROR ("uncovered case " ^ Pos.pos_2str p_))
   137   | add (Tactic.Specify_Theory' domID) (_, ctxt) (pt, pos as (p,_)) = 
   138       (pos, [] , Test_Out.PpcKF  (Test_Out.Upblmet, P_Model.from (Proof_Context.theory_of ctxt) [] []),
   139         Ctree.update_domID pt p domID)
   140   | add (Tactic.Specify_Problem' (pI, (_, (itms, _)))) (_, ctxt) (pt, (p, _)) =
   141       let
   142         val pt = Ctree.update_pbl pt p itms
   143         val pt = Ctree.update_pblID pt p pI
   144       in
   145         ((p, Pos.Pbl), [], Test_Out.PpcKF (Test_Out.Upblmet, P_Model.from (Proof_Context.theory_of ctxt) [] []), pt)
   146       end
   147   | add (Tactic.Specify_Method' (mID, oris, itms)) (_, ctxt) (pt, (p, _)) =
   148       let
   149         val pt = Ctree.update_oris pt p oris
   150         val pt = Ctree.update_met pt p itms                            
   151         val pt = Ctree.update_metID pt p mID
   152       in
   153         ((p, Pos.Met), [], Test_Out.PpcKF (Test_Out.Upblmet, P_Model.from (Proof_Context.theory_of ctxt) [] []), pt)
   154       end
   155   | add (Tactic.Refine_Tacitly' (_, pIre, domID, metID, pbl)) (_, ctxt) (pt, pos as (p, _)) = 
   156       let
   157         val pt = Ctree.update_pbl pt p pbl
   158         val pt = Ctree.update_orispec pt p (domID, pIre, metID)
   159       in
   160         (pos, [], Test_Out.PpcKF (Test_Out.Upblmet, P_Model.from (Proof_Context.theory_of ctxt) [] []), pt)
   161       end
   162   | add (Tactic.Refine_Problem' (pI, _)) (_, ctxt) (pt, pos as (p, _)) =
   163       let
   164         val (dI, _, mI) = Ctree.get_obj Ctree.g_spec pt p
   165         val pt = Ctree.update_spec pt p (dI, pI, mI)
   166       in
   167         (pos, [], Test_Out.PpcKF (Test_Out.Upblmet, P_Model.from (Proof_Context.theory_of ctxt) [] []), pt)
   168       end
   169   | add (Tactic.Begin_Trans' t) l (pt, (p, Frm)) =
   170       let
   171         val (pt, c) = Ctree.cappend_form pt p l t
   172         val pt = Ctree.update_branch pt p Ctree.TransitiveB
   173         val p = (Pos.lev_on o Pos.lev_dn (* starts with [...,0] *)) p
   174         val (pt, c') = Ctree.cappend_form pt p l t
   175       in
   176         ((p, Frm), c @ c', Test_Out.FormKF (UnparseC.term t), pt)
   177       end
   178   | add (Tactic.End_Trans' tasm) l (pt, (p, _)) = (* used at all ? *)
   179       let
   180         val p' = Pos.lev_up p
   181         val (pt, c) = Ctree.append_result pt p' l tasm Ctree.Complete
   182       in
   183         ((p', Pos.Res), c, Test_Out.FormKF "DUMMY" (*term2str t ..ERROR (t) has not been declared*), pt)
   184       end
   185   | add m' _ (_, pos) =
   186       raise ERROR ("Specify_Step.add: not impl.for " ^ Tactic.string_of m' ^ " at " ^ Pos.pos'2str pos)
   187 
   188 (**)end(**);