src/Tools/isac/MathEngBasic/tactic.sml
author Walther Neuper <walther.neuper@jku.at>
Fri, 01 May 2020 17:17:41 +0200
changeset 59923 cd730f07c9ac
parent 59914 ab5bd5c37e13
child 59924 eb40bce6d6f1
permissions -rw-r--r--
unify sequence of tactics
wneuper@59571
     1
(* Title:  Tactics; tac_ for interaction with frontend, input for internal use.
wneuper@59304
     2
   Author: Walther Neuper 170121
wneuper@59304
     3
   (c) due to copyright terms
wneuper@59304
     4
wneuper@59304
     5
regular expression for search:
wneuper@59304
     6
walther@59812
     7
Add_Find|Add_Given|Add_Relation|Apply_Assumption|Apply_Method|Begin_Sequ|Begin_Trans|Split_And|Split_Or|Split_Intersect|Conclude_And|Conclude_Or|Collect_Trues|End_Sequ|End_Trans|End_Ruleset|End_Subproblem|End_Intersect|End_Proof|CAScmd|Calculate|Check_Postcond|Check_elementwise|Del_Find|Del_Given|Del_Relation|Derive|Detail_Set|Detail_Set_Inst|End_Detail|Empty_Tac|Free_Solve|Init_Proof|Model_Problem Or_to_List|Refine_Problem|Refine_Tacitly| Rewrite|Rewrite_Inst|Rewrite_Set|Rewrite_Set_Inst|Specify_Method|Specify_Problem|Specify_Theory|Subproblem|Substitute|Tac|Take|Take_Inst
wneuper@59304
     8
wneuper@59304
     9
*)
wneuper@59298
    10
signature TACTIC =
wneuper@59298
    11
sig
walther@59846
    12
  datatype T =
walther@59865
    13
    Add_Find' of TermC.as_string * Model.itm list | Add_Given' of TermC.as_string * Model.itm list 
walther@59865
    14
  | Add_Relation' of TermC.as_string * Model.itm list
walther@59903
    15
  | Apply_Method' of Method.id * term option * Istate_Def.T * Proof.context
walther@59923
    16
  | Del_Find' of TermC.as_string | Del_Given' of TermC.as_string | Del_Relation' of TermC.as_string
walther@59923
    17
  | Init_Proof' of TermC.as_string list * Spec.T
walther@59923
    18
  | Model_Problem' of Problem.id * Model.itm list * Model.itm list
walther@59923
    19
  | Refine_Problem' of Problem.id * (Model.itm list * (bool * term) list)
walther@59923
    20
  | Refine_Tacitly' of Problem.id * Problem.id * ThyC.id * Method.id * Model.itm list
walther@59923
    21
  | Specify_Method' of Method.id * Model.ori list * Model.itm list
walther@59923
    22
  | Specify_Problem' of Problem.id * (bool * (Model.itm list * (bool * term) list))
walther@59923
    23
  | Specify_Theory' of ThyC.id
walther@59923
    24
  (* ^^^^^--------------------- for specify-phase, for solve-phase ---------------------vvvvv*)
walther@59846
    25
  | CAScmd' of term
walther@59879
    26
  | Calculate' of ThyC.id * string * term * (term * ThmC.T)
walther@59903
    27
  | Check_Postcond' of Problem.id * term
walther@59865
    28
  | Check_elementwise' of term * TermC.as_string * Selem.result
walther@59923
    29
(*RM*)| Derive' of Rule_Set.T      
walther@59846
    30
  | Empty_Tac_
walther@59846
    31
  | Free_Solve'
walther@59923
    32
(*RM* )| Apply_Assumption of TermC.as_string list( *RM*)
walther@59846
    33
  | Or_to_List' of term * term
walther@59879
    34
  | Rewrite' of ThyC.id * Rewrite_Ord.rew_ord' * Rule_Set.T * bool * ThmC.T * term * Selem.result
walther@59910
    35
  | Rewrite_Inst' of ThyC.id * Rewrite_Ord.rew_ord' * Rule_Set.T * bool * subst * ThmC.T * term * Selem.result
walther@59879
    36
  | Rewrite_Set' of ThyC.id * bool * Rule_Set.T * term * Selem.result
walther@59910
    37
  | Rewrite_Set_Inst' of ThyC.id * bool * subst * Rule_Set.T * term * Selem.result
walther@59846
    38
  | Subproblem' of
walther@59903
    39
      Spec.T * Model.ori list *
walther@59846
    40
      term *          (* CAScmd, e.g. "solve (-1 + x = 0, x)" *)
walther@59846
    41
      Selem.fmz_ *    (* either input to root-probl.  or derived from prog. in ???  *)
walther@59846
    42
      (*Istate.T *       ?       *)
walther@59846
    43
      Proof.context * (* derived from prog. in ???  *)
walther@59846
    44
      term            (* ?UNUSED, e.g."Subproblem\n (''Test'',\n  ??.\<^const>String.char.Char ''LINEAR'' ''univariate'' ''equation''\n   ''test'')" *)
walther@59912
    45
  | Substitute' of Rule_Def.rew_ord_ * Rule_Set.T * Subst.as_eqs * term * term
walther@59923
    46
(*RM*)| Tac_ of theory * string * string * string
walther@59846
    47
  | Take' of term
walther@59923
    48
(*RM* )| Take_Inst of TermC.as_string( *RM*)
walther@59923
    49
(*RM*)| Begin_Sequ' | Begin_Trans' of term
walther@59923
    50
(*RM*)| Split_And' of term | Split_Or' of term | Split_Intersect' of term
walther@59923
    51
(*RM*)| Conclude_And' of term | Conclude_Or' of term | Collect_Trues' of term
walther@59923
    52
(*RM*)| End_Sequ' | End_Trans' of Selem.result
walther@59923
    53
(*RM*)| End_Ruleset' of term | End_Intersect' of term | End_Proof''
walther@59923
    54
(*RM*)| Detail_Set' of ThyC.id * bool * Rule_Set.T * term * Selem.result
walther@59923
    55
(*RM*)| Detail_Set_Inst' of ThyC.id * bool * subst * Rule_Set.T * term * Selem.result
walther@59923
    56
(*RM*)| End_Detail' of Selem.result;
walther@59923
    57
walther@59812
    58
  val string_of: T -> string
walther@59741
    59
walther@59846
    60
  datatype input =
walther@59923
    61
    Add_Find of TermC.as_string | Add_Given of TermC.as_string
walther@59923
    62
  | Add_Relation of TermC.as_string
walther@59903
    63
  | Apply_Method of Method.id
walther@59923
    64
  | Del_Find of TermC.as_string | Del_Given of TermC.as_string | Del_Relation of TermC.as_string
walther@59923
    65
  | Init_Proof of TermC.as_string list * Spec.T
walther@59923
    66
  | Model_Problem
walther@59923
    67
  | Refine_Problem of Problem.id
walther@59923
    68
  | Refine_Tacitly of Problem.id
walther@59923
    69
  | Specify_Method of Method.id
walther@59923
    70
  | Specify_Problem of Problem.id
walther@59923
    71
  | Specify_Theory of ThyC.id
walther@59923
    72
  (* ^^^^^--------------------- for specify-phase, for solve-phase ---------------------vvvvv*)
walther@59865
    73
  | CAScmd of TermC.as_string
walther@59846
    74
  | Calculate of string
walther@59903
    75
  | Check_Postcond of Problem.id
walther@59865
    76
  | Check_elementwise of TermC.as_string
walther@59923
    77
(*RM*)| Derive of Rule_Set.id(*RM*)
walther@59846
    78
  | Empty_Tac
walther@59923
    79
(*RM*)| Free_Solve
walther@59923
    80
(*RM*)| Apply_Assumption of TermC.as_string list
walther@59846
    81
  | Or_to_List
walther@59874
    82
  | Rewrite of ThmC.T
walther@59911
    83
  | Rewrite_Inst of Subst.input * ThmC.T
walther@59867
    84
  | Rewrite_Set of Rule_Set.id
walther@59911
    85
  | Rewrite_Set_Inst of Subst.input * Rule_Set.id
walther@59923
    86
  | Subproblem of ThyC.id * Problem.id
walther@59923
    87
  | Substitute of Subst.input
walther@59923
    88
(*RM*)| Tac of string
walther@59923
    89
  | Take of TermC.as_string
walther@59923
    90
(*RM*)| Take_Inst of TermC.as_string
walther@59923
    91
(*RM*)| Begin_Sequ | Begin_Trans
walther@59923
    92
(*RM*)| Split_And | Split_Or | Split_Intersect
walther@59923
    93
(*RM*)| Conclude_And | Conclude_Or | Collect_Trues
walther@59923
    94
(*RM*)| End_Sequ | End_Trans
walther@59923
    95
(*RM*)| End_Ruleset | End_Subproblem | End_Intersect
walther@59923
    96
(*RM*)| Detail_Set of Rule_Set.id
walther@59923
    97
(*RM*)| Detail_Set_Inst of Subst.input * Rule_Set.id
walther@59923
    98
(*RM*)| End_Detail
walther@59923
    99
  | End_Proof';
walther@59846
   100
walther@59846
   101
  val input_to_string : input -> string
walther@59812
   102
  val tac2IDstr : input -> string
walther@59844
   103
  val is_empty : input -> bool
wneuper@59302
   104
walther@59728
   105
  val eq_tac : input * input -> bool
walther@59728
   106
  val is_rewtac : input -> bool
walther@59728
   107
  val is_rewset : input -> bool
walther@59867
   108
  val rls_of : input -> Rule_Set.id
walther@59728
   109
  val rule2tac : theory -> (term * term) list ->  Rule.rule -> input
walther@59914
   110
  val applicable : theory -> string -> Rule_Set.T -> term -> input ->input list
walther@59914
   111
  val for_specify: input -> bool
walther@59914
   112
walther@59704
   113
  val input_from_T : T -> input
walther@59728
   114
  val result : T -> term
walther@59728
   115
  val creates_assms: T -> term list
walther@59728
   116
  val insert_assumptions: T -> Proof.context -> Proof.context
walther@59749
   117
  val for_specify': T -> bool
walther@59735
   118
wneuper@59310
   119
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
wneuper@59310
   120
  (* NONE *)
walther@59886
   121
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
wneuper@59310
   122
  (* NONE *)
walther@59886
   123
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59298
   124
wneuper@59310
   125
(*----- unused code, kept as hints to design ideas ---------------------------------------------*)
wneuper@59310
   126
  (* NONE *)
wneuper@59298
   127
end
wneuper@59298
   128
walther@59728
   129
(**)
wneuper@59571
   130
structure Tactic(**): TACTIC(**) =
wneuper@59298
   131
struct
walther@59728
   132
(**)
wneuper@59298
   133
walther@59914
   134
(** tactics for user at front-end **)
walther@59914
   135
walther@59846
   136
(* tactics for user at front-end.
walther@59846
   137
   input propagates the construction of the calc-tree;
walther@59846
   138
   there are
walther@59846
   139
   (a) 'specsteps' for the specify-phase, and others for the solve-phase
walther@59846
   140
   (b) those of the solve-phase are 'initac's and others;
walther@59846
   141
       initacs start with a formula different from the preceding formula.
walther@59846
   142
   see 'type tac_' for the internal representation of tactics
walther@59846
   143
*)
walther@59846
   144
datatype input =
walther@59865
   145
    Add_Find of TermC.as_string | Add_Given of TermC.as_string | Add_Relation of TermC.as_string
walther@59865
   146
  | Apply_Assumption of TermC.as_string list
walther@59903
   147
  | Apply_Method of Method.id
walther@59846
   148
    (* creates an "istate" in PblObj.env; in case of "implicit_take" 
walther@59846
   149
      creates a formula at ((lev_on o lev_dn) p, Frm) and in this "ppobj.loc"
walther@59846
   150
      a "SOME istate" at fst of "loc".
walther@59846
   151
      As each step (in the solve-phase) has a resulting formula (at the front-end)
walther@59846
   152
      Apply_Method also does the 1st step in the script (an "initac") if there is no "implicit_take" *)  
walther@59846
   153
  (*/--- TODO: re-design ? -----------------------------------------------------------------\*)
walther@59846
   154
  | Begin_Sequ | Begin_Trans
walther@59846
   155
  | Split_And | Split_Or | Split_Intersect
walther@59846
   156
  | Conclude_And | Conclude_Or | Collect_Trues
walther@59846
   157
  | End_Sequ | End_Trans
walther@59846
   158
  | End_Ruleset | End_Subproblem (* WN0509 drop *) | End_Intersect | End_Proof'
walther@59846
   159
  (*\--- TODO: re-design ? -----------------------------------------------------------------/*)
walther@59865
   160
  | CAScmd of TermC.as_string
walther@59846
   161
  | Calculate of string
walther@59903
   162
  | Check_Postcond of Problem.id
walther@59865
   163
  | Check_elementwise of TermC.as_string
walther@59865
   164
  | Del_Find of TermC.as_string | Del_Given of TermC.as_string | Del_Relation of TermC.as_string
wneuper@59302
   165
walther@59867
   166
  | Derive of Rule_Set.id                 (* WN0509 drop *)
walther@59867
   167
  | Detail_Set of Rule_Set.id             (* WN0509 drop *)
walther@59911
   168
  | Detail_Set_Inst of Subst.input * Rule_Set.id (* WN0509 drop *)
walther@59846
   169
  | End_Detail                     (* WN0509 drop *)
walther@59846
   170
walther@59846
   171
  | Empty_Tac
walther@59846
   172
  | Free_Solve
walther@59846
   173
walther@59903
   174
  | Init_Proof of TermC.as_string list * Spec.T
walther@59846
   175
  | Model_Problem
walther@59846
   176
  | Or_to_List
walther@59903
   177
  | Refine_Problem of Problem.id
walther@59903
   178
  | Refine_Tacitly of Problem.id
walther@59846
   179
walther@59846
   180
   (* rewrite-tactics can transport a (thmID, thm) to and (!) from the java-front-end
walther@59846
   181
     because there all the thms are present with both (thmID, thm)
walther@59846
   182
     (where user-views can show both or only one of (thmID, thm)),
walther@59846
   183
     and thm is created from ThmID by assoc_thm'' when entering isabisac *)
walther@59874
   184
  | Rewrite of ThmC.T
walther@59911
   185
  | Rewrite_Inst of Subst.input * ThmC.T
walther@59867
   186
  | Rewrite_Set of Rule_Set.id
walther@59911
   187
  | Rewrite_Set_Inst of Subst.input * Rule_Set.id
walther@59846
   188
walther@59903
   189
  | Specify_Method of Method.id
walther@59903
   190
  | Specify_Problem of Problem.id
walther@59879
   191
  | Specify_Theory of ThyC.id
walther@59903
   192
  | Subproblem of ThyC.id * Problem.id (* WN0509 drop *)
walther@59846
   193
walther@59912
   194
  | Substitute of Subst.input
walther@59846
   195
  | Tac of string               (* WN0509 drop *)
walther@59865
   196
  | Take of TermC.as_string | Take_Inst of TermC.as_string
walther@59846
   197
walther@59846
   198
fun input_to_string ma = case ma of
walther@59846
   199
    Init_Proof (ppc, spec)  => 
walther@59902
   200
      "Init_Proof "^(pair2str (strs2str ppc, Spec.to_string spec))
walther@59846
   201
  | Model_Problem           => "Model_Problem "
walther@59846
   202
  | Refine_Tacitly pblID    => "Refine_Tacitly " ^ strs2str pblID 
walther@59846
   203
  | Refine_Problem pblID    => "Refine_Problem " ^ strs2str pblID 
walther@59846
   204
  | Add_Given cterm'        => "Add_Given " ^ cterm'
walther@59846
   205
  | Del_Given cterm'        => "Del_Given " ^ cterm'
walther@59846
   206
  | Add_Find cterm'         => "Add_Find " ^ cterm'
walther@59846
   207
  | Del_Find cterm'         => "Del_Find " ^ cterm'
walther@59846
   208
  | Add_Relation cterm'     => "Add_Relation " ^ cterm'
walther@59846
   209
  | Del_Relation cterm'     => "Del_Relation " ^ cterm'
walther@59846
   210
walther@59846
   211
  | Specify_Theory domID    => "Specify_Theory " ^ quote domID
walther@59846
   212
  | Specify_Problem pblID   => "Specify_Problem " ^ strs2str pblID
walther@59846
   213
  | Specify_Method metID    => "Specify_Method " ^ strs2str metID
walther@59846
   214
  | Apply_Method metID      => "Apply_Method " ^ strs2str metID
walther@59846
   215
  | Check_Postcond pblID    => "Check_Postcond " ^ strs2str pblID
walther@59846
   216
  | Free_Solve              => "Free_Solve"
walther@59846
   217
walther@59846
   218
  | Rewrite_Inst (subs, (id, thm)) =>
walther@59868
   219
    "Rewrite_Inst " ^ (pair2str (subs2str subs, spair2str (id, thm |> Thm.prop_of |> UnparseC.term)))
walther@59868
   220
  | Rewrite (id, thm) => "Rewrite " ^ spair2str (id, thm |> Thm.prop_of |> UnparseC.term)
walther@59846
   221
  | Rewrite_Set_Inst (subs, rls) => 
walther@59846
   222
    "Rewrite_Set_Inst " ^ pair2str (subs2str subs, quote rls)
walther@59846
   223
  | Rewrite_Set rls         => "Rewrite_Set " ^ quote rls
walther@59846
   224
  | Detail_Set rls          => "Detail_Set " ^ quote rls
walther@59846
   225
  | Detail_Set_Inst (subs, rls) =>  "Detail_Set_Inst " ^ pair2str (subs2str subs, quote rls)
walther@59846
   226
  | End_Detail              => "End_Detail"
walther@59846
   227
  | Derive rls'             => "Derive " ^ rls' 
walther@59911
   228
  | Calculate op_           => "Calculate " ^ op_
walther@59912
   229
  | Substitute sube         => "Substitute " ^ Subst.string_eqs_to_string sube	     
walther@59846
   230
  | Apply_Assumption ct's   => "Apply_Assumption " ^ strs2str ct's
walther@59846
   231
walther@59846
   232
  | Take cterm'             => "Take " ^ quote cterm'
walther@59846
   233
  | Take_Inst cterm'        => "Take_Inst " ^ quote cterm'
walther@59846
   234
  | Subproblem (domID, pblID) => "Subproblem " ^ pair2str (domID, strs2str pblID)
walther@59846
   235
  | End_Subproblem          => "End_Subproblem"
walther@59846
   236
  | CAScmd cterm'           => "CAScmd " ^ quote cterm'
walther@59846
   237
walther@59846
   238
  | Check_elementwise cterm'=> "Check_elementwise " ^ quote cterm'
walther@59846
   239
  | Or_to_List              => "Or_to_List "
walther@59846
   240
  | Collect_Trues           => "Collect_Trues"
walther@59846
   241
walther@59846
   242
  | Empty_Tac               => "Empty_Tac"
walther@59846
   243
  | Tac string              => "Tac " ^ string
walther@59846
   244
  | End_Proof'              => "input End_Proof'"
walther@59846
   245
  | _                       => "input_to_string not impl. for ?!";
walther@59846
   246
walther@59846
   247
fun tac2IDstr ma = case ma of
walther@59846
   248
    Model_Problem => "Model_Problem"
walther@59846
   249
  | Refine_Tacitly _ => "Refine_Tacitly"
walther@59846
   250
  | Refine_Problem _ => "Refine_Problem"
walther@59846
   251
  | Add_Given _ => "Add_Given"
walther@59846
   252
  | Del_Given _ => "Del_Given"
walther@59846
   253
  | Add_Find _ => "Add_Find"
walther@59846
   254
  | Del_Find _ => "Del_Find"
walther@59846
   255
  | Add_Relation _ => "Add_Relation"
walther@59846
   256
  | Del_Relation _ => "Del_Relation"
walther@59846
   257
walther@59846
   258
  | Specify_Theory _ => "Specify_Theory"
walther@59846
   259
  | Specify_Problem _ => "Specify_Problem"
walther@59846
   260
  | Specify_Method _ => "Specify_Method"
walther@59846
   261
  | Apply_Method _ => "Apply_Method"
walther@59846
   262
  | Check_Postcond _ => "Check_Postcond"
walther@59846
   263
  | Free_Solve => "Free_Solve"
walther@59846
   264
walther@59846
   265
  | Rewrite_Inst _ => "Rewrite_Inst"
walther@59846
   266
  | Rewrite _ => "Rewrite"
walther@59846
   267
  | Rewrite_Set_Inst _ => "Rewrite_Set_Inst"
walther@59846
   268
  | Rewrite_Set _ => "Rewrite_Set"
walther@59846
   269
  | Detail_Set _ => "Detail_Set"
walther@59846
   270
  | Detail_Set_Inst _ => "Detail_Set_Inst"
walther@59846
   271
  | Derive _ => "Derive "
walther@59846
   272
  | Calculate _ => "Calculate "
walther@59846
   273
  | Substitute _ => "Substitute" 
walther@59846
   274
  | Apply_Assumption _ => "Apply_Assumption"
walther@59846
   275
walther@59846
   276
  | Take _ => "Take"
walther@59846
   277
  | Take_Inst _ => "Take_Inst"
walther@59846
   278
  | Subproblem _ => "Subproblem"
walther@59846
   279
  | End_Subproblem => "End_Subproblem"
walther@59846
   280
  | CAScmd _ => "CAScmd"
walther@59846
   281
walther@59846
   282
  | Check_elementwise _ => "Check_elementwise"
walther@59846
   283
  | Or_to_List => "Or_to_List "
walther@59846
   284
  | Collect_Trues => "Collect_Trues"
walther@59846
   285
walther@59846
   286
  | Empty_Tac => "Empty_Tac"
walther@59846
   287
  | Tac _ => "Tac "
walther@59846
   288
  | End_Proof' => "End_Proof'"
walther@59846
   289
  | _ => "input_to_string not impl. for ?!";
walther@59846
   290
walther@59846
   291
fun is_empty input = case input of Empty_Tac => true | _ => false
wneuper@59302
   292
wneuper@59302
   293
fun eq_tac (Rewrite (id1, _), Rewrite (id2, _)) = id1 = id2
wneuper@59302
   294
  | eq_tac (Rewrite_Inst (_, (id1, _)), Rewrite_Inst (_, (id2, _))) = id1 = id2
wneuper@59302
   295
  | eq_tac (Rewrite_Set id1, Rewrite_Set id2) = id1 = id2
wneuper@59302
   296
  | eq_tac (Rewrite_Set_Inst (_, id1), Rewrite_Set_Inst (_, id2)) = id1 = id2
wneuper@59302
   297
  | eq_tac (Calculate id1, Calculate id2) = id1 = id2
wneuper@59302
   298
  | eq_tac _ = false
wneuper@59302
   299
wneuper@59302
   300
fun is_rewset (Rewrite_Set_Inst _) = true
wneuper@59302
   301
  | is_rewset (Rewrite_Set _) = true 
wneuper@59302
   302
  | is_rewset _ = false;
wneuper@59302
   303
fun is_rewtac (Rewrite _) = true
wneuper@59302
   304
  | is_rewtac (Rewrite_Inst _) = true
wneuper@59571
   305
  | is_rewtac input = is_rewset input;
wneuper@59302
   306
wneuper@59302
   307
wneuper@59302
   308
fun rls_of (Rewrite_Set_Inst (_, rls)) = rls
wneuper@59302
   309
  | rls_of (Rewrite_Set rls) = rls
walther@59846
   310
  | rls_of input = error ("rls_of: called with input \"" ^ tac2IDstr input ^ "\"");
wneuper@59302
   311
walther@59878
   312
fun rule2tac thy _ (Rule.Eval (opID, _)) = Calculate (assoc_calc thy opID)
wneuper@59416
   313
  | rule2tac _ [] (Rule.Thm thm'') = Rewrite thm''
wneuper@59416
   314
  | rule2tac _ subst (Rule.Thm thm'') = 
walther@59911
   315
    Rewrite_Inst (Subst.T_to_input subst, thm'')
walther@59867
   316
  | rule2tac _ [] (Rule.Rls_ rls) = Rewrite_Set (Rule_Set.id rls)
wneuper@59416
   317
  | rule2tac _ subst (Rule.Rls_ rls) = 
walther@59911
   318
    Rewrite_Set_Inst (Subst.T_to_input subst, (Rule_Set.id rls))
wneuper@59302
   319
  | rule2tac _ _ rule = 
walther@59867
   320
    error ("rule2tac: called with \"" ^ Rule.to_string rule ^ "\"");
wneuper@59302
   321
walther@59914
   322
(* try if a rewrite-rule is applicable to a given formula; 
walther@59914
   323
   in case of rule-sets (recursivley) collect all _atomic_ rewrites *) 
walther@59914
   324
fun try_rew thy ((_, ro) : Rewrite_Ord.rew_ord) erls (subst : subst) f (thm' as Rule.Thm (_, thm)) =
walther@59914
   325
    if Auto_Prog.contains_bdv thm
walther@59914
   326
    then case Rewrite.rewrite_inst_ thy ro erls false subst thm f of
walther@59914
   327
	    SOME _ => [rule2tac thy subst thm']
walther@59914
   328
	  | NONE => []
walther@59914
   329
    else (case Rewrite.rewrite_ thy ro erls false thm f of
walther@59914
   330
	    SOME _ => [rule2tac thy [] thm']
walther@59914
   331
	  | NONE => [])
walther@59914
   332
  | try_rew thy _ _ _ f (cal as Rule.Eval c) = 
walther@59914
   333
    (case Eval.adhoc_thm thy c f of
walther@59914
   334
	    SOME _ => [rule2tac thy [] cal]
walther@59914
   335
    | NONE => [])
walther@59914
   336
  | try_rew thy _ _ _ f (cal as Rule.Cal1 c) = 
walther@59914
   337
    (case Eval.adhoc_thm thy c f of
walther@59914
   338
	      SOME _ => [rule2tac thy [] cal]
walther@59914
   339
      | NONE => [])
walther@59914
   340
  | try_rew thy _ _ subst f (Rule.Rls_ rls) = filter_appl_rews thy subst f rls
walther@59914
   341
  | try_rew _ _ _ _ _ _ = error "try_rew: uncovered case"
walther@59914
   342
and filter_appl_rews thy subst f (Rule_Def.Repeat {rew_ord = ro, erls, rules, ...}) = 
walther@59914
   343
    gen_distinct eq_tac (flat (map (try_rew thy ro erls subst f) rules))
walther@59914
   344
  | filter_appl_rews thy subst f (Rule_Set.Sequence {rew_ord = ro, erls, rules,...}) = 
walther@59914
   345
    gen_distinct eq_tac (flat (map (try_rew thy ro erls subst f) rules))
walther@59914
   346
  | filter_appl_rews _ _ _ (Rule_Set.Rrls _) = []
walther@59914
   347
  | filter_appl_rews _ _ _ _ = error "filter_appl_rews: uncovered case"
walther@59914
   348
walther@59914
   349
(* decide if a tactic is applicable to a given formula; 
walther@59914
   350
   in case of Rewrite_Set* go down to _atomic_ rewrite-tactics *)
walther@59914
   351
fun applicable thy _ _ f (Calculate scrID) =
walther@59914
   352
    try_rew thy Rewrite_Ord.e_rew_ordX Rule_Set.empty [] f (Rule.Eval (assoc_calc' thy scrID |> snd))
walther@59914
   353
  | applicable thy ro erls f (Rewrite thm'') =
walther@59914
   354
    try_rew thy (ro, Rewrite_Ord.assoc_rew_ord ro) erls [] f (Rule.Thm thm'')
walther@59914
   355
  | applicable thy ro erls f (Rewrite_Inst (subs, thm'')) =
walther@59914
   356
    try_rew thy (ro, Rewrite_Ord.assoc_rew_ord ro) erls (Subst.T_from_input thy subs) f (Rule.Thm thm'')
walther@59914
   357
walther@59914
   358
  | applicable thy _ _ f (Rewrite_Set rls') =
walther@59914
   359
    filter_appl_rews thy [] f (assoc_rls rls')
walther@59914
   360
  | applicable thy _ _ f (Rewrite_Set_Inst (subs, rls')) =
walther@59914
   361
    filter_appl_rews thy (Subst.T_from_input thy subs) f (assoc_rls rls')
walther@59914
   362
  | applicable _ _ _ _ tac = 
walther@59914
   363
    (tracing ("### applicable: not impl. for tac = '" ^ input_to_string tac ^ "'"); []);
walther@59914
   364
walther@59914
   365
walther@59914
   366
(** tactics for internal use **)
walther@59914
   367
walther@59846
   368
(* tactics for for internal use, compare "input" for user at the front-end.
walther@59846
   369
  tac_ contains results from check in 'fun applicable_in'.
walther@59846
   370
  This is useful for costly results, e.g. from rewriting;
walther@59846
   371
  however, these results might be changed by Scripts like
walther@59846
   372
      "      eq = (Rewrite_Set ''ansatz_rls'' False) eql;" ^
walther@59846
   373
      "      eq = (Rewrite_Set equival_trans False) eq;" ^
walther@59846
   374
  TODO.WN120106 ANALOGOUSLY TO Substitute':
walther@59846
   375
  So tac_ contains the term t the result was calculated from
walther@59846
   376
  in order to compare t with t' possibly changed by "Expr "
walther@59846
   377
  and re-calculate result if t<>t'
walther@59846
   378
  TODO.WN161219: replace *every* cterm' by term
walther@59846
   379
*)
walther@59846
   380
  datatype T =
walther@59865
   381
    Add_Find' of TermC.as_string * Model.itm list | Add_Given' of TermC.as_string * Model.itm list 
walther@59865
   382
  | Add_Relation' of TermC.as_string * Model.itm list
walther@59846
   383
  | Apply_Method' of (* creates the 1st step visible in a (sub-) comprising
walther@59846
   384
                      * tactic Apply_Method metID
walther@59846
   385
                      * formula term                                        *)
walther@59903
   386
      Method.id *  (* key for Know_Store                                     *)
walther@59846
   387
      term option *  (* the first formula of Calc.T. TODO: rm option        *)           
walther@59846
   388
      Istate_Def.T * (* for the newly started program                       *)
walther@59846
   389
      Proof.context  (* for the newly started program                       *)
walther@59846
   390
  (*/--- TODO: re-design ? -----------------------------------------------------------------\*)
walther@59846
   391
  | Begin_Sequ' | Begin_Trans' of term
walther@59846
   392
  | Split_And' of term | Split_Or' of term | Split_Intersect' of term
walther@59846
   393
  | Conclude_And' of term | Conclude_Or' of term | Collect_Trues' of term
walther@59846
   394
  | End_Sequ' | End_Trans' of Selem.result
walther@59846
   395
  | End_Ruleset' of term | End_Intersect' of term | End_Proof''
walther@59846
   396
  (*\--- TODO: re-design ? -----------------------------------------------------------------/*)
walther@59846
   397
  | CAScmd' of term
walther@59879
   398
  | Calculate' of ThyC.id * string * term * (term * ThmC.T)
walther@59903
   399
  | Check_Postcond' of Problem.id *
walther@59846
   400
    term         (* returnvalue of program in solve *)
walther@59846
   401
  | Check_elementwise' of (* DEPRECATED, made idle for Calc.T in df00a2b5c4cc *)
walther@59846
   402
    term *       (* (1) the current formula: [x=1,x=...]                      *)
walther@59846
   403
    string *     (* (2) the pred from Check_elementwise                       *)
walther@59846
   404
    Selem.result (* (3) composed from (1) and (2): {x. pred}                  *)
walther@59865
   405
  | Del_Find' of TermC.as_string | Del_Given' of TermC.as_string | Del_Relation' of TermC.as_string
wneuper@59302
   406
walther@59851
   407
  | Derive' of Rule_Set.T
walther@59879
   408
  | Detail_Set' of ThyC.id * bool * Rule_Set.T * term * Selem.result
walther@59910
   409
  | Detail_Set_Inst' of ThyC.id * bool * subst * Rule_Set.T * term * Selem.result
walther@59846
   410
  | End_Detail' of Selem.result
walther@59846
   411
walther@59846
   412
  | Empty_Tac_
walther@59846
   413
  | Free_Solve'
walther@59846
   414
walther@59903
   415
  | Init_Proof' of TermC.as_string list * Spec.T
walther@59846
   416
  | Model_Problem' of (* first step in specifying   *)
walther@59903
   417
    Problem.id *     (* key into Know_Store           *)
walther@59846
   418
    Model.itm list *  (* the 'untouched' pbl        *)
walther@59846
   419
    Model.itm list    (* the casually completed met *)
walther@59846
   420
  | Or_to_List' of term * term
walther@59903
   421
  | Refine_Problem' of Problem.id * (Model.itm list * (bool * term) list)
walther@59846
   422
  | Refine_Tacitly' of
walther@59903
   423
    Problem.id *     (* input                                                                *)
walther@59903
   424
    Problem.id *     (* the refined from applicable_in                                       *)
walther@59879
   425
    ThyC.id *      (* from new pbt?! filled in specify                                     *)
walther@59903
   426
    Method.id *     (* from new pbt?! filled in specify                                     *)
walther@59846
   427
    Model.itm list    (* drop ! 9.03: remains [] for Model_Problem recognizing its activation *)
walther@59879
   428
  | Rewrite' of ThyC.id * Rewrite_Ord.rew_ord' * Rule_Set.T * bool * ThmC.T * term * Selem.result
walther@59910
   429
  | Rewrite_Inst' of ThyC.id * Rewrite_Ord.rew_ord' * Rule_Set.T * bool * subst * ThmC.T * term * Selem.result
walther@59879
   430
  | Rewrite_Set' of ThyC.id * bool * Rule_Set.T * term * Selem.result
walther@59910
   431
  | Rewrite_Set_Inst' of ThyC.id * bool * subst * Rule_Set.T * term * Selem.result
walther@59846
   432
walther@59903
   433
  | Specify_Method' of Method.id * Model.ori list * Model.itm list
walther@59903
   434
  | Specify_Problem' of Problem.id * 
walther@59846
   435
    (bool *                  (* matches	                                  *)
walther@59846
   436
      (Model.itm list *      (* ppc	                                      *)
walther@59846
   437
        (bool * term) list)) (* preconditions marked true/false           *)
walther@59879
   438
  | Specify_Theory' of ThyC.id
walther@59846
   439
  | Subproblem' of
walther@59903
   440
    Spec.T * 
walther@59846
   441
		(Model.ori list) *       (* filled in associate Subproblem'           *)
walther@59846
   442
		term *                   (* filled -"-, headline of calc-head         *)
walther@59846
   443
		Selem.fmz_ *             (* string list from arguments                *)
walther@59846
   444
    Proof.context *          (* for specify-phase                         *)
walther@59846
   445
		term                     (* Subproblem (thyID, pbl) OR cascmd         *)  
walther@59846
   446
  | Substitute' of           
walther@59850
   447
    Rule_Def.rew_ord_ *          (* for re-calculation                        *)
walther@59851
   448
    Rule_Set.T *               (* for re-calculation                        *)
walther@59912
   449
    Subst.as_eqs *            (* the 'substitution': terms of type bool    *)
walther@59846
   450
    term *                   (* to be substituted into                    *)
walther@59846
   451
    term                     (* resulting from the substitution           *)
walther@59846
   452
  | Tac_ of theory * string * string * string
walther@59846
   453
  | Take' of term
walther@59846
   454
walther@59846
   455
fun string_of ma = case ma of
walther@59902
   456
    Init_Proof' (ppc, spec)  => "Init_Proof' " ^ pair2str (strs2str ppc, Spec.to_string spec)
walther@59846
   457
  | Model_Problem' (pblID, _, _) => "Model_Problem' " ^ strs2str pblID
walther@59846
   458
  | Refine_Tacitly'(p, prefin, domID, metID, _) => "Refine_Tacitly' (" ^ strs2str p ^ ", " ^
walther@59846
   459
    strs2str prefin ^ ", " ^ domID ^ ", " ^ strs2str metID ^ ", pbl-itms)"
walther@59846
   460
  | Refine_Problem' _ => "Refine_Problem' (" ^ (*matchs2str ms*)"..." ^ ")"
walther@59846
   461
  | Add_Given' _ => "Add_Given' "(*^cterm'*)
walther@59846
   462
  | Del_Given' _ => "Del_Given' "(*^cterm'*)
walther@59846
   463
  | Add_Find' _ => "Add_Find' "(*^cterm'*)
walther@59846
   464
  | Del_Find' _ => "Del_Find' "(*^cterm'*)
walther@59846
   465
  | Add_Relation' _ => "Add_Relation' "(*^cterm'*)
walther@59846
   466
  | Del_Relation' _ => "Del_Relation' "(*^cterm'*)
walther@59846
   467
walther@59846
   468
  | Specify_Theory' domID => "Specify_Theory' " ^ quote domID
walther@59846
   469
  | Specify_Problem' (pI, (ok, _)) =>  "Specify_Problem' " ^ 
walther@59846
   470
    spair2str (strs2str pI, spair2str (bool2str ok, spair2str ("itms2str_ itms", "items2str pre")))
walther@59846
   471
  | Specify_Method' (pI, oris, _) => "Specify_Method' (" ^ 
walther@59903
   472
    Method.id_to_string pI ^ ", " ^ Model.oris2str oris ^ ", )"
walther@59846
   473
walther@59846
   474
  | Apply_Method' (metID, _, _, _) => "Apply_Method' " ^ strs2str metID
walther@59846
   475
  | Check_Postcond' (pblID, scval) => "Check_Postcond' " ^
walther@59868
   476
      (spair2str (strs2str pblID, UnparseC.term scval))
walther@59846
   477
walther@59846
   478
  | Free_Solve' => "Free_Solve'"
walther@59846
   479
walther@59846
   480
  | Rewrite_Inst' (*subs,thm'*) _ => "Rewrite_Inst' "(*^(pair2str (subs2str subs, spair2str thm'))*)
walther@59846
   481
  | Rewrite' _(*thm'*) => "Rewrite' "(*^(spair2str thm')*)
walther@59846
   482
  | Rewrite_Set_Inst' _(*subs,thm'*) => "Rewrite_Set_Inst' "(*^(pair2str (subs2str subs, quote rls))*)
walther@59846
   483
  | Rewrite_Set' (thy', pasm, rls', f, (f', asm)) => "Rewrite_Set' (" ^ thy' ^ "," ^ bool2str pasm ^
walther@59868
   484
    "," ^ Rule_Set.id rls' ^ "," ^ UnparseC.term f ^ ",(" ^ UnparseC.term f' ^ "," ^ UnparseC.terms asm ^ "))"
walther@59846
   485
  | End_Detail' _ => "End_Detail' xxx"
walther@59846
   486
  | Detail_Set' _ => "Detail_Set' xxx"
walther@59846
   487
  | Detail_Set_Inst' _ => "Detail_Set_Inst' xxx"
walther@59846
   488
walther@59867
   489
  | Derive' rls => "Derive' " ^ Rule_Set.id rls
walther@59846
   490
  | Calculate'  _ => "Calculate' "
walther@59846
   491
  | Substitute' _ => "Substitute' "(*^(subs2str subs)*)    
walther@59846
   492
walther@59846
   493
  | Take' _(*cterm'*) => "Take' "(*^(quote cterm'	)*)
walther@59846
   494
  | Subproblem' _(*(spec, oris, _, _, _, pbl_form)*) => 
walther@59846
   495
    "Subproblem' "(*^(pair2str (domID, strs2str ,))*)
walther@59846
   496
  | CAScmd' _(*cterm'*) => "CAScmd' "(*^(quote cterm')*)
walther@59846
   497
walther@59846
   498
  | Empty_Tac_ => "Empty_Tac_"
walther@59846
   499
  | Tac_ (_, form, id, result) => "Tac_ (thy," ^ form ^ "," ^ id ^ "," ^ result ^ ")"
walther@59846
   500
  | _  => "string_of not impl. for arg";
wneuper@59302
   501
walther@59704
   502
fun input_from_T (Refine_Tacitly' (pI, _, _, _, _)) = Refine_Tacitly pI
walther@59704
   503
  | input_from_T (Model_Problem' (_, _, _)) = Model_Problem
walther@59704
   504
  | input_from_T (Add_Given' (t, _)) = Add_Given t
walther@59704
   505
  | input_from_T (Add_Find' (t, _)) = Add_Find t
walther@59704
   506
  | input_from_T (Add_Relation' (t, _)) = Add_Relation t
walther@59704
   507
 
walther@59704
   508
  | input_from_T (Specify_Theory' dI) = Specify_Theory dI
walther@59704
   509
  | input_from_T (Specify_Problem' (dI, _)) = Specify_Problem dI
walther@59704
   510
  | input_from_T (Specify_Method' (dI, _, _)) = Specify_Method dI
walther@59704
   511
  
walther@59704
   512
  | input_from_T (Rewrite' (_, _, _, _, thm, _, _)) = Rewrite thm
walther@59911
   513
  | input_from_T (Rewrite_Inst' (_, _, _, _, sub, thm, _, _)) = Rewrite_Inst (Subst.T_to_input sub, thm)
walther@59704
   514
walther@59867
   515
  | input_from_T (Rewrite_Set' (_, _, rls, _, _)) = Rewrite_Set (Rule_Set.id rls)
walther@59867
   516
  | input_from_T (Detail_Set' (_, _, rls, _, _)) = Detail_Set (Rule_Set.id rls)
walther@59704
   517
walther@59704
   518
  | input_from_T (Rewrite_Set_Inst' (_, _, sub, rls, _, _)) = 
walther@59911
   519
    Rewrite_Set_Inst (Subst.T_to_input sub, Rule_Set.id rls)
walther@59704
   520
  | input_from_T (Detail_Set_Inst' (_, _, sub, rls, _, _)) = 
walther@59911
   521
    Detail_Set_Inst (Subst.T_to_input sub, Rule_Set.id rls)
walther@59704
   522
walther@59704
   523
  | input_from_T (Calculate' (_, op_, _, _)) = Calculate (op_)
walther@59704
   524
  | input_from_T (Check_elementwise' (_, pred, _)) = Check_elementwise pred
walther@59704
   525
walther@59704
   526
  | input_from_T (Or_to_List' _) = Or_to_List
walther@59868
   527
  | input_from_T (Take' term) = Take (UnparseC.term term)
walther@59912
   528
  | input_from_T (Substitute' (_, _, subte, _, _)) = Substitute (Subst.eqs_to_input subte) 
walther@59704
   529
  | input_from_T (Tac_ (_, _, id, _)) = Tac id
walther@59704
   530
walther@59704
   531
  | input_from_T (Subproblem' ((domID, pblID, _), _, _, _,_ ,_)) = Subproblem (domID, pblID)
walther@59704
   532
  | input_from_T (Check_Postcond' (pblID, _)) = Check_Postcond pblID
walther@59704
   533
  | input_from_T Empty_Tac_ = Empty_Tac
walther@59846
   534
  | input_from_T m = raise ERROR (": not impl. for "^(string_of m));
walther@59704
   535
walther@59728
   536
fun res (Rewrite_Inst' (_ , _, _, _, _, _, _, res)) = res
walther@59728
   537
  | res (Rewrite' (_, _, _, _, _, _, res)) = res
walther@59728
   538
  | res (Rewrite_Set_Inst' (_, _, _, _, _, res)) = res
walther@59728
   539
  | res (Rewrite_Set' (_, _, _, _, res)) = res
walther@59728
   540
  | res (Calculate' (_, _, _, (t, _))) = (t, [])
walther@59728
   541
  | res (Check_elementwise' (_, _, res)) = res
walther@59728
   542
  | res (Subproblem' (_, _, _, _, _, t)) = (t, [])
walther@59728
   543
  | res (Take' t) = (t, [])
walther@59728
   544
  | res (Substitute' (_, _, _, _, t)) = (t, [])
walther@59728
   545
  | res (Or_to_List' (_,  t)) = (t, [])
walther@59846
   546
  | res m = raise ERROR ("result: not impl.for " ^ string_of m)
walther@59728
   547
walther@59728
   548
(*fun result m = (fst o res) m; TODO*)
walther@59728
   549
fun result tac = (fst o res) tac;
walther@59728
   550
fun creates_assms tac = (snd o res) tac;
walther@59728
   551
walther@59728
   552
fun insert_assumptions tac ctxt  = ContextC.insert_assumptions (creates_assms tac) ctxt
walther@59728
   553
walther@59749
   554
fun for_specify (Init_Proof _) = true
walther@59749
   555
  | for_specify Model_Problem  = true
walther@59749
   556
  | for_specify (Refine_Tacitly _) = true
walther@59749
   557
  | for_specify (Refine_Problem _) = true
walther@59749
   558
  | for_specify (Add_Given _) = true
walther@59749
   559
  | for_specify (Del_Given _) = true
walther@59749
   560
  | for_specify (Add_Find _) = true
walther@59749
   561
  | for_specify (Del_Find _) = true
walther@59749
   562
  | for_specify (Add_Relation _) = true
walther@59749
   563
  | for_specify (Del_Relation _) = true
walther@59749
   564
  | for_specify (Specify_Theory _) = true
walther@59749
   565
  | for_specify (Specify_Problem _) = true
walther@59749
   566
  | for_specify (Specify_Method _) = true
walther@59749
   567
  | for_specify _ = false
walther@59749
   568
fun for_specify' (Init_Proof' _) = true
walther@59749
   569
  | for_specify' (Model_Problem' _) = true
walther@59749
   570
  | for_specify' (Refine_Tacitly' _) = true
walther@59749
   571
  | for_specify' (Refine_Problem' _) = true
walther@59749
   572
  | for_specify' (Add_Given' _) = true
walther@59749
   573
  | for_specify' (Del_Given' _) = true
walther@59749
   574
  | for_specify' (Add_Find' _) = true
walther@59749
   575
  | for_specify' (Del_Find' _) = true
walther@59749
   576
  | for_specify' (Add_Relation' _) = true
walther@59749
   577
  | for_specify' (Del_Relation' _) = true
walther@59749
   578
  | for_specify' (Specify_Theory' _) = true
walther@59749
   579
  | for_specify' (Specify_Problem' _) = true
walther@59749
   580
  | for_specify' (Specify_Method' _) = true
walther@59749
   581
  | for_specify' _ = false
walther@59749
   582
walther@59728
   583
(**)end(**)