src/Pure/Isar/method.ML
author wenzelm
Mon May 03 14:25:56 2010 +0200 (2010-05-03)
changeset 36610 bafd82950e24
parent 36096 abc6a2ea4b88
child 36950 75b8f26f2f07
permissions -rw-r--r--
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
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@32738
    11
  val trace_rules: bool Unsynchronized.ref
wenzelm@5824
    12
end;
wenzelm@5824
    13
wenzelm@5824
    14
signature METHOD =
wenzelm@5824
    15
sig
wenzelm@5824
    16
  include BASIC_METHOD
wenzelm@30508
    17
  type method
wenzelm@32193
    18
  val apply: (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
wenzelm@30234
    41
  val assm_tac: Proof.context -> int -> tactic
wenzelm@30567
    42
  val all_assm_tac: Proof.context -> tactic
wenzelm@20289
    43
  val assumption: Proof.context -> method
wenzelm@20289
    44
  val close: bool -> Proof.context -> method
wenzelm@20289
    45
  val trace: Proof.context -> thm list -> unit
wenzelm@6091
    46
  val rule_tac: thm list -> thm list -> int -> tactic
wenzelm@20289
    47
  val some_rule_tac: thm list -> Proof.context -> thm list -> int -> tactic
haftmann@25270
    48
  val intros_tac: thm list -> thm list -> tactic
ballarin@36093
    49
  val try_intros_tac: thm list -> thm list -> tactic
wenzelm@17110
    50
  val rule: thm list -> method
wenzelm@17110
    51
  val erule: int -> thm list -> method
wenzelm@17110
    52
  val drule: int -> thm list -> method
wenzelm@17110
    53
  val frule: int -> thm list -> method
wenzelm@27235
    54
  val set_tactic: (thm list -> tactic) -> Proof.context -> Proof.context
wenzelm@26385
    55
  val tactic: string * Position.T -> Proof.context -> method
wenzelm@27235
    56
  val raw_tactic: string * Position.T -> Proof.context -> method
wenzelm@27729
    57
  type src = Args.src
wenzelm@5824
    58
  datatype text =
wenzelm@32193
    59
    Basic of Proof.context -> method |
wenzelm@15703
    60
    Source of src |
wenzelm@20030
    61
    Source_i of src |
wenzelm@5824
    62
    Then of text list |
wenzelm@5824
    63
    Orelse of text list |
wenzelm@5824
    64
    Try of text |
wenzelm@19186
    65
    Repeat1 of text |
wenzelm@19186
    66
    SelectGoals of int * text
wenzelm@17857
    67
  val primitive_text: (thm -> thm) -> text
wenzelm@17857
    68
  val succeed_text: text
wenzelm@17110
    69
  val default_text: text
wenzelm@17110
    70
  val this_text: text
wenzelm@17110
    71
  val done_text: text
wenzelm@17356
    72
  val sorry_text: bool -> text
wenzelm@32193
    73
  val finish_text: text option * bool -> text
wenzelm@27729
    74
  val print_methods: theory -> unit
wenzelm@26892
    75
  val intern: theory -> xstring -> string
wenzelm@26892
    76
  val defined: theory -> string -> bool
wenzelm@20289
    77
  val method: theory -> src -> Proof.context -> method
wenzelm@20289
    78
  val method_i: theory -> src -> Proof.context -> method
wenzelm@30512
    79
  val syntax: 'a context_parser -> src -> Proof.context -> 'a * Proof.context
wenzelm@30512
    80
  val setup: binding -> (Proof.context -> method) context_parser -> string -> theory -> theory
wenzelm@30575
    81
  val method_setup: bstring * Position.T -> Symbol_Pos.text * Position.T -> string ->
wenzelm@30575
    82
    theory -> theory
wenzelm@30540
    83
  type modifier = (Proof.context -> Proof.context) * attribute
wenzelm@30540
    84
  val section: modifier parser list -> thm list context_parser
wenzelm@30540
    85
  val sections: modifier parser list -> thm list list context_parser
wenzelm@30512
    86
  val parse: text parser
wenzelm@5824
    87
end;
wenzelm@5824
    88
wenzelm@5824
    89
structure Method: METHOD =
wenzelm@5824
    90
struct
wenzelm@5824
    91
wenzelm@17110
    92
(** generic tools **)
wenzelm@17110
    93
wenzelm@17110
    94
(* goal addressing *)
wenzelm@17110
    95
wenzelm@17110
    96
fun FINDGOAL tac st =
wenzelm@17110
    97
  let fun find i n = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1) n)
wenzelm@17110
    98
  in find 1 (Thm.nprems_of st) st end;
wenzelm@17110
    99
wenzelm@17110
   100
fun HEADGOAL tac = tac 1;
wenzelm@17110
   101
wenzelm@17110
   102
wenzelm@5824
   103
wenzelm@12324
   104
(** proof methods **)
wenzelm@12324
   105
wenzelm@17110
   106
(* datatype method *)
wenzelm@11731
   107
wenzelm@18227
   108
datatype method = Meth of thm list -> cases_tactic;
wenzelm@11731
   109
wenzelm@32193
   110
fun apply meth ctxt = let val Meth m = meth ctxt in m end;
wenzelm@11731
   111
wenzelm@17756
   112
val RAW_METHOD_CASES = Meth;
wenzelm@11731
   113
wenzelm@17110
   114
fun RAW_METHOD tac = RAW_METHOD_CASES (NO_CASES o tac);
wenzelm@12144
   115
wenzelm@17110
   116
fun METHOD_CASES tac = RAW_METHOD_CASES (fn facts =>
wenzelm@21687
   117
  Seq.THEN (ALLGOALS Goal.conjunction_tac, tac facts));
wenzelm@8372
   118
wenzelm@21687
   119
fun METHOD tac = RAW_METHOD (fn facts => ALLGOALS Goal.conjunction_tac THEN tac facts);
wenzelm@5824
   120
wenzelm@5824
   121
val fail = METHOD (K no_tac);
wenzelm@5824
   122
val succeed = METHOD (K all_tac);
wenzelm@5824
   123
wenzelm@5824
   124
wenzelm@17110
   125
(* insert facts *)
wenzelm@7419
   126
wenzelm@7419
   127
local
wenzelm@5824
   128
wenzelm@21579
   129
fun cut_rule_tac rule =
wenzelm@21579
   130
  Tactic.rtac (Drule.forall_intr_vars rule COMP_INCR revcut_rl);
wenzelm@6981
   131
wenzelm@7419
   132
in
wenzelm@5824
   133
wenzelm@7419
   134
fun insert_tac [] i = all_tac
wenzelm@7419
   135
  | insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts);
wenzelm@6981
   136
wenzelm@7555
   137
val insert_facts = METHOD (ALLGOALS o insert_tac);
wenzelm@7664
   138
fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms));
wenzelm@7419
   139
wenzelm@9706
   140
fun SIMPLE_METHOD tac = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN tac);
wenzelm@21592
   141
fun SIMPLE_METHOD'' quant tac = METHOD (fn facts => quant (insert_tac facts THEN' tac));
wenzelm@21592
   142
val SIMPLE_METHOD' = SIMPLE_METHOD'' HEADGOAL;
wenzelm@9706
   143
wenzelm@12324
   144
end;
wenzelm@12324
   145
wenzelm@9706
   146
wenzelm@17110
   147
(* shuffle subgoals *)
wenzelm@17110
   148
wenzelm@17110
   149
fun prefer i = METHOD (K (Tactic.defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1)));
wenzelm@18939
   150
fun defer opt_i = METHOD (K (Tactic.defer_tac (the_default 1 opt_i)));
wenzelm@17110
   151
wenzelm@17110
   152
wenzelm@17356
   153
(* cheating *)
wenzelm@17356
   154
wenzelm@32969
   155
fun cheating int ctxt =
wenzelm@32969
   156
  if int orelse ! quick_and_dirty then
wenzelm@32969
   157
    METHOD (K (Skip_Proof.cheat_tac (ProofContext.theory_of ctxt)))
wenzelm@32969
   158
  else error "Cheating requires quick_and_dirty mode!";
wenzelm@17356
   159
wenzelm@17356
   160
wenzelm@17110
   161
(* unfold intro/elim rules *)
wenzelm@17110
   162
wenzelm@21592
   163
fun intro ths = SIMPLE_METHOD' (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.match_tac ths));
wenzelm@21592
   164
fun elim ths = SIMPLE_METHOD' (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.ematch_tac ths));
wenzelm@17110
   165
wenzelm@17110
   166
wenzelm@12384
   167
(* unfold/fold definitions *)
wenzelm@12384
   168
wenzelm@35624
   169
fun unfold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (Local_Defs.unfold_tac ctxt ths));
wenzelm@35624
   170
fun fold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (Local_Defs.fold_tac ctxt ths));
wenzelm@6532
   171
wenzelm@12384
   172
wenzelm@12829
   173
(* atomize rule statements *)
wenzelm@12829
   174
wenzelm@35625
   175
fun atomize false = SIMPLE_METHOD' (CHANGED_PROP o Object_Logic.atomize_prems_tac)
wenzelm@35625
   176
  | atomize true = RAW_METHOD (K (HEADGOAL (CHANGED_PROP o Object_Logic.full_atomize_tac)));
wenzelm@12829
   177
wenzelm@12829
   178
wenzelm@18039
   179
(* this -- resolve facts directly *)
wenzelm@12384
   180
wenzelm@17110
   181
val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac));
wenzelm@9484
   182
wenzelm@9484
   183
wenzelm@18039
   184
(* fact -- composition by facts from context *)
wenzelm@18039
   185
wenzelm@21592
   186
fun fact [] ctxt = SIMPLE_METHOD' (ProofContext.some_fact_tac ctxt)
wenzelm@21592
   187
  | fact rules _ = SIMPLE_METHOD' (ProofContext.fact_tac rules);
wenzelm@18039
   188
wenzelm@18039
   189
wenzelm@17110
   190
(* assumption *)
wenzelm@7419
   191
wenzelm@7419
   192
local
wenzelm@7419
   193
wenzelm@19778
   194
fun cond_rtac cond rule = SUBGOAL (fn (prop, i) =>
wenzelm@19778
   195
  if cond (Logic.strip_assums_concl prop)
wenzelm@19778
   196
  then Tactic.rtac rule i else no_tac);
wenzelm@7419
   197
kleing@29857
   198
in
kleing@29857
   199
wenzelm@30234
   200
fun assm_tac ctxt =
wenzelm@17110
   201
  assume_tac APPEND'
wenzelm@23349
   202
  Goal.assume_rule_tac ctxt APPEND'
wenzelm@19778
   203
  cond_rtac (can Logic.dest_equals) Drule.reflexive_thm APPEND'
wenzelm@19778
   204
  cond_rtac (can Logic.dest_term) Drule.termI;
wenzelm@17110
   205
wenzelm@30567
   206
fun all_assm_tac ctxt st = EVERY1 (replicate (Thm.nprems_of st) (assm_tac ctxt)) st;
wenzelm@30567
   207
wenzelm@23349
   208
fun assumption ctxt = METHOD (HEADGOAL o
wenzelm@30234
   209
  (fn [] => assm_tac ctxt
wenzelm@23349
   210
    | [fact] => solve_tac [fact]
wenzelm@23349
   211
    | _ => K no_tac));
wenzelm@23349
   212
wenzelm@17356
   213
fun close immed ctxt = METHOD (K
wenzelm@30567
   214
  (FILTER Thm.no_prems ((if immed then all_assm_tac ctxt else all_tac) THEN flexflex_tac)));
wenzelm@7419
   215
wenzelm@7419
   216
end;
wenzelm@7419
   217
wenzelm@7419
   218
wenzelm@17110
   219
(* rule etc. -- single-step refinements *)
wenzelm@12347
   220
wenzelm@32738
   221
val trace_rules = Unsynchronized.ref false;
wenzelm@12347
   222
wenzelm@17110
   223
fun trace ctxt rules =
wenzelm@21962
   224
  if ! trace_rules andalso not (null rules) then
wenzelm@32091
   225
    Pretty.big_list "rules:" (map (Display.pretty_thm ctxt) rules)
wenzelm@21962
   226
    |> Pretty.string_of |> tracing
wenzelm@21962
   227
  else ();
wenzelm@12347
   228
wenzelm@12347
   229
local
wenzelm@12347
   230
wenzelm@18841
   231
fun gen_rule_tac tac rules facts =
wenzelm@18841
   232
  (fn i => fn st =>
wenzelm@18841
   233
    if null facts then tac rules i st
wenzelm@18841
   234
    else Seq.maps (fn rule => (tac o single) rule i st) (Drule.multi_resolves facts rules))
wenzelm@21687
   235
  THEN_ALL_NEW Goal.norm_hhf_tac;
wenzelm@7130
   236
wenzelm@10744
   237
fun gen_arule_tac tac j rules facts =
wenzelm@10744
   238
  EVERY' (gen_rule_tac tac rules facts :: replicate j Tactic.assume_tac);
wenzelm@10744
   239
wenzelm@11785
   240
fun gen_some_rule_tac tac arg_rules ctxt facts = SUBGOAL (fn (goal, i) =>
wenzelm@11785
   241
  let
wenzelm@11785
   242
    val rules =
wenzelm@11785
   243
      if not (null arg_rules) then arg_rules
wenzelm@33369
   244
      else flat (Context_Rules.find_rules false facts goal ctxt)
wenzelm@12055
   245
  in trace ctxt rules; tac rules facts i end);
wenzelm@10309
   246
wenzelm@10744
   247
fun meth tac x = METHOD (HEADGOAL o tac x);
wenzelm@10744
   248
fun meth' tac x y = METHOD (HEADGOAL o tac x y);
wenzelm@8220
   249
wenzelm@7419
   250
in
wenzelm@7419
   251
wenzelm@10744
   252
val rule_tac = gen_rule_tac Tactic.resolve_tac;
wenzelm@10744
   253
val rule = meth rule_tac;
wenzelm@10744
   254
val some_rule_tac = gen_some_rule_tac rule_tac;
wenzelm@10744
   255
val some_rule = meth' some_rule_tac;
wenzelm@10744
   256
wenzelm@10744
   257
val erule = meth' (gen_arule_tac Tactic.eresolve_tac);
wenzelm@10744
   258
val drule = meth' (gen_arule_tac Tactic.dresolve_tac);
wenzelm@10744
   259
val frule = meth' (gen_arule_tac Tactic.forward_tac);
wenzelm@5824
   260
wenzelm@7419
   261
end;
wenzelm@7419
   262
wenzelm@7419
   263
haftmann@25270
   264
(* intros_tac -- pervasive search spanned by intro rules *)
haftmann@25270
   265
ballarin@36093
   266
fun gen_intros_tac goals intros facts =
ballarin@36093
   267
  goals (insert_tac facts THEN'
haftmann@25270
   268
      REPEAT_ALL_NEW (resolve_tac intros))
haftmann@25270
   269
    THEN Tactic.distinct_subgoals_tac;
haftmann@25270
   270
ballarin@36093
   271
val intros_tac = gen_intros_tac ALLGOALS;
ballarin@36093
   272
val try_intros_tac = gen_intros_tac TRYALL;
haftmann@25270
   273
wenzelm@8351
   274
(* ML tactics *)
wenzelm@8351
   275
wenzelm@33519
   276
structure TacticData = Proof_Data
wenzelm@26472
   277
(
wenzelm@27235
   278
  type T = thm list -> tactic;
wenzelm@26472
   279
  fun init _ = undefined;
wenzelm@26472
   280
);
wenzelm@26472
   281
wenzelm@26472
   282
val set_tactic = TacticData.put;
wenzelm@8351
   283
wenzelm@26472
   284
fun ml_tactic (txt, pos) ctxt =
wenzelm@26472
   285
  let
wenzelm@26472
   286
    val ctxt' = ctxt |> Context.proof_map
wenzelm@27235
   287
      (ML_Context.expression pos
wenzelm@27235
   288
        "fun tactic (facts: thm list) : tactic"
wenzelm@26472
   289
        "Context.map_proof (Method.set_tactic tactic)" txt);
wenzelm@27235
   290
  in Context.setmp_thread_data (SOME (Context.Proof ctxt)) (TacticData.get ctxt') end;
wenzelm@23425
   291
wenzelm@23425
   292
fun tactic txt ctxt = METHOD (ml_tactic txt ctxt);
wenzelm@23425
   293
fun raw_tactic txt ctxt = RAW_METHOD (ml_tactic txt ctxt);
wenzelm@8351
   294
wenzelm@8351
   295
wenzelm@5824
   296
wenzelm@17110
   297
(** method syntax **)
wenzelm@17110
   298
wenzelm@17110
   299
(* method text *)
wenzelm@17110
   300
wenzelm@17110
   301
type src = Args.src;
wenzelm@5824
   302
wenzelm@17110
   303
datatype text =
wenzelm@32193
   304
  Basic of Proof.context -> method |
wenzelm@17110
   305
  Source of src |
wenzelm@20030
   306
  Source_i of src |
wenzelm@17110
   307
  Then of text list |
wenzelm@17110
   308
  Orelse of text list |
wenzelm@17110
   309
  Try of text |
wenzelm@19186
   310
  Repeat1 of text |
wenzelm@19186
   311
  SelectGoals of int * text;
wenzelm@17110
   312
wenzelm@32193
   313
fun primitive_text r = Basic (K (SIMPLE_METHOD (PRIMITIVE r)));
wenzelm@32193
   314
val succeed_text = Basic (K succeed);
wenzelm@17110
   315
val default_text = Source (Args.src (("default", []), Position.none));
wenzelm@32193
   316
val this_text = Basic (K this);
wenzelm@32193
   317
val done_text = Basic (K (SIMPLE_METHOD all_tac));
wenzelm@32193
   318
fun sorry_text int = Basic (cheating int);
wenzelm@17110
   319
wenzelm@32193
   320
fun finish_text (NONE, immed) = Basic (close immed)
wenzelm@32193
   321
  | finish_text (SOME txt, immed) = Then [txt, Basic (close immed)];
wenzelm@17110
   322
wenzelm@17110
   323
wenzelm@17110
   324
(* method definitions *)
wenzelm@5824
   325
wenzelm@33522
   326
structure Methods = Theory_Data
wenzelm@22846
   327
(
wenzelm@33095
   328
  type T = ((src -> Proof.context -> method) * string) Name_Space.table;
wenzelm@33159
   329
  val empty : T = Name_Space.empty_table "method";
wenzelm@16448
   330
  val extend = I;
wenzelm@33522
   331
  fun merge data : T = Name_Space.merge_tables data;
wenzelm@22846
   332
);
wenzelm@5824
   333
wenzelm@22846
   334
fun print_methods thy =
wenzelm@22846
   335
  let
wenzelm@24116
   336
    val meths = Methods.get thy;
wenzelm@33092
   337
    fun prt_meth (name, (_, comment)) = Pretty.block
wenzelm@22846
   338
      [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@22846
   339
  in
wenzelm@33095
   340
    [Pretty.big_list "methods:" (map prt_meth (Name_Space.extern_table meths))]
wenzelm@22846
   341
    |> Pretty.chunks |> Pretty.writeln
wenzelm@22846
   342
  end;
wenzelm@7611
   343
wenzelm@33092
   344
fun add_method name meth comment thy = thy
wenzelm@33095
   345
  |> Methods.map (#2 o Name_Space.define true (Sign.naming_of thy) (name, (meth, comment)));
wenzelm@31304
   346
wenzelm@5824
   347
wenzelm@5824
   348
(* get methods *)
wenzelm@5824
   349
wenzelm@33095
   350
val intern = Name_Space.intern o #1 o Methods.get;
wenzelm@26892
   351
val defined = Symtab.defined o #2 o Methods.get;
wenzelm@26892
   352
wenzelm@20030
   353
fun method_i thy =
wenzelm@5824
   354
  let
wenzelm@24116
   355
    val meths = #2 (Methods.get thy);
wenzelm@5884
   356
    fun meth src =
wenzelm@20030
   357
      let val ((name, _), pos) = Args.dest_src src in
wenzelm@17412
   358
        (case Symtab.lookup meths name of
skalberg@15531
   359
          NONE => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
wenzelm@33092
   360
        | SOME (mth, _) => (Position.report (Markup.method name) pos; mth src))
wenzelm@5824
   361
      end;
wenzelm@5824
   362
  in meth end;
wenzelm@5824
   363
wenzelm@33095
   364
fun method thy = method_i thy o Args.map_name (Name_Space.intern (#1 (Methods.get thy)));
wenzelm@20030
   365
wenzelm@5824
   366
wenzelm@30512
   367
(* method setup *)
wenzelm@30512
   368
wenzelm@30512
   369
fun syntax scan = Args.context_syntax "method" scan;
wenzelm@30512
   370
wenzelm@31304
   371
fun setup name scan =
wenzelm@31304
   372
  add_method name
wenzelm@31304
   373
    (fn src => fn ctxt => let val (m, ctxt') = syntax scan src ctxt in m ctxt' end);
wenzelm@17356
   374
wenzelm@26385
   375
fun method_setup name (txt, pos) cmt =
wenzelm@26472
   376
  Context.theory_map (ML_Context.expression pos
wenzelm@30544
   377
    "val (name, scan, comment): binding * (Proof.context -> Proof.method) context_parser * string"
wenzelm@30544
   378
    "Context.map_theory (Method.setup name scan comment)"
wenzelm@30544
   379
    ("(" ^ ML_Syntax.make_binding name ^ ", " ^ txt ^ ", " ^ ML_Syntax.print_string cmt ^ ")"));
wenzelm@17356
   380
wenzelm@17356
   381
wenzelm@5884
   382
wenzelm@17110
   383
(** concrete syntax **)
wenzelm@5824
   384
wenzelm@27813
   385
structure P = OuterParse;
wenzelm@27813
   386
wenzelm@27813
   387
wenzelm@5884
   388
(* sections *)
wenzelm@5824
   389
wenzelm@20289
   390
type modifier = (Proof.context -> Proof.context) * attribute;
wenzelm@7268
   391
wenzelm@7268
   392
local
wenzelm@7268
   393
wenzelm@24010
   394
fun thms ss = Scan.repeat (Scan.unless (Scan.lift (Scan.first ss)) Attrib.multi_thm) >> flat;
wenzelm@30190
   395
fun app (f, att) (context, ths) = Library.foldl_map att (Context.map_proof f context, ths);
wenzelm@5824
   396
wenzelm@30540
   397
in
wenzelm@30540
   398
wenzelm@24022
   399
fun section ss = Scan.depend (fn context => (Scan.first ss -- Scan.pass context (thms ss)) :|--
wenzelm@24022
   400
  (fn (m, ths) => Scan.succeed (app m (context, ths))));
wenzelm@5884
   401
wenzelm@30540
   402
fun sections ss = Scan.repeat (section ss);
wenzelm@5824
   403
wenzelm@7268
   404
end;
wenzelm@7268
   405
wenzelm@5824
   406
wenzelm@30515
   407
(* extra rule methods *)
wenzelm@30515
   408
wenzelm@30515
   409
fun xrule_meth m =
wenzelm@30515
   410
  Scan.lift (Scan.optional (Args.parens OuterParse.nat) 0) -- Attrib.thms >>
wenzelm@30515
   411
  (fn (n, ths) => K (m n ths));
wenzelm@30515
   412
wenzelm@30515
   413
wenzelm@27813
   414
(* outer parser *)
wenzelm@27813
   415
wenzelm@27813
   416
fun is_symid_meth s =
wenzelm@27813
   417
  s <> "|" andalso s <> "?" andalso s <> "+" andalso OuterLex.ident_or_symbolic s;
wenzelm@27813
   418
wenzelm@27813
   419
local
wenzelm@27813
   420
wenzelm@27813
   421
fun meth4 x =
wenzelm@27813
   422
 (P.position (P.xname >> rpair []) >> (Source o Args.src) ||
wenzelm@27813
   423
  P.$$$ "(" |-- P.!!! (meth0 --| P.$$$ ")")) x
wenzelm@27813
   424
and meth3 x =
wenzelm@27813
   425
 (meth4 --| P.$$$ "?" >> Try ||
wenzelm@27813
   426
  meth4 --| P.$$$ "+" >> Repeat1 ||
wenzelm@27813
   427
  meth4 -- (P.$$$ "[" |-- Scan.optional P.nat 1 --| P.$$$ "]") >> (SelectGoals o swap) ||
wenzelm@27813
   428
  meth4) x
wenzelm@27813
   429
and meth2 x =
wenzelm@27813
   430
 (P.position (P.xname -- Args.parse1 is_symid_meth) >> (Source o Args.src) ||
wenzelm@27813
   431
  meth3) x
wenzelm@27813
   432
and meth1 x = (P.enum1 "," meth2 >> (fn [m] => m | ms => Then ms)) x
wenzelm@27813
   433
and meth0 x = (P.enum1 "|" meth1 >> (fn [m] => m | ms => Orelse ms)) x;
wenzelm@27813
   434
wenzelm@27813
   435
in val parse = meth3 end;
wenzelm@27813
   436
wenzelm@27813
   437
wenzelm@18708
   438
(* theory setup *)
wenzelm@5824
   439
wenzelm@26463
   440
val _ = Context.>> (Context.map_theory
wenzelm@30515
   441
 (setup (Binding.name "fail") (Scan.succeed (K fail)) "force failure" #>
wenzelm@30515
   442
  setup (Binding.name "succeed") (Scan.succeed (K succeed)) "succeed" #>
wenzelm@30515
   443
  setup (Binding.name "-") (Scan.succeed (K insert_facts))
wenzelm@30515
   444
    "do nothing (insert current facts only)" #>
wenzelm@30515
   445
  setup (Binding.name "insert") (Attrib.thms >> (K o insert))
wenzelm@30515
   446
    "insert theorems, ignoring facts (improper)" #>
wenzelm@30515
   447
  setup (Binding.name "intro") (Attrib.thms >> (K o intro))
wenzelm@30515
   448
    "repeatedly apply introduction rules" #>
wenzelm@30515
   449
  setup (Binding.name "elim") (Attrib.thms >> (K o elim))
wenzelm@30515
   450
    "repeatedly apply elimination rules" #>
wenzelm@30515
   451
  setup (Binding.name "unfold") (Attrib.thms >> unfold_meth) "unfold definitions" #>
wenzelm@30515
   452
  setup (Binding.name "fold") (Attrib.thms >> fold_meth) "fold definitions" #>
wenzelm@30515
   453
  setup (Binding.name "atomize") (Args.mode "full" >> (K o atomize))
wenzelm@30515
   454
    "present local premises as object-level statements" #>
wenzelm@30515
   455
  setup (Binding.name "rule") (Attrib.thms >> some_rule) "apply some intro/elim rule" #>
wenzelm@30515
   456
  setup (Binding.name "erule") (xrule_meth erule) "apply rule in elimination manner (improper)" #>
wenzelm@30515
   457
  setup (Binding.name "drule") (xrule_meth drule) "apply rule in destruct manner (improper)" #>
wenzelm@30515
   458
  setup (Binding.name "frule") (xrule_meth frule) "apply rule in forward manner (improper)" #>
wenzelm@30515
   459
  setup (Binding.name "this") (Scan.succeed (K this)) "apply current facts as rules" #>
wenzelm@30515
   460
  setup (Binding.name "fact") (Attrib.thms >> fact) "composition by facts from context" #>
wenzelm@30515
   461
  setup (Binding.name "assumption") (Scan.succeed assumption)
wenzelm@30515
   462
    "proof by assumption, preferring facts" #>
wenzelm@30515
   463
  setup (Binding.name "rename_tac") (Args.goal_spec -- Scan.lift (Scan.repeat1 Args.name) >>
wenzelm@30515
   464
    (fn (quant, xs) => K (SIMPLE_METHOD'' quant (Tactic.rename_tac xs))))
wenzelm@30515
   465
    "rename parameters of goal" #>
wenzelm@30515
   466
  setup (Binding.name "rotate_tac") (Args.goal_spec -- Scan.lift (Scan.optional P.int 1) >>
wenzelm@30515
   467
    (fn (quant, i) => K (SIMPLE_METHOD'' quant (Tactic.rotate_tac i))))
wenzelm@30515
   468
      "rotate assumptions of goal" #>
wenzelm@30515
   469
  setup (Binding.name "tactic") (Scan.lift (P.position Args.name) >> tactic)
wenzelm@30515
   470
    "ML tactic as proof method" #>
wenzelm@30515
   471
  setup (Binding.name "raw_tactic") (Scan.lift (P.position Args.name) >> raw_tactic)
wenzelm@30515
   472
    "ML tactic as raw proof method"));
wenzelm@5824
   473
wenzelm@5824
   474
wenzelm@16145
   475
(*final declarations of this structure!*)
wenzelm@16145
   476
val unfold = unfold_meth;
wenzelm@16145
   477
val fold = fold_meth;
wenzelm@16145
   478
wenzelm@5824
   479
end;
wenzelm@5824
   480
wenzelm@32193
   481
structure Basic_Method: BASIC_METHOD = Method;
wenzelm@32193
   482
open Basic_Method;
wenzelm@30508
   483
wenzelm@30508
   484
val RAW_METHOD_CASES = Method.RAW_METHOD_CASES;
wenzelm@30508
   485
val RAW_METHOD = Method.RAW_METHOD;
wenzelm@30508
   486
val METHOD_CASES = Method.METHOD_CASES;
wenzelm@30508
   487
val METHOD = Method.METHOD;
wenzelm@30508
   488
val SIMPLE_METHOD = Method.SIMPLE_METHOD;
wenzelm@30508
   489
val SIMPLE_METHOD' = Method.SIMPLE_METHOD';
wenzelm@30508
   490
val SIMPLE_METHOD'' = Method.SIMPLE_METHOD'';
wenzelm@30508
   491