src/Pure/Isar/method.ML
author wenzelm
Wed, 18 Nov 1998 10:56:38 +0100
changeset 5921 50005d6ba609
parent 5916 4039395e29ce
child 6091 e3cdbd929a24
permissions -rw-r--r--
removed trace;
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
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    17
  val LIFT: tactic -> thm -> (thm * (indexname * term) list * (string * tthm list) list) Seq.seq
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    18
  val METHOD: (tthm list -> tactic) -> Proof.method
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    19
  val METHOD0: tactic -> Proof.method
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    20
  val fail: Proof.method
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    21
  val succeed: Proof.method
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    22
  val trivial: Proof.method
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    23
  val assumption: Proof.method
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    24
  val forward_chain: thm list -> thm list -> thm Seq.seq
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    25
  val rule_tac: tthm list -> tthm list -> int -> tactic
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    26
  val rule: tthm list -> Proof.method
5916
4039395e29ce exception METHOD_FAIL;
wenzelm
parents: 5884
diff changeset
    27
  exception METHOD_FAIL of (string * Position.T) * exn
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    28
  val method: theory -> Args.src -> Proof.context -> Proof.method
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    29
  val add_methods: (bstring * (Args.src -> Proof.context -> Proof.method) * string) list
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    30
    -> theory -> theory
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    31
  val syntax: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    32
    Proof.context -> Args.src -> Proof.context * 'a
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    33
  val no_args: Proof.method -> Args.src -> Proof.context -> Proof.method
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    34
  val sectioned_args: ((Args.T list -> Proof.context attribute * Args.T list) list ->
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    35
      Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    36
    (Args.T list -> Proof.context attribute * Args.T list) list ->
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    37
    ('a -> Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    38
  val default_sectioned_args: Proof.context attribute ->
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    39
    (Args.T list -> Proof.context attribute * Args.T list) list ->
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    40
    (Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    41
  val only_sectioned_args: (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 thms_args: (tthm list -> Proof.method) -> Args.src -> Proof.context -> Proof.method
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    44
  datatype text =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    45
    Basic of (Proof.context -> Proof.method) |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    46
    Source of Args.src |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    47
    Then of text list |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    48
    Orelse of text list |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    49
    Try of text |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    50
    Repeat of text |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    51
    Repeat1 of text
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    52
  val dynamic_method: string -> (Proof.context -> Proof.method)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    53
  val refine: text -> Proof.state -> Proof.state Seq.seq
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    54
  val tac: text -> Proof.state -> Proof.state Seq.seq
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    55
  val then_tac: text -> Proof.state -> Proof.state Seq.seq
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    56
  val proof: text option -> Proof.state -> Proof.state Seq.seq
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    57
  val end_block: Proof.state -> Proof.state Seq.seq
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    58
  val terminal_proof: text -> Proof.state -> Proof.state Seq.seq
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    59
  val trivial_proof: Proof.state -> Proof.state Seq.seq
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    60
  val default_proof: Proof.state -> Proof.state Seq.seq
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    61
  val qed: bstring option -> theory attribute list option -> Proof.state
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    62
    -> theory * (string * string * tthm)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    63
  val setup: (theory -> theory) list
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    64
end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    65
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    66
structure Method: METHOD =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    67
struct
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    68
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    69
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    70
(** proof methods **)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    71
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    72
(* method from tactic *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    73
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    74
fun LIFT tac goal = Seq.map (fn x => (x, [], [])) (tac goal);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    75
fun METHOD tacf = Proof.method (LIFT o tacf);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    76
fun METHOD0 tac = METHOD (fn [] => tac | _ => error "Method may not be used with facts");
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    77
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    78
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    79
(* primitive *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    80
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    81
val fail = METHOD (K no_tac);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    82
val succeed = METHOD (K all_tac);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    83
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    84
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    85
(* trivial, assumption *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    86
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    87
fun trivial_tac [] = K all_tac
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    88
  | trivial_tac facts =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    89
      let
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
    90
        val thms = Attribute.thms_of facts;
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    91
        val r = ~ (length facts);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    92
      in metacuts_tac thms THEN' rotate_tac r end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    93
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    94
val trivial = METHOD (ALLGOALS o trivial_tac);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    95
val assumption = METHOD (fn facts => FIRSTGOAL (trivial_tac facts THEN' assume_tac));
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    96
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    97
val asm_finish = METHOD (K (TRYALL assume_tac));
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    98
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
    99
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   100
(* rule *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   101
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   102
fun res th i rule = Thm.biresolution false [(false, th)] i rule handle THM _ => Seq.empty;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   103
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   104
fun multi_resolve facts rule =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   105
  let
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   106
    fun multi_res i [] = Seq.single rule
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   107
      | multi_res i (th :: ths) = Seq.flat (Seq.map (res th i) (multi_res (i + 1) ths))
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   108
  in multi_res 1 facts end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   109
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   110
fun forward_chain facts rules = Seq.flat (Seq.map (multi_resolve facts) (Seq.of_list rules));
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   111
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   112
fun rule_tac rules [] = resolve_tac (Attribute.thms_of rules)
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   113
  | rule_tac erules facts =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   114
      let
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   115
        val rules = forward_chain (Attribute.thms_of facts) (Attribute.thms_of erules);
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   116
        fun tac i state = Seq.flat (Seq.map (fn rule => Tactic.rtac rule i state) rules);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   117
      in tac end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   118
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   119
fun rule rules = METHOD (FIRSTGOAL o rule_tac rules);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   120
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   121
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   122
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   123
(** methods theory data **)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   124
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   125
(* data kind 'Isar/methods' *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   126
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   127
structure MethodsDataArgs =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   128
struct
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   129
  val name = "Isar/methods";
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   130
  type T =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   131
    {space: NameSpace.T,
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   132
     meths: (((Args.src -> Proof.context -> Proof.method) * string) * stamp) Symtab.table};
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   133
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   134
  val empty = {space = NameSpace.empty, meths = Symtab.empty};
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   135
  val prep_ext = I;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   136
  fun merge ({space = space1, meths = meths1}, {space = space2, meths = meths2}) =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   137
    {space = NameSpace.merge (space1, space2),
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   138
      meths = Symtab.merge eq_snd (meths1, meths2) handle Symtab.DUPS dups =>
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   139
        error ("Attempt to merge different versions of methods " ^ commas_quote dups)};
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   140
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   141
  fun print _ {space, meths} =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   142
    let
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   143
      fun prt_meth (name, ((_, comment), _)) = Pretty.block
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   144
        [Pretty.str (NameSpace.cond_extern space name ^ ":"), Pretty.brk 2, Pretty.str comment];
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   145
    in
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   146
      Pretty.writeln (Display.pretty_name_space ("method name space", space));
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   147
      Pretty.writeln (Pretty.big_list "methods:" (map prt_meth (Symtab.dest meths)))
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   148
    end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   149
end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   150
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   151
structure MethodsData = TheoryDataFun(MethodsDataArgs);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   152
val print_methods = MethodsData.print;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   153
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   154
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   155
(* get methods *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   156
5916
4039395e29ce exception METHOD_FAIL;
wenzelm
parents: 5884
diff changeset
   157
exception METHOD_FAIL of (string * Position.T) * exn;
4039395e29ce exception METHOD_FAIL;
wenzelm
parents: 5884
diff changeset
   158
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   159
fun method thy =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   160
  let
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   161
    val {space, meths} = MethodsData.get thy;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   162
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   163
    fun meth src =
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   164
      let
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   165
        val ((raw_name, _), pos) = Args.dest_src src;
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   166
        val name = NameSpace.intern space raw_name;
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   167
      in
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   168
        (case Symtab.lookup (meths, name) of
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   169
          None => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
5916
4039395e29ce exception METHOD_FAIL;
wenzelm
parents: 5884
diff changeset
   170
        | Some ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src))
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   171
      end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   172
  in meth end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   173
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   174
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   175
(* add_methods *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   176
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   177
fun add_methods raw_meths thy =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   178
  let
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   179
    val full = Sign.full_name (Theory.sign_of thy);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   180
    val new_meths =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   181
      map (fn (name, f, comment) => (full name, ((f, comment), stamp ()))) raw_meths;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   182
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   183
    val {space, meths} = MethodsData.get thy;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   184
    val space' = NameSpace.extend (space, map fst new_meths);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   185
    val meths' = Symtab.extend (meths, new_meths) handle Symtab.DUPS dups =>
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   186
      error ("Duplicate declaration of method(s) " ^ commas_quote dups);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   187
  in
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   188
    thy |> MethodsData.put {space = space', meths = meths'}
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   189
  end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   190
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   191
(*implicit version*)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   192
fun Method name meth cmt = Context.>> (add_methods [(name, meth, cmt)]);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   193
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   194
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   195
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   196
(** method syntax **)
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   197
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   198
(* basic *)
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   199
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   200
fun syntax (scan: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))) =
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   201
  Args.syntax "method" scan;
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   202
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   203
fun no_args (x: Proof.method) src ctxt = #2 (syntax (Scan.succeed x) ctxt src);
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   204
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   205
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   206
(* sections *)
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   207
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   208
fun sect ss = Scan.first (map (fn s => Scan.lift (s --| Args.$$$ ":")) ss);
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   209
fun thms ss = Scan.unless (sect ss) Attrib.local_thms;
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   210
fun thmss ss = Scan.repeat (thms ss) >> flat;
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   211
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   212
fun apply att (ctxt, ths) = Attribute.applys ((ctxt, ths), [att]);
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   213
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   214
fun section ss = (sect ss -- thmss ss) :-- (fn (att, ths) => Scan.depend (fn ctxt =>
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   215
  Scan.succeed (apply att (ctxt, ths)))) >> #2;
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   216
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   217
fun sectioned args ss = args ss -- Scan.repeat (section ss);
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   218
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   219
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   220
fun sectioned_args args ss f src ctxt =
5921
50005d6ba609 removed trace;
wenzelm
parents: 5916
diff changeset
   221
  let val (ctxt', (x, _)) = syntax (sectioned args ss) ctxt src
50005d6ba609 removed trace;
wenzelm
parents: 5916
diff changeset
   222
  in f x ctxt' end;
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   223
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   224
fun default_sectioned_args att ss f src ctxt =
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   225
  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
   226
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   227
fun only_sectioned_args ss f = sectioned_args (K (Scan.succeed ())) ss (fn () => f);
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   228
fun thms_args f = sectioned_args thmss [] (fn ths => fn _ => f ths);
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   229
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   230
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   231
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   232
(** method text **)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   233
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   234
(* datatype text *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   235
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   236
datatype text =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   237
  Basic of (Proof.context -> Proof.method) |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   238
  Source of Args.src |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   239
  Then of text list |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   240
  Orelse of text list |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   241
  Try of text |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   242
  Repeat of text |
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   243
  Repeat1 of text;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   244
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   245
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   246
(* dynamic methods *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   247
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   248
fun dynamic_method name = (fn ctxt =>
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   249
  method (ProofContext.theory_of ctxt) (Args.src ((name, []), Position.none)) ctxt);
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   250
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   251
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   252
(* refine *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   253
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   254
fun refine text state =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   255
  let
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   256
    val thy = Proof.theory_of state;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   257
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   258
    fun eval (Basic mth) = Proof.refine mth
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   259
      | eval (Source src) = Proof.refine (method thy src)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   260
      | eval (Then txts) = Seq.EVERY (map eval txts)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   261
      | eval (Orelse txts) = Seq.FIRST (map eval txts)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   262
      | eval (Try txt) = Seq.TRY (eval txt)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   263
      | eval (Repeat txt) = Seq.REPEAT (eval txt)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   264
      | eval (Repeat1 txt) = Seq.REPEAT1 (eval txt);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   265
  in eval text state end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   266
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   267
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   268
(* unstructured steps *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   269
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   270
fun tac text state =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   271
  state
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   272
  |> Proof.goal_facts (K [])
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   273
  |> refine text;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   274
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   275
fun then_tac text state =
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   276
  state
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   277
  |> Proof.goal_facts Proof.the_facts
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   278
  |> refine text;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   279
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   280
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   281
(* proof steps *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   282
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   283
val default_txt = Source (Args.src (("default", []), Position.none));
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   284
val finishN = "finish";
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   285
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   286
fun proof opt_text state =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   287
  state
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   288
  |> Proof.assert_backward
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   289
  |> refine (if_none opt_text default_txt)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   290
  |> Seq.map Proof.enter_forward;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   291
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   292
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   293
(* conclusions *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   294
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   295
val end_block = Proof.end_block (dynamic_method finishN);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   296
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   297
fun terminal_proof text = Seq.THEN (proof (Some text), end_block);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   298
val trivial_proof = terminal_proof (Basic (K trivial));
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   299
val default_proof = terminal_proof default_txt;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   300
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   301
val qed = Proof.qed (dynamic_method finishN);
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   302
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   303
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   304
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   305
(** theory setup **)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   306
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   307
(* pure_methods *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   308
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   309
val pure_methods =
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   310
 [("fail", no_args fail, "force failure"),
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   311
  ("succeed", no_args succeed, "succeed"),
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   312
  ("trivial", no_args trivial, "proof all goals trivially"),
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   313
  ("assumption", no_args assumption, "proof by assumption"),
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   314
  ("finish", no_args asm_finish, "finish proof by assumption"),
5884
113badd4dae5 several args parsers;
wenzelm
parents: 5824
diff changeset
   315
  ("rule", thms_args rule, "apply primitive rule")];
5824
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   316
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   317
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   318
(* setup *)
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   319
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   320
val setup = [MethodsData.init, add_methods pure_methods];
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   321
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   322
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   323
end;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   324
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   325
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   326
structure BasicMethod: BASIC_METHOD = Method;
91113aa09371 Proof methods.
wenzelm
parents:
diff changeset
   327
open BasicMethod;