src/Pure/Isar/method.ML
author wenzelm
Thu Nov 29 01:50:50 2001 +0100 (2001-11-29)
changeset 12324 5db4b4596d1a
parent 12311 ce5f9e61c037
child 12347 6ee66b76d813
permissions -rw-r--r--
rule context and attributes moved to rule_context.ML;
wenzelm@5824
     1
(*  Title:      Pure/Isar/method.ML
wenzelm@5824
     2
    ID:         $Id$
wenzelm@5824
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8807
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@5824
     5
wenzelm@5824
     6
Proof methods.
wenzelm@5824
     7
*)
wenzelm@5824
     8
wenzelm@5824
     9
signature BASIC_METHOD =
wenzelm@5824
    10
sig
wenzelm@11731
    11
  val trace_rules: bool ref
wenzelm@5824
    12
  val print_methods: theory -> unit
wenzelm@5824
    13
  val Method: bstring -> (Args.src -> Proof.context -> Proof.method) -> string -> unit
wenzelm@5824
    14
end;
wenzelm@5824
    15
wenzelm@5824
    16
signature METHOD =
wenzelm@5824
    17
sig
wenzelm@5824
    18
  include BASIC_METHOD
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@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
wenzelm@12119
    47
  val impose_hyps_tac: Proof.context -> 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
wenzelm@5824
   113
  val setup: (theory -> theory) list
wenzelm@5824
   114
end;
wenzelm@5824
   115
wenzelm@5824
   116
structure Method: METHOD =
wenzelm@5824
   117
struct
wenzelm@5824
   118
wenzelm@5824
   119
wenzelm@12324
   120
(** proof methods **)
wenzelm@12324
   121
wenzelm@12324
   122
(* tracing *)
wenzelm@11731
   123
wenzelm@11731
   124
val trace_rules = ref false;
wenzelm@11731
   125
wenzelm@12055
   126
fun trace ctxt rules =
wenzelm@12262
   127
  conditional (! trace_rules andalso not (null rules)) (fn () =>
wenzelm@12262
   128
    Pretty.big_list "rules:" (map (ProofContext.pretty_thm ctxt) rules)
wenzelm@12262
   129
    |> Pretty.string_of |> tracing);
wenzelm@11731
   130
wenzelm@11731
   131
wenzelm@8372
   132
(* make methods *)
wenzelm@5824
   133
wenzelm@12144
   134
val RAW_METHOD = Proof.method;
wenzelm@12144
   135
val RAW_METHOD_CASES = Proof.method_cases;
wenzelm@12144
   136
wenzelm@12144
   137
fun METHOD m = Proof.method (fn facts => TRY Tactic.conjunction_tac THEN m facts);
wenzelm@12144
   138
fun METHOD_CASES m =
wenzelm@12144
   139
  Proof.method_cases (fn facts => Seq.THEN (TRY Tactic.conjunction_tac, m facts));
wenzelm@8372
   140
wenzelm@5824
   141
wenzelm@5824
   142
(* primitive *)
wenzelm@5824
   143
wenzelm@5824
   144
val fail = METHOD (K no_tac);
wenzelm@5824
   145
val succeed = METHOD (K all_tac);
wenzelm@5824
   146
wenzelm@5824
   147
wenzelm@8167
   148
(* shuffle *)
wenzelm@8167
   149
wenzelm@8240
   150
fun prefer i = METHOD (K (Tactic.defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1)));
wenzelm@8167
   151
fun defer opt_i = METHOD (K (Tactic.defer_tac (if_none opt_i 1)));
wenzelm@8167
   152
wenzelm@8167
   153
wenzelm@7419
   154
(* insert *)
wenzelm@7419
   155
wenzelm@7419
   156
local
wenzelm@5824
   157
wenzelm@6981
   158
fun cut_rule_tac raw_rule =
wenzelm@6981
   159
  let
wenzelm@6981
   160
    val rule = Drule.forall_intr_vars raw_rule;
wenzelm@6981
   161
    val revcut_rl = Drule.incr_indexes_wrt [] [] [] [rule] Drule.revcut_rl;
wenzelm@7555
   162
  in Tactic.rtac (rule COMP revcut_rl) end;
wenzelm@6981
   163
wenzelm@7419
   164
in
wenzelm@5824
   165
wenzelm@7419
   166
fun insert_tac [] i = all_tac
wenzelm@7419
   167
  | insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts);
wenzelm@6981
   168
wenzelm@7555
   169
val insert_facts = METHOD (ALLGOALS o insert_tac);
wenzelm@7664
   170
fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms));
wenzelm@7419
   171
wenzelm@9706
   172
fun SIMPLE_METHOD tac = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN tac);
wenzelm@9706
   173
fun SIMPLE_METHOD' quant tac = METHOD (fn facts => quant (insert_tac facts THEN' tac));
wenzelm@9706
   174
wenzelm@12324
   175
end;
wenzelm@12324
   176
wenzelm@9706
   177
wenzelm@7601
   178
(* unfold / fold definitions *)
wenzelm@6532
   179
wenzelm@10821
   180
fun unfold thms = SIMPLE_METHOD (CHANGED_PROP (rewrite_goals_tac thms));
wenzelm@10821
   181
fun fold thms = SIMPLE_METHOD (CHANGED_PROP (fold_goals_tac thms));
wenzelm@9484
   182
wenzelm@9484
   183
wenzelm@7419
   184
(* multi_resolve *)
wenzelm@7419
   185
wenzelm@7419
   186
local
wenzelm@7419
   187
wenzelm@7419
   188
fun res th i rule =
wenzelm@7419
   189
  Thm.biresolution false [(false, th)] i rule handle THM _ => Seq.empty;
wenzelm@7419
   190
wenzelm@7419
   191
fun multi_res _ [] rule = Seq.single rule
wenzelm@7419
   192
  | multi_res i (th :: ths) rule = Seq.flat (Seq.map (res th i) (multi_res (i + 1) ths rule));
wenzelm@7419
   193
wenzelm@7419
   194
in
wenzelm@7419
   195
wenzelm@7419
   196
val multi_resolve = multi_res 1;
wenzelm@8372
   197
fun multi_resolves facts rules = Seq.flat (Seq.map (multi_resolve facts) (Seq.of_list rules));
wenzelm@7419
   198
wenzelm@7419
   199
end;
wenzelm@7419
   200
wenzelm@7419
   201
wenzelm@12324
   202
(* single rules *)
wenzelm@8372
   203
wenzelm@7419
   204
local
wenzelm@5824
   205
wenzelm@12324
   206
fun may_unify t nets = RuleContext.orderlist (flat (map (fn net => Net.unify_term net t) nets));
wenzelm@12324
   207
wenzelm@12324
   208
fun find_erules [] = K []
wenzelm@12324
   209
  | find_erules (fact :: _) = may_unify (Logic.strip_assums_concl (#prop (Thm.rep_thm fact)));
wenzelm@12324
   210
wenzelm@12324
   211
fun find_rules goal = may_unify (Logic.strip_assums_concl goal);
wenzelm@12324
   212
wenzelm@12324
   213
wenzelm@10541
   214
fun gen_rule_tac tac rules [] i st = tac rules i st
wenzelm@12324
   215
  | gen_rule_tac tac rules facts i st =
wenzelm@12324
   216
      Seq.flat (Seq.map (fn rule => (tac o single) rule i st) (multi_resolves facts rules));
wenzelm@7130
   217
wenzelm@10744
   218
fun gen_arule_tac tac j rules facts =
wenzelm@10744
   219
  EVERY' (gen_rule_tac tac rules facts :: replicate j Tactic.assume_tac);
wenzelm@10744
   220
wenzelm@11785
   221
fun gen_some_rule_tac tac arg_rules ctxt facts = SUBGOAL (fn (goal, i) =>
wenzelm@11785
   222
  let
wenzelm@12324
   223
    val netpairs = RuleContext.netpairs ctxt;
wenzelm@11785
   224
    val rules =
wenzelm@11785
   225
      if not (null arg_rules) then arg_rules
wenzelm@12324
   226
      else map #2 (find_erules facts (map #2 netpairs) @ find_rules goal (map #1 netpairs));
wenzelm@12055
   227
  in trace ctxt rules; tac rules facts i end);
wenzelm@10309
   228
wenzelm@10744
   229
fun meth tac x = METHOD (HEADGOAL o tac x);
wenzelm@10744
   230
fun meth' tac x y = METHOD (HEADGOAL o tac x y);
wenzelm@8220
   231
wenzelm@7419
   232
in
wenzelm@7419
   233
wenzelm@10744
   234
val rule_tac = gen_rule_tac Tactic.resolve_tac;
wenzelm@10744
   235
val rule = meth rule_tac;
wenzelm@10744
   236
val some_rule_tac = gen_some_rule_tac rule_tac;
wenzelm@10744
   237
val some_rule = meth' some_rule_tac;
wenzelm@10744
   238
wenzelm@10744
   239
val erule = meth' (gen_arule_tac Tactic.eresolve_tac);
wenzelm@10744
   240
val drule = meth' (gen_arule_tac Tactic.dresolve_tac);
wenzelm@10744
   241
val frule = meth' (gen_arule_tac Tactic.forward_tac);
wenzelm@5824
   242
wenzelm@7419
   243
end;
wenzelm@7419
   244
wenzelm@7419
   245
wenzelm@8195
   246
(* this *)
wenzelm@8195
   247
wenzelm@8671
   248
val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac));
wenzelm@8195
   249
wenzelm@8195
   250
wenzelm@8195
   251
(* assumption *)
wenzelm@7555
   252
wenzelm@10378
   253
fun asm_tac ths =
wenzelm@10378
   254
  foldr (op APPEND') (map (fn th => Tactic.rtac th THEN_ALL_NEW assume_tac) ths, K no_tac);
wenzelm@10378
   255
wenzelm@10405
   256
fun assm_tac ctxt =
wenzelm@10405
   257
  assume_tac APPEND'
wenzelm@10405
   258
  asm_tac (ProofContext.prems_of ctxt) APPEND'
wenzelm@10405
   259
  Tactic.rtac Drule.reflexive_thm;
wenzelm@7419
   260
wenzelm@7555
   261
fun assumption_tac ctxt [] = assm_tac ctxt
wenzelm@10378
   262
  | assumption_tac _ [fact] = asm_tac [fact]
wenzelm@7555
   263
  | assumption_tac _ _ = K no_tac;
wenzelm@7419
   264
wenzelm@8671
   265
fun assumption ctxt = METHOD (HEADGOAL o assumption_tac ctxt);
wenzelm@7419
   266
wenzelm@7419
   267
wenzelm@9539
   268
(* res_inst_tac etc. *)
wenzelm@8238
   269
wenzelm@12119
   270
(*robustify instantiation by imposing (part of) the present static context*)
wenzelm@12119
   271
val impose_hyps_tac =
wenzelm@12119
   272
  PRIMITIVE o Drule.impose_hyps o flat o map #1 o ProofContext.assumptions_of;
wenzelm@12119
   273
wenzelm@9539
   274
(*Note: insts refer to the implicit (!!) goal context; use at your own risk*)
wenzelm@12119
   275
fun gen_res_inst _ tac _ (quant, ([], thms)) =
wenzelm@9565
   276
      METHOD (fn facts => (quant (insert_tac facts THEN' tac thms)))
wenzelm@12119
   277
  | gen_res_inst tac _ ctxt (quant, (insts, [thm])) =
wenzelm@12119
   278
      METHOD (fn facts => (impose_hyps_tac ctxt THEN quant (insert_tac facts THEN' tac insts thm)))
wenzelm@12119
   279
  | gen_res_inst _ _ _ _ = error "Cannot have instantiations with multiple rules";
wenzelm@8238
   280
wenzelm@9565
   281
val res_inst = gen_res_inst Tactic.res_inst_tac Tactic.resolve_tac;
wenzelm@9565
   282
val eres_inst = gen_res_inst Tactic.eres_inst_tac Tactic.eresolve_tac;
wenzelm@9565
   283
val dres_inst = gen_res_inst Tactic.dres_inst_tac Tactic.dresolve_tac;
wenzelm@9565
   284
val forw_inst = gen_res_inst Tactic.forw_inst_tac Tactic.forward_tac;
wenzelm@9565
   285
val cut_inst = gen_res_inst Tactic.cut_inst_tac Tactic.cut_facts_tac;
wenzelm@8238
   286
wenzelm@8238
   287
wenzelm@8329
   288
(* simple Prolog interpreter *)
wenzelm@8329
   289
wenzelm@8329
   290
fun prolog_tac rules facts =
wenzelm@8329
   291
  DEPTH_SOLVE_1 (HEADGOAL (Tactic.assume_tac APPEND' Tactic.resolve_tac (facts @ rules)));
wenzelm@8329
   292
wenzelm@8329
   293
val prolog = METHOD o prolog_tac;
wenzelm@8329
   294
wenzelm@8329
   295
wenzelm@8351
   296
(* ML tactics *)
wenzelm@8351
   297
wenzelm@8351
   298
val tactic_ref = ref ((fn _ => raise Match): Proof.context -> thm list -> tactic);
wenzelm@8351
   299
fun set_tactic f = tactic_ref := f;
wenzelm@8351
   300
wenzelm@8351
   301
fun tactic txt ctxt = METHOD (fn facts =>
wenzelm@9631
   302
  (Context.use_mltext
wenzelm@9631
   303
    ("let fun tactic (ctxt: PureIsar.Proof.context) (facts: thm list) : tactic = \
wenzelm@9631
   304
     \let val thm = PureIsar.ProofContext.get_thm_closure ctxt\n\
wenzelm@9631
   305
     \  and thms = PureIsar.ProofContext.get_thms_closure ctxt in\n"
wenzelm@9631
   306
     ^ txt ^
wenzelm@9631
   307
     "\nend in PureIsar.Method.set_tactic tactic end")
wenzelm@9631
   308
    false None;
wenzelm@9631
   309
    Context.setmp (Some (ProofContext.theory_of ctxt)) (! tactic_ref ctxt) facts));
wenzelm@8351
   310
wenzelm@8351
   311
wenzelm@5824
   312
wenzelm@5824
   313
(** methods theory data **)
wenzelm@5824
   314
wenzelm@5824
   315
(* data kind 'Isar/methods' *)
wenzelm@5824
   316
wenzelm@5824
   317
structure MethodsDataArgs =
wenzelm@5824
   318
struct
wenzelm@5824
   319
  val name = "Isar/methods";
wenzelm@5824
   320
  type T =
wenzelm@5824
   321
    {space: NameSpace.T,
wenzelm@5824
   322
     meths: (((Args.src -> Proof.context -> Proof.method) * string) * stamp) Symtab.table};
wenzelm@5824
   323
wenzelm@5824
   324
  val empty = {space = NameSpace.empty, meths = Symtab.empty};
wenzelm@6546
   325
  val copy = I;
wenzelm@5824
   326
  val prep_ext = I;
wenzelm@5824
   327
  fun merge ({space = space1, meths = meths1}, {space = space2, meths = meths2}) =
wenzelm@5824
   328
    {space = NameSpace.merge (space1, space2),
wenzelm@5824
   329
      meths = Symtab.merge eq_snd (meths1, meths2) handle Symtab.DUPS dups =>
wenzelm@5824
   330
        error ("Attempt to merge different versions of methods " ^ commas_quote dups)};
wenzelm@5824
   331
wenzelm@9222
   332
  fun print _ {space, meths} =
wenzelm@5824
   333
    let
wenzelm@5824
   334
      fun prt_meth (name, ((_, comment), _)) = Pretty.block
wenzelm@6849
   335
        [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@5824
   336
    in
wenzelm@8720
   337
      [Pretty.big_list "methods:" (map prt_meth (NameSpace.cond_extern_table space meths))]
wenzelm@9222
   338
      |> Pretty.chunks |> Pretty.writeln
wenzelm@5824
   339
    end;
wenzelm@5824
   340
end;
wenzelm@5824
   341
wenzelm@5824
   342
structure MethodsData = TheoryDataFun(MethodsDataArgs);
wenzelm@5824
   343
val print_methods = MethodsData.print;
wenzelm@7611
   344
wenzelm@5824
   345
wenzelm@5824
   346
(* get methods *)
wenzelm@5824
   347
wenzelm@5916
   348
exception METHOD_FAIL of (string * Position.T) * exn;
wenzelm@5916
   349
wenzelm@5824
   350
fun method thy =
wenzelm@5824
   351
  let
wenzelm@5824
   352
    val {space, meths} = MethodsData.get thy;
wenzelm@5824
   353
wenzelm@5884
   354
    fun meth src =
wenzelm@5884
   355
      let
wenzelm@5884
   356
        val ((raw_name, _), pos) = Args.dest_src src;
wenzelm@5884
   357
        val name = NameSpace.intern space raw_name;
wenzelm@5884
   358
      in
wenzelm@5824
   359
        (case Symtab.lookup (meths, name) of
wenzelm@5824
   360
          None => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
wenzelm@5916
   361
        | Some ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src))
wenzelm@5824
   362
      end;
wenzelm@5824
   363
  in meth end;
wenzelm@5824
   364
wenzelm@5824
   365
wenzelm@9194
   366
(* add_method(s) *)
wenzelm@5824
   367
wenzelm@5824
   368
fun add_methods raw_meths thy =
wenzelm@5824
   369
  let
wenzelm@5824
   370
    val full = Sign.full_name (Theory.sign_of thy);
wenzelm@5824
   371
    val new_meths =
wenzelm@5824
   372
      map (fn (name, f, comment) => (full name, ((f, comment), stamp ()))) raw_meths;
wenzelm@5824
   373
wenzelm@5824
   374
    val {space, meths} = MethodsData.get thy;
wenzelm@5824
   375
    val space' = NameSpace.extend (space, map fst new_meths);
wenzelm@5824
   376
    val meths' = Symtab.extend (meths, new_meths) handle Symtab.DUPS dups =>
wenzelm@5824
   377
      error ("Duplicate declaration of method(s) " ^ commas_quote dups);
wenzelm@5824
   378
  in
wenzelm@5824
   379
    thy |> MethodsData.put {space = space', meths = meths'}
wenzelm@5824
   380
  end;
wenzelm@5824
   381
wenzelm@9194
   382
val add_method = add_methods o Library.single;
wenzelm@9194
   383
wenzelm@5824
   384
(*implicit version*)
wenzelm@5824
   385
fun Method name meth cmt = Context.>> (add_methods [(name, meth, cmt)]);
wenzelm@5824
   386
wenzelm@5824
   387
wenzelm@5884
   388
wenzelm@5884
   389
(** method syntax **)
wenzelm@5824
   390
wenzelm@5884
   391
(* basic *)
wenzelm@5884
   392
wenzelm@5884
   393
fun syntax (scan: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))) =
wenzelm@5884
   394
  Args.syntax "method" scan;
wenzelm@5824
   395
wenzelm@8351
   396
fun simple_args scan f src ctxt : Proof.method =
wenzelm@8351
   397
  #2 (syntax (Scan.lift (scan >> (fn x => f x ctxt))) src ctxt);
wenzelm@8351
   398
wenzelm@7555
   399
fun ctxt_args (f: Proof.context -> Proof.method) src ctxt =
wenzelm@8282
   400
  #2 (syntax (Scan.succeed (f ctxt)) src ctxt);
wenzelm@7555
   401
wenzelm@7555
   402
fun no_args m = ctxt_args (K m);
wenzelm@5884
   403
wenzelm@5884
   404
wenzelm@5884
   405
(* sections *)
wenzelm@5824
   406
wenzelm@7268
   407
type modifier = (Proof.context -> Proof.context) * Proof.context attribute;
wenzelm@7268
   408
wenzelm@7268
   409
local
wenzelm@7268
   410
wenzelm@8381
   411
fun sect ss = Scan.first (map Scan.lift ss);
wenzelm@5884
   412
fun thms ss = Scan.unless (sect ss) Attrib.local_thms;
wenzelm@5884
   413
fun thmss ss = Scan.repeat (thms ss) >> flat;
wenzelm@5884
   414
wenzelm@7268
   415
fun apply (f, att) (ctxt, ths) = Thm.applys_attributes ((f ctxt, ths), [att]);
wenzelm@5824
   416
wenzelm@7268
   417
fun section ss = (sect ss -- thmss ss) :-- (fn (m, ths) => Scan.depend (fn ctxt =>
wenzelm@7268
   418
  Scan.succeed (apply m (ctxt, ths)))) >> #2;
wenzelm@5884
   419
wenzelm@7601
   420
fun sectioned args ss = args -- Scan.repeat (section ss);
wenzelm@5884
   421
wenzelm@7268
   422
in
wenzelm@5824
   423
wenzelm@5884
   424
fun sectioned_args args ss f src ctxt =
wenzelm@8282
   425
  let val (ctxt', (x, _)) = syntax (sectioned args ss) src ctxt
wenzelm@5921
   426
  in f x ctxt' end;
wenzelm@5884
   427
wenzelm@7601
   428
fun bang_sectioned_args ss f = sectioned_args Args.bang_facts ss f;
wenzelm@9777
   429
fun bang_sectioned_args' ss scan f =
wenzelm@9777
   430
  sectioned_args (Args.bang_facts -- scan >> swap) ss (uncurry f);
wenzelm@7601
   431
fun only_sectioned_args ss f = sectioned_args (Scan.succeed ()) ss (fn () => f);
wenzelm@7268
   432
wenzelm@8093
   433
fun thms_ctxt_args f = sectioned_args (thmss []) [] f;
wenzelm@8093
   434
fun thms_args f = thms_ctxt_args (K o f);
wenzelm@9706
   435
fun thm_args f = thms_args (fn [thm] => f thm | _ => error "Single theorem expected");
wenzelm@5824
   436
wenzelm@7268
   437
end;
wenzelm@7268
   438
wenzelm@5824
   439
wenzelm@9539
   440
(* tactic syntax *)
wenzelm@8238
   441
wenzelm@10744
   442
fun nat_thms_args f = uncurry f oo
wenzelm@10744
   443
  (#2 oo syntax (Scan.lift (Scan.optional (Args.parens Args.nat) 0) -- Attrib.local_thmss));
wenzelm@10744
   444
wenzelm@8238
   445
val insts =
wenzelm@9539
   446
  Scan.optional
wenzelm@9565
   447
    (Args.enum1 "and" (Scan.lift (Args.name -- (Args.$$$ "=" |-- Args.!!! Args.name))) --|
wenzelm@9565
   448
      Scan.lift (Args.$$$ "in")) [] -- Attrib.local_thmss;
wenzelm@8238
   449
wenzelm@12119
   450
fun inst_args f src ctxt = f ctxt (#2 (syntax (Args.goal_spec HEADGOAL -- insts) src ctxt));
wenzelm@8537
   451
wenzelm@8537
   452
wenzelm@12119
   453
fun goal_args' args tac src ctxt = #2 (syntax (Args.goal_spec HEADGOAL -- args >>
wenzelm@12119
   454
  (fn (quant, s) => SIMPLE_METHOD' quant (K (impose_hyps_tac ctxt) THEN' tac s))) src ctxt);
wenzelm@8537
   455
wenzelm@9539
   456
fun goal_args args tac = goal_args' (Scan.lift args) tac;
wenzelm@8238
   457
wenzelm@8238
   458
wenzelm@5824
   459
wenzelm@5824
   460
(** method text **)
wenzelm@5824
   461
wenzelm@5824
   462
(* datatype text *)
wenzelm@5824
   463
wenzelm@5824
   464
datatype text =
wenzelm@5824
   465
  Basic of (Proof.context -> Proof.method) |
wenzelm@5824
   466
  Source of Args.src |
wenzelm@5824
   467
  Then of text list |
wenzelm@5824
   468
  Orelse of text list |
wenzelm@5824
   469
  Try of text |
wenzelm@5824
   470
  Repeat1 of text;
wenzelm@5824
   471
wenzelm@5824
   472
wenzelm@5824
   473
(* refine *)
wenzelm@5824
   474
wenzelm@8238
   475
fun gen_refine f text state =
wenzelm@5824
   476
  let
wenzelm@5824
   477
    val thy = Proof.theory_of state;
wenzelm@5824
   478
wenzelm@8238
   479
    fun eval (Basic mth) = f mth
wenzelm@8238
   480
      | eval (Source src) = f (method thy src)
wenzelm@5824
   481
      | eval (Then txts) = Seq.EVERY (map eval txts)
wenzelm@5824
   482
      | eval (Orelse txts) = Seq.FIRST (map eval txts)
wenzelm@5824
   483
      | eval (Try txt) = Seq.TRY (eval txt)
wenzelm@5824
   484
      | eval (Repeat1 txt) = Seq.REPEAT1 (eval txt);
wenzelm@5824
   485
  in eval text state end;
wenzelm@5824
   486
wenzelm@8238
   487
val refine = gen_refine Proof.refine;
wenzelm@8238
   488
val refine_end = gen_refine Proof.refine_end;
wenzelm@6404
   489
wenzelm@5824
   490
wenzelm@6404
   491
(* structured proof steps *)
wenzelm@5824
   492
wenzelm@7506
   493
val default_text = Source (Args.src (("default", []), Position.none));
wenzelm@8195
   494
val this_text = Basic (K this);
wenzelm@9706
   495
val done_text = Basic (K (SIMPLE_METHOD all_tac));
wenzelm@7555
   496
wenzelm@8966
   497
fun close_text asm = Basic (fn ctxt => METHOD (K
wenzelm@8966
   498
  (FILTER Thm.no_prems ((if asm then ALLGOALS (assm_tac ctxt) else all_tac) THEN flexflex_tac))));
wenzelm@8966
   499
wenzelm@8966
   500
fun finish_text asm None = close_text asm
wenzelm@8966
   501
  | finish_text asm (Some txt) = Then [txt, close_text asm];
wenzelm@6872
   502
wenzelm@5824
   503
fun proof opt_text state =
wenzelm@5824
   504
  state
wenzelm@5824
   505
  |> Proof.assert_backward
wenzelm@6404
   506
  |> refine (if_none opt_text default_text)
wenzelm@8242
   507
  |> Seq.map (Proof.goal_facts (K []))
wenzelm@5824
   508
  |> Seq.map Proof.enter_forward;
wenzelm@5824
   509
wenzelm@8966
   510
fun local_qed asm opt_text = Proof.local_qed (refine (finish_text asm opt_text));
wenzelm@8966
   511
fun local_terminal_proof (text, opt_text) pr =
wenzelm@8966
   512
  Seq.THEN (proof (Some text), local_qed true opt_text pr);
wenzelm@8966
   513
val local_default_proof = local_terminal_proof (default_text, None);
wenzelm@8195
   514
val local_immediate_proof = local_terminal_proof (this_text, None);
wenzelm@8966
   515
fun local_done_proof pr = Seq.THEN (proof (Some done_text), local_qed false None pr);
wenzelm@5824
   516
wenzelm@6872
   517
wenzelm@8966
   518
fun global_qeds asm opt_text = Proof.global_qed (refine (finish_text asm opt_text));
wenzelm@5824
   519
wenzelm@8966
   520
fun global_qed asm opt_text state =
wenzelm@6872
   521
  state
wenzelm@8966
   522
  |> global_qeds asm opt_text
wenzelm@6872
   523
  |> Proof.check_result "Failed to finish proof" state
wenzelm@6872
   524
  |> Seq.hd;
wenzelm@6872
   525
wenzelm@8966
   526
fun global_term_proof asm (text, opt_text) state =
wenzelm@6872
   527
  state
wenzelm@6872
   528
  |> proof (Some text)
wenzelm@6872
   529
  |> Proof.check_result "Terminal proof method failed" state
wenzelm@8966
   530
  |> (Seq.flat o Seq.map (global_qeds asm opt_text))
wenzelm@6872
   531
  |> Proof.check_result "Failed to finish proof (after successful terminal method)" state
wenzelm@6872
   532
  |> Seq.hd;
wenzelm@6872
   533
wenzelm@8966
   534
val global_terminal_proof = global_term_proof true;
wenzelm@6934
   535
val global_default_proof = global_terminal_proof (default_text, None);
wenzelm@8966
   536
val global_immediate_proof = global_terminal_proof (this_text, None);
wenzelm@8966
   537
val global_done_proof = global_term_proof false (done_text, None);
wenzelm@5824
   538
wenzelm@5824
   539
wenzelm@12324
   540
wenzelm@5824
   541
(** theory setup **)
wenzelm@5824
   542
wenzelm@9539
   543
(* misc tactic emulations *)
wenzelm@9539
   544
wenzelm@9539
   545
val subgoal_meth = goal_args (Scan.repeat1 Args.name) Tactic.subgoals_tac;
wenzelm@9539
   546
val thin_meth = goal_args Args.name Tactic.thin_tac;
wenzelm@9539
   547
val rename_meth = goal_args (Scan.repeat1 Args.name) Tactic.rename_params_tac;
wenzelm@9631
   548
val rotate_meth = goal_args (Scan.optional Args.int 1) Tactic.rotate_tac;
wenzelm@9539
   549
wenzelm@9539
   550
wenzelm@5824
   551
(* pure_methods *)
wenzelm@5824
   552
wenzelm@5824
   553
val pure_methods =
wenzelm@5824
   554
 [("fail", no_args fail, "force failure"),
wenzelm@5824
   555
  ("succeed", no_args succeed, "succeed"),
wenzelm@9587
   556
  ("-", no_args insert_facts, "do nothing (insert current facts only)"),
wenzelm@9539
   557
  ("insert", thms_args insert, "insert theorems, ignoring facts (improper)"),
wenzelm@7601
   558
  ("unfold", thms_args unfold, "unfold definitions"),
wenzelm@7601
   559
  ("fold", thms_args fold, "fold definitions"),
wenzelm@12007
   560
  ("atomize", no_args (SIMPLE_METHOD' HEADGOAL (CHANGED_PROP o ObjectLogic.atomize_tac)),
wenzelm@11962
   561
    "present local premises as object-level statements"),
wenzelm@8153
   562
  ("rule", thms_ctxt_args some_rule, "apply some rule"),
wenzelm@10744
   563
  ("erule", nat_thms_args erule, "apply rule in elimination manner (improper)"),
wenzelm@10744
   564
  ("drule", nat_thms_args drule, "apply rule in destruct manner (improper)"),
wenzelm@10744
   565
  ("frule", nat_thms_args frule, "apply rule in forward manner (improper)"),
wenzelm@8195
   566
  ("this", no_args this, "apply current facts as rules"),
wenzelm@8238
   567
  ("assumption", ctxt_args assumption, "proof by assumption, preferring facts"),
wenzelm@9539
   568
  ("rule_tac", inst_args res_inst, "apply rule (dynamic instantiation!)"),
wenzelm@9539
   569
  ("erule_tac", inst_args eres_inst, "apply rule in elimination manner (dynamic instantiation!)"),
wenzelm@9539
   570
  ("drule_tac", inst_args dres_inst, "apply rule in destruct manner (dynamic instantiation!)"),
wenzelm@9539
   571
  ("frule_tac", inst_args forw_inst, "apply rule in forward manner (dynamic instantiation!)"),
wenzelm@9539
   572
  ("cut_tac", inst_args cut_inst, "cut rule (dynamic instantiation!)"),
wenzelm@9565
   573
  ("subgoal_tac", subgoal_meth, "insert subgoal (dynamic instantiation!)"),
wenzelm@9565
   574
  ("thin_tac", thin_meth, "remove premise (dynamic instantiation!)"),
wenzelm@9631
   575
  ("rename_tac", rename_meth, "rename parameters of goal (dynamic instantiation!)"),
wenzelm@9631
   576
  ("rotate_tac", rotate_meth, "rotate assumptions of goal"),
wenzelm@8351
   577
  ("prolog", thms_args prolog, "simple prolog interpreter"),
wenzelm@8351
   578
  ("tactic", simple_args Args.name tactic, "ML tactic as proof method")];
wenzelm@5824
   579
wenzelm@5824
   580
wenzelm@5824
   581
(* setup *)
wenzelm@5824
   582
wenzelm@8153
   583
val setup =
wenzelm@12324
   584
 [MethodsData.init, add_methods pure_methods,
wenzelm@12324
   585
  (#1 o PureThy.add_thms [(("", Drule.equal_intr_rule), [RuleContext.intro_query_global None])])];
wenzelm@5824
   586
wenzelm@5824
   587
wenzelm@5824
   588
end;
wenzelm@5824
   589
wenzelm@5824
   590
wenzelm@5824
   591
structure BasicMethod: BASIC_METHOD = Method;
wenzelm@5824
   592
open BasicMethod;