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