src/Pure/Isar/method.ML
author wenzelm
Thu Jun 09 12:03:34 2005 +0200 (2005-06-09)
changeset 16347 9b3265182607
parent 16145 1bb17485602f
child 16448 6c45c5416b79
permissions -rw-r--r--
meths: NameSpace.table;
wenzelm@5824
     1
(*  Title:      Pure/Isar/method.ML
wenzelm@5824
     2
    ID:         $Id$
wenzelm@5824
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5824
     4
wenzelm@5824
     5
Proof methods.
wenzelm@5824
     6
*)
wenzelm@5824
     7
wenzelm@5824
     8
signature BASIC_METHOD =
wenzelm@5824
     9
sig
wenzelm@11731
    10
  val trace_rules: bool ref
wenzelm@5824
    11
  val print_methods: theory -> unit
wenzelm@5824
    12
  val Method: bstring -> (Args.src -> Proof.context -> Proof.method) -> string -> unit
wenzelm@5824
    13
end;
wenzelm@5824
    14
wenzelm@5824
    15
signature METHOD =
wenzelm@5824
    16
sig
wenzelm@5824
    17
  include BASIC_METHOD
wenzelm@15703
    18
  type src
wenzelm@12055
    19
  val trace: Proof.context -> thm list -> unit
wenzelm@12144
    20
  val RAW_METHOD: (thm list -> tactic) -> Proof.method
wenzelm@12144
    21
  val RAW_METHOD_CASES:
wenzelm@16145
    22
    (thm list -> thm -> (thm * (string * RuleCases.T option) list) Seq.seq) -> Proof.method
wenzelm@6091
    23
  val METHOD: (thm list -> tactic) -> Proof.method
wenzelm@8372
    24
  val METHOD_CASES:
wenzelm@16145
    25
    (thm list -> thm -> (thm * (string * RuleCases.T option) list) Seq.seq) -> Proof.method
wenzelm@9706
    26
  val SIMPLE_METHOD: tactic -> Proof.method
wenzelm@9706
    27
  val SIMPLE_METHOD': ((int -> tactic) -> tactic) -> (int -> tactic) -> Proof.method
wenzelm@5824
    28
  val fail: Proof.method
wenzelm@5824
    29
  val succeed: Proof.method
wenzelm@8167
    30
  val defer: int option -> Proof.method
wenzelm@8167
    31
  val prefer: int -> Proof.method
wenzelm@7419
    32
  val insert_tac: thm list -> int -> tactic
wenzelm@7574
    33
  val insert: thm list -> Proof.method
wenzelm@7555
    34
  val insert_facts: Proof.method
wenzelm@7601
    35
  val unfold: thm list -> Proof.method
wenzelm@7419
    36
  val fold: thm list -> Proof.method
wenzelm@7419
    37
  val multi_resolve: thm list -> thm -> thm Seq.seq
wenzelm@7419
    38
  val multi_resolves: thm list -> thm list -> thm Seq.seq
wenzelm@12347
    39
  val rules_tac: Proof.context -> int option -> int -> tactic
wenzelm@6091
    40
  val rule_tac: thm list -> thm list -> int -> tactic
wenzelm@10309
    41
  val some_rule_tac: thm list -> Proof.context -> thm list -> int -> tactic
wenzelm@6091
    42
  val rule: thm list -> Proof.method
wenzelm@10744
    43
  val erule: int -> thm list -> Proof.method
wenzelm@10744
    44
  val drule: int -> thm list -> Proof.method
wenzelm@10744
    45
  val frule: int -> thm list -> Proof.method
wenzelm@8195
    46
  val this: Proof.method
wenzelm@7555
    47
  val assumption: Proof.context -> Proof.method
ballarin@14174
    48
  val bires_inst_tac: bool -> Proof.context -> (indexname * string) list -> thm -> int -> tactic
wenzelm@8351
    49
  val set_tactic: (Proof.context -> thm list -> tactic) -> unit
wenzelm@8351
    50
  val tactic: string -> Proof.context -> Proof.method
wenzelm@5916
    51
  exception METHOD_FAIL of (string * Position.T) * exn
wenzelm@15703
    52
  val method: theory -> src -> Proof.context -> Proof.method
wenzelm@15703
    53
  val add_method: bstring * (src -> Proof.context -> Proof.method) * string
wenzelm@9539
    54
    -> theory -> theory
wenzelm@15703
    55
  val add_methods: (bstring * (src -> Proof.context -> Proof.method) * string) list
wenzelm@5824
    56
    -> theory -> theory
wenzelm@5884
    57
  val syntax: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
wenzelm@15703
    58
    src -> Proof.context -> Proof.context * 'a
wenzelm@8351
    59
  val simple_args: (Args.T list -> 'a * Args.T list)
wenzelm@15703
    60
    -> ('a -> Proof.context -> Proof.method) -> src -> Proof.context -> Proof.method
wenzelm@15703
    61
  val ctxt_args: (Proof.context -> Proof.method) -> src -> Proof.context -> Proof.method
wenzelm@15703
    62
  val no_args: Proof.method -> src -> Proof.context -> Proof.method
wenzelm@7268
    63
  type modifier
wenzelm@7601
    64
  val sectioned_args: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
wenzelm@7268
    65
    (Args.T list -> modifier * Args.T list) list ->
wenzelm@15703
    66
    ('a -> Proof.context -> 'b) -> src -> Proof.context -> 'b
wenzelm@7601
    67
  val bang_sectioned_args:
wenzelm@7601
    68
    (Args.T list -> modifier * Args.T list) list ->
wenzelm@15703
    69
    (thm list -> Proof.context -> 'a) -> src -> Proof.context -> 'a
wenzelm@9777
    70
  val bang_sectioned_args':
wenzelm@9777
    71
    (Args.T list -> modifier * Args.T list) list ->
wenzelm@9777
    72
    (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
wenzelm@15703
    73
    ('a -> thm list -> Proof.context -> 'b) -> src -> Proof.context -> 'b
wenzelm@7601
    74
  val only_sectioned_args:
wenzelm@7601
    75
    (Args.T list -> modifier * Args.T list) list ->
wenzelm@15703
    76
    (Proof.context -> 'a) -> src -> Proof.context -> 'a
wenzelm@15703
    77
  val thms_ctxt_args: (thm list -> Proof.context -> 'a) -> src -> Proof.context -> 'a
wenzelm@15703
    78
  val thms_args: (thm list -> 'a) -> src -> Proof.context -> 'a
wenzelm@15703
    79
  val thm_args: (thm -> 'a) -> src -> Proof.context -> 'a
wenzelm@5824
    80
  datatype text =
wenzelm@5824
    81
    Basic of (Proof.context -> Proof.method) |
wenzelm@15703
    82
    Source of src |
wenzelm@5824
    83
    Then of text list |
wenzelm@5824
    84
    Orelse of text list |
wenzelm@5824
    85
    Try of text |
wenzelm@5824
    86
    Repeat1 of text
wenzelm@5824
    87
  val refine: text -> Proof.state -> Proof.state Seq.seq
wenzelm@8238
    88
  val refine_end: text -> Proof.state -> Proof.state Seq.seq
wenzelm@5824
    89
  val proof: text option -> Proof.state -> Proof.state Seq.seq
wenzelm@8966
    90
  val local_qed: bool -> text option
wenzelm@12144
    91
    -> (Proof.context -> string * (string * thm list) list -> unit) *
wenzelm@12055
    92
      (Proof.context -> thm -> unit) -> Proof.state -> Proof.state Seq.seq
wenzelm@6981
    93
  val local_terminal_proof: text * text option
wenzelm@12144
    94
    -> (Proof.context -> string * (string * thm list) list -> unit) *
wenzelm@12055
    95
      (Proof.context -> thm -> unit) -> Proof.state -> Proof.state Seq.seq
wenzelm@12144
    96
  val local_default_proof: (Proof.context -> string * (string * thm list) list -> unit) *
wenzelm@12055
    97
    (Proof.context -> thm -> unit) -> Proof.state -> Proof.state Seq.seq
wenzelm@12144
    98
  val local_immediate_proof: (Proof.context -> string * (string * thm list) list -> unit) *
wenzelm@12055
    99
    (Proof.context -> thm -> unit) -> Proof.state -> Proof.state Seq.seq
wenzelm@12144
   100
  val local_done_proof: (Proof.context -> string * (string * thm list) list -> unit) *
wenzelm@12055
   101
    (Proof.context -> thm -> unit) -> Proof.state -> Proof.state Seq.seq
wenzelm@8966
   102
  val global_qed: bool -> text option
wenzelm@12244
   103
    -> Proof.state -> theory * ((string * string) * (string * thm list) list)
wenzelm@6934
   104
  val global_terminal_proof: text * text option
wenzelm@12244
   105
    -> Proof.state -> theory * ((string * string) * (string * thm list) list)
wenzelm@12244
   106
  val global_default_proof: Proof.state -> theory * ((string * string) * (string * thm list) list)
wenzelm@12244
   107
  val global_immediate_proof: Proof.state ->
wenzelm@12244
   108
    theory * ((string * string) * (string * thm list) list)
wenzelm@12244
   109
  val global_done_proof: Proof.state -> theory * ((string * string) * (string * thm list) list)
wenzelm@9539
   110
  val goal_args: (Args.T list -> 'a * Args.T list) -> ('a -> int -> tactic)
wenzelm@15703
   111
    -> src -> Proof.context -> Proof.method
wenzelm@9539
   112
  val goal_args': (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))
wenzelm@15703
   113
    -> ('a -> int -> tactic) -> src -> Proof.context -> Proof.method
ballarin@14174
   114
  val goal_args_ctxt: (Args.T list -> 'a * Args.T list) -> (Proof.context -> 'a -> int -> tactic)
wenzelm@15703
   115
    -> src -> Proof.context -> Proof.method
ballarin@14174
   116
  val goal_args_ctxt': (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))
wenzelm@15703
   117
    -> (Proof.context -> 'a -> int -> tactic) -> src -> Proof.context -> Proof.method
wenzelm@5824
   118
end;
wenzelm@5824
   119
wenzelm@5824
   120
structure Method: METHOD =
wenzelm@5824
   121
struct
wenzelm@5824
   122
wenzelm@15703
   123
type src = Args.src;
wenzelm@15703
   124
wenzelm@5824
   125
wenzelm@12324
   126
(** proof methods **)
wenzelm@12324
   127
wenzelm@12324
   128
(* tracing *)
wenzelm@11731
   129
wenzelm@11731
   130
val trace_rules = ref false;
wenzelm@11731
   131
wenzelm@12055
   132
fun trace ctxt rules =
wenzelm@12262
   133
  conditional (! trace_rules andalso not (null rules)) (fn () =>
wenzelm@12262
   134
    Pretty.big_list "rules:" (map (ProofContext.pretty_thm ctxt) rules)
wenzelm@12262
   135
    |> Pretty.string_of |> tracing);
wenzelm@11731
   136
wenzelm@11731
   137
wenzelm@8372
   138
(* make methods *)
wenzelm@5824
   139
wenzelm@12144
   140
val RAW_METHOD = Proof.method;
wenzelm@12144
   141
val RAW_METHOD_CASES = Proof.method_cases;
wenzelm@12144
   142
wenzelm@12144
   143
fun METHOD m = Proof.method (fn facts => TRY Tactic.conjunction_tac THEN m facts);
wenzelm@12144
   144
fun METHOD_CASES m =
wenzelm@12144
   145
  Proof.method_cases (fn facts => Seq.THEN (TRY Tactic.conjunction_tac, m facts));
wenzelm@8372
   146
wenzelm@5824
   147
wenzelm@5824
   148
(* primitive *)
wenzelm@5824
   149
wenzelm@5824
   150
val fail = METHOD (K no_tac);
wenzelm@5824
   151
val succeed = METHOD (K all_tac);
wenzelm@5824
   152
wenzelm@5824
   153
wenzelm@8167
   154
(* shuffle *)
wenzelm@8167
   155
wenzelm@8240
   156
fun prefer i = METHOD (K (Tactic.defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1)));
wenzelm@15973
   157
fun defer opt_i = METHOD (K (Tactic.defer_tac (if_none opt_i 1)));
wenzelm@8167
   158
wenzelm@8167
   159
wenzelm@7419
   160
(* insert *)
wenzelm@7419
   161
wenzelm@7419
   162
local
wenzelm@5824
   163
wenzelm@6981
   164
fun cut_rule_tac raw_rule =
wenzelm@6981
   165
  let
wenzelm@6981
   166
    val rule = Drule.forall_intr_vars raw_rule;
wenzelm@6981
   167
    val revcut_rl = Drule.incr_indexes_wrt [] [] [] [rule] Drule.revcut_rl;
wenzelm@7555
   168
  in Tactic.rtac (rule COMP revcut_rl) end;
wenzelm@6981
   169
wenzelm@7419
   170
in
wenzelm@5824
   171
wenzelm@7419
   172
fun insert_tac [] i = all_tac
wenzelm@7419
   173
  | insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts);
wenzelm@6981
   174
wenzelm@7555
   175
val insert_facts = METHOD (ALLGOALS o insert_tac);
wenzelm@7664
   176
fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms));
wenzelm@7419
   177
wenzelm@9706
   178
fun SIMPLE_METHOD tac = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN tac);
wenzelm@9706
   179
fun SIMPLE_METHOD' quant tac = METHOD (fn facts => quant (insert_tac facts THEN' tac));
wenzelm@9706
   180
wenzelm@12324
   181
end;
wenzelm@12324
   182
wenzelm@9706
   183
wenzelm@12384
   184
(* unfold/fold definitions *)
wenzelm@12384
   185
wenzelm@16145
   186
fun unfold_meth ths = SIMPLE_METHOD (CHANGED_PROP (rewrite_goals_tac ths));
wenzelm@16145
   187
fun fold_meth ths = SIMPLE_METHOD (CHANGED_PROP (fold_goals_tac ths));
wenzelm@6532
   188
wenzelm@12384
   189
wenzelm@12829
   190
(* atomize rule statements *)
wenzelm@12829
   191
wenzelm@12829
   192
fun atomize false = SIMPLE_METHOD' HEADGOAL (CHANGED_PROP o ObjectLogic.atomize_tac)
wenzelm@12829
   193
  | atomize true = RAW_METHOD (K (HEADGOAL (CHANGED_PROP o ObjectLogic.full_atomize_tac)));
wenzelm@12829
   194
wenzelm@12829
   195
wenzelm@12384
   196
(* unfold intro/elim rules *)
wenzelm@12384
   197
wenzelm@12384
   198
fun intro ths = SIMPLE_METHOD' HEADGOAL (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.match_tac ths));
wenzelm@12384
   199
fun elim ths = SIMPLE_METHOD' HEADGOAL (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.ematch_tac ths));
wenzelm@9484
   200
wenzelm@9484
   201
wenzelm@7419
   202
(* multi_resolve *)
wenzelm@7419
   203
wenzelm@7419
   204
local
wenzelm@7419
   205
wenzelm@7419
   206
fun res th i rule =
wenzelm@7419
   207
  Thm.biresolution false [(false, th)] i rule handle THM _ => Seq.empty;
wenzelm@7419
   208
wenzelm@7419
   209
fun multi_res _ [] rule = Seq.single rule
wenzelm@7419
   210
  | multi_res i (th :: ths) rule = Seq.flat (Seq.map (res th i) (multi_res (i + 1) ths rule));
wenzelm@7419
   211
wenzelm@7419
   212
in
wenzelm@7419
   213
wenzelm@7419
   214
val multi_resolve = multi_res 1;
wenzelm@8372
   215
fun multi_resolves facts rules = Seq.flat (Seq.map (multi_resolve facts) (Seq.of_list rules));
wenzelm@7419
   216
wenzelm@7419
   217
end;
wenzelm@7419
   218
wenzelm@7419
   219
wenzelm@12347
   220
(* rules_tac *)
wenzelm@8372
   221
wenzelm@7419
   222
local
wenzelm@5824
   223
wenzelm@12359
   224
val remdups_tac = SUBGOAL (fn (g, i) =>
wenzelm@12359
   225
  let val prems = Logic.strip_assums_hyp g in
wenzelm@12359
   226
    REPEAT_DETERM_N (length prems - length (gen_distinct op aconv prems))
wenzelm@12359
   227
    (Tactic.ematch_tac [Drule.remdups_rl] i THEN Tactic.eq_assume_tac i)
wenzelm@12359
   228
  end);
wenzelm@12347
   229
wenzelm@12347
   230
fun REMDUPS tac = tac THEN_ALL_NEW remdups_tac;
wenzelm@12347
   231
wenzelm@12347
   232
fun gen_eq_set e s1 s2 =
wenzelm@12347
   233
  length s1 = length s2 andalso
wenzelm@12347
   234
  gen_subset e (s1, s2) andalso gen_subset e (s2, s1);
wenzelm@12347
   235
wenzelm@12350
   236
val bires_tac = Tactic.biresolution_from_nets_tac ContextRules.orderlist;
wenzelm@12347
   237
wenzelm@12347
   238
fun safe_step_tac ctxt =
wenzelm@12350
   239
  ContextRules.Swrap ctxt
wenzelm@12350
   240
   (eq_assume_tac ORELSE'
wenzelm@12350
   241
    bires_tac true (ContextRules.netpair_bang ctxt));
wenzelm@12347
   242
wenzelm@12347
   243
fun unsafe_step_tac ctxt =
wenzelm@12350
   244
  ContextRules.wrap ctxt
wenzelm@12350
   245
   (assume_tac APPEND'
wenzelm@12350
   246
    bires_tac false (ContextRules.netpair_bang ctxt) APPEND'
wenzelm@12350
   247
    bires_tac false (ContextRules.netpair ctxt));
wenzelm@12347
   248
wenzelm@12347
   249
fun step_tac ctxt i =
wenzelm@12347
   250
  REPEAT_DETERM1 (REMDUPS (safe_step_tac ctxt) i) ORELSE
wenzelm@12347
   251
  REMDUPS (unsafe_step_tac ctxt) i;
wenzelm@12347
   252
wenzelm@12347
   253
fun intpr_tac ctxt gs d lim = SUBGOAL (fn (g, i) => if d > lim then no_tac else
wenzelm@12347
   254
  let
wenzelm@12347
   255
    val ps = Logic.strip_assums_hyp g;
wenzelm@12347
   256
    val c = Logic.strip_assums_concl g;
wenzelm@12347
   257
  in
wenzelm@12347
   258
    if gen_mem (fn ((ps1, c1), (ps2, c2)) =>
wenzelm@12347
   259
      c1 aconv c2 andalso gen_eq_set op aconv ps1 ps2) ((ps, c), gs) then no_tac
wenzelm@12347
   260
    else (step_tac ctxt THEN_ALL_NEW intpr_tac ctxt ((ps, c) :: gs) (d + 1) lim) i
wenzelm@12347
   261
  end);
wenzelm@12347
   262
wenzelm@12347
   263
in
wenzelm@12347
   264
wenzelm@12359
   265
fun rules_tac ctxt opt_lim =
wenzelm@15973
   266
  SELECT_GOAL (DEEPEN (2, if_none opt_lim 20) (intpr_tac ctxt [] 0) 4 1);
wenzelm@12347
   267
wenzelm@12347
   268
end;
wenzelm@12347
   269
wenzelm@12347
   270
wenzelm@12347
   271
(* rule_tac etc. *)
wenzelm@12347
   272
wenzelm@12347
   273
local
wenzelm@12347
   274
wenzelm@10541
   275
fun gen_rule_tac tac rules [] i st = tac rules i st
wenzelm@12324
   276
  | gen_rule_tac tac rules facts i st =
wenzelm@12324
   277
      Seq.flat (Seq.map (fn rule => (tac o single) rule i st) (multi_resolves facts rules));
wenzelm@7130
   278
wenzelm@10744
   279
fun gen_arule_tac tac j rules facts =
wenzelm@10744
   280
  EVERY' (gen_rule_tac tac rules facts :: replicate j Tactic.assume_tac);
wenzelm@10744
   281
wenzelm@11785
   282
fun gen_some_rule_tac tac arg_rules ctxt facts = SUBGOAL (fn (goal, i) =>
wenzelm@11785
   283
  let
wenzelm@11785
   284
    val rules =
wenzelm@11785
   285
      if not (null arg_rules) then arg_rules
skalberg@15570
   286
      else List.concat (ContextRules.find_rules false facts goal ctxt)
wenzelm@12055
   287
  in trace ctxt rules; tac rules facts i end);
wenzelm@10309
   288
wenzelm@10744
   289
fun meth tac x = METHOD (HEADGOAL o tac x);
wenzelm@10744
   290
fun meth' tac x y = METHOD (HEADGOAL o tac x y);
wenzelm@8220
   291
wenzelm@7419
   292
in
wenzelm@7419
   293
wenzelm@10744
   294
val rule_tac = gen_rule_tac Tactic.resolve_tac;
wenzelm@10744
   295
val rule = meth rule_tac;
wenzelm@10744
   296
val some_rule_tac = gen_some_rule_tac rule_tac;
wenzelm@10744
   297
val some_rule = meth' some_rule_tac;
wenzelm@10744
   298
wenzelm@10744
   299
val erule = meth' (gen_arule_tac Tactic.eresolve_tac);
wenzelm@10744
   300
val drule = meth' (gen_arule_tac Tactic.dresolve_tac);
wenzelm@10744
   301
val frule = meth' (gen_arule_tac Tactic.forward_tac);
wenzelm@5824
   302
wenzelm@7419
   303
end;
wenzelm@7419
   304
wenzelm@7419
   305
wenzelm@8195
   306
(* this *)
wenzelm@8195
   307
wenzelm@8671
   308
val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac));
wenzelm@8195
   309
wenzelm@8195
   310
wenzelm@8195
   311
(* assumption *)
wenzelm@7555
   312
wenzelm@10378
   313
fun asm_tac ths =
skalberg@15574
   314
  foldr (op APPEND') (K no_tac) (map (fn th => Tactic.rtac th THEN_ALL_NEW assume_tac) ths);
wenzelm@10378
   315
wenzelm@10405
   316
fun assm_tac ctxt =
wenzelm@10405
   317
  assume_tac APPEND'
wenzelm@10405
   318
  asm_tac (ProofContext.prems_of ctxt) APPEND'
wenzelm@10405
   319
  Tactic.rtac Drule.reflexive_thm;
wenzelm@7419
   320
wenzelm@7555
   321
fun assumption_tac ctxt [] = assm_tac ctxt
wenzelm@10378
   322
  | assumption_tac _ [fact] = asm_tac [fact]
wenzelm@7555
   323
  | assumption_tac _ _ = K no_tac;
wenzelm@7419
   324
wenzelm@8671
   325
fun assumption ctxt = METHOD (HEADGOAL o assumption_tac ctxt);
wenzelm@7419
   326
wenzelm@7419
   327
wenzelm@9539
   328
(* res_inst_tac etc. *)
wenzelm@8238
   329
wenzelm@14718
   330
(*Reimplemented to support both static (Isar) and dynamic (proof state)
wenzelm@14718
   331
  context.  By Clemens Ballarin.*)
wenzelm@12119
   332
ballarin@14174
   333
fun bires_inst_tac bires_flag ctxt insts thm =
ballarin@14174
   334
  let
ballarin@14174
   335
    val sign = ProofContext.sign_of ctxt;
ballarin@14174
   336
    (* Separate type and term insts *)
ballarin@14174
   337
    fun has_type_var ((x, _), _) = (case Symbol.explode x of
ballarin@14174
   338
          "'"::cs => true | cs => false);
skalberg@15570
   339
    val Tinsts = List.filter has_type_var insts;
ballarin@14174
   340
    val tinsts = filter_out has_type_var insts;
ballarin@14174
   341
    (* Tactic *)
ballarin@14174
   342
    fun tac i st =
ballarin@14174
   343
      let
ballarin@14174
   344
        (* Preprocess state: extract environment information:
ballarin@14174
   345
           - variables and their types
ballarin@14174
   346
           - type variables and their sorts
ballarin@14174
   347
           - parameters and their types *)
ballarin@14174
   348
        val (types, sorts) = types_sorts st;
ballarin@14174
   349
    (* Process type insts: Tinsts_env *)
ballarin@14174
   350
    fun absent xi = error
wenzelm@14718
   351
          ("No such variable in theorem: " ^ Syntax.string_of_vname xi);
ballarin@14174
   352
    val (rtypes, rsorts) = types_sorts thm;
ballarin@14174
   353
    fun readT (xi, s) =
skalberg@15531
   354
        let val S = case rsorts xi of SOME S => S | NONE => absent xi;
ballarin@14174
   355
            val T = Sign.read_typ (sign, sorts) s;
berghofe@15798
   356
            val U = TVar (xi, S);
berghofe@15798
   357
        in if Sign.typ_instance sign (T, U) then (U, T)
ballarin@14174
   358
           else error
ballarin@14174
   359
             ("Instantiation of " ^ Syntax.string_of_vname xi ^ " fails")
ballarin@14174
   360
        end;
ballarin@14174
   361
    val Tinsts_env = map readT Tinsts;
ballarin@14174
   362
    (* Preprocess rule: extract vars and their types, apply Tinsts *)
ballarin@14174
   363
    fun get_typ xi =
ballarin@14174
   364
      (case rtypes xi of
berghofe@15798
   365
           SOME T => typ_subst_atomic Tinsts_env T
skalberg@15531
   366
         | NONE => absent xi);
ballarin@14174
   367
    val (xis, ss) = Library.split_list tinsts;
ballarin@14174
   368
    val Ts = map get_typ xis;
wenzelm@14718
   369
        val (_, _, Bi, _) = dest_state(st,i)
wenzelm@14718
   370
        val params = Logic.strip_params Bi
wenzelm@14718
   371
                             (* params of subgoal i as string typ pairs *)
wenzelm@14718
   372
        val params = rev(Term.rename_wrt_term Bi params)
wenzelm@14718
   373
                           (* as they are printed: bound variables with *)
ballarin@14508
   374
                           (* the same name are renamed during printing *)
ballarin@14174
   375
        fun types' (a, ~1) = (case assoc (params, a) of
skalberg@15531
   376
                NONE => types (a, ~1)
ballarin@14174
   377
              | some => some)
ballarin@14174
   378
          | types' xi = types xi;
wenzelm@15973
   379
        fun internal x = is_some (types' (x, ~1));
wenzelm@15703
   380
        val used = Drule.add_used thm (Drule.add_used st []);
wenzelm@14718
   381
        val (ts, envT) =
wenzelm@14718
   382
          ProofContext.read_termTs_schematic ctxt internal types' sorts used (ss ~~ Ts);
berghofe@15798
   383
        val envT' = map (fn (ixn, T) =>
wenzelm@15973
   384
          (TVar (ixn, the (rsorts ixn)), T)) envT @ Tinsts_env;
wenzelm@14718
   385
        val cenv =
wenzelm@14718
   386
          map
wenzelm@14718
   387
            (fn (xi, t) =>
wenzelm@14718
   388
              pairself (Thm.cterm_of sign) (Var (xi, fastype_of t), t))
wenzelm@14718
   389
            (gen_distinct
wenzelm@14718
   390
              (fn ((x1, t1), (x2, t2)) => x1 = x2 andalso t1 aconv t2)
wenzelm@14718
   391
              (xis ~~ ts));
wenzelm@14718
   392
        (* Lift and instantiate rule *)
wenzelm@14718
   393
        val {maxidx, ...} = rep_thm st;
wenzelm@14718
   394
        val paramTs = map #2 params
wenzelm@14718
   395
        and inc = maxidx+1
wenzelm@14718
   396
        fun liftvar (Var ((a,j), T)) =
wenzelm@14718
   397
              Var((a, j+inc), paramTs ---> incr_tvar inc T)
wenzelm@14718
   398
          | liftvar t = raise TERM("Variable expected", [t]);
wenzelm@14718
   399
        fun liftterm t = list_abs_free
wenzelm@14718
   400
              (params, Logic.incr_indexes(paramTs,inc) t)
wenzelm@14718
   401
        fun liftpair (cv,ct) =
wenzelm@14718
   402
              (cterm_fun liftvar cv, cterm_fun liftterm ct)
berghofe@15798
   403
        val lifttvar = pairself (ctyp_of sign o incr_tvar inc);
wenzelm@14718
   404
        val rule = Drule.instantiate
berghofe@15798
   405
              (map lifttvar envT', map liftpair cenv)
wenzelm@14718
   406
              (lift_rule (st, i) thm)
ballarin@14174
   407
      in
wenzelm@14718
   408
        if i > nprems_of st then no_tac st
wenzelm@14718
   409
        else st |>
wenzelm@14718
   410
          compose_tac (bires_flag, rule, nprems_of thm) i
ballarin@14174
   411
      end
wenzelm@14718
   412
           handle TERM (msg,_)   => (warning msg; no_tac st)
wenzelm@14718
   413
                | THM  (msg,_,_) => (warning msg; no_tac st);
ballarin@14174
   414
  in
ballarin@14174
   415
    tac
ballarin@14174
   416
  end;
wenzelm@8238
   417
ballarin@14174
   418
fun gen_inst _ tac _ (quant, ([], thms)) =
ballarin@14174
   419
      METHOD (fn facts => quant (insert_tac facts THEN' tac thms))
ballarin@14174
   420
  | gen_inst inst_tac _ ctxt (quant, (insts, [thm])) =
ballarin@14174
   421
      METHOD (fn facts =>
ballarin@14174
   422
        quant (insert_tac facts THEN' inst_tac ctxt insts thm))
ballarin@14174
   423
  | gen_inst _ _ _ _ = error "Cannot have instantiations with multiple rules";
wenzelm@14718
   424
ballarin@14174
   425
val res_inst_meth = gen_inst (bires_inst_tac false) Tactic.resolve_tac;
ballarin@14174
   426
ballarin@14174
   427
val eres_inst_meth = gen_inst (bires_inst_tac true) Tactic.eresolve_tac;
ballarin@14174
   428
ballarin@14174
   429
(* Preserve Var indexes of rl; increment revcut_rl instead.
ballarin@14174
   430
   Copied from tactic.ML *)
ballarin@14174
   431
fun make_elim_preserve rl =
ballarin@14174
   432
  let val {maxidx,...} = rep_thm rl
ballarin@14174
   433
      fun cvar xi = cterm_of (Theory.sign_of ProtoPure.thy) (Var(xi,propT));
ballarin@14174
   434
      val revcut_rl' =
ballarin@14174
   435
          instantiate ([],  [(cvar("V",0), cvar("V",maxidx+1)),
ballarin@14174
   436
                             (cvar("W",0), cvar("W",maxidx+1))]) revcut_rl
ballarin@14174
   437
      val arg = (false, rl, nprems_of rl)
ballarin@14174
   438
      val [th] = Seq.list_of (bicompose false arg 1 revcut_rl')
ballarin@14174
   439
  in  th  end
ballarin@14174
   440
  handle Bind => raise THM("make_elim_preserve", 1, [rl]);
wenzelm@8238
   441
ballarin@14174
   442
val cut_inst_meth =
ballarin@14174
   443
  gen_inst
ballarin@14174
   444
    (fn ctxt => fn insts => fn thm =>
ballarin@14174
   445
       bires_inst_tac false ctxt insts (make_elim_preserve thm))
ballarin@14174
   446
    Tactic.cut_rules_tac;
ballarin@14174
   447
ballarin@14174
   448
val dres_inst_meth =
ballarin@14174
   449
  gen_inst
ballarin@14174
   450
    (fn ctxt => fn insts => fn rule =>
ballarin@14174
   451
       bires_inst_tac true ctxt insts (make_elim_preserve rule))
ballarin@14174
   452
    Tactic.dresolve_tac;
ballarin@14174
   453
ballarin@14174
   454
val forw_inst_meth =
ballarin@14174
   455
  gen_inst
ballarin@14174
   456
    (fn ctxt => fn insts => fn rule =>
ballarin@14174
   457
       bires_inst_tac false ctxt insts (make_elim_preserve rule) THEN'
ballarin@14174
   458
       assume_tac)
ballarin@14174
   459
    Tactic.forward_tac;
ballarin@14174
   460
ballarin@14174
   461
fun subgoal_tac ctxt sprop =
ballarin@14174
   462
  DETERM o bires_inst_tac false ctxt [(("psi", 0), sprop)] cut_rl THEN'
ballarin@14174
   463
  SUBGOAL (fn (prop, _) =>
ballarin@14174
   464
    let val concl' = Logic.strip_assums_concl prop in
ballarin@14174
   465
      if null (term_tvars concl') then ()
ballarin@14174
   466
      else warning "Type variables in new subgoal: add a type constraint?";
ballarin@14174
   467
      all_tac
ballarin@14174
   468
  end);
ballarin@14174
   469
ballarin@14174
   470
fun subgoals_tac ctxt sprops = EVERY' (map (subgoal_tac ctxt) sprops);
ballarin@14174
   471
ballarin@14174
   472
fun thin_tac ctxt s =
ballarin@14174
   473
  bires_inst_tac true ctxt [(("V", 0), s)] thin_rl;
wenzelm@8238
   474
wenzelm@14718
   475
wenzelm@8329
   476
(* simple Prolog interpreter *)
wenzelm@8329
   477
wenzelm@8329
   478
fun prolog_tac rules facts =
wenzelm@8329
   479
  DEPTH_SOLVE_1 (HEADGOAL (Tactic.assume_tac APPEND' Tactic.resolve_tac (facts @ rules)));
wenzelm@8329
   480
wenzelm@8329
   481
val prolog = METHOD o prolog_tac;
wenzelm@8329
   482
wenzelm@8329
   483
wenzelm@8351
   484
(* ML tactics *)
wenzelm@8351
   485
wenzelm@8351
   486
val tactic_ref = ref ((fn _ => raise Match): Proof.context -> thm list -> tactic);
wenzelm@8351
   487
fun set_tactic f = tactic_ref := f;
wenzelm@8351
   488
wenzelm@8351
   489
fun tactic txt ctxt = METHOD (fn facts =>
wenzelm@9631
   490
  (Context.use_mltext
wenzelm@15829
   491
    ("let fun tactic (ctxt: Proof.context) (facts: thm list) : tactic = \
wenzelm@15829
   492
     \let val thm = ProofContext.get_thm_closure ctxt o rpair NONE\n\
wenzelm@15829
   493
     \  and thms = ProofContext.get_thms_closure ctxt o rpair NONE in\n"
wenzelm@9631
   494
     ^ txt ^
wenzelm@15829
   495
     "\nend in Method.set_tactic tactic end")
skalberg@15531
   496
    false NONE;
skalberg@15531
   497
    Context.setmp (SOME (ProofContext.theory_of ctxt)) (! tactic_ref ctxt) facts));
wenzelm@8351
   498
wenzelm@8351
   499
wenzelm@5824
   500
wenzelm@5824
   501
(** methods theory data **)
wenzelm@5824
   502
wenzelm@5824
   503
(* data kind 'Isar/methods' *)
wenzelm@5824
   504
wenzelm@5824
   505
structure MethodsDataArgs =
wenzelm@5824
   506
struct
wenzelm@5824
   507
  val name = "Isar/methods";
wenzelm@16347
   508
  type T = (((src -> Proof.context -> Proof.method) * string) * stamp) NameSpace.table;
wenzelm@5824
   509
wenzelm@16347
   510
  val empty = NameSpace.empty_table;
wenzelm@6546
   511
  val copy = I;
wenzelm@5824
   512
  val prep_ext = I;
wenzelm@16347
   513
  fun merge tables = NameSpace.merge_tables eq_snd tables handle Symtab.DUPS dups =>
wenzelm@16347
   514
    error ("Attempt to merge different versions of method(s) " ^ commas_quote dups);
wenzelm@5824
   515
wenzelm@16347
   516
  fun print _ meths =
wenzelm@5824
   517
    let
wenzelm@5824
   518
      fun prt_meth (name, ((_, comment), _)) = Pretty.block
wenzelm@6849
   519
        [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@5824
   520
    in
wenzelm@16347
   521
      [Pretty.big_list "methods:" (map prt_meth (NameSpace.extern_table meths))]
wenzelm@9222
   522
      |> Pretty.chunks |> Pretty.writeln
wenzelm@5824
   523
    end;
wenzelm@5824
   524
end;
wenzelm@5824
   525
wenzelm@5824
   526
structure MethodsData = TheoryDataFun(MethodsDataArgs);
wenzelm@15801
   527
val _ = Context.add_setup [MethodsData.init];
wenzelm@5824
   528
val print_methods = MethodsData.print;
wenzelm@7611
   529
wenzelm@5824
   530
wenzelm@5824
   531
(* get methods *)
wenzelm@5824
   532
wenzelm@5916
   533
exception METHOD_FAIL of (string * Position.T) * exn;
wenzelm@5916
   534
wenzelm@5824
   535
fun method thy =
wenzelm@5824
   536
  let
wenzelm@16347
   537
    val (space, meths) = MethodsData.get thy;
wenzelm@5884
   538
    fun meth src =
wenzelm@5884
   539
      let
wenzelm@5884
   540
        val ((raw_name, _), pos) = Args.dest_src src;
wenzelm@5884
   541
        val name = NameSpace.intern space raw_name;
wenzelm@5884
   542
      in
wenzelm@5824
   543
        (case Symtab.lookup (meths, name) of
skalberg@15531
   544
          NONE => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
skalberg@15531
   545
        | SOME ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src))
wenzelm@5824
   546
      end;
wenzelm@5824
   547
  in meth end;
wenzelm@5824
   548
wenzelm@5824
   549
wenzelm@9194
   550
(* add_method(s) *)
wenzelm@5824
   551
wenzelm@5824
   552
fun add_methods raw_meths thy =
wenzelm@5824
   553
  let
wenzelm@16145
   554
    val sg = Theory.sign_of thy;
wenzelm@16145
   555
    val new_meths = raw_meths |> map (fn (name, f, comment) =>
wenzelm@16347
   556
      (name, ((f, comment), stamp ())));
wenzelm@5824
   557
wenzelm@16347
   558
    fun add meths = NameSpace.extend_table (Sign.naming_of sg) (meths, new_meths)
wenzelm@16347
   559
      handle Symtab.DUPS dups =>
wenzelm@16347
   560
        error ("Duplicate declaration of method(s) " ^ commas_quote dups);
wenzelm@16347
   561
  in MethodsData.map add thy end;
wenzelm@5824
   562
wenzelm@9194
   563
val add_method = add_methods o Library.single;
wenzelm@9194
   564
wenzelm@5824
   565
(*implicit version*)
wenzelm@5824
   566
fun Method name meth cmt = Context.>> (add_methods [(name, meth, cmt)]);
wenzelm@5824
   567
wenzelm@5824
   568
wenzelm@5884
   569
wenzelm@5884
   570
(** method syntax **)
wenzelm@5824
   571
wenzelm@5884
   572
(* basic *)
wenzelm@5884
   573
wenzelm@5884
   574
fun syntax (scan: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))) =
wenzelm@5884
   575
  Args.syntax "method" scan;
wenzelm@5824
   576
wenzelm@8351
   577
fun simple_args scan f src ctxt : Proof.method =
wenzelm@8351
   578
  #2 (syntax (Scan.lift (scan >> (fn x => f x ctxt))) src ctxt);
wenzelm@8351
   579
wenzelm@7555
   580
fun ctxt_args (f: Proof.context -> Proof.method) src ctxt =
wenzelm@8282
   581
  #2 (syntax (Scan.succeed (f ctxt)) src ctxt);
wenzelm@7555
   582
wenzelm@7555
   583
fun no_args m = ctxt_args (K m);
wenzelm@5884
   584
wenzelm@5884
   585
wenzelm@5884
   586
(* sections *)
wenzelm@5824
   587
wenzelm@7268
   588
type modifier = (Proof.context -> Proof.context) * Proof.context attribute;
wenzelm@7268
   589
wenzelm@7268
   590
local
wenzelm@7268
   591
wenzelm@8381
   592
fun sect ss = Scan.first (map Scan.lift ss);
wenzelm@5884
   593
fun thms ss = Scan.unless (sect ss) Attrib.local_thms;
skalberg@15570
   594
fun thmss ss = Scan.repeat (thms ss) >> List.concat;
wenzelm@5884
   595
wenzelm@7268
   596
fun apply (f, att) (ctxt, ths) = Thm.applys_attributes ((f ctxt, ths), [att]);
wenzelm@5824
   597
wenzelm@7268
   598
fun section ss = (sect ss -- thmss ss) :-- (fn (m, ths) => Scan.depend (fn ctxt =>
wenzelm@7268
   599
  Scan.succeed (apply m (ctxt, ths)))) >> #2;
wenzelm@5884
   600
wenzelm@7601
   601
fun sectioned args ss = args -- Scan.repeat (section ss);
wenzelm@5884
   602
wenzelm@7268
   603
in
wenzelm@5824
   604
wenzelm@5884
   605
fun sectioned_args args ss f src ctxt =
wenzelm@8282
   606
  let val (ctxt', (x, _)) = syntax (sectioned args ss) src ctxt
wenzelm@5921
   607
  in f x ctxt' end;
wenzelm@5884
   608
wenzelm@7601
   609
fun bang_sectioned_args ss f = sectioned_args Args.bang_facts ss f;
wenzelm@9777
   610
fun bang_sectioned_args' ss scan f =
wenzelm@9777
   611
  sectioned_args (Args.bang_facts -- scan >> swap) ss (uncurry f);
wenzelm@7601
   612
fun only_sectioned_args ss f = sectioned_args (Scan.succeed ()) ss (fn () => f);
wenzelm@7268
   613
wenzelm@8093
   614
fun thms_ctxt_args f = sectioned_args (thmss []) [] f;
wenzelm@8093
   615
fun thms_args f = thms_ctxt_args (K o f);
wenzelm@9706
   616
fun thm_args f = thms_args (fn [thm] => f thm | _ => error "Single theorem expected");
wenzelm@5824
   617
wenzelm@7268
   618
end;
wenzelm@7268
   619
wenzelm@5824
   620
wenzelm@12347
   621
(* rules syntax *)
wenzelm@12347
   622
wenzelm@12347
   623
local
wenzelm@12347
   624
wenzelm@12347
   625
val introN = "intro";
wenzelm@12347
   626
val elimN = "elim";
wenzelm@12347
   627
val destN = "dest";
wenzelm@12347
   628
val ruleN = "rule";
wenzelm@12347
   629
wenzelm@12347
   630
fun modifier name kind kind' att =
skalberg@15531
   631
  Args.$$$ name |-- (kind >> K NONE || kind' |-- Args.nat --| Args.colon >> SOME)
wenzelm@12347
   632
    >> (pair (I: Proof.context -> Proof.context) o att);
wenzelm@12347
   633
wenzelm@12347
   634
val rules_modifiers =
wenzelm@12384
   635
 [modifier destN Args.bang_colon Args.bang ContextRules.dest_bang_local,
wenzelm@12350
   636
  modifier destN Args.colon (Scan.succeed ()) ContextRules.dest_local,
wenzelm@12350
   637
  modifier elimN Args.bang_colon Args.bang ContextRules.elim_bang_local,
wenzelm@12350
   638
  modifier elimN Args.colon (Scan.succeed ()) ContextRules.elim_local,
wenzelm@12350
   639
  modifier introN Args.bang_colon Args.bang ContextRules.intro_bang_local,
wenzelm@12350
   640
  modifier introN Args.colon (Scan.succeed ()) ContextRules.intro_local,
wenzelm@12350
   641
  Args.del -- Args.colon >> K (I, ContextRules.rule_del_local)];
wenzelm@12347
   642
wenzelm@12347
   643
in
wenzelm@12347
   644
wenzelm@12347
   645
fun rules_args m = bang_sectioned_args' rules_modifiers (Scan.lift (Scan.option Args.nat)) m;
wenzelm@12347
   646
wenzelm@12347
   647
fun rules_meth n prems ctxt = METHOD (fn facts =>
wenzelm@12350
   648
  HEADGOAL (insert_tac (prems @ facts) THEN' ObjectLogic.atomize_tac THEN' rules_tac ctxt n));
wenzelm@12347
   649
wenzelm@12347
   650
end;
wenzelm@12347
   651
wenzelm@12347
   652
wenzelm@9539
   653
(* tactic syntax *)
wenzelm@8238
   654
wenzelm@10744
   655
fun nat_thms_args f = uncurry f oo
wenzelm@10744
   656
  (#2 oo syntax (Scan.lift (Scan.optional (Args.parens Args.nat) 0) -- Attrib.local_thmss));
wenzelm@10744
   657
wenzelm@8238
   658
val insts =
wenzelm@9539
   659
  Scan.optional
wenzelm@9565
   660
    (Args.enum1 "and" (Scan.lift (Args.name -- (Args.$$$ "=" |-- Args.!!! Args.name))) --|
wenzelm@9565
   661
      Scan.lift (Args.$$$ "in")) [] -- Attrib.local_thmss;
wenzelm@8238
   662
wenzelm@12119
   663
fun inst_args f src ctxt = f ctxt (#2 (syntax (Args.goal_spec HEADGOAL -- insts) src ctxt));
wenzelm@8537
   664
ballarin@14174
   665
val insts_var =
ballarin@14174
   666
  Scan.optional
ballarin@14174
   667
    (Args.enum1 "and" (Scan.lift (Args.var -- (Args.$$$ "=" |-- Args.!!! Args.name))) --|
ballarin@14174
   668
      Scan.lift (Args.$$$ "in")) [] -- Attrib.local_thmss;
ballarin@14174
   669
ballarin@14174
   670
fun inst_args_var f src ctxt = f ctxt (#2 (syntax (Args.goal_spec HEADGOAL -- insts_var) src ctxt));
wenzelm@8537
   671
wenzelm@12119
   672
fun goal_args' args tac src ctxt = #2 (syntax (Args.goal_spec HEADGOAL -- args >>
ballarin@14215
   673
  (fn (quant, s) => SIMPLE_METHOD' quant (tac s))) src ctxt);
wenzelm@8537
   674
wenzelm@9539
   675
fun goal_args args tac = goal_args' (Scan.lift args) tac;
wenzelm@8238
   676
ballarin@14174
   677
fun goal_args_ctxt' args tac src ctxt =
ballarin@14174
   678
  #2 (syntax (Args.goal_spec HEADGOAL -- args >>
ballarin@14174
   679
  (fn (quant, s) => SIMPLE_METHOD' quant (tac ctxt s))) src ctxt);
wenzelm@8238
   680
ballarin@14174
   681
fun goal_args_ctxt args tac = goal_args_ctxt' (Scan.lift args) tac;
wenzelm@5824
   682
wenzelm@14718
   683
wenzelm@5824
   684
(** method text **)
wenzelm@5824
   685
wenzelm@5824
   686
(* datatype text *)
wenzelm@5824
   687
wenzelm@5824
   688
datatype text =
wenzelm@5824
   689
  Basic of (Proof.context -> Proof.method) |
wenzelm@15703
   690
  Source of src |
wenzelm@5824
   691
  Then of text list |
wenzelm@5824
   692
  Orelse of text list |
wenzelm@5824
   693
  Try of text |
wenzelm@5824
   694
  Repeat1 of text;
wenzelm@5824
   695
wenzelm@5824
   696
wenzelm@5824
   697
(* refine *)
wenzelm@5824
   698
wenzelm@8238
   699
fun gen_refine f text state =
wenzelm@5824
   700
  let
wenzelm@5824
   701
    val thy = Proof.theory_of state;
wenzelm@5824
   702
wenzelm@8238
   703
    fun eval (Basic mth) = f mth
wenzelm@8238
   704
      | eval (Source src) = f (method thy src)
wenzelm@5824
   705
      | eval (Then txts) = Seq.EVERY (map eval txts)
wenzelm@5824
   706
      | eval (Orelse txts) = Seq.FIRST (map eval txts)
wenzelm@5824
   707
      | eval (Try txt) = Seq.TRY (eval txt)
wenzelm@5824
   708
      | eval (Repeat1 txt) = Seq.REPEAT1 (eval txt);
wenzelm@5824
   709
  in eval text state end;
wenzelm@5824
   710
wenzelm@8238
   711
val refine = gen_refine Proof.refine;
wenzelm@8238
   712
val refine_end = gen_refine Proof.refine_end;
wenzelm@6404
   713
wenzelm@5824
   714
wenzelm@6404
   715
(* structured proof steps *)
wenzelm@5824
   716
wenzelm@7506
   717
val default_text = Source (Args.src (("default", []), Position.none));
wenzelm@8195
   718
val this_text = Basic (K this);
wenzelm@9706
   719
val done_text = Basic (K (SIMPLE_METHOD all_tac));
wenzelm@7555
   720
wenzelm@8966
   721
fun close_text asm = Basic (fn ctxt => METHOD (K
wenzelm@8966
   722
  (FILTER Thm.no_prems ((if asm then ALLGOALS (assm_tac ctxt) else all_tac) THEN flexflex_tac))));
wenzelm@8966
   723
skalberg@15531
   724
fun finish_text asm NONE = close_text asm
skalberg@15531
   725
  | finish_text asm (SOME txt) = Then [txt, close_text asm];
wenzelm@6872
   726
wenzelm@5824
   727
fun proof opt_text state =
wenzelm@5824
   728
  state
wenzelm@5824
   729
  |> Proof.assert_backward
wenzelm@15973
   730
  |> refine (if_none opt_text default_text)
wenzelm@8242
   731
  |> Seq.map (Proof.goal_facts (K []))
wenzelm@5824
   732
  |> Seq.map Proof.enter_forward;
wenzelm@5824
   733
wenzelm@8966
   734
fun local_qed asm opt_text = Proof.local_qed (refine (finish_text asm opt_text));
wenzelm@8966
   735
fun local_terminal_proof (text, opt_text) pr =
skalberg@15531
   736
  Seq.THEN (proof (SOME text), local_qed true opt_text pr);
skalberg@15531
   737
val local_default_proof = local_terminal_proof (default_text, NONE);
skalberg@15531
   738
val local_immediate_proof = local_terminal_proof (this_text, NONE);
skalberg@15531
   739
fun local_done_proof pr = Seq.THEN (proof (SOME done_text), local_qed false NONE pr);
wenzelm@5824
   740
wenzelm@6872
   741
wenzelm@8966
   742
fun global_qeds asm opt_text = Proof.global_qed (refine (finish_text asm opt_text));
wenzelm@5824
   743
wenzelm@8966
   744
fun global_qed asm opt_text state =
wenzelm@6872
   745
  state
wenzelm@8966
   746
  |> global_qeds asm opt_text
wenzelm@6872
   747
  |> Proof.check_result "Failed to finish proof" state
wenzelm@6872
   748
  |> Seq.hd;
wenzelm@6872
   749
wenzelm@8966
   750
fun global_term_proof asm (text, opt_text) state =
wenzelm@6872
   751
  state
skalberg@15531
   752
  |> proof (SOME text)
wenzelm@6872
   753
  |> Proof.check_result "Terminal proof method failed" state
wenzelm@8966
   754
  |> (Seq.flat o Seq.map (global_qeds asm opt_text))
wenzelm@6872
   755
  |> Proof.check_result "Failed to finish proof (after successful terminal method)" state
wenzelm@6872
   756
  |> Seq.hd;
wenzelm@6872
   757
wenzelm@8966
   758
val global_terminal_proof = global_term_proof true;
skalberg@15531
   759
val global_default_proof = global_terminal_proof (default_text, NONE);
skalberg@15531
   760
val global_immediate_proof = global_terminal_proof (this_text, NONE);
skalberg@15531
   761
val global_done_proof = global_term_proof false (done_text, NONE);
wenzelm@5824
   762
wenzelm@5824
   763
wenzelm@9539
   764
(* misc tactic emulations *)
wenzelm@9539
   765
ballarin@14174
   766
val subgoal_meth = goal_args_ctxt (Scan.repeat1 Args.name) subgoals_tac;
ballarin@14174
   767
val thin_meth = goal_args_ctxt Args.name thin_tac;
wenzelm@9539
   768
val rename_meth = goal_args (Scan.repeat1 Args.name) Tactic.rename_params_tac;
wenzelm@9631
   769
val rotate_meth = goal_args (Scan.optional Args.int 1) Tactic.rotate_tac;
wenzelm@9539
   770
wenzelm@9539
   771
wenzelm@5824
   772
(* pure_methods *)
wenzelm@5824
   773
wenzelm@5824
   774
val pure_methods =
wenzelm@5824
   775
 [("fail", no_args fail, "force failure"),
wenzelm@5824
   776
  ("succeed", no_args succeed, "succeed"),
wenzelm@9587
   777
  ("-", no_args insert_facts, "do nothing (insert current facts only)"),
wenzelm@9539
   778
  ("insert", thms_args insert, "insert theorems, ignoring facts (improper)"),
wenzelm@16145
   779
  ("unfold", thms_args unfold_meth, "unfold definitions"),
wenzelm@12384
   780
  ("intro", thms_args intro, "repeatedly apply introduction rules"),
wenzelm@12384
   781
  ("elim", thms_args elim, "repeatedly apply elimination rules"),
wenzelm@16145
   782
  ("fold", thms_args fold_meth, "fold definitions"),
wenzelm@12829
   783
  ("atomize", (atomize o #2) oo syntax (Args.mode "full"),
wenzelm@11962
   784
    "present local premises as object-level statements"),
wenzelm@12347
   785
  ("rules", rules_args rules_meth, "apply many rules, including proof search"),
wenzelm@12384
   786
  ("rule", thms_ctxt_args some_rule, "apply some intro/elim rule"),
wenzelm@10744
   787
  ("erule", nat_thms_args erule, "apply rule in elimination manner (improper)"),
wenzelm@10744
   788
  ("drule", nat_thms_args drule, "apply rule in destruct manner (improper)"),
wenzelm@10744
   789
  ("frule", nat_thms_args frule, "apply rule in forward manner (improper)"),
wenzelm@8195
   790
  ("this", no_args this, "apply current facts as rules"),
wenzelm@8238
   791
  ("assumption", ctxt_args assumption, "proof by assumption, preferring facts"),
ballarin@14174
   792
  ("rule_tac", inst_args_var res_inst_meth, "apply rule (dynamic instantiation)"),
ballarin@14174
   793
  ("erule_tac", inst_args_var eres_inst_meth, "apply rule in elimination manner (dynamic instantiation)"),
ballarin@14174
   794
  ("drule_tac", inst_args_var dres_inst_meth, "apply rule in destruct manner (dynamic instantiation)"),
ballarin@14174
   795
  ("frule_tac", inst_args_var forw_inst_meth, "apply rule in forward manner (dynamic instantiation)"),
ballarin@14174
   796
  ("cut_tac", inst_args_var cut_inst_meth, "cut rule (dynamic instantiation)"),
ballarin@14174
   797
  ("subgoal_tac", subgoal_meth, "insert subgoal (dynamic instantiation)"),
ballarin@14174
   798
  ("thin_tac", thin_meth, "remove premise (dynamic instantiation)"),
ballarin@14174
   799
  ("rename_tac", rename_meth, "rename parameters of goal (dynamic instantiation)"),
wenzelm@9631
   800
  ("rotate_tac", rotate_meth, "rotate assumptions of goal"),
wenzelm@8351
   801
  ("prolog", thms_args prolog, "simple prolog interpreter"),
wenzelm@8351
   802
  ("tactic", simple_args Args.name tactic, "ML tactic as proof method")];
wenzelm@5824
   803
wenzelm@15801
   804
val _ = Context.add_setup [add_methods pure_methods];
wenzelm@5824
   805
wenzelm@5824
   806
wenzelm@16145
   807
(*final declarations of this structure!*)
wenzelm@16145
   808
val unfold = unfold_meth;
wenzelm@16145
   809
val fold = fold_meth;
wenzelm@16145
   810
wenzelm@5824
   811
end;
wenzelm@5824
   812
wenzelm@5824
   813
structure BasicMethod: BASIC_METHOD = Method;
wenzelm@5824
   814
open BasicMethod;