src/Pure/Isar/method.ML
author wenzelm
Thu Aug 03 17:30:43 2006 +0200 (2006-08-03)
changeset 20335 b5eca86ef9cc
parent 20289 ba7a7c56bed5
child 21579 abd2b4386a63
permissions -rw-r--r--
moved bires_inst_tac etc. to rule_insts.ML;
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@17110
     5
Isar proof methods.
wenzelm@5824
     6
*)
wenzelm@5824
     7
wenzelm@5824
     8
signature BASIC_METHOD =
wenzelm@5824
     9
sig
wenzelm@17110
    10
  val FINDGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq
wenzelm@17110
    11
  val HEADGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq
wenzelm@17110
    12
  type method
wenzelm@11731
    13
  val trace_rules: bool ref
wenzelm@5824
    14
  val print_methods: theory -> unit
wenzelm@20289
    15
  val Method: bstring -> (Args.src -> Proof.context -> method) -> string -> unit
wenzelm@5824
    16
end;
wenzelm@5824
    17
wenzelm@5824
    18
signature METHOD =
wenzelm@5824
    19
sig
wenzelm@5824
    20
  include BASIC_METHOD
wenzelm@18227
    21
  val apply: method -> thm list -> cases_tactic
wenzelm@18227
    22
  val RAW_METHOD_CASES: (thm list -> cases_tactic) -> method
wenzelm@17110
    23
  val RAW_METHOD: (thm list -> tactic) -> method
wenzelm@18227
    24
  val METHOD_CASES: (thm list -> cases_tactic) -> method
wenzelm@17110
    25
  val METHOD: (thm list -> tactic) -> method
wenzelm@17110
    26
  val fail: method
wenzelm@17110
    27
  val succeed: method
wenzelm@17110
    28
  val insert_tac: thm list -> int -> tactic
wenzelm@17110
    29
  val insert: thm list -> method
wenzelm@17110
    30
  val insert_facts: method
wenzelm@17110
    31
  val SIMPLE_METHOD: tactic -> method
wenzelm@17110
    32
  val SIMPLE_METHOD': ((int -> tactic) -> tactic) -> (int -> tactic) -> method
wenzelm@17110
    33
  val defer: int option -> method
wenzelm@17110
    34
  val prefer: int -> method
wenzelm@20289
    35
  val cheating: bool -> Proof.context -> method
wenzelm@17110
    36
  val intro: thm list -> method
wenzelm@17110
    37
  val elim: thm list -> method
wenzelm@20289
    38
  val unfold: thm list -> Proof.context -> method
wenzelm@20289
    39
  val fold: thm list -> Proof.context -> method
wenzelm@17110
    40
  val atomize: bool -> method
wenzelm@17110
    41
  val this: method
wenzelm@20289
    42
  val fact: thm list -> Proof.context -> method
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
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@20289
    52
  val iprover_tac: Proof.context -> int option -> int -> tactic
wenzelm@20289
    53
  val set_tactic: (Proof.context -> thm list -> tactic) -> unit
wenzelm@20289
    54
  val tactic: string -> Proof.context -> method
wenzelm@17110
    55
  type src
wenzelm@5824
    56
  datatype text =
wenzelm@20289
    57
    Basic of (Proof.context -> method) |
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@17356
    71
  val finish_text: text option * bool -> text
wenzelm@17110
    72
  exception METHOD_FAIL of (string * Position.T) * exn
wenzelm@20289
    73
  val method: theory -> src -> Proof.context -> method
wenzelm@20289
    74
  val method_i: theory -> src -> Proof.context -> method
wenzelm@20289
    75
  val add_methods: (bstring * (src -> Proof.context -> method) * string) list
wenzelm@17110
    76
    -> theory -> theory
wenzelm@20289
    77
  val add_method: bstring * (src -> Proof.context -> method) * string
wenzelm@17110
    78
    -> theory -> theory
wenzelm@17356
    79
  val method_setup: bstring * string * string -> theory -> theory
wenzelm@18999
    80
  val syntax: (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list))
wenzelm@20289
    81
    -> src -> Proof.context -> Proof.context * 'a
wenzelm@17110
    82
  val simple_args: (Args.T list -> 'a * Args.T list)
wenzelm@20289
    83
    -> ('a -> Proof.context -> method) -> src -> Proof.context -> method
wenzelm@20289
    84
  val ctxt_args: (Proof.context -> method) -> src -> Proof.context -> method
wenzelm@20289
    85
  val no_args: method -> src -> Proof.context -> method
wenzelm@17110
    86
  type modifier
wenzelm@18999
    87
  val sectioned_args: (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) ->
wenzelm@17110
    88
    (Args.T list -> modifier * Args.T list) list ->
wenzelm@20289
    89
    ('a -> Proof.context -> 'b) -> src -> Proof.context -> 'b
wenzelm@17110
    90
  val bang_sectioned_args:
wenzelm@17110
    91
    (Args.T list -> modifier * Args.T list) list ->
wenzelm@20289
    92
    (thm list -> Proof.context -> 'a) -> src -> Proof.context -> 'a
wenzelm@17110
    93
  val bang_sectioned_args':
wenzelm@17110
    94
    (Args.T list -> modifier * Args.T list) list ->
wenzelm@18999
    95
    (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) ->
wenzelm@20289
    96
    ('a -> thm list -> Proof.context -> 'b) -> src -> Proof.context -> 'b
wenzelm@17110
    97
  val only_sectioned_args:
wenzelm@17110
    98
    (Args.T list -> modifier * Args.T list) list ->
wenzelm@20289
    99
    (Proof.context -> 'a) -> src -> Proof.context -> 'a
wenzelm@20289
   100
  val thms_ctxt_args: (thm list -> Proof.context -> 'a) -> src ->
wenzelm@20289
   101
    Proof.context -> 'a
wenzelm@20289
   102
  val thms_args: (thm list -> 'a) -> src -> Proof.context -> 'a
wenzelm@20289
   103
  val thm_args: (thm -> 'a) -> src -> Proof.context -> 'a
wenzelm@9539
   104
  val goal_args: (Args.T list -> 'a * Args.T list) -> ('a -> int -> tactic)
wenzelm@20289
   105
    -> src -> Proof.context -> method
wenzelm@18999
   106
  val goal_args': (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list))
wenzelm@20289
   107
    -> ('a -> int -> tactic) -> src -> Proof.context -> method
wenzelm@17110
   108
  val goal_args_ctxt: (Args.T list -> 'a * Args.T list) ->
wenzelm@20289
   109
    (Proof.context -> 'a -> int -> tactic) -> src -> Proof.context -> method
wenzelm@18999
   110
  val goal_args_ctxt': (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) ->
wenzelm@20289
   111
    (Proof.context -> 'a -> int -> tactic) -> src -> Proof.context -> method
wenzelm@5824
   112
end;
wenzelm@5824
   113
wenzelm@5824
   114
structure Method: METHOD =
wenzelm@5824
   115
struct
wenzelm@5824
   116
wenzelm@17110
   117
(** generic tools **)
wenzelm@17110
   118
wenzelm@17110
   119
(* goal addressing *)
wenzelm@17110
   120
wenzelm@17110
   121
fun FINDGOAL tac st =
wenzelm@17110
   122
  let fun find i n = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1) n)
wenzelm@17110
   123
  in find 1 (Thm.nprems_of st) st end;
wenzelm@17110
   124
wenzelm@17110
   125
fun HEADGOAL tac = tac 1;
wenzelm@17110
   126
wenzelm@17110
   127
wenzelm@5824
   128
wenzelm@12324
   129
(** proof methods **)
wenzelm@12324
   130
wenzelm@17110
   131
(* datatype method *)
wenzelm@11731
   132
wenzelm@18227
   133
datatype method = Meth of thm list -> cases_tactic;
wenzelm@11731
   134
wenzelm@17756
   135
fun apply (Meth m) = m;
wenzelm@11731
   136
wenzelm@17756
   137
val RAW_METHOD_CASES = Meth;
wenzelm@11731
   138
wenzelm@17110
   139
fun RAW_METHOD tac = RAW_METHOD_CASES (NO_CASES o tac);
wenzelm@12144
   140
wenzelm@17110
   141
fun METHOD_CASES tac = RAW_METHOD_CASES (fn facts =>
wenzelm@18474
   142
  Seq.THEN (ALLGOALS Tactic.conjunction_tac, tac facts));
wenzelm@8372
   143
wenzelm@18474
   144
fun METHOD tac = RAW_METHOD (fn facts => ALLGOALS Tactic.conjunction_tac THEN tac facts);
wenzelm@5824
   145
wenzelm@5824
   146
val fail = METHOD (K no_tac);
wenzelm@5824
   147
val succeed = METHOD (K all_tac);
wenzelm@5824
   148
wenzelm@5824
   149
wenzelm@17110
   150
(* insert facts *)
wenzelm@7419
   151
wenzelm@7419
   152
local
wenzelm@5824
   153
wenzelm@6981
   154
fun cut_rule_tac raw_rule =
wenzelm@6981
   155
  let
wenzelm@6981
   156
    val rule = Drule.forall_intr_vars raw_rule;
wenzelm@18039
   157
    val revcut_rl = Drule.incr_indexes rule Drule.revcut_rl;
wenzelm@7555
   158
  in Tactic.rtac (rule COMP revcut_rl) end;
wenzelm@6981
   159
wenzelm@7419
   160
in
wenzelm@5824
   161
wenzelm@7419
   162
fun insert_tac [] i = all_tac
wenzelm@7419
   163
  | insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts);
wenzelm@6981
   164
wenzelm@7555
   165
val insert_facts = METHOD (ALLGOALS o insert_tac);
wenzelm@7664
   166
fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms));
wenzelm@7419
   167
wenzelm@9706
   168
fun SIMPLE_METHOD tac = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN tac);
wenzelm@9706
   169
fun SIMPLE_METHOD' quant tac = METHOD (fn facts => quant (insert_tac facts THEN' tac));
wenzelm@9706
   170
wenzelm@12324
   171
end;
wenzelm@12324
   172
wenzelm@9706
   173
wenzelm@17110
   174
(* shuffle subgoals *)
wenzelm@17110
   175
wenzelm@17110
   176
fun prefer i = METHOD (K (Tactic.defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1)));
wenzelm@18939
   177
fun defer opt_i = METHOD (K (Tactic.defer_tac (the_default 1 opt_i)));
wenzelm@17110
   178
wenzelm@17110
   179
wenzelm@17356
   180
(* cheating *)
wenzelm@17356
   181
wenzelm@17356
   182
fun cheating int ctxt = METHOD (K (setmp quick_and_dirty (int orelse ! quick_and_dirty)
wenzelm@17356
   183
    (SkipProof.cheat_tac (ProofContext.theory_of ctxt))));
wenzelm@17356
   184
wenzelm@17356
   185
wenzelm@17110
   186
(* unfold intro/elim rules *)
wenzelm@17110
   187
wenzelm@17110
   188
fun intro ths = SIMPLE_METHOD' HEADGOAL (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.match_tac ths));
wenzelm@17110
   189
fun elim ths = SIMPLE_METHOD' HEADGOAL (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.ematch_tac ths));
wenzelm@17110
   190
wenzelm@17110
   191
wenzelm@12384
   192
(* unfold/fold definitions *)
wenzelm@12384
   193
wenzelm@18877
   194
fun unfold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (LocalDefs.unfold_tac ctxt ths));
wenzelm@18877
   195
fun fold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (LocalDefs.fold_tac ctxt ths));
wenzelm@6532
   196
wenzelm@12384
   197
wenzelm@12829
   198
(* atomize rule statements *)
wenzelm@12829
   199
wenzelm@12829
   200
fun atomize false = SIMPLE_METHOD' HEADGOAL (CHANGED_PROP o ObjectLogic.atomize_tac)
wenzelm@12829
   201
  | atomize true = RAW_METHOD (K (HEADGOAL (CHANGED_PROP o ObjectLogic.full_atomize_tac)));
wenzelm@12829
   202
wenzelm@12829
   203
wenzelm@18039
   204
(* this -- resolve facts directly *)
wenzelm@12384
   205
wenzelm@17110
   206
val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac));
wenzelm@9484
   207
wenzelm@9484
   208
wenzelm@18039
   209
(* fact -- composition by facts from context *)
wenzelm@18039
   210
wenzelm@18309
   211
fun fact [] ctxt = SIMPLE_METHOD' HEADGOAL (ProofContext.some_fact_tac ctxt)
wenzelm@18309
   212
  | fact rules _ = SIMPLE_METHOD' HEADGOAL (ProofContext.fact_tac rules);
wenzelm@18039
   213
wenzelm@18039
   214
wenzelm@17110
   215
(* assumption *)
wenzelm@7419
   216
wenzelm@7419
   217
local
wenzelm@7419
   218
wenzelm@17110
   219
fun asm_tac ths =
wenzelm@17110
   220
  foldr (op APPEND') (K no_tac) (map (fn th => Tactic.rtac th THEN_ALL_NEW assume_tac) ths);
wenzelm@17110
   221
wenzelm@19778
   222
fun cond_rtac cond rule = SUBGOAL (fn (prop, i) =>
wenzelm@19778
   223
  if cond (Logic.strip_assums_concl prop)
wenzelm@19778
   224
  then Tactic.rtac rule i else no_tac);
wenzelm@7419
   225
wenzelm@17110
   226
fun assm_tac ctxt =
wenzelm@17110
   227
  assume_tac APPEND'
wenzelm@20224
   228
  asm_tac (Assumption.prems_of ctxt) APPEND'
wenzelm@19778
   229
  cond_rtac (can Logic.dest_equals) Drule.reflexive_thm APPEND'
wenzelm@19778
   230
  cond_rtac (can Logic.dest_term) Drule.termI;
wenzelm@17110
   231
wenzelm@17110
   232
fun assumption_tac ctxt [] = assm_tac ctxt
wenzelm@17110
   233
  | assumption_tac _ [fact] = asm_tac [fact]
wenzelm@17110
   234
  | assumption_tac _ _ = K no_tac;
wenzelm@7419
   235
wenzelm@7419
   236
in
wenzelm@7419
   237
wenzelm@17110
   238
fun assumption ctxt = METHOD (HEADGOAL o assumption_tac ctxt);
wenzelm@17356
   239
fun close immed ctxt = METHOD (K
wenzelm@17356
   240
  (FILTER Thm.no_prems ((if immed then ALLGOALS (assm_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@17110
   250
  conditional (! trace_rules andalso not (null rules)) (fn () =>
wenzelm@17110
   251
    Pretty.big_list "rules:" (map (ProofContext.pretty_thm ctxt) rules)
wenzelm@17110
   252
    |> Pretty.string_of |> tracing);
wenzelm@12347
   253
wenzelm@12347
   254
local
wenzelm@12347
   255
wenzelm@18841
   256
fun gen_rule_tac tac rules facts =
wenzelm@18841
   257
  (fn i => fn st =>
wenzelm@18841
   258
    if null facts then tac rules i st
wenzelm@18841
   259
    else Seq.maps (fn rule => (tac o single) rule i st) (Drule.multi_resolves facts rules))
wenzelm@18841
   260
  THEN_ALL_NEW Tactic.norm_hhf_tac;
wenzelm@7130
   261
wenzelm@10744
   262
fun gen_arule_tac tac j rules facts =
wenzelm@10744
   263
  EVERY' (gen_rule_tac tac rules facts :: replicate j Tactic.assume_tac);
wenzelm@10744
   264
wenzelm@11785
   265
fun gen_some_rule_tac tac arg_rules ctxt facts = SUBGOAL (fn (goal, i) =>
wenzelm@11785
   266
  let
wenzelm@11785
   267
    val rules =
wenzelm@11785
   268
      if not (null arg_rules) then arg_rules
wenzelm@19482
   269
      else flat (ContextRules.find_rules false facts goal ctxt)
wenzelm@12055
   270
  in trace ctxt rules; tac rules facts i end);
wenzelm@10309
   271
wenzelm@10744
   272
fun meth tac x = METHOD (HEADGOAL o tac x);
wenzelm@10744
   273
fun meth' tac x y = METHOD (HEADGOAL o tac x y);
wenzelm@8220
   274
wenzelm@7419
   275
in
wenzelm@7419
   276
wenzelm@10744
   277
val rule_tac = gen_rule_tac Tactic.resolve_tac;
wenzelm@10744
   278
val rule = meth rule_tac;
wenzelm@10744
   279
val some_rule_tac = gen_some_rule_tac rule_tac;
wenzelm@10744
   280
val some_rule = meth' some_rule_tac;
wenzelm@10744
   281
wenzelm@10744
   282
val erule = meth' (gen_arule_tac Tactic.eresolve_tac);
wenzelm@10744
   283
val drule = meth' (gen_arule_tac Tactic.dresolve_tac);
wenzelm@10744
   284
val frule = meth' (gen_arule_tac Tactic.forward_tac);
wenzelm@5824
   285
wenzelm@7419
   286
end;
wenzelm@7419
   287
wenzelm@7419
   288
nipkow@17587
   289
(* iprover -- intuitionistic proof search *)
wenzelm@17110
   290
wenzelm@17110
   291
local
wenzelm@17110
   292
wenzelm@17110
   293
val remdups_tac = SUBGOAL (fn (g, i) =>
wenzelm@17110
   294
  let val prems = Logic.strip_assums_hyp g in
wenzelm@19046
   295
    REPEAT_DETERM_N (length prems - length (distinct op aconv prems))
wenzelm@17110
   296
    (Tactic.ematch_tac [Drule.remdups_rl] i THEN Tactic.eq_assume_tac i)
wenzelm@17110
   297
  end);
wenzelm@17110
   298
wenzelm@17110
   299
fun REMDUPS tac = tac THEN_ALL_NEW remdups_tac;
wenzelm@17110
   300
wenzelm@17110
   301
val bires_tac = Tactic.biresolution_from_nets_tac ContextRules.orderlist;
wenzelm@17110
   302
wenzelm@17110
   303
fun safe_step_tac ctxt =
wenzelm@17110
   304
  ContextRules.Swrap ctxt
wenzelm@17110
   305
   (eq_assume_tac ORELSE'
wenzelm@17110
   306
    bires_tac true (ContextRules.netpair_bang ctxt));
wenzelm@8195
   307
wenzelm@17110
   308
fun unsafe_step_tac ctxt =
wenzelm@17110
   309
  ContextRules.wrap ctxt
wenzelm@17110
   310
   (assume_tac APPEND'
wenzelm@17110
   311
    bires_tac false (ContextRules.netpair_bang ctxt) APPEND'
wenzelm@17110
   312
    bires_tac false (ContextRules.netpair ctxt));
wenzelm@17110
   313
wenzelm@17110
   314
fun step_tac ctxt i =
wenzelm@17110
   315
  REPEAT_DETERM1 (REMDUPS (safe_step_tac ctxt) i) ORELSE
wenzelm@17110
   316
  REMDUPS (unsafe_step_tac ctxt) i;
wenzelm@17110
   317
wenzelm@17110
   318
fun intprover_tac ctxt gs d lim = SUBGOAL (fn (g, i) => if d > lim then no_tac else
wenzelm@17110
   319
  let
wenzelm@17110
   320
    val ps = Logic.strip_assums_hyp g;
wenzelm@17110
   321
    val c = Logic.strip_assums_concl g;
wenzelm@17110
   322
  in
wenzelm@18921
   323
    if member (fn ((ps1, c1), (ps2, c2)) =>
wenzelm@19307
   324
        c1 aconv c2 andalso
wenzelm@19307
   325
        length ps1 = length ps2 andalso
wenzelm@19307
   326
        gen_eq_set (op aconv) (ps1, ps2)) gs (ps, c) then no_tac
wenzelm@17110
   327
    else (step_tac ctxt THEN_ALL_NEW intprover_tac ctxt ((ps, c) :: gs) (d + 1) lim) i
wenzelm@17110
   328
  end);
wenzelm@17110
   329
wenzelm@17110
   330
in
wenzelm@17110
   331
nipkow@17587
   332
fun iprover_tac ctxt opt_lim =
wenzelm@18939
   333
  SELECT_GOAL (DEEPEN (2, the_default 20 opt_lim) (intprover_tac ctxt [] 0) 4 1);
wenzelm@17110
   334
wenzelm@17110
   335
end;
wenzelm@8195
   336
wenzelm@8195
   337
wenzelm@8351
   338
(* ML tactics *)
wenzelm@8351
   339
wenzelm@20289
   340
val tactic_ref = ref ((fn _ => raise Match): Proof.context -> thm list -> tactic);
wenzelm@8351
   341
fun set_tactic f = tactic_ref := f;
wenzelm@8351
   342
wenzelm@8351
   343
fun tactic txt ctxt = METHOD (fn facts =>
wenzelm@9631
   344
  (Context.use_mltext
wenzelm@20289
   345
    ("let fun tactic (ctxt: Proof.context) (facts: thm list) : tactic = \
wenzelm@16500
   346
       \let val thm = ProofContext.get_thm_closure ctxt o PureThy.Name\n\
wenzelm@16500
   347
       \  and thms = ProofContext.get_thms_closure ctxt o PureThy.Name in\n"
wenzelm@16500
   348
       ^ txt ^
wenzelm@16500
   349
       "\nend in Method.set_tactic tactic end")
skalberg@15531
   350
    false NONE;
skalberg@15531
   351
    Context.setmp (SOME (ProofContext.theory_of ctxt)) (! tactic_ref ctxt) facts));
wenzelm@8351
   352
wenzelm@8351
   353
wenzelm@5824
   354
wenzelm@17110
   355
(** method syntax **)
wenzelm@17110
   356
wenzelm@17110
   357
(* method text *)
wenzelm@17110
   358
wenzelm@17110
   359
type src = Args.src;
wenzelm@5824
   360
wenzelm@17110
   361
datatype text =
wenzelm@20289
   362
  Basic of (Proof.context -> method) |
wenzelm@17110
   363
  Source of src |
wenzelm@20030
   364
  Source_i of src |
wenzelm@17110
   365
  Then of text list |
wenzelm@17110
   366
  Orelse of text list |
wenzelm@17110
   367
  Try of text |
wenzelm@19186
   368
  Repeat1 of text |
wenzelm@19186
   369
  SelectGoals of int * text;
wenzelm@17110
   370
wenzelm@17857
   371
val primitive_text = Basic o K o SIMPLE_METHOD o PRIMITIVE;
wenzelm@17857
   372
val succeed_text = Basic (K succeed);
wenzelm@17110
   373
val default_text = Source (Args.src (("default", []), Position.none));
wenzelm@17110
   374
val this_text = Basic (K this);
wenzelm@17110
   375
val done_text = Basic (K (SIMPLE_METHOD all_tac));
wenzelm@17356
   376
val sorry_text = Basic o cheating;
wenzelm@17110
   377
wenzelm@17356
   378
fun finish_text (NONE, immed) = Basic (close immed)
wenzelm@17356
   379
  | finish_text (SOME txt, immed) = Then [txt, Basic (close immed)];
wenzelm@17110
   380
wenzelm@17110
   381
wenzelm@17110
   382
(* method definitions *)
wenzelm@5824
   383
wenzelm@16448
   384
structure MethodsData = TheoryDataFun
wenzelm@16448
   385
(struct
wenzelm@5824
   386
  val name = "Isar/methods";
wenzelm@20289
   387
  type T = (((src -> Proof.context -> method) * string) * stamp) NameSpace.table;
wenzelm@5824
   388
wenzelm@16347
   389
  val empty = NameSpace.empty_table;
wenzelm@6546
   390
  val copy = I;
wenzelm@16448
   391
  val extend = I;
haftmann@17496
   392
  fun merge _ tables = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUPS dups =>
wenzelm@16347
   393
    error ("Attempt to merge different versions of method(s) " ^ commas_quote dups);
wenzelm@5824
   394
wenzelm@16347
   395
  fun print _ meths =
wenzelm@5824
   396
    let
wenzelm@5824
   397
      fun prt_meth (name, ((_, comment), _)) = Pretty.block
wenzelm@6849
   398
        [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@5824
   399
    in
wenzelm@16347
   400
      [Pretty.big_list "methods:" (map prt_meth (NameSpace.extern_table meths))]
wenzelm@9222
   401
      |> Pretty.chunks |> Pretty.writeln
wenzelm@5824
   402
    end;
wenzelm@16448
   403
end);
wenzelm@5824
   404
wenzelm@18708
   405
val _ = Context.add_setup MethodsData.init;
wenzelm@5824
   406
val print_methods = MethodsData.print;
wenzelm@7611
   407
wenzelm@5824
   408
wenzelm@5824
   409
(* get methods *)
wenzelm@5824
   410
wenzelm@5916
   411
exception METHOD_FAIL of (string * Position.T) * exn;
wenzelm@5916
   412
wenzelm@20030
   413
fun method_i thy =
wenzelm@5824
   414
  let
wenzelm@20030
   415
    val meths = #2 (MethodsData.get thy);
wenzelm@5884
   416
    fun meth src =
wenzelm@20030
   417
      let val ((name, _), pos) = Args.dest_src src in
wenzelm@17412
   418
        (case Symtab.lookup meths name of
skalberg@15531
   419
          NONE => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
skalberg@15531
   420
        | SOME ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src))
wenzelm@5824
   421
      end;
wenzelm@5824
   422
  in meth end;
wenzelm@5824
   423
wenzelm@20030
   424
fun method thy = method_i thy o Args.map_name (NameSpace.intern (#1 (MethodsData.get thy)));
wenzelm@20030
   425
wenzelm@5824
   426
wenzelm@17110
   427
(* add method *)
wenzelm@5824
   428
wenzelm@5824
   429
fun add_methods raw_meths thy =
wenzelm@5824
   430
  let
wenzelm@16145
   431
    val new_meths = raw_meths |> map (fn (name, f, comment) =>
wenzelm@16347
   432
      (name, ((f, comment), stamp ())));
wenzelm@5824
   433
wenzelm@17110
   434
    fun add meths = NameSpace.extend_table (Sign.naming_of thy) (meths, new_meths)
wenzelm@16347
   435
      handle Symtab.DUPS dups =>
wenzelm@16347
   436
        error ("Duplicate declaration of method(s) " ^ commas_quote dups);
wenzelm@16347
   437
  in MethodsData.map add thy end;
wenzelm@5824
   438
wenzelm@9194
   439
val add_method = add_methods o Library.single;
wenzelm@9194
   440
wenzelm@18728
   441
fun Method name meth cmt = Context.>> (add_method (name, meth, cmt));
wenzelm@5824
   442
wenzelm@5824
   443
wenzelm@17356
   444
(* method_setup *)
wenzelm@17356
   445
wenzelm@17356
   446
fun method_setup (name, txt, cmt) =
wenzelm@17356
   447
  Context.use_let
wenzelm@17356
   448
    "val thm = PureThy.get_thm_closure (Context.the_context ()) o PureThy.Name;\n\
wenzelm@17356
   449
    \val thms = PureThy.get_thms_closure (Context.the_context ()) o PureThy.Name;\n\
wenzelm@20289
   450
    \val method: bstring * (Method.src -> Proof.context -> Proof.method) * string"
wenzelm@17356
   451
    "Method.add_method method"
wenzelm@17356
   452
    ("(" ^ Library.quote name ^ ", " ^ txt ^ ", " ^ Library.quote cmt ^ ")");
wenzelm@17356
   453
wenzelm@17356
   454
wenzelm@5884
   455
wenzelm@17110
   456
(** concrete syntax **)
wenzelm@5824
   457
wenzelm@5884
   458
(* basic *)
wenzelm@5884
   459
wenzelm@18999
   460
fun syntax scan = Args.context_syntax "method" scan;
wenzelm@5824
   461
wenzelm@17110
   462
fun simple_args scan f src ctxt : method =
wenzelm@8351
   463
  #2 (syntax (Scan.lift (scan >> (fn x => f x ctxt))) src ctxt);
wenzelm@8351
   464
wenzelm@20289
   465
fun ctxt_args (f: Proof.context -> method) src ctxt =
wenzelm@8282
   466
  #2 (syntax (Scan.succeed (f ctxt)) src ctxt);
wenzelm@7555
   467
wenzelm@7555
   468
fun no_args m = ctxt_args (K m);
wenzelm@5884
   469
wenzelm@5884
   470
wenzelm@5884
   471
(* sections *)
wenzelm@5824
   472
wenzelm@20289
   473
type modifier = (Proof.context -> Proof.context) * attribute;
wenzelm@7268
   474
wenzelm@7268
   475
local
wenzelm@7268
   476
wenzelm@8381
   477
fun sect ss = Scan.first (map Scan.lift ss);
wenzelm@19482
   478
fun thms ss = Scan.repeat (Scan.unless (sect ss) Attrib.multi_thm) >> flat;
wenzelm@5884
   479
wenzelm@19186
   480
fun app (f, att) (context, ths) = foldl_map att (Context.map_proof f context, ths);
wenzelm@5824
   481
wenzelm@18999
   482
fun section ss = (sect ss -- thms ss) :-- (fn (m, ths) => Scan.depend (fn context =>
wenzelm@19186
   483
  Scan.succeed (app m (context, ths)))) >> #2;
wenzelm@5884
   484
wenzelm@7601
   485
fun sectioned args ss = args -- Scan.repeat (section ss);
wenzelm@5884
   486
wenzelm@7268
   487
in
wenzelm@5824
   488
wenzelm@5884
   489
fun sectioned_args args ss f src ctxt =
wenzelm@8282
   490
  let val (ctxt', (x, _)) = syntax (sectioned args ss) src ctxt
wenzelm@5921
   491
  in f x ctxt' end;
wenzelm@5884
   492
wenzelm@7601
   493
fun bang_sectioned_args ss f = sectioned_args Args.bang_facts ss f;
wenzelm@9777
   494
fun bang_sectioned_args' ss scan f =
wenzelm@9777
   495
  sectioned_args (Args.bang_facts -- scan >> swap) ss (uncurry f);
wenzelm@7601
   496
fun only_sectioned_args ss f = sectioned_args (Scan.succeed ()) ss (fn () => f);
wenzelm@7268
   497
wenzelm@18999
   498
fun thms_ctxt_args f = sectioned_args (thms []) [] f;
wenzelm@8093
   499
fun thms_args f = thms_ctxt_args (K o f);
wenzelm@9706
   500
fun thm_args f = thms_args (fn [thm] => f thm | _ => error "Single theorem expected");
wenzelm@5824
   501
wenzelm@7268
   502
end;
wenzelm@7268
   503
wenzelm@5824
   504
nipkow@17587
   505
(* iprover syntax *)
wenzelm@12347
   506
wenzelm@12347
   507
local
wenzelm@12347
   508
wenzelm@12347
   509
val introN = "intro";
wenzelm@12347
   510
val elimN = "elim";
wenzelm@12347
   511
val destN = "dest";
wenzelm@12347
   512
val ruleN = "rule";
wenzelm@12347
   513
wenzelm@12347
   514
fun modifier name kind kind' att =
skalberg@15531
   515
  Args.$$$ name |-- (kind >> K NONE || kind' |-- Args.nat --| Args.colon >> SOME)
wenzelm@20289
   516
    >> (pair (I: Proof.context -> Proof.context) o att);
wenzelm@12347
   517
nipkow@17587
   518
val iprover_modifiers =
wenzelm@18728
   519
 [modifier destN Args.bang_colon Args.bang ContextRules.dest_bang,
wenzelm@18728
   520
  modifier destN Args.colon (Scan.succeed ()) ContextRules.dest,
wenzelm@18728
   521
  modifier elimN Args.bang_colon Args.bang ContextRules.elim_bang,
wenzelm@18728
   522
  modifier elimN Args.colon (Scan.succeed ()) ContextRules.elim,
wenzelm@18728
   523
  modifier introN Args.bang_colon Args.bang ContextRules.intro_bang,
wenzelm@18728
   524
  modifier introN Args.colon (Scan.succeed ()) ContextRules.intro,
wenzelm@18728
   525
  Args.del -- Args.colon >> K (I, ContextRules.rule_del)];
wenzelm@12347
   526
wenzelm@12347
   527
in
wenzelm@12347
   528
wenzelm@18640
   529
val iprover_meth =
wenzelm@18640
   530
  bang_sectioned_args' iprover_modifiers (Scan.lift (Scan.option Args.nat))
wenzelm@18640
   531
    (fn n => fn prems => fn ctxt => METHOD (fn facts =>
wenzelm@18640
   532
      HEADGOAL (insert_tac (prems @ facts) THEN'
wenzelm@18640
   533
      ObjectLogic.atomize_tac THEN' iprover_tac ctxt n)));
wenzelm@12347
   534
wenzelm@12347
   535
end;
wenzelm@12347
   536
wenzelm@12347
   537
wenzelm@9539
   538
(* tactic syntax *)
wenzelm@8238
   539
wenzelm@10744
   540
fun nat_thms_args f = uncurry f oo
wenzelm@18999
   541
  (#2 oo syntax (Scan.lift (Scan.optional (Args.parens Args.nat) 0) -- Attrib.thms));
wenzelm@10744
   542
wenzelm@12119
   543
fun goal_args' args tac src ctxt = #2 (syntax (Args.goal_spec HEADGOAL -- args >>
ballarin@14215
   544
  (fn (quant, s) => SIMPLE_METHOD' quant (tac s))) src ctxt);
wenzelm@8537
   545
wenzelm@9539
   546
fun goal_args args tac = goal_args' (Scan.lift args) tac;
wenzelm@8238
   547
ballarin@14174
   548
fun goal_args_ctxt' args tac src ctxt =
ballarin@14174
   549
  #2 (syntax (Args.goal_spec HEADGOAL -- args >>
ballarin@14174
   550
  (fn (quant, s) => SIMPLE_METHOD' quant (tac ctxt s))) src ctxt);
wenzelm@8238
   551
ballarin@14174
   552
fun goal_args_ctxt args tac = goal_args_ctxt' (Scan.lift args) tac;
wenzelm@5824
   553
wenzelm@14718
   554
wenzelm@18708
   555
(* theory setup *)
wenzelm@5824
   556
wenzelm@18708
   557
val _ = Context.add_setup (add_methods
wenzelm@5824
   558
 [("fail", no_args fail, "force failure"),
wenzelm@5824
   559
  ("succeed", no_args succeed, "succeed"),
wenzelm@9587
   560
  ("-", no_args insert_facts, "do nothing (insert current facts only)"),
wenzelm@9539
   561
  ("insert", thms_args insert, "insert theorems, ignoring facts (improper)"),
wenzelm@12384
   562
  ("intro", thms_args intro, "repeatedly apply introduction rules"),
wenzelm@12384
   563
  ("elim", thms_args elim, "repeatedly apply elimination rules"),
wenzelm@18877
   564
  ("unfold", thms_ctxt_args unfold_meth, "unfold definitions"),
wenzelm@18877
   565
  ("fold", thms_ctxt_args fold_meth, "fold definitions"),
wenzelm@12829
   566
  ("atomize", (atomize o #2) oo syntax (Args.mode "full"),
wenzelm@11962
   567
    "present local premises as object-level statements"),
wenzelm@18640
   568
  ("iprover", iprover_meth, "intuitionistic proof search"),
wenzelm@12384
   569
  ("rule", thms_ctxt_args some_rule, "apply some intro/elim rule"),
wenzelm@10744
   570
  ("erule", nat_thms_args erule, "apply rule in elimination manner (improper)"),
wenzelm@10744
   571
  ("drule", nat_thms_args drule, "apply rule in destruct manner (improper)"),
wenzelm@10744
   572
  ("frule", nat_thms_args frule, "apply rule in forward manner (improper)"),
wenzelm@8195
   573
  ("this", no_args this, "apply current facts as rules"),
wenzelm@18039
   574
  ("fact", thms_ctxt_args fact, "composition by facts from context"),
wenzelm@8238
   575
  ("assumption", ctxt_args assumption, "proof by assumption, preferring facts"),
wenzelm@20335
   576
  ("rename_tac", goal_args (Scan.repeat1 Args.name) Tactic.rename_params_tac,
wenzelm@20335
   577
    "rename parameters of goal (dynamic instantiation)"),
wenzelm@20335
   578
  ("rotate_tac", goal_args (Scan.optional Args.int 1) Tactic.rotate_tac,
wenzelm@20335
   579
    "rotate assumptions of goal"),
wenzelm@18708
   580
  ("tactic", simple_args Args.name tactic, "ML tactic as proof method")]);
wenzelm@5824
   581
wenzelm@5824
   582
wenzelm@16145
   583
(*final declarations of this structure!*)
wenzelm@16145
   584
val unfold = unfold_meth;
wenzelm@16145
   585
val fold = fold_meth;
wenzelm@16145
   586
wenzelm@5824
   587
end;
wenzelm@5824
   588
wenzelm@5824
   589
structure BasicMethod: BASIC_METHOD = Method;
wenzelm@5824
   590
open BasicMethod;