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