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