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