src/Pure/Isar/method.ML
author wenzelm
Thu, 01 Jul 1999 17:41:16 +0200
changeset 6872 b250da153b1e
parent 6849 0b660860c0ad
child 6934 04d051190a5f
permissions -rw-r--r--
fixed backtracking of global_qed;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/method.ML
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
     4
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
     5
Proof methods.
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
     6
*)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
     7
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
     8
signature BASIC_METHOD =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
     9
sig
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    10
  val print_methods: theory -> unit
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    11
  val Method: bstring -> (Args.src -> Proof.context -> Proof.method) -> string -> unit
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    12
end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    13
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    14
signature METHOD =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    15
sig
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    16
  include BASIC_METHOD
6775
9d96ce9c27d6 export multi_resolve;
wenzelm
parents: 6736
diff changeset
    17
  val multi_resolve: thm list -> thm -> thm Seq.seq
6500
68ba555ac59a added FINISHED, same_tac;
wenzelm
parents: 6404
diff changeset
    18
  val FINISHED: tactic -> tactic
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5921
diff changeset
    19
  val METHOD: (thm list -> tactic) -> Proof.method
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    20
  val METHOD0: tactic -> Proof.method
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    21
  val fail: Proof.method
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    22
  val succeed: Proof.method
6500
68ba555ac59a added FINISHED, same_tac;
wenzelm
parents: 6404
diff changeset
    23
  val same_tac: thm list -> int -> tactic
6108
2c9ed58c30ba 'same' method, 'immediate' proof;
wenzelm
parents: 6104
diff changeset
    24
  val same: Proof.method
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    25
  val assumption: Proof.method
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    26
  val forward_chain: thm list -> thm list -> thm Seq.seq
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5921
diff changeset
    27
  val rule_tac: thm list -> thm list -> int -> tactic
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5921
diff changeset
    28
  val rule: thm list -> Proof.method
5916
4039395e29ce exception METHOD_FAIL;
wenzelm
parents: 5884
diff changeset
    29
  exception METHOD_FAIL of (string * Position.T) * exn
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    30
  val method: theory -> Args.src -> Proof.context -> Proof.method
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    31
  val add_methods: (bstring * (Args.src -> Proof.context -> Proof.method) * string) list
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    32
    -> theory -> theory
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    33
  val syntax: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    34
    Proof.context -> Args.src -> Proof.context * 'a
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    35
  val no_args: Proof.method -> Args.src -> Proof.context -> Proof.method
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    36
  val sectioned_args: ((Args.T list -> Proof.context attribute * Args.T list) list ->
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    37
      Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    38
    (Args.T list -> Proof.context attribute * Args.T list) list ->
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    39
    ('a -> Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    40
  val default_sectioned_args: Proof.context attribute ->
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    41
    (Args.T list -> Proof.context attribute * Args.T list) list ->
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    42
    (Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    43
  val only_sectioned_args: (Args.T list -> Proof.context attribute * Args.T list) list ->
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    44
    (Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5921
diff changeset
    45
  val thms_args: (thm list -> Proof.method) -> Args.src -> Proof.context -> Proof.method
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    46
  datatype text =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    47
    Basic of (Proof.context -> Proof.method) |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    48
    Source of Args.src |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    49
    Then of text list |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    50
    Orelse of text list |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    51
    Try of text |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    52
    Repeat of text |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    53
    Repeat1 of text
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    54
  val refine: text -> Proof.state -> Proof.state Seq.seq
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    55
  val tac: text -> Proof.state -> Proof.state Seq.seq
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    56
  val then_tac: text -> Proof.state -> Proof.state Seq.seq
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    57
  val proof: text option -> Proof.state -> Proof.state Seq.seq
6736
a0b2cfa12d0d local_qed: print_result;
wenzelm
parents: 6683
diff changeset
    58
  val local_qed: text option -> ({kind: string, name: string, thm: thm} -> unit)
a0b2cfa12d0d local_qed: print_result;
wenzelm
parents: 6683
diff changeset
    59
    -> Proof.state -> Proof.state Seq.seq
a0b2cfa12d0d local_qed: print_result;
wenzelm
parents: 6683
diff changeset
    60
  val local_terminal_proof: text -> ({kind: string, name: string, thm: thm} -> unit)
a0b2cfa12d0d local_qed: print_result;
wenzelm
parents: 6683
diff changeset
    61
    -> Proof.state -> Proof.state Seq.seq
a0b2cfa12d0d local_qed: print_result;
wenzelm
parents: 6683
diff changeset
    62
  val local_immediate_proof: ({kind: string, name: string, thm: thm} -> unit)
a0b2cfa12d0d local_qed: print_result;
wenzelm
parents: 6683
diff changeset
    63
    -> Proof.state -> Proof.state Seq.seq
a0b2cfa12d0d local_qed: print_result;
wenzelm
parents: 6683
diff changeset
    64
  val local_default_proof: ({kind: string, name: string, thm: thm} -> unit)
a0b2cfa12d0d local_qed: print_result;
wenzelm
parents: 6683
diff changeset
    65
    -> Proof.state -> Proof.state Seq.seq
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6108
diff changeset
    66
  val global_qed: bstring option -> theory attribute list option -> text option
6532
9d79a304aecc fold / unfold methods;
wenzelm
parents: 6500
diff changeset
    67
    -> Proof.state -> theory * {kind: string, name: string, thm: thm}
9d79a304aecc fold / unfold methods;
wenzelm
parents: 6500
diff changeset
    68
  val global_terminal_proof: text -> Proof.state -> theory * {kind: string, name: string, thm: thm}
9d79a304aecc fold / unfold methods;
wenzelm
parents: 6500
diff changeset
    69
  val global_immediate_proof: Proof.state -> theory * {kind: string, name: string, thm: thm}
9d79a304aecc fold / unfold methods;
wenzelm
parents: 6500
diff changeset
    70
  val global_default_proof: Proof.state -> theory * {kind: string, name: string, thm: thm}
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    71
  val setup: (theory -> theory) list
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    72
end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    73
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    74
structure Method: METHOD =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    75
struct
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    76
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    77
6500
68ba555ac59a added FINISHED, same_tac;
wenzelm
parents: 6404
diff changeset
    78
(** utils **)
68ba555ac59a added FINISHED, same_tac;
wenzelm
parents: 6404
diff changeset
    79
68ba555ac59a added FINISHED, same_tac;
wenzelm
parents: 6404
diff changeset
    80
val FINISHED = FILTER (equal 0 o Thm.nprems_of);
68ba555ac59a added FINISHED, same_tac;
wenzelm
parents: 6404
diff changeset
    81
68ba555ac59a added FINISHED, same_tac;
wenzelm
parents: 6404
diff changeset
    82
68ba555ac59a added FINISHED, same_tac;
wenzelm
parents: 6404
diff changeset
    83
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    84
(** proof methods **)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    85
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    86
(* method from tactic *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    87
6849
0b660860c0ad cond_extern_table;
wenzelm
parents: 6775
diff changeset
    88
val METHOD = Proof.method;
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    89
fun METHOD0 tac = METHOD (fn [] => tac | _ => error "Method may not be used with facts");
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    90
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    91
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    92
(* primitive *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    93
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    94
val fail = METHOD (K no_tac);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    95
val succeed = METHOD (K all_tac);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    96
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    97
6108
2c9ed58c30ba 'same' method, 'immediate' proof;
wenzelm
parents: 6104
diff changeset
    98
(* same, assumption *)
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    99
6108
2c9ed58c30ba 'same' method, 'immediate' proof;
wenzelm
parents: 6104
diff changeset
   100
fun same_tac [] = K all_tac
2c9ed58c30ba 'same' method, 'immediate' proof;
wenzelm
parents: 6104
diff changeset
   101
  | same_tac facts =
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5921
diff changeset
   102
      let val r = ~ (length facts);
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5921
diff changeset
   103
      in metacuts_tac facts THEN' rotate_tac r end;
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   104
6108
2c9ed58c30ba 'same' method, 'immediate' proof;
wenzelm
parents: 6104
diff changeset
   105
val same = METHOD (ALLGOALS o same_tac);
2c9ed58c30ba 'same' method, 'immediate' proof;
wenzelm
parents: 6104
diff changeset
   106
val assumption = METHOD (fn facts => FIRSTGOAL (same_tac facts THEN' assume_tac));
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   107
6500
68ba555ac59a added FINISHED, same_tac;
wenzelm
parents: 6404
diff changeset
   108
val asm_finish = METHOD (K (FINISHED (ALLGOALS assume_tac)));
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   109
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   110
6532
9d79a304aecc fold / unfold methods;
wenzelm
parents: 6500
diff changeset
   111
(* fold / unfold definitions *)
9d79a304aecc fold / unfold methods;
wenzelm
parents: 6500
diff changeset
   112
9d79a304aecc fold / unfold methods;
wenzelm
parents: 6500
diff changeset
   113
val fold = METHOD0 o fold_goals_tac;
9d79a304aecc fold / unfold methods;
wenzelm
parents: 6500
diff changeset
   114
val unfold = METHOD0 o rewrite_goals_tac;
9d79a304aecc fold / unfold methods;
wenzelm
parents: 6500
diff changeset
   115
9d79a304aecc fold / unfold methods;
wenzelm
parents: 6500
diff changeset
   116
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   117
(* rule *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   118
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   119
fun res th i rule = Thm.biresolution false [(false, th)] i rule handle THM _ => Seq.empty;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   120
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   121
fun multi_resolve facts rule =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   122
  let
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   123
    fun multi_res i [] = Seq.single rule
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   124
      | multi_res i (th :: ths) = Seq.flat (Seq.map (res th i) (multi_res (i + 1) ths))
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   125
  in multi_res 1 facts end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   126
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   127
fun forward_chain facts rules = Seq.flat (Seq.map (multi_resolve facts) (Seq.of_list rules));
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   128
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5921
diff changeset
   129
fun rule_tac rules [] = resolve_tac rules
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   130
  | rule_tac erules facts =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   131
      let
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5921
diff changeset
   132
        val rules = forward_chain facts erules;
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   133
        fun tac i state = Seq.flat (Seq.map (fn rule => Tactic.rtac rule i state) rules);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   134
      in tac end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   135
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   136
fun rule rules = METHOD (FIRSTGOAL o rule_tac rules);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   137
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   138
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   139
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   140
(** methods theory data **)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   141
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   142
(* data kind 'Isar/methods' *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   143
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   144
structure MethodsDataArgs =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   145
struct
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   146
  val name = "Isar/methods";
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   147
  type T =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   148
    {space: NameSpace.T,
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   149
     meths: (((Args.src -> Proof.context -> Proof.method) * string) * stamp) Symtab.table};
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   150
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   151
  val empty = {space = NameSpace.empty, meths = Symtab.empty};
6546
995a66249a9b theory data: copy;
wenzelm
parents: 6532
diff changeset
   152
  val copy = I;
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   153
  val prep_ext = I;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   154
  fun merge ({space = space1, meths = meths1}, {space = space2, meths = meths2}) =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   155
    {space = NameSpace.merge (space1, space2),
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   156
      meths = Symtab.merge eq_snd (meths1, meths2) handle Symtab.DUPS dups =>
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   157
        error ("Attempt to merge different versions of methods " ^ commas_quote dups)};
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   158
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   159
  fun print _ {space, meths} =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   160
    let
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   161
      fun prt_meth (name, ((_, comment), _)) = Pretty.block
6849
0b660860c0ad cond_extern_table;
wenzelm
parents: 6775
diff changeset
   162
        [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   163
    in
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   164
      Pretty.writeln (Display.pretty_name_space ("method name space", space));
6849
0b660860c0ad cond_extern_table;
wenzelm
parents: 6775
diff changeset
   165
      Pretty.writeln (Pretty.big_list "methods:"
0b660860c0ad cond_extern_table;
wenzelm
parents: 6775
diff changeset
   166
        (map prt_meth (NameSpace.cond_extern_table space meths)))
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   167
    end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   168
end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   169
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   170
structure MethodsData = TheoryDataFun(MethodsDataArgs);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   171
val print_methods = MethodsData.print;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   172
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   173
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   174
(* get methods *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   175
5916
4039395e29ce exception METHOD_FAIL;
wenzelm
parents: 5884
diff changeset
   176
exception METHOD_FAIL of (string * Position.T) * exn;
4039395e29ce exception METHOD_FAIL;
wenzelm
parents: 5884
diff changeset
   177
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   178
fun method thy =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   179
  let
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   180
    val {space, meths} = MethodsData.get thy;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   181
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   182
    fun meth src =
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   183
      let
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   184
        val ((raw_name, _), pos) = Args.dest_src src;
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   185
        val name = NameSpace.intern space raw_name;
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   186
      in
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   187
        (case Symtab.lookup (meths, name) of
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   188
          None => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
5916
4039395e29ce exception METHOD_FAIL;
wenzelm
parents: 5884
diff changeset
   189
        | Some ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src))
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   190
      end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   191
  in meth end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   192
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   193
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   194
(* add_methods *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   195
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   196
fun add_methods raw_meths thy =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   197
  let
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   198
    val full = Sign.full_name (Theory.sign_of thy);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   199
    val new_meths =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   200
      map (fn (name, f, comment) => (full name, ((f, comment), stamp ()))) raw_meths;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   201
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   202
    val {space, meths} = MethodsData.get thy;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   203
    val space' = NameSpace.extend (space, map fst new_meths);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   204
    val meths' = Symtab.extend (meths, new_meths) handle Symtab.DUPS dups =>
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   205
      error ("Duplicate declaration of method(s) " ^ commas_quote dups);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   206
  in
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   207
    thy |> MethodsData.put {space = space', meths = meths'}
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   208
  end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   209
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   210
(*implicit version*)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   211
fun Method name meth cmt = Context.>> (add_methods [(name, meth, cmt)]);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   212
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   213
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   214
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   215
(** method syntax **)
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   216
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   217
(* basic *)
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   218
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   219
fun syntax (scan: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))) =
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   220
  Args.syntax "method" scan;
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   221
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   222
fun no_args (x: Proof.method) src ctxt = #2 (syntax (Scan.succeed x) ctxt src);
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   223
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   224
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   225
(* sections *)
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   226
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   227
fun sect ss = Scan.first (map (fn s => Scan.lift (s --| Args.$$$ ":")) ss);
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   228
fun thms ss = Scan.unless (sect ss) Attrib.local_thms;
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   229
fun thmss ss = Scan.repeat (thms ss) >> flat;
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   230
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5921
diff changeset
   231
fun apply att (ctxt, ths) = Thm.applys_attributes ((ctxt, ths), [att]);
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   232
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   233
fun section ss = (sect ss -- thmss ss) :-- (fn (att, ths) => Scan.depend (fn ctxt =>
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   234
  Scan.succeed (apply att (ctxt, ths)))) >> #2;
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   235
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   236
fun sectioned args ss = args ss -- Scan.repeat (section ss);
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   237
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   238
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   239
fun sectioned_args args ss f src ctxt =
5921
50005d6ba609 removed trace;
wenzelm
parents: 5916
diff changeset
   240
  let val (ctxt', (x, _)) = syntax (sectioned args ss) ctxt src
50005d6ba609 removed trace;
wenzelm
parents: 5916
diff changeset
   241
  in f x ctxt' end;
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   242
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   243
fun default_sectioned_args att ss f src ctxt =
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   244
  sectioned_args thmss ss (fn ths => fn ctxt' => f (#1 (apply att (ctxt', ths)))) src ctxt;
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   245
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   246
fun only_sectioned_args ss f = sectioned_args (K (Scan.succeed ())) ss (fn () => f);
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   247
fun thms_args f = sectioned_args thmss [] (fn ths => fn _ => f ths);
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   248
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   249
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   250
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   251
(** method text **)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   252
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   253
(* datatype text *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   254
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   255
datatype text =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   256
  Basic of (Proof.context -> Proof.method) |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   257
  Source of Args.src |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   258
  Then of text list |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   259
  Orelse of text list |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   260
  Try of text |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   261
  Repeat of text |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   262
  Repeat1 of text;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   263
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   264
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   265
(* refine *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   266
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   267
fun refine text state =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   268
  let
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   269
    val thy = Proof.theory_of state;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   270
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   271
    fun eval (Basic mth) = Proof.refine mth
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   272
      | eval (Source src) = Proof.refine (method thy src)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   273
      | eval (Then txts) = Seq.EVERY (map eval txts)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   274
      | eval (Orelse txts) = Seq.FIRST (map eval txts)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   275
      | eval (Try txt) = Seq.TRY (eval txt)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   276
      | eval (Repeat txt) = Seq.REPEAT (eval txt)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   277
      | eval (Repeat1 txt) = Seq.REPEAT1 (eval txt);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   278
  in eval text state end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   279
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6108
diff changeset
   280
fun named_method name = Source (Args.src ((name, []), Position.none));
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6108
diff changeset
   281
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   282
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   283
(* unstructured steps *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   284
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   285
fun tac text state =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   286
  state
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   287
  |> Proof.goal_facts (K [])
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   288
  |> refine text;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   289
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   290
fun then_tac text state =
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   291
  state
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   292
  |> Proof.goal_facts Proof.the_facts
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   293
  |> refine text;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   294
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   295
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6108
diff changeset
   296
(* structured proof steps *)
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   297
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6108
diff changeset
   298
val default_text = named_method "default";
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6108
diff changeset
   299
val finish_text = named_method "finish";
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   300
6872
b250da153b1e fixed backtracking of global_qed;
wenzelm
parents: 6849
diff changeset
   301
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   302
fun proof opt_text state =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   303
  state
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   304
  |> Proof.assert_backward
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6108
diff changeset
   305
  |> refine (if_none opt_text default_text)
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   306
  |> Seq.map Proof.enter_forward;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   307
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6108
diff changeset
   308
fun local_qed opt_text = Proof.local_qed (refine (if_none opt_text finish_text));
6736
a0b2cfa12d0d local_qed: print_result;
wenzelm
parents: 6683
diff changeset
   309
fun local_terminal_proof text pr = Seq.THEN (proof (Some text), local_qed None pr);
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6108
diff changeset
   310
val local_immediate_proof = local_terminal_proof (Basic (K same));
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6108
diff changeset
   311
val local_default_proof = local_terminal_proof default_text;
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   312
6872
b250da153b1e fixed backtracking of global_qed;
wenzelm
parents: 6849
diff changeset
   313
b250da153b1e fixed backtracking of global_qed;
wenzelm
parents: 6849
diff changeset
   314
fun global_qeds alt_name alt_atts opt_text =
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6108
diff changeset
   315
  Proof.global_qed (refine (if_none opt_text finish_text)) alt_name alt_atts;
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   316
6872
b250da153b1e fixed backtracking of global_qed;
wenzelm
parents: 6849
diff changeset
   317
fun global_qed alt_name alt_atts opt_text state =
b250da153b1e fixed backtracking of global_qed;
wenzelm
parents: 6849
diff changeset
   318
  state
b250da153b1e fixed backtracking of global_qed;
wenzelm
parents: 6849
diff changeset
   319
  |> global_qeds alt_name alt_atts opt_text
b250da153b1e fixed backtracking of global_qed;
wenzelm
parents: 6849
diff changeset
   320
  |> Proof.check_result "Failed to finish proof" state
b250da153b1e fixed backtracking of global_qed;
wenzelm
parents: 6849
diff changeset
   321
  |> Seq.hd;
b250da153b1e fixed backtracking of global_qed;
wenzelm
parents: 6849
diff changeset
   322
b250da153b1e fixed backtracking of global_qed;
wenzelm
parents: 6849
diff changeset
   323
fun global_terminal_proof text state =
b250da153b1e fixed backtracking of global_qed;
wenzelm
parents: 6849
diff changeset
   324
  state
b250da153b1e fixed backtracking of global_qed;
wenzelm
parents: 6849
diff changeset
   325
  |> proof (Some text)
b250da153b1e fixed backtracking of global_qed;
wenzelm
parents: 6849
diff changeset
   326
  |> Proof.check_result "Terminal proof method failed" state
b250da153b1e fixed backtracking of global_qed;
wenzelm
parents: 6849
diff changeset
   327
  |> (Seq.flat o Seq.map (global_qeds None None None))
b250da153b1e fixed backtracking of global_qed;
wenzelm
parents: 6849
diff changeset
   328
  |> Proof.check_result "Failed to finish proof (after successful terminal method)" state
b250da153b1e fixed backtracking of global_qed;
wenzelm
parents: 6849
diff changeset
   329
  |> Seq.hd;
b250da153b1e fixed backtracking of global_qed;
wenzelm
parents: 6849
diff changeset
   330
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6108
diff changeset
   331
val global_immediate_proof = global_terminal_proof (Basic (K same));
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6108
diff changeset
   332
val global_default_proof = global_terminal_proof default_text;
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   333
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   334
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   335
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   336
(** theory setup **)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   337
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   338
(* pure_methods *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   339
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   340
val pure_methods =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   341
 [("fail", no_args fail, "force failure"),
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   342
  ("succeed", no_args succeed, "succeed"),
6108
2c9ed58c30ba 'same' method, 'immediate' proof;
wenzelm
parents: 6104
diff changeset
   343
  ("same", no_args same, "insert facts, nothing else"),
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   344
  ("assumption", no_args assumption, "proof by assumption"),
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   345
  ("finish", no_args asm_finish, "finish proof by assumption"),
6532
9d79a304aecc fold / unfold methods;
wenzelm
parents: 6500
diff changeset
   346
  ("fold", thms_args fold, "fold definitions"),
9d79a304aecc fold / unfold methods;
wenzelm
parents: 6500
diff changeset
   347
  ("unfold", thms_args unfold, "unfold definitions"),
6849
0b660860c0ad cond_extern_table;
wenzelm
parents: 6775
diff changeset
   348
  ("rule", thms_args rule, "apply some rule")];
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   349
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   350
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   351
(* setup *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   352
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   353
val setup = [MethodsData.init, add_methods pure_methods];
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   354
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   355
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   356
end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   357
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   358
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   359
structure BasicMethod: BASIC_METHOD = Method;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   360
open BasicMethod;