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