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