src/Tools/isac/MathEngBasic/tactic.sml
author Walther Neuper <walther.neuper@jku.at>
Sat, 02 May 2020 09:15:39 +0200
changeset 59924 eb40bce6d6f1
parent 59923 cd730f07c9ac
child 59925 caf3839e53c5
permissions -rw-r--r--
comment tactics in struct. (only)
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@59924
    16
(*RM*)| Del_Find' of TermC.as_string | Del_Given' of TermC.as_string | Del_Relation' of TermC.as_string
walther@59924
    17
(*RM*)| 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@59924
    38
  | Subproblem' of Spec.T * Model.ori list * term * Selem.fmz_ * Proof.context * term
walther@59912
    39
  | Substitute' of Rule_Def.rew_ord_ * Rule_Set.T * Subst.as_eqs * term * term
walther@59923
    40
(*RM*)| Tac_ of theory * string * string * string
walther@59846
    41
  | Take' of term
walther@59923
    42
(*RM* )| Take_Inst of TermC.as_string( *RM*)
walther@59923
    43
(*RM*)| Begin_Sequ' | Begin_Trans' of term
walther@59923
    44
(*RM*)| Split_And' of term | Split_Or' of term | Split_Intersect' of term
walther@59923
    45
(*RM*)| Conclude_And' of term | Conclude_Or' of term | Collect_Trues' of term
walther@59923
    46
(*RM*)| End_Sequ' | End_Trans' of Selem.result
walther@59923
    47
(*RM*)| End_Ruleset' of term | End_Intersect' of term | End_Proof''
walther@59923
    48
(*RM*)| Detail_Set' of ThyC.id * bool * Rule_Set.T * term * Selem.result
walther@59923
    49
(*RM*)| Detail_Set_Inst' of ThyC.id * bool * subst * Rule_Set.T * term * Selem.result
walther@59923
    50
(*RM*)| End_Detail' of Selem.result;
walther@59923
    51
walther@59812
    52
  val string_of: T -> string
walther@59741
    53
walther@59846
    54
  datatype input =
walther@59923
    55
    Add_Find of TermC.as_string | Add_Given of TermC.as_string
walther@59923
    56
  | Add_Relation of TermC.as_string
walther@59903
    57
  | Apply_Method of Method.id
walther@59923
    58
  | Del_Find of TermC.as_string | Del_Given of TermC.as_string | Del_Relation of TermC.as_string
walther@59923
    59
  | Init_Proof of TermC.as_string list * Spec.T
walther@59923
    60
  | Model_Problem
walther@59923
    61
  | Refine_Problem of Problem.id
walther@59923
    62
  | Refine_Tacitly of Problem.id
walther@59923
    63
  | Specify_Method of Method.id
walther@59923
    64
  | Specify_Problem of Problem.id
walther@59923
    65
  | Specify_Theory of ThyC.id
walther@59923
    66
  (* ^^^^^--------------------- for specify-phase, for solve-phase ---------------------vvvvv*)
walther@59865
    67
  | CAScmd of TermC.as_string
walther@59846
    68
  | Calculate of string
walther@59903
    69
  | Check_Postcond of Problem.id
walther@59865
    70
  | Check_elementwise of TermC.as_string
walther@59923
    71
(*RM*)| Derive of Rule_Set.id(*RM*)
walther@59846
    72
  | Empty_Tac
walther@59923
    73
(*RM*)| Free_Solve
walther@59923
    74
(*RM*)| Apply_Assumption of TermC.as_string list
walther@59846
    75
  | Or_to_List
walther@59874
    76
  | Rewrite of ThmC.T
walther@59911
    77
  | Rewrite_Inst of Subst.input * ThmC.T
walther@59867
    78
  | Rewrite_Set of Rule_Set.id
walther@59911
    79
  | Rewrite_Set_Inst of Subst.input * Rule_Set.id
walther@59923
    80
  | Subproblem of ThyC.id * Problem.id
walther@59923
    81
  | Substitute of Subst.input
walther@59923
    82
(*RM*)| Tac of string
walther@59923
    83
  | Take of TermC.as_string
walther@59923
    84
(*RM*)| Take_Inst of TermC.as_string
walther@59923
    85
(*RM*)| Begin_Sequ | Begin_Trans
walther@59923
    86
(*RM*)| Split_And | Split_Or | Split_Intersect
walther@59923
    87
(*RM*)| Conclude_And | Conclude_Or | Collect_Trues
walther@59923
    88
(*RM*)| End_Sequ | End_Trans
walther@59923
    89
(*RM*)| End_Ruleset | End_Subproblem | End_Intersect
walther@59923
    90
(*RM*)| Detail_Set of Rule_Set.id
walther@59923
    91
(*RM*)| Detail_Set_Inst of Subst.input * Rule_Set.id
walther@59923
    92
(*RM*)| End_Detail
walther@59923
    93
  | End_Proof';
walther@59846
    94
walther@59846
    95
  val input_to_string : input -> string
walther@59812
    96
  val tac2IDstr : input -> string
walther@59844
    97
  val is_empty : input -> bool
wneuper@59302
    98
walther@59728
    99
  val eq_tac : input * input -> bool
walther@59728
   100
  val is_rewtac : input -> bool
walther@59728
   101
  val is_rewset : input -> bool
walther@59867
   102
  val rls_of : input -> Rule_Set.id
walther@59728
   103
  val rule2tac : theory -> (term * term) list ->  Rule.rule -> input
walther@59914
   104
  val applicable : theory -> string -> Rule_Set.T -> term -> input ->input list
walther@59914
   105
  val for_specify: input -> bool
walther@59914
   106
walther@59704
   107
  val input_from_T : T -> input
walther@59728
   108
  val result : T -> term
walther@59728
   109
  val creates_assms: T -> term list
walther@59728
   110
  val insert_assumptions: T -> Proof.context -> Proof.context
walther@59749
   111
  val for_specify': T -> bool
walther@59735
   112
wneuper@59310
   113
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
wneuper@59310
   114
  (* NONE *)
walther@59886
   115
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
wneuper@59310
   116
  (* NONE *)
walther@59886
   117
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59298
   118
wneuper@59310
   119
(*----- unused code, kept as hints to design ideas ---------------------------------------------*)
wneuper@59310
   120
  (* NONE *)
wneuper@59298
   121
end
wneuper@59298
   122
walther@59728
   123
(**)
wneuper@59571
   124
structure Tactic(**): TACTIC(**) =
wneuper@59298
   125
struct
walther@59728
   126
(**)
wneuper@59298
   127
walther@59914
   128
(** tactics for user at front-end **)
walther@59914
   129
walther@59846
   130
datatype input =
walther@59865
   131
    Add_Find of TermC.as_string | Add_Given of TermC.as_string | Add_Relation of TermC.as_string
walther@59903
   132
  | Apply_Method of Method.id
walther@59924
   133
  | Del_Find of TermC.as_string | Del_Given of TermC.as_string | Del_Relation of TermC.as_string
walther@59924
   134
  | Init_Proof of TermC.as_string list * Spec.T
walther@59924
   135
  | Model_Problem
walther@59924
   136
  | Refine_Problem of Problem.id
walther@59924
   137
  | Refine_Tacitly of Problem.id
walther@59924
   138
  | Specify_Method of Method.id
walther@59924
   139
  | Specify_Problem of Problem.id
walther@59924
   140
  | Specify_Theory of ThyC.id
walther@59924
   141
  (* ^^^^^--------------------- for specify-phase, for solve-phase ---------------------vvvvv*)
walther@59865
   142
  | CAScmd of TermC.as_string
walther@59846
   143
  | Calculate of string
walther@59903
   144
  | Check_Postcond of Problem.id
walther@59865
   145
  | Check_elementwise of TermC.as_string
walther@59924
   146
(*RM*)| Derive of Rule_Set.id
walther@59846
   147
  | Empty_Tac
walther@59924
   148
(*RM*)| Free_Solve
walther@59924
   149
(*RM*)| Apply_Assumption of TermC.as_string list
walther@59846
   150
  | Or_to_List
walther@59874
   151
  | Rewrite of ThmC.T
walther@59911
   152
  | Rewrite_Inst of Subst.input * ThmC.T
walther@59867
   153
  | Rewrite_Set of Rule_Set.id
walther@59911
   154
  | Rewrite_Set_Inst of Subst.input * Rule_Set.id
walther@59924
   155
  | Subproblem of ThyC.id * Problem.id
walther@59912
   156
  | Substitute of Subst.input
walther@59924
   157
(*RM*)| Tac of string
walther@59924
   158
  | Take of TermC.as_string
walther@59924
   159
(*RM*)| Take_Inst of TermC.as_string
walther@59924
   160
(*RM*)| Begin_Sequ | Begin_Trans
walther@59924
   161
(*RM*)| Split_And | Split_Or | Split_Intersect
walther@59924
   162
(*RM*)| Conclude_And | Conclude_Or | Collect_Trues
walther@59924
   163
(*RM*)| End_Sequ | End_Trans
walther@59924
   164
(*RM*)| End_Ruleset | End_Subproblem | End_Intersect
walther@59924
   165
(*RM*)| Detail_Set of Rule_Set.id
walther@59924
   166
(*RM*)| Detail_Set_Inst of Subst.input * Rule_Set.id
walther@59924
   167
(*RM*)| End_Detail
walther@59924
   168
  | End_Proof';
walther@59846
   169
walther@59846
   170
fun input_to_string ma = case ma of
walther@59846
   171
    Init_Proof (ppc, spec)  => 
walther@59902
   172
      "Init_Proof "^(pair2str (strs2str ppc, Spec.to_string spec))
walther@59846
   173
  | Model_Problem           => "Model_Problem "
walther@59846
   174
  | Refine_Tacitly pblID    => "Refine_Tacitly " ^ strs2str pblID 
walther@59846
   175
  | Refine_Problem pblID    => "Refine_Problem " ^ strs2str pblID 
walther@59846
   176
  | Add_Given cterm'        => "Add_Given " ^ cterm'
walther@59846
   177
  | Del_Given cterm'        => "Del_Given " ^ cterm'
walther@59846
   178
  | Add_Find cterm'         => "Add_Find " ^ cterm'
walther@59846
   179
  | Del_Find cterm'         => "Del_Find " ^ cterm'
walther@59846
   180
  | Add_Relation cterm'     => "Add_Relation " ^ cterm'
walther@59846
   181
  | Del_Relation cterm'     => "Del_Relation " ^ cterm'
walther@59846
   182
walther@59846
   183
  | Specify_Theory domID    => "Specify_Theory " ^ quote domID
walther@59846
   184
  | Specify_Problem pblID   => "Specify_Problem " ^ strs2str pblID
walther@59846
   185
  | Specify_Method metID    => "Specify_Method " ^ strs2str metID
walther@59846
   186
  | Apply_Method metID      => "Apply_Method " ^ strs2str metID
walther@59846
   187
  | Check_Postcond pblID    => "Check_Postcond " ^ strs2str pblID
walther@59846
   188
  | Free_Solve              => "Free_Solve"
walther@59846
   189
walther@59846
   190
  | Rewrite_Inst (subs, (id, thm)) =>
walther@59868
   191
    "Rewrite_Inst " ^ (pair2str (subs2str subs, spair2str (id, thm |> Thm.prop_of |> UnparseC.term)))
walther@59868
   192
  | Rewrite (id, thm) => "Rewrite " ^ spair2str (id, thm |> Thm.prop_of |> UnparseC.term)
walther@59846
   193
  | Rewrite_Set_Inst (subs, rls) => 
walther@59846
   194
    "Rewrite_Set_Inst " ^ pair2str (subs2str subs, quote rls)
walther@59846
   195
  | Rewrite_Set rls         => "Rewrite_Set " ^ quote rls
walther@59846
   196
  | Detail_Set rls          => "Detail_Set " ^ quote rls
walther@59846
   197
  | Detail_Set_Inst (subs, rls) =>  "Detail_Set_Inst " ^ pair2str (subs2str subs, quote rls)
walther@59846
   198
  | End_Detail              => "End_Detail"
walther@59846
   199
  | Derive rls'             => "Derive " ^ rls' 
walther@59911
   200
  | Calculate op_           => "Calculate " ^ op_
walther@59912
   201
  | Substitute sube         => "Substitute " ^ Subst.string_eqs_to_string sube	     
walther@59846
   202
  | Apply_Assumption ct's   => "Apply_Assumption " ^ strs2str ct's
walther@59846
   203
walther@59846
   204
  | Take cterm'             => "Take " ^ quote cterm'
walther@59846
   205
  | Take_Inst cterm'        => "Take_Inst " ^ quote cterm'
walther@59846
   206
  | Subproblem (domID, pblID) => "Subproblem " ^ pair2str (domID, strs2str pblID)
walther@59846
   207
  | End_Subproblem          => "End_Subproblem"
walther@59846
   208
  | CAScmd cterm'           => "CAScmd " ^ quote cterm'
walther@59846
   209
walther@59846
   210
  | Check_elementwise cterm'=> "Check_elementwise " ^ quote cterm'
walther@59846
   211
  | Or_to_List              => "Or_to_List "
walther@59846
   212
  | Collect_Trues           => "Collect_Trues"
walther@59846
   213
walther@59846
   214
  | Empty_Tac               => "Empty_Tac"
walther@59846
   215
  | Tac string              => "Tac " ^ string
walther@59846
   216
  | End_Proof'              => "input End_Proof'"
walther@59846
   217
  | _                       => "input_to_string not impl. for ?!";
walther@59846
   218
walther@59846
   219
fun tac2IDstr ma = case ma of
walther@59846
   220
    Model_Problem => "Model_Problem"
walther@59846
   221
  | Refine_Tacitly _ => "Refine_Tacitly"
walther@59846
   222
  | Refine_Problem _ => "Refine_Problem"
walther@59846
   223
  | Add_Given _ => "Add_Given"
walther@59846
   224
  | Del_Given _ => "Del_Given"
walther@59846
   225
  | Add_Find _ => "Add_Find"
walther@59846
   226
  | Del_Find _ => "Del_Find"
walther@59846
   227
  | Add_Relation _ => "Add_Relation"
walther@59846
   228
  | Del_Relation _ => "Del_Relation"
walther@59846
   229
walther@59846
   230
  | Specify_Theory _ => "Specify_Theory"
walther@59846
   231
  | Specify_Problem _ => "Specify_Problem"
walther@59846
   232
  | Specify_Method _ => "Specify_Method"
walther@59846
   233
  | Apply_Method _ => "Apply_Method"
walther@59846
   234
  | Check_Postcond _ => "Check_Postcond"
walther@59846
   235
  | Free_Solve => "Free_Solve"
walther@59846
   236
walther@59846
   237
  | Rewrite_Inst _ => "Rewrite_Inst"
walther@59846
   238
  | Rewrite _ => "Rewrite"
walther@59846
   239
  | Rewrite_Set_Inst _ => "Rewrite_Set_Inst"
walther@59846
   240
  | Rewrite_Set _ => "Rewrite_Set"
walther@59846
   241
  | Detail_Set _ => "Detail_Set"
walther@59846
   242
  | Detail_Set_Inst _ => "Detail_Set_Inst"
walther@59846
   243
  | Derive _ => "Derive "
walther@59846
   244
  | Calculate _ => "Calculate "
walther@59846
   245
  | Substitute _ => "Substitute" 
walther@59846
   246
  | Apply_Assumption _ => "Apply_Assumption"
walther@59846
   247
walther@59846
   248
  | Take _ => "Take"
walther@59846
   249
  | Take_Inst _ => "Take_Inst"
walther@59846
   250
  | Subproblem _ => "Subproblem"
walther@59846
   251
  | End_Subproblem => "End_Subproblem"
walther@59846
   252
  | CAScmd _ => "CAScmd"
walther@59846
   253
walther@59846
   254
  | Check_elementwise _ => "Check_elementwise"
walther@59846
   255
  | Or_to_List => "Or_to_List "
walther@59846
   256
  | Collect_Trues => "Collect_Trues"
walther@59846
   257
walther@59846
   258
  | Empty_Tac => "Empty_Tac"
walther@59846
   259
  | Tac _ => "Tac "
walther@59846
   260
  | End_Proof' => "End_Proof'"
walther@59846
   261
  | _ => "input_to_string not impl. for ?!";
walther@59846
   262
walther@59846
   263
fun is_empty input = case input of Empty_Tac => true | _ => false
wneuper@59302
   264
wneuper@59302
   265
fun eq_tac (Rewrite (id1, _), Rewrite (id2, _)) = id1 = id2
wneuper@59302
   266
  | eq_tac (Rewrite_Inst (_, (id1, _)), Rewrite_Inst (_, (id2, _))) = id1 = id2
wneuper@59302
   267
  | eq_tac (Rewrite_Set id1, Rewrite_Set id2) = id1 = id2
wneuper@59302
   268
  | eq_tac (Rewrite_Set_Inst (_, id1), Rewrite_Set_Inst (_, id2)) = id1 = id2
wneuper@59302
   269
  | eq_tac (Calculate id1, Calculate id2) = id1 = id2
wneuper@59302
   270
  | eq_tac _ = false
wneuper@59302
   271
wneuper@59302
   272
fun is_rewset (Rewrite_Set_Inst _) = true
wneuper@59302
   273
  | is_rewset (Rewrite_Set _) = true 
wneuper@59302
   274
  | is_rewset _ = false;
wneuper@59302
   275
fun is_rewtac (Rewrite _) = true
wneuper@59302
   276
  | is_rewtac (Rewrite_Inst _) = true
wneuper@59571
   277
  | is_rewtac input = is_rewset input;
wneuper@59302
   278
wneuper@59302
   279
wneuper@59302
   280
fun rls_of (Rewrite_Set_Inst (_, rls)) = rls
wneuper@59302
   281
  | rls_of (Rewrite_Set rls) = rls
walther@59846
   282
  | rls_of input = error ("rls_of: called with input \"" ^ tac2IDstr input ^ "\"");
wneuper@59302
   283
walther@59878
   284
fun rule2tac thy _ (Rule.Eval (opID, _)) = Calculate (assoc_calc thy opID)
wneuper@59416
   285
  | rule2tac _ [] (Rule.Thm thm'') = Rewrite thm''
wneuper@59416
   286
  | rule2tac _ subst (Rule.Thm thm'') = 
walther@59911
   287
    Rewrite_Inst (Subst.T_to_input subst, thm'')
walther@59867
   288
  | rule2tac _ [] (Rule.Rls_ rls) = Rewrite_Set (Rule_Set.id rls)
wneuper@59416
   289
  | rule2tac _ subst (Rule.Rls_ rls) = 
walther@59911
   290
    Rewrite_Set_Inst (Subst.T_to_input subst, (Rule_Set.id rls))
wneuper@59302
   291
  | rule2tac _ _ rule = 
walther@59867
   292
    error ("rule2tac: called with \"" ^ Rule.to_string rule ^ "\"");
wneuper@59302
   293
walther@59914
   294
(* try if a rewrite-rule is applicable to a given formula; 
walther@59914
   295
   in case of rule-sets (recursivley) collect all _atomic_ rewrites *) 
walther@59914
   296
fun try_rew thy ((_, ro) : Rewrite_Ord.rew_ord) erls (subst : subst) f (thm' as Rule.Thm (_, thm)) =
walther@59914
   297
    if Auto_Prog.contains_bdv thm
walther@59914
   298
    then case Rewrite.rewrite_inst_ thy ro erls false subst thm f of
walther@59914
   299
	    SOME _ => [rule2tac thy subst thm']
walther@59914
   300
	  | NONE => []
walther@59914
   301
    else (case Rewrite.rewrite_ thy ro erls false thm f of
walther@59914
   302
	    SOME _ => [rule2tac thy [] thm']
walther@59914
   303
	  | NONE => [])
walther@59914
   304
  | try_rew thy _ _ _ f (cal as Rule.Eval c) = 
walther@59914
   305
    (case Eval.adhoc_thm thy c f of
walther@59914
   306
	    SOME _ => [rule2tac thy [] cal]
walther@59914
   307
    | NONE => [])
walther@59914
   308
  | try_rew thy _ _ _ f (cal as Rule.Cal1 c) = 
walther@59914
   309
    (case Eval.adhoc_thm thy c f of
walther@59914
   310
	      SOME _ => [rule2tac thy [] cal]
walther@59914
   311
      | NONE => [])
walther@59914
   312
  | try_rew thy _ _ subst f (Rule.Rls_ rls) = filter_appl_rews thy subst f rls
walther@59914
   313
  | try_rew _ _ _ _ _ _ = error "try_rew: uncovered case"
walther@59914
   314
and filter_appl_rews thy subst f (Rule_Def.Repeat {rew_ord = ro, erls, rules, ...}) = 
walther@59914
   315
    gen_distinct eq_tac (flat (map (try_rew thy ro erls subst f) rules))
walther@59914
   316
  | filter_appl_rews thy subst f (Rule_Set.Sequence {rew_ord = ro, erls, rules,...}) = 
walther@59914
   317
    gen_distinct eq_tac (flat (map (try_rew thy ro erls subst f) rules))
walther@59914
   318
  | filter_appl_rews _ _ _ (Rule_Set.Rrls _) = []
walther@59914
   319
  | filter_appl_rews _ _ _ _ = error "filter_appl_rews: uncovered case"
walther@59914
   320
walther@59914
   321
(* decide if a tactic is applicable to a given formula; 
walther@59914
   322
   in case of Rewrite_Set* go down to _atomic_ rewrite-tactics *)
walther@59914
   323
fun applicable thy _ _ f (Calculate scrID) =
walther@59914
   324
    try_rew thy Rewrite_Ord.e_rew_ordX Rule_Set.empty [] f (Rule.Eval (assoc_calc' thy scrID |> snd))
walther@59914
   325
  | applicable thy ro erls f (Rewrite thm'') =
walther@59914
   326
    try_rew thy (ro, Rewrite_Ord.assoc_rew_ord ro) erls [] f (Rule.Thm thm'')
walther@59914
   327
  | applicable thy ro erls f (Rewrite_Inst (subs, thm'')) =
walther@59914
   328
    try_rew thy (ro, Rewrite_Ord.assoc_rew_ord ro) erls (Subst.T_from_input thy subs) f (Rule.Thm thm'')
walther@59914
   329
walther@59914
   330
  | applicable thy _ _ f (Rewrite_Set rls') =
walther@59914
   331
    filter_appl_rews thy [] f (assoc_rls rls')
walther@59914
   332
  | applicable thy _ _ f (Rewrite_Set_Inst (subs, rls')) =
walther@59914
   333
    filter_appl_rews thy (Subst.T_from_input thy subs) f (assoc_rls rls')
walther@59914
   334
  | applicable _ _ _ _ tac = 
walther@59914
   335
    (tracing ("### applicable: not impl. for tac = '" ^ input_to_string tac ^ "'"); []);
walther@59914
   336
walther@59914
   337
walther@59914
   338
(** tactics for internal use **)
walther@59914
   339
walther@59846
   340
  datatype T =
walther@59865
   341
    Add_Find' of TermC.as_string * Model.itm list | Add_Given' of TermC.as_string * Model.itm list 
walther@59924
   342
  | Add_Relation' of TermC.as_string * Model.itm list (* for Step.do_next    *)
walther@59924
   343
  | Apply_Method' of   (* last step in specifu-phse, switch to solve-phase   *)
walther@59924
   344
      Method.id *      (* id in the Know_Store                               *)
walther@59924
   345
      term option *    (* first formula in the (sub-)Problem TODO: rm option *)           
walther@59924
   346
      Istate_Def.T *   (* for starting the Program                           *)
walther@59924
   347
      Proof.context    (* for starting the Program                           *)
walther@59924
   348
(*RM*)| Del_Find' of TermC.as_string | Del_Given' of TermC.as_string | Del_Relation' of TermC.as_string
walther@59924
   349
(*RM*)| Init_Proof' of TermC.as_string list * Spec.T
walther@59924
   350
  | Model_Problem' of  (* first step in specify-phase                        *)
walther@59924
   351
      Problem.id *     (* id in the Know_Store                               *)
walther@59924
   352
      Model.itm list * (* the model for the Problem                          *)
walther@59924
   353
      Model.itm list   (* the model for the method                           *)
walther@59924
   354
  | Refine_Problem' of Problem.id * (Model.itm list * (bool * term) list)
walther@59924
   355
  | Refine_Tacitly' of                                                      
walther@59924
   356
      Problem.id *       (* the original id in the Know_Store                *)
walther@59924
   357
      Problem.id *       (* the id of the refined Problem                    *)
walther@59924
   358
      ThyC.id *          (* the id of the refined theory                     *)
walther@59924
   359
      Method.id *        (* the id of the refined Method                     *)
walther@59924
   360
      Model.itm list     (* RM 9.03: remains [] for Model_Problem recognizing its activation *)
walther@59924
   361
  | Specify_Method' of Method.id * Model.ori list * Model.itm list
walther@59924
   362
  | Specify_Problem' of Problem.id * 
walther@59924
   363
      (bool *                  (* all preconditions evaluate to True         *)
walther@59924
   364
        (Model.itm list *      (* the model checked for the input id         *)
walther@59924
   365
          (bool * term) list)) (* individual preconditions marked true/false *)
walther@59924
   366
  | Specify_Theory' of ThyC.id
walther@59924
   367
  (* ^^^^^--------------------- for specify-phase, for solve-phase ---------------------vvvvv*)
walther@59846
   368
  | CAScmd' of term
walther@59879
   369
  | Calculate' of ThyC.id * string * term * (term * ThmC.T)
walther@59924
   370
  | Check_Postcond' of   (* last step in solving a (sub-)Problem             *)
walther@59924
   371
      Problem.id *       (* id of the Problem to be checked                  *)
walther@59924
   372
      term               (* return value of the program                      *)
walther@59924
   373
  | Check_elementwise' of(* DEPRECATED, made idle for Calc.T in df00a2b5c4cc *)
walther@59924
   374
      term *             (* the current formula: [x=1,x=...]                 *)
walther@59924
   375
      string *           (* the pred from Check_elementwise                  *)
walther@59924
   376
      Selem.result       (* composed from (1) and (2): {x. pred}             *)
walther@59924
   377
(*RM*)| Derive' of Rule_Set.T
walther@59846
   378
  | Empty_Tac_
walther@59846
   379
  | Free_Solve'
walther@59924
   380
(*RM* )| Apply_Assumption of TermC.as_string list( *RM*)
walther@59846
   381
  | Or_to_List' of term * term
walther@59879
   382
  | Rewrite' of ThyC.id * Rewrite_Ord.rew_ord' * Rule_Set.T * bool * ThmC.T * term * Selem.result
walther@59910
   383
  | Rewrite_Inst' of ThyC.id * Rewrite_Ord.rew_ord' * Rule_Set.T * bool * subst * ThmC.T * term * Selem.result
walther@59879
   384
  | Rewrite_Set' of ThyC.id * bool * Rule_Set.T * term * Selem.result
walther@59910
   385
  | Rewrite_Set_Inst' of ThyC.id * bool * subst * Rule_Set.T * term * Selem.result
walther@59924
   386
  | Subproblem' of       (* switch from solve-phase to specify-phase         *)
walther@59924
   387
      Spec.T *           (* specification of the SubProblem                  *)
walther@59924
   388
  		(Model.ori list) * (* original model, filled in associate Subproblem'  *)
walther@59924
   389
  		term *             (* headline of calc-head, filled -"-                *)
walther@59924
   390
  		Selem.fmz_ *       (* string list from arguments of the calling Program*)
walther@59924
   391
      Proof.context *    (* for the specify-phase                            *)
walther@59924
   392
  		term               (* Subproblem (thyID, pbl) OR CAS_Cmd               *)  
walther@59924
   393
  | Substitute' of       (* substitute variables (TODO: from the context)    *)    
walther@59924
   394
      Rule_Def.rew_ord_ *(* for re-calculation                               *)
walther@59924
   395
      Rule_Set.T *       (* for re-calculation                               *)
walther@59924
   396
      Subst.as_eqs *     (* the substitution: terms of type bool             *)
walther@59924
   397
      term *             (* to be substituted into                           *)
walther@59924
   398
      term               (* resulting from the substitution                  *)
walther@59924
   399
(*RM*)| Tac_ of theory * string * string * string
walther@59846
   400
  | Take' of term
walther@59924
   401
(*RM*)| Begin_Sequ' | Begin_Trans' of term
walther@59924
   402
(*RM*)| Split_And' of term | Split_Or' of term | Split_Intersect' of term
walther@59924
   403
(*RM*)| Conclude_And' of term | Conclude_Or' of term | Collect_Trues' of term
walther@59924
   404
(*RM*)| End_Sequ' | End_Trans' of Selem.result
walther@59924
   405
(*RM*)| End_Ruleset' of term | End_Intersect' of term | End_Proof''
walther@59924
   406
(*RM*)| Detail_Set' of ThyC.id * bool * Rule_Set.T * term * Selem.result
walther@59924
   407
(*RM*)| Detail_Set_Inst' of ThyC.id * bool * subst * Rule_Set.T * term * Selem.result
walther@59924
   408
(*RM*)| End_Detail' of Selem.result;
walther@59846
   409
walther@59846
   410
fun string_of ma = case ma of
walther@59902
   411
    Init_Proof' (ppc, spec)  => "Init_Proof' " ^ pair2str (strs2str ppc, Spec.to_string spec)
walther@59846
   412
  | Model_Problem' (pblID, _, _) => "Model_Problem' " ^ strs2str pblID
walther@59846
   413
  | Refine_Tacitly'(p, prefin, domID, metID, _) => "Refine_Tacitly' (" ^ strs2str p ^ ", " ^
walther@59846
   414
    strs2str prefin ^ ", " ^ domID ^ ", " ^ strs2str metID ^ ", pbl-itms)"
walther@59846
   415
  | Refine_Problem' _ => "Refine_Problem' (" ^ (*matchs2str ms*)"..." ^ ")"
walther@59846
   416
  | Add_Given' _ => "Add_Given' "(*^cterm'*)
walther@59846
   417
  | Del_Given' _ => "Del_Given' "(*^cterm'*)
walther@59846
   418
  | Add_Find' _ => "Add_Find' "(*^cterm'*)
walther@59846
   419
  | Del_Find' _ => "Del_Find' "(*^cterm'*)
walther@59846
   420
  | Add_Relation' _ => "Add_Relation' "(*^cterm'*)
walther@59846
   421
  | Del_Relation' _ => "Del_Relation' "(*^cterm'*)
walther@59846
   422
walther@59846
   423
  | Specify_Theory' domID => "Specify_Theory' " ^ quote domID
walther@59846
   424
  | Specify_Problem' (pI, (ok, _)) =>  "Specify_Problem' " ^ 
walther@59846
   425
    spair2str (strs2str pI, spair2str (bool2str ok, spair2str ("itms2str_ itms", "items2str pre")))
walther@59846
   426
  | Specify_Method' (pI, oris, _) => "Specify_Method' (" ^ 
walther@59903
   427
    Method.id_to_string pI ^ ", " ^ Model.oris2str oris ^ ", )"
walther@59846
   428
walther@59846
   429
  | Apply_Method' (metID, _, _, _) => "Apply_Method' " ^ strs2str metID
walther@59846
   430
  | Check_Postcond' (pblID, scval) => "Check_Postcond' " ^
walther@59868
   431
      (spair2str (strs2str pblID, UnparseC.term scval))
walther@59846
   432
walther@59846
   433
  | Free_Solve' => "Free_Solve'"
walther@59846
   434
walther@59846
   435
  | Rewrite_Inst' (*subs,thm'*) _ => "Rewrite_Inst' "(*^(pair2str (subs2str subs, spair2str thm'))*)
walther@59846
   436
  | Rewrite' _(*thm'*) => "Rewrite' "(*^(spair2str thm')*)
walther@59846
   437
  | Rewrite_Set_Inst' _(*subs,thm'*) => "Rewrite_Set_Inst' "(*^(pair2str (subs2str subs, quote rls))*)
walther@59846
   438
  | Rewrite_Set' (thy', pasm, rls', f, (f', asm)) => "Rewrite_Set' (" ^ thy' ^ "," ^ bool2str pasm ^
walther@59868
   439
    "," ^ Rule_Set.id rls' ^ "," ^ UnparseC.term f ^ ",(" ^ UnparseC.term f' ^ "," ^ UnparseC.terms asm ^ "))"
walther@59846
   440
  | End_Detail' _ => "End_Detail' xxx"
walther@59846
   441
  | Detail_Set' _ => "Detail_Set' xxx"
walther@59846
   442
  | Detail_Set_Inst' _ => "Detail_Set_Inst' xxx"
walther@59846
   443
walther@59867
   444
  | Derive' rls => "Derive' " ^ Rule_Set.id rls
walther@59846
   445
  | Calculate'  _ => "Calculate' "
walther@59846
   446
  | Substitute' _ => "Substitute' "(*^(subs2str subs)*)    
walther@59846
   447
walther@59846
   448
  | Take' _(*cterm'*) => "Take' "(*^(quote cterm'	)*)
walther@59846
   449
  | Subproblem' _(*(spec, oris, _, _, _, pbl_form)*) => 
walther@59846
   450
    "Subproblem' "(*^(pair2str (domID, strs2str ,))*)
walther@59846
   451
  | CAScmd' _(*cterm'*) => "CAScmd' "(*^(quote cterm')*)
walther@59846
   452
walther@59846
   453
  | Empty_Tac_ => "Empty_Tac_"
walther@59846
   454
  | Tac_ (_, form, id, result) => "Tac_ (thy," ^ form ^ "," ^ id ^ "," ^ result ^ ")"
walther@59846
   455
  | _  => "string_of not impl. for arg";
wneuper@59302
   456
walther@59704
   457
fun input_from_T (Refine_Tacitly' (pI, _, _, _, _)) = Refine_Tacitly pI
walther@59704
   458
  | input_from_T (Model_Problem' (_, _, _)) = Model_Problem
walther@59704
   459
  | input_from_T (Add_Given' (t, _)) = Add_Given t
walther@59704
   460
  | input_from_T (Add_Find' (t, _)) = Add_Find t
walther@59704
   461
  | input_from_T (Add_Relation' (t, _)) = Add_Relation t
walther@59704
   462
 
walther@59704
   463
  | input_from_T (Specify_Theory' dI) = Specify_Theory dI
walther@59704
   464
  | input_from_T (Specify_Problem' (dI, _)) = Specify_Problem dI
walther@59704
   465
  | input_from_T (Specify_Method' (dI, _, _)) = Specify_Method dI
walther@59704
   466
  
walther@59704
   467
  | input_from_T (Rewrite' (_, _, _, _, thm, _, _)) = Rewrite thm
walther@59911
   468
  | input_from_T (Rewrite_Inst' (_, _, _, _, sub, thm, _, _)) = Rewrite_Inst (Subst.T_to_input sub, thm)
walther@59704
   469
walther@59867
   470
  | input_from_T (Rewrite_Set' (_, _, rls, _, _)) = Rewrite_Set (Rule_Set.id rls)
walther@59867
   471
  | input_from_T (Detail_Set' (_, _, rls, _, _)) = Detail_Set (Rule_Set.id rls)
walther@59704
   472
walther@59704
   473
  | input_from_T (Rewrite_Set_Inst' (_, _, sub, rls, _, _)) = 
walther@59911
   474
    Rewrite_Set_Inst (Subst.T_to_input sub, Rule_Set.id rls)
walther@59704
   475
  | input_from_T (Detail_Set_Inst' (_, _, sub, rls, _, _)) = 
walther@59911
   476
    Detail_Set_Inst (Subst.T_to_input sub, Rule_Set.id rls)
walther@59704
   477
walther@59704
   478
  | input_from_T (Calculate' (_, op_, _, _)) = Calculate (op_)
walther@59704
   479
  | input_from_T (Check_elementwise' (_, pred, _)) = Check_elementwise pred
walther@59704
   480
walther@59704
   481
  | input_from_T (Or_to_List' _) = Or_to_List
walther@59868
   482
  | input_from_T (Take' term) = Take (UnparseC.term term)
walther@59912
   483
  | input_from_T (Substitute' (_, _, subte, _, _)) = Substitute (Subst.eqs_to_input subte) 
walther@59704
   484
  | input_from_T (Tac_ (_, _, id, _)) = Tac id
walther@59704
   485
walther@59704
   486
  | input_from_T (Subproblem' ((domID, pblID, _), _, _, _,_ ,_)) = Subproblem (domID, pblID)
walther@59704
   487
  | input_from_T (Check_Postcond' (pblID, _)) = Check_Postcond pblID
walther@59704
   488
  | input_from_T Empty_Tac_ = Empty_Tac
walther@59846
   489
  | input_from_T m = raise ERROR (": not impl. for "^(string_of m));
walther@59704
   490
walther@59728
   491
fun res (Rewrite_Inst' (_ , _, _, _, _, _, _, res)) = res
walther@59728
   492
  | res (Rewrite' (_, _, _, _, _, _, res)) = res
walther@59728
   493
  | res (Rewrite_Set_Inst' (_, _, _, _, _, res)) = res
walther@59728
   494
  | res (Rewrite_Set' (_, _, _, _, res)) = res
walther@59728
   495
  | res (Calculate' (_, _, _, (t, _))) = (t, [])
walther@59728
   496
  | res (Check_elementwise' (_, _, res)) = res
walther@59728
   497
  | res (Subproblem' (_, _, _, _, _, t)) = (t, [])
walther@59728
   498
  | res (Take' t) = (t, [])
walther@59728
   499
  | res (Substitute' (_, _, _, _, t)) = (t, [])
walther@59728
   500
  | res (Or_to_List' (_,  t)) = (t, [])
walther@59846
   501
  | res m = raise ERROR ("result: not impl.for " ^ string_of m)
walther@59728
   502
walther@59728
   503
(*fun result m = (fst o res) m; TODO*)
walther@59728
   504
fun result tac = (fst o res) tac;
walther@59728
   505
fun creates_assms tac = (snd o res) tac;
walther@59728
   506
walther@59728
   507
fun insert_assumptions tac ctxt  = ContextC.insert_assumptions (creates_assms tac) ctxt
walther@59728
   508
walther@59749
   509
fun for_specify (Init_Proof _) = true
walther@59749
   510
  | for_specify Model_Problem  = true
walther@59749
   511
  | for_specify (Refine_Tacitly _) = true
walther@59749
   512
  | for_specify (Refine_Problem _) = true
walther@59749
   513
  | for_specify (Add_Given _) = true
walther@59749
   514
  | for_specify (Del_Given _) = true
walther@59749
   515
  | for_specify (Add_Find _) = true
walther@59749
   516
  | for_specify (Del_Find _) = true
walther@59749
   517
  | for_specify (Add_Relation _) = true
walther@59749
   518
  | for_specify (Del_Relation _) = true
walther@59749
   519
  | for_specify (Specify_Theory _) = true
walther@59749
   520
  | for_specify (Specify_Problem _) = true
walther@59749
   521
  | for_specify (Specify_Method _) = true
walther@59749
   522
  | for_specify _ = false
walther@59749
   523
fun for_specify' (Init_Proof' _) = true
walther@59749
   524
  | for_specify' (Model_Problem' _) = true
walther@59749
   525
  | for_specify' (Refine_Tacitly' _) = true
walther@59749
   526
  | for_specify' (Refine_Problem' _) = true
walther@59749
   527
  | for_specify' (Add_Given' _) = true
walther@59749
   528
  | for_specify' (Del_Given' _) = true
walther@59749
   529
  | for_specify' (Add_Find' _) = true
walther@59749
   530
  | for_specify' (Del_Find' _) = true
walther@59749
   531
  | for_specify' (Add_Relation' _) = true
walther@59749
   532
  | for_specify' (Del_Relation' _) = true
walther@59749
   533
  | for_specify' (Specify_Theory' _) = true
walther@59749
   534
  | for_specify' (Specify_Problem' _) = true
walther@59749
   535
  | for_specify' (Specify_Method' _) = true
walther@59749
   536
  | for_specify' _ = false
walther@59749
   537
walther@59728
   538
(**)end(**)