src/Pure/Isar/method.ML
author wenzelm
Sat Feb 28 14:52:21 2009 +0100 (2009-02-28)
changeset 30165 6ee87f67d9cd
parent 29857 2cc976ed8a3c
child 30190 479806475f3c
permissions -rw-r--r--
moved generic intuitionistic prover to src/Tools/intuitionistic.ML;
wenzelm@5824
     1
(*  Title:      Pure/Isar/method.ML
wenzelm@5824
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5824
     3
wenzelm@17110
     4
Isar proof methods.
wenzelm@5824
     5
*)
wenzelm@5824
     6
wenzelm@5824
     7
signature BASIC_METHOD =
wenzelm@5824
     8
sig
wenzelm@17110
     9
  val FINDGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq
wenzelm@17110
    10
  val HEADGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq
wenzelm@17110
    11
  type method
wenzelm@11731
    12
  val trace_rules: bool ref
wenzelm@5824
    13
end;
wenzelm@5824
    14
wenzelm@5824
    15
signature METHOD =
wenzelm@5824
    16
sig
wenzelm@5824
    17
  include BASIC_METHOD
wenzelm@25957
    18
  val apply: Position.T -> (Proof.context -> method) -> Proof.context -> thm list -> cases_tactic
wenzelm@18227
    19
  val RAW_METHOD_CASES: (thm list -> cases_tactic) -> method
wenzelm@17110
    20
  val RAW_METHOD: (thm list -> tactic) -> method
wenzelm@18227
    21
  val METHOD_CASES: (thm list -> cases_tactic) -> method
wenzelm@17110
    22
  val METHOD: (thm list -> tactic) -> method
wenzelm@17110
    23
  val fail: method
wenzelm@17110
    24
  val succeed: method
wenzelm@17110
    25
  val insert_tac: thm list -> int -> tactic
wenzelm@17110
    26
  val insert: thm list -> method
wenzelm@17110
    27
  val insert_facts: method
wenzelm@17110
    28
  val SIMPLE_METHOD: tactic -> method
wenzelm@21592
    29
  val SIMPLE_METHOD': (int -> tactic) -> method
wenzelm@21592
    30
  val SIMPLE_METHOD'': ((int -> tactic) -> tactic) -> (int -> tactic) -> method
wenzelm@17110
    31
  val defer: int option -> method
wenzelm@17110
    32
  val prefer: int -> method
wenzelm@20289
    33
  val cheating: bool -> Proof.context -> method
wenzelm@17110
    34
  val intro: thm list -> method
wenzelm@17110
    35
  val elim: thm list -> method
wenzelm@20289
    36
  val unfold: thm list -> Proof.context -> method
wenzelm@20289
    37
  val fold: thm list -> Proof.context -> method
wenzelm@17110
    38
  val atomize: bool -> method
wenzelm@17110
    39
  val this: method
wenzelm@20289
    40
  val fact: thm list -> Proof.context -> method
kleing@29857
    41
  val assumption_tac: Proof.context -> int -> tactic
wenzelm@20289
    42
  val assumption: Proof.context -> method
wenzelm@20289
    43
  val close: bool -> Proof.context -> method
wenzelm@20289
    44
  val trace: Proof.context -> thm list -> unit
wenzelm@6091
    45
  val rule_tac: thm list -> thm list -> int -> tactic
wenzelm@20289
    46
  val some_rule_tac: thm list -> Proof.context -> thm list -> int -> tactic
haftmann@25270
    47
  val intros_tac: thm list -> thm list -> tactic
wenzelm@17110
    48
  val rule: thm list -> method
wenzelm@17110
    49
  val erule: int -> thm list -> method
wenzelm@17110
    50
  val drule: int -> thm list -> method
wenzelm@17110
    51
  val frule: int -> thm list -> method
wenzelm@27235
    52
  val set_tactic: (thm list -> tactic) -> Proof.context -> Proof.context
wenzelm@26385
    53
  val tactic: string * Position.T -> Proof.context -> method
wenzelm@27235
    54
  val raw_tactic: string * Position.T -> Proof.context -> method
wenzelm@27729
    55
  type src = Args.src
wenzelm@5824
    56
  datatype text =
wenzelm@23349
    57
    Basic of (Proof.context -> method) * Position.T |
wenzelm@15703
    58
    Source of src |
wenzelm@20030
    59
    Source_i of src |
wenzelm@5824
    60
    Then of text list |
wenzelm@5824
    61
    Orelse of text list |
wenzelm@5824
    62
    Try of text |
wenzelm@19186
    63
    Repeat1 of text |
wenzelm@19186
    64
    SelectGoals of int * text
wenzelm@17857
    65
  val primitive_text: (thm -> thm) -> text
wenzelm@17857
    66
  val succeed_text: text
wenzelm@17110
    67
  val default_text: text
wenzelm@17110
    68
  val this_text: text
wenzelm@17110
    69
  val done_text: text
wenzelm@17356
    70
  val sorry_text: bool -> text
wenzelm@23349
    71
  val finish_text: text option * bool -> Position.T -> text
wenzelm@27729
    72
  val print_methods: theory -> unit
wenzelm@26892
    73
  val intern: theory -> xstring -> string
wenzelm@26892
    74
  val defined: theory -> string -> bool
wenzelm@20289
    75
  val method: theory -> src -> Proof.context -> method
wenzelm@20289
    76
  val method_i: theory -> src -> Proof.context -> method
wenzelm@20289
    77
  val add_methods: (bstring * (src -> Proof.context -> method) * string) list
wenzelm@17110
    78
    -> theory -> theory
wenzelm@20289
    79
  val add_method: bstring * (src -> Proof.context -> method) * string
wenzelm@17110
    80
    -> theory -> theory
wenzelm@26385
    81
  val method_setup: bstring -> string * Position.T -> string -> theory -> theory
wenzelm@18999
    82
  val syntax: (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list))
haftmann@21879
    83
    -> src -> Proof.context -> 'a * Proof.context
wenzelm@17110
    84
  val simple_args: (Args.T list -> 'a * Args.T list)
wenzelm@20289
    85
    -> ('a -> Proof.context -> method) -> src -> Proof.context -> method
wenzelm@20289
    86
  val ctxt_args: (Proof.context -> method) -> src -> Proof.context -> method
wenzelm@20289
    87
  val no_args: method -> src -> Proof.context -> method
wenzelm@17110
    88
  type modifier
wenzelm@18999
    89
  val sectioned_args: (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) ->
wenzelm@17110
    90
    (Args.T list -> modifier * Args.T list) list ->
wenzelm@20289
    91
    ('a -> Proof.context -> 'b) -> src -> Proof.context -> 'b
wenzelm@17110
    92
  val bang_sectioned_args:
wenzelm@17110
    93
    (Args.T list -> modifier * Args.T list) list ->
wenzelm@20289
    94
    (thm list -> Proof.context -> 'a) -> src -> Proof.context -> 'a
wenzelm@17110
    95
  val bang_sectioned_args':
wenzelm@17110
    96
    (Args.T list -> modifier * Args.T list) list ->
wenzelm@18999
    97
    (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) ->
wenzelm@20289
    98
    ('a -> thm list -> Proof.context -> 'b) -> src -> Proof.context -> 'b
wenzelm@17110
    99
  val only_sectioned_args:
wenzelm@17110
   100
    (Args.T list -> modifier * Args.T list) list ->
wenzelm@20289
   101
    (Proof.context -> 'a) -> src -> Proof.context -> 'a
wenzelm@20289
   102
  val thms_ctxt_args: (thm list -> Proof.context -> 'a) -> src ->
wenzelm@20289
   103
    Proof.context -> 'a
wenzelm@20289
   104
  val thms_args: (thm list -> 'a) -> src -> Proof.context -> 'a
wenzelm@20289
   105
  val thm_args: (thm -> 'a) -> src -> Proof.context -> 'a
wenzelm@9539
   106
  val goal_args: (Args.T list -> 'a * Args.T list) -> ('a -> int -> tactic)
wenzelm@20289
   107
    -> src -> Proof.context -> method
wenzelm@18999
   108
  val goal_args': (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list))
wenzelm@20289
   109
    -> ('a -> int -> tactic) -> src -> Proof.context -> method
wenzelm@17110
   110
  val goal_args_ctxt: (Args.T list -> 'a * Args.T list) ->
wenzelm@20289
   111
    (Proof.context -> 'a -> int -> tactic) -> src -> Proof.context -> method
wenzelm@18999
   112
  val goal_args_ctxt': (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) ->
wenzelm@20289
   113
    (Proof.context -> 'a -> int -> tactic) -> src -> Proof.context -> method
wenzelm@22118
   114
  val parse: OuterLex.token list -> text * OuterLex.token list
wenzelm@5824
   115
end;
wenzelm@5824
   116
wenzelm@5824
   117
structure Method: METHOD =
wenzelm@5824
   118
struct
wenzelm@5824
   119
wenzelm@17110
   120
(** generic tools **)
wenzelm@17110
   121
wenzelm@17110
   122
(* goal addressing *)
wenzelm@17110
   123
wenzelm@17110
   124
fun FINDGOAL tac st =
wenzelm@17110
   125
  let fun find i n = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1) n)
wenzelm@17110
   126
  in find 1 (Thm.nprems_of st) st end;
wenzelm@17110
   127
wenzelm@17110
   128
fun HEADGOAL tac = tac 1;
wenzelm@17110
   129
wenzelm@17110
   130
wenzelm@5824
   131
wenzelm@12324
   132
(** proof methods **)
wenzelm@12324
   133
wenzelm@17110
   134
(* datatype method *)
wenzelm@11731
   135
wenzelm@18227
   136
datatype method = Meth of thm list -> cases_tactic;
wenzelm@11731
   137
wenzelm@25957
   138
fun apply pos meth_fun ctxt facts goal = Position.setmp_thread_data_seq pos
wenzelm@25957
   139
  (fn () => let val Meth meth = meth_fun ctxt in meth facts goal end) ();
wenzelm@11731
   140
wenzelm@17756
   141
val RAW_METHOD_CASES = Meth;
wenzelm@11731
   142
wenzelm@17110
   143
fun RAW_METHOD tac = RAW_METHOD_CASES (NO_CASES o tac);
wenzelm@12144
   144
wenzelm@17110
   145
fun METHOD_CASES tac = RAW_METHOD_CASES (fn facts =>
wenzelm@21687
   146
  Seq.THEN (ALLGOALS Goal.conjunction_tac, tac facts));
wenzelm@8372
   147
wenzelm@21687
   148
fun METHOD tac = RAW_METHOD (fn facts => ALLGOALS Goal.conjunction_tac THEN tac facts);
wenzelm@5824
   149
wenzelm@5824
   150
val fail = METHOD (K no_tac);
wenzelm@5824
   151
val succeed = METHOD (K all_tac);
wenzelm@5824
   152
wenzelm@5824
   153
wenzelm@17110
   154
(* insert facts *)
wenzelm@7419
   155
wenzelm@7419
   156
local
wenzelm@5824
   157
wenzelm@21579
   158
fun cut_rule_tac rule =
wenzelm@21579
   159
  Tactic.rtac (Drule.forall_intr_vars rule COMP_INCR revcut_rl);
wenzelm@6981
   160
wenzelm@7419
   161
in
wenzelm@5824
   162
wenzelm@7419
   163
fun insert_tac [] i = all_tac
wenzelm@7419
   164
  | insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts);
wenzelm@6981
   165
wenzelm@7555
   166
val insert_facts = METHOD (ALLGOALS o insert_tac);
wenzelm@7664
   167
fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms));
wenzelm@7419
   168
wenzelm@9706
   169
fun SIMPLE_METHOD tac = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN tac);
wenzelm@21592
   170
fun SIMPLE_METHOD'' quant tac = METHOD (fn facts => quant (insert_tac facts THEN' tac));
wenzelm@21592
   171
val SIMPLE_METHOD' = SIMPLE_METHOD'' HEADGOAL;
wenzelm@9706
   172
wenzelm@12324
   173
end;
wenzelm@12324
   174
wenzelm@9706
   175
wenzelm@17110
   176
(* shuffle subgoals *)
wenzelm@17110
   177
wenzelm@17110
   178
fun prefer i = METHOD (K (Tactic.defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1)));
wenzelm@18939
   179
fun defer opt_i = METHOD (K (Tactic.defer_tac (the_default 1 opt_i)));
wenzelm@17110
   180
wenzelm@17110
   181
wenzelm@17356
   182
(* cheating *)
wenzelm@17356
   183
wenzelm@17356
   184
fun cheating int ctxt = METHOD (K (setmp quick_and_dirty (int orelse ! quick_and_dirty)
wenzelm@17356
   185
    (SkipProof.cheat_tac (ProofContext.theory_of ctxt))));
wenzelm@17356
   186
wenzelm@17356
   187
wenzelm@17110
   188
(* unfold intro/elim rules *)
wenzelm@17110
   189
wenzelm@21592
   190
fun intro ths = SIMPLE_METHOD' (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.match_tac ths));
wenzelm@21592
   191
fun elim ths = SIMPLE_METHOD' (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.ematch_tac ths));
wenzelm@17110
   192
wenzelm@17110
   193
wenzelm@12384
   194
(* unfold/fold definitions *)
wenzelm@12384
   195
wenzelm@18877
   196
fun unfold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (LocalDefs.unfold_tac ctxt ths));
wenzelm@18877
   197
fun fold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (LocalDefs.fold_tac ctxt ths));
wenzelm@6532
   198
wenzelm@12384
   199
wenzelm@12829
   200
(* atomize rule statements *)
wenzelm@12829
   201
wenzelm@23590
   202
fun atomize false = SIMPLE_METHOD' (CHANGED_PROP o ObjectLogic.atomize_prems_tac)
wenzelm@12829
   203
  | atomize true = RAW_METHOD (K (HEADGOAL (CHANGED_PROP o ObjectLogic.full_atomize_tac)));
wenzelm@12829
   204
wenzelm@12829
   205
wenzelm@18039
   206
(* this -- resolve facts directly *)
wenzelm@12384
   207
wenzelm@17110
   208
val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac));
wenzelm@9484
   209
wenzelm@9484
   210
wenzelm@18039
   211
(* fact -- composition by facts from context *)
wenzelm@18039
   212
wenzelm@21592
   213
fun fact [] ctxt = SIMPLE_METHOD' (ProofContext.some_fact_tac ctxt)
wenzelm@21592
   214
  | fact rules _ = SIMPLE_METHOD' (ProofContext.fact_tac rules);
wenzelm@18039
   215
wenzelm@18039
   216
wenzelm@17110
   217
(* assumption *)
wenzelm@7419
   218
wenzelm@7419
   219
local
wenzelm@7419
   220
wenzelm@19778
   221
fun cond_rtac cond rule = SUBGOAL (fn (prop, i) =>
wenzelm@19778
   222
  if cond (Logic.strip_assums_concl prop)
wenzelm@19778
   223
  then Tactic.rtac rule i else no_tac);
wenzelm@7419
   224
kleing@29857
   225
in
kleing@29857
   226
kleing@29857
   227
fun assumption_tac ctxt =
wenzelm@17110
   228
  assume_tac APPEND'
wenzelm@23349
   229
  Goal.assume_rule_tac ctxt APPEND'
wenzelm@19778
   230
  cond_rtac (can Logic.dest_equals) Drule.reflexive_thm APPEND'
wenzelm@19778
   231
  cond_rtac (can Logic.dest_term) Drule.termI;
wenzelm@17110
   232
wenzelm@23349
   233
fun assumption ctxt = METHOD (HEADGOAL o
kleing@29857
   234
  (fn [] => assumption_tac ctxt
wenzelm@23349
   235
    | [fact] => solve_tac [fact]
wenzelm@23349
   236
    | _ => K no_tac));
wenzelm@23349
   237
wenzelm@17356
   238
fun close immed ctxt = METHOD (K
wenzelm@23349
   239
  (FILTER Thm.no_prems
kleing@29857
   240
    ((if immed then ALLGOALS (assumption_tac ctxt) else all_tac) THEN flexflex_tac)));
wenzelm@7419
   241
wenzelm@7419
   242
end;
wenzelm@7419
   243
wenzelm@7419
   244
wenzelm@17110
   245
(* rule etc. -- single-step refinements *)
wenzelm@12347
   246
wenzelm@17110
   247
val trace_rules = ref false;
wenzelm@12347
   248
wenzelm@17110
   249
fun trace ctxt rules =
wenzelm@21962
   250
  if ! trace_rules andalso not (null rules) then
wenzelm@17110
   251
    Pretty.big_list "rules:" (map (ProofContext.pretty_thm ctxt) rules)
wenzelm@21962
   252
    |> Pretty.string_of |> tracing
wenzelm@21962
   253
  else ();
wenzelm@12347
   254
wenzelm@12347
   255
local
wenzelm@12347
   256
wenzelm@18841
   257
fun gen_rule_tac tac rules facts =
wenzelm@18841
   258
  (fn i => fn st =>
wenzelm@18841
   259
    if null facts then tac rules i st
wenzelm@18841
   260
    else Seq.maps (fn rule => (tac o single) rule i st) (Drule.multi_resolves facts rules))
wenzelm@21687
   261
  THEN_ALL_NEW Goal.norm_hhf_tac;
wenzelm@7130
   262
wenzelm@10744
   263
fun gen_arule_tac tac j rules facts =
wenzelm@10744
   264
  EVERY' (gen_rule_tac tac rules facts :: replicate j Tactic.assume_tac);
wenzelm@10744
   265
wenzelm@11785
   266
fun gen_some_rule_tac tac arg_rules ctxt facts = SUBGOAL (fn (goal, i) =>
wenzelm@11785
   267
  let
wenzelm@11785
   268
    val rules =
wenzelm@11785
   269
      if not (null arg_rules) then arg_rules
wenzelm@19482
   270
      else flat (ContextRules.find_rules false facts goal ctxt)
wenzelm@12055
   271
  in trace ctxt rules; tac rules facts i end);
wenzelm@10309
   272
wenzelm@10744
   273
fun meth tac x = METHOD (HEADGOAL o tac x);
wenzelm@10744
   274
fun meth' tac x y = METHOD (HEADGOAL o tac x y);
wenzelm@8220
   275
wenzelm@7419
   276
in
wenzelm@7419
   277
wenzelm@10744
   278
val rule_tac = gen_rule_tac Tactic.resolve_tac;
wenzelm@10744
   279
val rule = meth rule_tac;
wenzelm@10744
   280
val some_rule_tac = gen_some_rule_tac rule_tac;
wenzelm@10744
   281
val some_rule = meth' some_rule_tac;
wenzelm@10744
   282
wenzelm@10744
   283
val erule = meth' (gen_arule_tac Tactic.eresolve_tac);
wenzelm@10744
   284
val drule = meth' (gen_arule_tac Tactic.dresolve_tac);
wenzelm@10744
   285
val frule = meth' (gen_arule_tac Tactic.forward_tac);
wenzelm@5824
   286
wenzelm@7419
   287
end;
wenzelm@7419
   288
wenzelm@7419
   289
haftmann@25270
   290
(* intros_tac -- pervasive search spanned by intro rules *)
haftmann@25270
   291
haftmann@25270
   292
fun intros_tac intros facts =
haftmann@25270
   293
  ALLGOALS (insert_tac facts THEN'
haftmann@25270
   294
      REPEAT_ALL_NEW (resolve_tac intros))
haftmann@25270
   295
    THEN Tactic.distinct_subgoals_tac;
haftmann@25270
   296
haftmann@25270
   297
wenzelm@8351
   298
(* ML tactics *)
wenzelm@8351
   299
wenzelm@26472
   300
structure TacticData = ProofDataFun
wenzelm@26472
   301
(
wenzelm@27235
   302
  type T = thm list -> tactic;
wenzelm@26472
   303
  fun init _ = undefined;
wenzelm@26472
   304
);
wenzelm@26472
   305
wenzelm@26472
   306
val set_tactic = TacticData.put;
wenzelm@8351
   307
wenzelm@26472
   308
fun ml_tactic (txt, pos) ctxt =
wenzelm@26472
   309
  let
wenzelm@26472
   310
    val ctxt' = ctxt |> Context.proof_map
wenzelm@27235
   311
      (ML_Context.expression pos
wenzelm@27235
   312
        "fun tactic (facts: thm list) : tactic"
wenzelm@26472
   313
        "Context.map_proof (Method.set_tactic tactic)" txt);
wenzelm@27235
   314
  in Context.setmp_thread_data (SOME (Context.Proof ctxt)) (TacticData.get ctxt') end;
wenzelm@23425
   315
wenzelm@23425
   316
fun tactic txt ctxt = METHOD (ml_tactic txt ctxt);
wenzelm@23425
   317
fun raw_tactic txt ctxt = RAW_METHOD (ml_tactic txt ctxt);
wenzelm@8351
   318
wenzelm@8351
   319
wenzelm@5824
   320
wenzelm@17110
   321
(** method syntax **)
wenzelm@17110
   322
wenzelm@17110
   323
(* method text *)
wenzelm@17110
   324
wenzelm@17110
   325
type src = Args.src;
wenzelm@5824
   326
wenzelm@17110
   327
datatype text =
wenzelm@23349
   328
  Basic of (Proof.context -> method) * Position.T |
wenzelm@17110
   329
  Source of src |
wenzelm@20030
   330
  Source_i of src |
wenzelm@17110
   331
  Then of text list |
wenzelm@17110
   332
  Orelse of text list |
wenzelm@17110
   333
  Try of text |
wenzelm@19186
   334
  Repeat1 of text |
wenzelm@19186
   335
  SelectGoals of int * text;
wenzelm@17110
   336
wenzelm@23349
   337
fun primitive_text r = Basic (K (SIMPLE_METHOD (PRIMITIVE r)), Position.none);
wenzelm@23349
   338
val succeed_text = Basic (K succeed, Position.none);
wenzelm@17110
   339
val default_text = Source (Args.src (("default", []), Position.none));
wenzelm@23349
   340
val this_text = Basic (K this, Position.none);
wenzelm@23349
   341
val done_text = Basic (K (SIMPLE_METHOD all_tac), Position.none);
wenzelm@23349
   342
fun sorry_text int = Basic (cheating int, Position.none);
wenzelm@17110
   343
wenzelm@23349
   344
fun finish_text (NONE, immed) pos = Basic (close immed, pos)
wenzelm@23349
   345
  | finish_text (SOME txt, immed) pos = Then [txt, Basic (close immed, pos)];
wenzelm@17110
   346
wenzelm@17110
   347
wenzelm@17110
   348
(* method definitions *)
wenzelm@5824
   349
wenzelm@24116
   350
structure Methods = TheoryDataFun
wenzelm@22846
   351
(
wenzelm@20289
   352
  type T = (((src -> Proof.context -> method) * string) * stamp) NameSpace.table;
wenzelm@16347
   353
  val empty = NameSpace.empty_table;
wenzelm@6546
   354
  val copy = I;
wenzelm@16448
   355
  val extend = I;
wenzelm@23655
   356
  fun merge _ tables : T = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUP dup =>
wenzelm@23655
   357
    error ("Attempt to merge different versions of method " ^ quote dup);
wenzelm@22846
   358
);
wenzelm@5824
   359
wenzelm@22846
   360
fun print_methods thy =
wenzelm@22846
   361
  let
wenzelm@24116
   362
    val meths = Methods.get thy;
wenzelm@22846
   363
    fun prt_meth (name, ((_, comment), _)) = Pretty.block
wenzelm@22846
   364
      [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@22846
   365
  in
wenzelm@22846
   366
    [Pretty.big_list "methods:" (map prt_meth (NameSpace.extern_table meths))]
wenzelm@22846
   367
    |> Pretty.chunks |> Pretty.writeln
wenzelm@22846
   368
  end;
wenzelm@7611
   369
wenzelm@5824
   370
wenzelm@5824
   371
(* get methods *)
wenzelm@5824
   372
wenzelm@26892
   373
val intern = NameSpace.intern o #1 o Methods.get;
wenzelm@26892
   374
val defined = Symtab.defined o #2 o Methods.get;
wenzelm@26892
   375
wenzelm@20030
   376
fun method_i thy =
wenzelm@5824
   377
  let
wenzelm@24116
   378
    val meths = #2 (Methods.get thy);
wenzelm@5884
   379
    fun meth src =
wenzelm@20030
   380
      let val ((name, _), pos) = Args.dest_src src in
wenzelm@17412
   381
        (case Symtab.lookup meths name of
skalberg@15531
   382
          NONE => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
wenzelm@27751
   383
        | SOME ((mth, _), _) => (Position.report (Markup.method name) pos; mth src))
wenzelm@5824
   384
      end;
wenzelm@5824
   385
  in meth end;
wenzelm@5824
   386
wenzelm@24116
   387
fun method thy = method_i thy o Args.map_name (NameSpace.intern (#1 (Methods.get thy)));
wenzelm@20030
   388
wenzelm@5824
   389
wenzelm@17110
   390
(* add method *)
wenzelm@5824
   391
wenzelm@5824
   392
fun add_methods raw_meths thy =
wenzelm@5824
   393
  let
wenzelm@16145
   394
    val new_meths = raw_meths |> map (fn (name, f, comment) =>
haftmann@29004
   395
      (Binding.name name, ((f, comment), stamp ())));
wenzelm@5824
   396
haftmann@29004
   397
    fun add meths = fold (snd oo NameSpace.bind (Sign.naming_of thy)) new_meths meths
wenzelm@23655
   398
      handle Symtab.DUP dup => error ("Duplicate declaration of method " ^ quote dup);
wenzelm@24116
   399
  in Methods.map add thy end;
wenzelm@5824
   400
wenzelm@9194
   401
val add_method = add_methods o Library.single;
wenzelm@9194
   402
wenzelm@5824
   403
wenzelm@17356
   404
(* method_setup *)
wenzelm@17356
   405
wenzelm@26385
   406
fun method_setup name (txt, pos) cmt =
wenzelm@26472
   407
  Context.theory_map (ML_Context.expression pos
wenzelm@22118
   408
    "val method: bstring * (Method.src -> Proof.context -> Proof.method) * string"
wenzelm@22086
   409
    "Context.map_theory (Method.add_method method)"
wenzelm@26472
   410
    ("(" ^ quote name ^ ", " ^ txt ^ ", " ^ quote cmt ^ ")"));
wenzelm@17356
   411
wenzelm@17356
   412
wenzelm@5884
   413
wenzelm@17110
   414
(** concrete syntax **)
wenzelm@5824
   415
wenzelm@27813
   416
structure P = OuterParse;
wenzelm@27813
   417
wenzelm@27813
   418
wenzelm@5884
   419
(* basic *)
wenzelm@5884
   420
wenzelm@18999
   421
fun syntax scan = Args.context_syntax "method" scan;
wenzelm@5824
   422
wenzelm@17110
   423
fun simple_args scan f src ctxt : method =
haftmann@21879
   424
  fst (syntax (Scan.lift (scan >> (fn x => f x ctxt))) src ctxt);
wenzelm@8351
   425
wenzelm@20289
   426
fun ctxt_args (f: Proof.context -> method) src ctxt =
haftmann@21879
   427
  fst (syntax (Scan.succeed (f ctxt)) src ctxt);
wenzelm@7555
   428
wenzelm@7555
   429
fun no_args m = ctxt_args (K m);
wenzelm@5884
   430
wenzelm@5884
   431
wenzelm@5884
   432
(* sections *)
wenzelm@5824
   433
wenzelm@20289
   434
type modifier = (Proof.context -> Proof.context) * attribute;
wenzelm@7268
   435
wenzelm@7268
   436
local
wenzelm@7268
   437
wenzelm@24010
   438
fun thms ss = Scan.repeat (Scan.unless (Scan.lift (Scan.first ss)) Attrib.multi_thm) >> flat;
wenzelm@19186
   439
fun app (f, att) (context, ths) = foldl_map att (Context.map_proof f context, ths);
wenzelm@5824
   440
wenzelm@24022
   441
fun section ss = Scan.depend (fn context => (Scan.first ss -- Scan.pass context (thms ss)) :|--
wenzelm@24022
   442
  (fn (m, ths) => Scan.succeed (app m (context, ths))));
wenzelm@5884
   443
wenzelm@7601
   444
fun sectioned args ss = args -- Scan.repeat (section ss);
wenzelm@5884
   445
wenzelm@7268
   446
in
wenzelm@5824
   447
wenzelm@5884
   448
fun sectioned_args args ss f src ctxt =
haftmann@21879
   449
  let val ((x, _), ctxt') = syntax (sectioned args ss) src ctxt
wenzelm@5921
   450
  in f x ctxt' end;
wenzelm@5884
   451
wenzelm@7601
   452
fun bang_sectioned_args ss f = sectioned_args Args.bang_facts ss f;
wenzelm@9777
   453
fun bang_sectioned_args' ss scan f =
wenzelm@9777
   454
  sectioned_args (Args.bang_facts -- scan >> swap) ss (uncurry f);
wenzelm@7601
   455
fun only_sectioned_args ss f = sectioned_args (Scan.succeed ()) ss (fn () => f);
wenzelm@7268
   456
wenzelm@18999
   457
fun thms_ctxt_args f = sectioned_args (thms []) [] f;
wenzelm@8093
   458
fun thms_args f = thms_ctxt_args (K o f);
wenzelm@9706
   459
fun thm_args f = thms_args (fn [thm] => f thm | _ => error "Single theorem expected");
wenzelm@5824
   460
wenzelm@7268
   461
end;
wenzelm@7268
   462
wenzelm@5824
   463
wenzelm@9539
   464
(* tactic syntax *)
wenzelm@8238
   465
wenzelm@10744
   466
fun nat_thms_args f = uncurry f oo
wenzelm@27813
   467
  (fst oo syntax (Scan.lift (Scan.optional (Args.parens P.nat) 0) -- Attrib.thms));
wenzelm@10744
   468
haftmann@21879
   469
fun goal_args' args tac src ctxt = fst (syntax (Args.goal_spec HEADGOAL -- args >>
wenzelm@21592
   470
  (fn (quant, s) => SIMPLE_METHOD'' quant (tac s))) src ctxt);
wenzelm@8537
   471
wenzelm@9539
   472
fun goal_args args tac = goal_args' (Scan.lift args) tac;
wenzelm@8238
   473
ballarin@14174
   474
fun goal_args_ctxt' args tac src ctxt =
haftmann@21879
   475
  fst (syntax (Args.goal_spec HEADGOAL -- args >>
wenzelm@21592
   476
  (fn (quant, s) => SIMPLE_METHOD'' quant (tac ctxt s))) src ctxt);
wenzelm@8238
   477
ballarin@14174
   478
fun goal_args_ctxt args tac = goal_args_ctxt' (Scan.lift args) tac;
wenzelm@5824
   479
wenzelm@14718
   480
wenzelm@27813
   481
(* outer parser *)
wenzelm@27813
   482
wenzelm@27813
   483
fun is_symid_meth s =
wenzelm@27813
   484
  s <> "|" andalso s <> "?" andalso s <> "+" andalso OuterLex.ident_or_symbolic s;
wenzelm@27813
   485
wenzelm@27813
   486
local
wenzelm@27813
   487
wenzelm@27813
   488
fun meth4 x =
wenzelm@27813
   489
 (P.position (P.xname >> rpair []) >> (Source o Args.src) ||
wenzelm@27813
   490
  P.$$$ "(" |-- P.!!! (meth0 --| P.$$$ ")")) x
wenzelm@27813
   491
and meth3 x =
wenzelm@27813
   492
 (meth4 --| P.$$$ "?" >> Try ||
wenzelm@27813
   493
  meth4 --| P.$$$ "+" >> Repeat1 ||
wenzelm@27813
   494
  meth4 -- (P.$$$ "[" |-- Scan.optional P.nat 1 --| P.$$$ "]") >> (SelectGoals o swap) ||
wenzelm@27813
   495
  meth4) x
wenzelm@27813
   496
and meth2 x =
wenzelm@27813
   497
 (P.position (P.xname -- Args.parse1 is_symid_meth) >> (Source o Args.src) ||
wenzelm@27813
   498
  meth3) x
wenzelm@27813
   499
and meth1 x = (P.enum1 "," meth2 >> (fn [m] => m | ms => Then ms)) x
wenzelm@27813
   500
and meth0 x = (P.enum1 "|" meth1 >> (fn [m] => m | ms => Orelse ms)) x;
wenzelm@27813
   501
wenzelm@27813
   502
in val parse = meth3 end;
wenzelm@27813
   503
wenzelm@27813
   504
wenzelm@18708
   505
(* theory setup *)
wenzelm@5824
   506
wenzelm@26463
   507
val _ = Context.>> (Context.map_theory
wenzelm@26463
   508
  (add_methods
wenzelm@26463
   509
   [("fail", no_args fail, "force failure"),
wenzelm@26463
   510
    ("succeed", no_args succeed, "succeed"),
wenzelm@26463
   511
    ("-", no_args insert_facts, "do nothing (insert current facts only)"),
wenzelm@26463
   512
    ("insert", thms_args insert, "insert theorems, ignoring facts (improper)"),
wenzelm@26463
   513
    ("intro", thms_args intro, "repeatedly apply introduction rules"),
wenzelm@26463
   514
    ("elim", thms_args elim, "repeatedly apply elimination rules"),
wenzelm@26463
   515
    ("unfold", thms_ctxt_args unfold_meth, "unfold definitions"),
wenzelm@26463
   516
    ("fold", thms_ctxt_args fold_meth, "fold definitions"),
wenzelm@26463
   517
    ("atomize", (atomize o fst) oo syntax (Args.mode "full"),
wenzelm@26463
   518
      "present local premises as object-level statements"),
wenzelm@26463
   519
    ("rule", thms_ctxt_args some_rule, "apply some intro/elim rule"),
wenzelm@26463
   520
    ("erule", nat_thms_args erule, "apply rule in elimination manner (improper)"),
wenzelm@26463
   521
    ("drule", nat_thms_args drule, "apply rule in destruct manner (improper)"),
wenzelm@26463
   522
    ("frule", nat_thms_args frule, "apply rule in forward manner (improper)"),
wenzelm@26463
   523
    ("this", no_args this, "apply current facts as rules"),
wenzelm@26463
   524
    ("fact", thms_ctxt_args fact, "composition by facts from context"),
wenzelm@26463
   525
    ("assumption", ctxt_args assumption, "proof by assumption, preferring facts"),
wenzelm@27244
   526
    ("rename_tac", goal_args (Scan.repeat1 Args.name) Tactic.rename_tac,
wenzelm@26463
   527
      "rename parameters of goal"),
wenzelm@27813
   528
    ("rotate_tac", goal_args (Scan.optional P.int 1) Tactic.rotate_tac,
wenzelm@26463
   529
      "rotate assumptions of goal"),
wenzelm@27813
   530
    ("tactic", simple_args (P.position Args.name) tactic, "ML tactic as proof method"),
wenzelm@27813
   531
    ("raw_tactic", simple_args (P.position Args.name) raw_tactic,
wenzelm@26463
   532
      "ML tactic as raw proof method")]));
wenzelm@5824
   533
wenzelm@5824
   534
wenzelm@16145
   535
(*final declarations of this structure!*)
wenzelm@16145
   536
val unfold = unfold_meth;
wenzelm@16145
   537
val fold = fold_meth;
wenzelm@16145
   538
wenzelm@5824
   539
end;
wenzelm@5824
   540
wenzelm@5824
   541
structure BasicMethod: BASIC_METHOD = Method;
wenzelm@5824
   542
open BasicMethod;