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