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