src/Pure/Isar/proof.ML
author wenzelm
Mon Feb 14 11:23:44 2000 +0100 (2000-02-14)
changeset 8239 07f25f7d2218
parent 8234 36a85a6c4852
child 8374 ffb2eb084078
permissions -rw-r--r--
tuned msg;
wenzelm@5820
     1
(*  Title:      Pure/Isar/proof.ML
wenzelm@5820
     2
    ID:         $Id$
wenzelm@5820
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5820
     4
wenzelm@5820
     5
Proof states and methods.
wenzelm@5820
     6
*)
wenzelm@5820
     7
wenzelm@8152
     8
signature BASIC_PROOF =
wenzelm@8152
     9
sig
wenzelm@8152
    10
  val FINDGOAL: (int -> tactic) -> tactic
wenzelm@8166
    11
  val HEADGOAL: (int -> tactic) -> tactic
wenzelm@8152
    12
end;
wenzelm@8152
    13
wenzelm@5820
    14
signature PROOF =
wenzelm@5820
    15
sig
wenzelm@8152
    16
  include BASIC_PROOF
wenzelm@5820
    17
  type context
wenzelm@5820
    18
  type state
wenzelm@5820
    19
  exception STATE of string * state
wenzelm@6871
    20
  val check_result: string -> state -> 'a Seq.seq -> 'a Seq.seq
wenzelm@7011
    21
  val init_state: theory -> state
wenzelm@5820
    22
  val context_of: state -> context
wenzelm@5820
    23
  val theory_of: state -> theory
wenzelm@5820
    24
  val sign_of: state -> Sign.sg
wenzelm@7924
    25
  val warn_extra_tfrees: state -> state -> state
wenzelm@7605
    26
  val reset_thms: string -> state -> state
wenzelm@6091
    27
  val the_facts: state -> thm list
wenzelm@7665
    28
  val get_goal: state -> term * (thm list * thm)
wenzelm@6091
    29
  val goal_facts: (state -> thm list) -> state -> state
wenzelm@5820
    30
  val use_facts: state -> state
wenzelm@5820
    31
  val reset_facts: state -> state
wenzelm@6891
    32
  val assert_forward: state -> state
wenzelm@5820
    33
  val assert_backward: state -> state
wenzelm@8206
    34
  val assert_no_chain: state -> state
wenzelm@5820
    35
  val enter_forward: state -> state
wenzelm@6982
    36
  val show_hyps: bool ref
wenzelm@6982
    37
  val pretty_thm: thm -> Pretty.T
wenzelm@7412
    38
  val pretty_thms: thm list -> Pretty.T
wenzelm@6529
    39
  val verbose: bool ref
wenzelm@7201
    40
  val print_state: int -> state -> unit
wenzelm@6776
    41
  val level: state -> int
wenzelm@5820
    42
  type method
wenzelm@6848
    43
  val method: (thm list -> tactic) -> method
wenzelm@5820
    44
  val refine: (context -> method) -> state -> state Seq.seq
wenzelm@8234
    45
  val refine_end: (context -> method) -> state -> state Seq.seq
wenzelm@7669
    46
  val find_free: term -> string -> term option
wenzelm@6932
    47
  val export_thm: context -> thm -> thm
wenzelm@5936
    48
  val match_bind: (string list * string) list -> state -> state
wenzelm@5936
    49
  val match_bind_i: (term list * term) list -> state -> state
wenzelm@6876
    50
  val have_thmss: thm list -> string -> context attribute list ->
wenzelm@6091
    51
    (thm list * context attribute list) list -> state -> state
wenzelm@6876
    52
  val simple_have_thms: string -> thm list -> state -> state
wenzelm@7412
    53
  val fix: (string list * string option) list -> state -> state
wenzelm@7665
    54
  val fix_i: (string list * typ option) list -> state -> state
wenzelm@7271
    55
  val assm: (int -> tactic) * (int -> tactic)
wenzelm@7271
    56
    -> (string * context attribute list * (string * (string list * string list)) list) list
wenzelm@7271
    57
    -> state -> state
wenzelm@7271
    58
  val assm_i: (int -> tactic) * (int -> tactic)
wenzelm@7271
    59
    -> (string * context attribute list * (term * (term list * term list)) list) list
wenzelm@6932
    60
    -> state -> state
wenzelm@7271
    61
  val assume: (string * context attribute list * (string * (string list * string list)) list) list
wenzelm@7271
    62
    -> state -> state
wenzelm@7271
    63
  val assume_i: (string * context attribute list * (term * (term list * term list)) list) list
wenzelm@6932
    64
    -> state -> state
wenzelm@7271
    65
  val presume: (string * context attribute list * (string * (string list * string list)) list) list
wenzelm@6932
    66
    -> state -> state
wenzelm@7271
    67
  val presume_i: (string * context attribute list * (term * (term list * term list)) list) list
wenzelm@6932
    68
    -> state -> state
wenzelm@6932
    69
  val theorem: bstring -> theory attribute list -> string * (string list * string list)
wenzelm@6932
    70
    -> theory -> state
wenzelm@6932
    71
  val theorem_i: bstring -> theory attribute list -> term * (term list * term list)
wenzelm@6932
    72
    -> theory -> state
wenzelm@6932
    73
  val lemma: bstring -> theory attribute list -> string * (string list * string list)
wenzelm@6932
    74
    -> theory -> state
wenzelm@6932
    75
  val lemma_i: bstring -> theory attribute list -> term * (term list * term list)
wenzelm@6932
    76
    -> theory -> state
wenzelm@5820
    77
  val chain: state -> state
wenzelm@6932
    78
  val export_chain: state -> state Seq.seq
wenzelm@6091
    79
  val from_facts: thm list -> state -> state
wenzelm@7176
    80
  val show: (state -> state Seq.seq) -> string -> context attribute list
wenzelm@7176
    81
    -> string * (string list * string list) -> state -> state
wenzelm@7176
    82
  val show_i: (state -> state Seq.seq) -> string -> context attribute list
wenzelm@7176
    83
    -> term * (term list * term list) -> state -> state
wenzelm@7176
    84
  val have: (state -> state Seq.seq) -> string -> context attribute list
wenzelm@7176
    85
    -> string * (string list * string list) -> state -> state
wenzelm@7176
    86
  val have_i: (state -> state Seq.seq) -> string -> context attribute list
wenzelm@7176
    87
    -> term * (term list * term list) -> state -> state
wenzelm@6404
    88
  val at_bottom: state -> bool
wenzelm@6982
    89
  val local_qed: (state -> state Seq.seq)
wenzelm@6982
    90
    -> ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit) -> state -> state Seq.seq
wenzelm@6950
    91
  val global_qed: (state -> state Seq.seq) -> state
wenzelm@6950
    92
    -> (theory * {kind: string, name: string, thm: thm}) Seq.seq
wenzelm@6896
    93
  val begin_block: state -> state
wenzelm@6932
    94
  val end_block: state -> state Seq.seq
wenzelm@6896
    95
  val next_block: state -> state
wenzelm@5820
    96
end;
wenzelm@5820
    97
wenzelm@8152
    98
signature PRIVATE_PROOF =
wenzelm@5820
    99
sig
wenzelm@5820
   100
  include PROOF
wenzelm@5820
   101
  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> state -> state
wenzelm@5820
   102
end;
wenzelm@5820
   103
wenzelm@8152
   104
structure Proof: PRIVATE_PROOF =
wenzelm@5820
   105
struct
wenzelm@5820
   106
wenzelm@5820
   107
wenzelm@5820
   108
(** proof state **)
wenzelm@5820
   109
wenzelm@5820
   110
type context = ProofContext.context;
wenzelm@5820
   111
wenzelm@5820
   112
wenzelm@5820
   113
(* type goal *)
wenzelm@5820
   114
wenzelm@5820
   115
datatype kind =
wenzelm@5820
   116
  Theorem of theory attribute list |    (*top-level theorem*)
wenzelm@5820
   117
  Lemma of theory attribute list |      (*top-level lemma*)
wenzelm@5820
   118
  Goal of context attribute list |      (*intermediate result, solving subgoal*)
wenzelm@5820
   119
  Aux of context attribute list ;       (*intermediate result*)
wenzelm@5820
   120
wenzelm@5820
   121
val kind_name =
wenzelm@6001
   122
  fn Theorem _ => "theorem" | Lemma _ => "lemma" | Goal _ => "show" | Aux _ => "have";
wenzelm@5820
   123
wenzelm@5820
   124
type goal =
wenzelm@6932
   125
 (kind *        (*result kind*)
wenzelm@6932
   126
  string *      (*result name*)
wenzelm@6932
   127
  term) *       (*result statement*)
wenzelm@6932
   128
 (thm list *    (*use facts*)
wenzelm@6932
   129
  thm);         (*goal: subgoals ==> statement*)
wenzelm@5820
   130
wenzelm@5820
   131
wenzelm@5820
   132
(* type mode *)
wenzelm@5820
   133
wenzelm@5820
   134
datatype mode = Forward | ForwardChain | Backward;
wenzelm@7201
   135
val mode_name = (fn Forward => "state" | ForwardChain => "chain" | Backward => "prove");
wenzelm@5820
   136
wenzelm@5820
   137
wenzelm@5820
   138
(* datatype state *)
wenzelm@5820
   139
wenzelm@7176
   140
datatype node =
wenzelm@7176
   141
  Node of
wenzelm@7176
   142
   {context: context,
wenzelm@7176
   143
    facts: thm list option,
wenzelm@7176
   144
    mode: mode,
wenzelm@7176
   145
    goal: (goal * (state -> state Seq.seq)) option}
wenzelm@7176
   146
and state =
wenzelm@5820
   147
  State of
wenzelm@5820
   148
    node *              (*current*)
wenzelm@5820
   149
    node list;          (*parents wrt. block structure*)
wenzelm@5820
   150
wenzelm@7176
   151
fun make_node (context, facts, mode, goal) =
wenzelm@7176
   152
  Node {context = context, facts = facts, mode = mode, goal = goal};
wenzelm@7176
   153
wenzelm@7176
   154
wenzelm@5820
   155
exception STATE of string * state;
wenzelm@5820
   156
wenzelm@5820
   157
fun err_malformed name state =
wenzelm@5820
   158
  raise STATE (name ^ ": internal error -- malformed proof state", state);
wenzelm@5820
   159
wenzelm@6871
   160
fun check_result msg state sq =
wenzelm@6871
   161
  (case Seq.pull sq of
wenzelm@6871
   162
    None => raise STATE (msg, state)
wenzelm@6871
   163
  | Some s_sq => Seq.cons s_sq);
wenzelm@6871
   164
wenzelm@5820
   165
wenzelm@7176
   166
fun map_current f (State (Node {context, facts, mode, goal}, nodes)) =
wenzelm@5820
   167
  State (make_node (f (context, facts, mode, goal)), nodes);
wenzelm@5820
   168
wenzelm@5820
   169
fun init_state thy =
wenzelm@5875
   170
  State (make_node (ProofContext.init thy, None, Forward, None), []);
wenzelm@5820
   171
wenzelm@5820
   172
wenzelm@5820
   173
wenzelm@5820
   174
(** basic proof state operations **)
wenzelm@5820
   175
wenzelm@5820
   176
(* context *)
wenzelm@5820
   177
wenzelm@7176
   178
fun context_of (State (Node {context, ...}, _)) = context;
wenzelm@5820
   179
val theory_of = ProofContext.theory_of o context_of;
wenzelm@5820
   180
val sign_of = ProofContext.sign_of o context_of;
wenzelm@5820
   181
wenzelm@5820
   182
fun map_context f = map_current (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@5820
   183
wenzelm@7176
   184
fun map_context_result f (state as State (Node {context, facts, mode, goal}, nodes)) =
wenzelm@5820
   185
  let val (context', result) = f context
wenzelm@5820
   186
  in (State (make_node (context', facts, mode, goal), nodes), result) end;
wenzelm@5820
   187
wenzelm@5820
   188
wenzelm@5820
   189
fun put_data kind f = map_context o ProofContext.put_data kind f;
wenzelm@7924
   190
val warn_extra_tfrees = map_context o ProofContext.warn_extra_tfrees o context_of;
wenzelm@6790
   191
val auto_bind_goal = map_context o ProofContext.auto_bind_goal;
wenzelm@6798
   192
val auto_bind_facts = map_context oo ProofContext.auto_bind_facts;
wenzelm@6091
   193
val put_thms = map_context o ProofContext.put_thms;
wenzelm@6091
   194
val put_thmss = map_context o ProofContext.put_thmss;
wenzelm@7605
   195
val reset_thms = map_context o ProofContext.reset_thms;
wenzelm@6932
   196
val assumptions = ProofContext.assumptions o context_of;
wenzelm@5820
   197
wenzelm@5820
   198
wenzelm@5820
   199
(* facts *)
wenzelm@5820
   200
wenzelm@7176
   201
fun the_facts (State (Node {facts = Some facts, ...}, _)) = facts
wenzelm@5820
   202
  | the_facts state = raise STATE ("No current facts available", state);
wenzelm@5820
   203
wenzelm@6848
   204
fun assert_facts state = (the_facts state; state);
wenzelm@7176
   205
fun get_facts (State (Node {facts, ...}, _)) = facts;
wenzelm@6848
   206
wenzelm@7605
   207
wenzelm@7605
   208
val thisN = "this";
wenzelm@7605
   209
wenzelm@5820
   210
fun put_facts facts state =
wenzelm@5820
   211
  state
wenzelm@5820
   212
  |> map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal))
wenzelm@7605
   213
  |> (case facts of None => reset_thms thisN | Some ths => put_thms (thisN, ths));
wenzelm@5820
   214
wenzelm@5820
   215
val reset_facts = put_facts None;
wenzelm@5820
   216
wenzelm@5820
   217
fun have_facts (name, facts) state =
wenzelm@5820
   218
  state
wenzelm@5820
   219
  |> put_facts (Some facts)
wenzelm@6798
   220
  |> put_thms (name, facts);
wenzelm@5820
   221
wenzelm@5820
   222
fun these_facts (state, ths) = have_facts ths state;
wenzelm@5820
   223
wenzelm@5820
   224
wenzelm@5820
   225
(* goal *)
wenzelm@5820
   226
wenzelm@7176
   227
fun find_goal i (state as State (Node {goal = Some goal, ...}, _)) = (context_of state, (i, goal))
wenzelm@7176
   228
  | find_goal i (State (Node {goal = None, ...}, node :: nodes)) =
wenzelm@5820
   229
      find_goal (i + 1) (State (node, nodes))
wenzelm@5820
   230
  | find_goal _ (state as State (_, [])) = err_malformed "find_goal" state;
wenzelm@5820
   231
wenzelm@7176
   232
fun get_goal state =
wenzelm@7665
   233
  let val (_, (_, (((_, _, t), goal), _))) = find_goal 0 state
wenzelm@7665
   234
  in (t, goal) end;
wenzelm@7176
   235
wenzelm@5820
   236
fun put_goal goal = map_current (fn (ctxt, facts, mode, _) => (ctxt, facts, mode, goal));
wenzelm@5820
   237
wenzelm@7176
   238
fun map_goal f (State (Node {context, facts, mode, goal = Some goal}, nodes)) =
wenzelm@5820
   239
      State (make_node (context, facts, mode, Some (f goal)), nodes)
wenzelm@5820
   240
  | map_goal f (State (nd, node :: nodes)) =
wenzelm@5820
   241
      let val State (node', nodes') = map_goal f (State (node, nodes))
wenzelm@5820
   242
      in State (nd, node' :: nodes') end
wenzelm@5820
   243
  | map_goal _ state = state;
wenzelm@5820
   244
wenzelm@5820
   245
fun goal_facts get state =
wenzelm@5820
   246
  state
wenzelm@7176
   247
  |> map_goal (fn ((result, (_, thm)), f) => ((result, (get state, thm)), f));
wenzelm@5820
   248
wenzelm@5820
   249
fun use_facts state =
wenzelm@5820
   250
  state
wenzelm@5820
   251
  |> goal_facts the_facts
wenzelm@5820
   252
  |> reset_facts;
wenzelm@5820
   253
wenzelm@5820
   254
wenzelm@5820
   255
(* mode *)
wenzelm@5820
   256
wenzelm@7176
   257
fun get_mode (State (Node {mode, ...}, _)) = mode;
wenzelm@5820
   258
fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal));
wenzelm@5820
   259
wenzelm@5820
   260
val enter_forward = put_mode Forward;
wenzelm@5820
   261
val enter_forward_chain = put_mode ForwardChain;
wenzelm@5820
   262
val enter_backward = put_mode Backward;
wenzelm@5820
   263
wenzelm@5820
   264
fun assert_mode pred state =
wenzelm@5820
   265
  let val mode = get_mode state in
wenzelm@5820
   266
    if pred mode then state
wenzelm@8239
   267
    else raise STATE ("Illegal application of command in " ^ mode_name mode ^ " mode", state)
wenzelm@5820
   268
  end;
wenzelm@5820
   269
wenzelm@5820
   270
fun is_chain state = get_mode state = ForwardChain;
wenzelm@5820
   271
val assert_forward = assert_mode (equal Forward);
wenzelm@5820
   272
val assert_forward_or_chain = assert_mode (equal Forward orf equal ForwardChain);
wenzelm@5820
   273
val assert_backward = assert_mode (equal Backward);
wenzelm@8206
   274
val assert_no_chain = assert_mode (not_equal ForwardChain);
wenzelm@5820
   275
wenzelm@5820
   276
wenzelm@5820
   277
(* blocks *)
wenzelm@5820
   278
wenzelm@6776
   279
fun level (State (_, nodes)) = length nodes;
wenzelm@6776
   280
wenzelm@5820
   281
fun open_block (State (node, nodes)) = State (node, node :: nodes);
wenzelm@5820
   282
wenzelm@5820
   283
fun new_block state =
wenzelm@5820
   284
  state
wenzelm@5820
   285
  |> open_block
wenzelm@5820
   286
  |> put_goal None;
wenzelm@5820
   287
wenzelm@7487
   288
fun close_block (state as State (_, node :: nodes)) = State (node, nodes)
wenzelm@5820
   289
  | close_block state = raise STATE ("Unbalanced block parentheses", state);
wenzelm@5820
   290
wenzelm@5820
   291
wenzelm@5820
   292
wenzelm@5820
   293
(** print_state **)
wenzelm@5820
   294
wenzelm@6982
   295
val show_hyps = ProofContext.show_hyps;
wenzelm@6982
   296
val pretty_thm = ProofContext.pretty_thm;
wenzelm@6982
   297
wenzelm@7412
   298
fun pretty_thms [th] = pretty_thm th
wenzelm@7580
   299
  | pretty_thms ths = Pretty.blk (0, Pretty.fbreaks (map pretty_thm ths));
wenzelm@7412
   300
wenzelm@7412
   301
wenzelm@6529
   302
val verbose = ProofContext.verbose;
wenzelm@6529
   303
wenzelm@6756
   304
fun print_facts _ None = ()
wenzelm@6982
   305
  | print_facts s (Some ths) =
wenzelm@7575
   306
     (Pretty.writeln (Pretty.big_list (s ^ "this:") (map pretty_thm ths)); writeln "");
wenzelm@6756
   307
wenzelm@7201
   308
fun print_state nr (state as State (Node {context, facts, mode, goal = _}, nodes)) =
wenzelm@5820
   309
  let
wenzelm@5945
   310
    val ref (_, _, begin_goal) = Goals.current_goals_markers;
wenzelm@5820
   311
wenzelm@5820
   312
    fun levels_up 0 = ""
wenzelm@7575
   313
      | levels_up 1 = ", 1 level up"
wenzelm@7575
   314
      | levels_up i = ", " ^ string_of_int i ^ " levels up";
wenzelm@5820
   315
wenzelm@7176
   316
    fun print_goal (_, (i, (((kind, name, _), (goal_facts, thm)), _))) =
wenzelm@7575
   317
      (print_facts "using "
wenzelm@7412
   318
          (if mode <> Backward orelse null goal_facts then None else Some goal_facts);
wenzelm@7575
   319
        writeln ("goal (" ^ kind_name kind ^ (if name = "" then "" else " " ^ name) ^
wenzelm@7575
   320
          levels_up (i div 2) ^ "):");
wenzelm@5945
   321
        Locale.print_goals_marker begin_goal (! goals_limit) thm);
wenzelm@6848
   322
wenzelm@7575
   323
    val ctxt_strings =
wenzelm@7575
   324
      if ! verbose orelse mode = Forward then ProofContext.strings_of_context context
wenzelm@7575
   325
      else if mode = Backward then ProofContext.strings_of_prems context
wenzelm@7575
   326
      else [];
wenzelm@5820
   327
  in
wenzelm@7201
   328
    writeln ("Proof(" ^ mode_name mode ^ "): step " ^ string_of_int nr ^
wenzelm@7201
   329
      ", depth " ^ string_of_int (length nodes div 2));
wenzelm@5993
   330
    writeln "";
wenzelm@7575
   331
    if null ctxt_strings then () else (seq writeln ctxt_strings; writeln "");
wenzelm@6529
   332
    if ! verbose orelse mode = Forward then
wenzelm@7575
   333
      (print_facts "" facts; print_goal (find_goal 0 state))
wenzelm@7575
   334
    else if mode = ForwardChain then print_facts "picking " facts
wenzelm@6529
   335
    else print_goal (find_goal 0 state)
wenzelm@5820
   336
  end;
wenzelm@5820
   337
wenzelm@5820
   338
wenzelm@5820
   339
wenzelm@5820
   340
(** proof steps **)
wenzelm@5820
   341
wenzelm@5820
   342
(* datatype method *)
wenzelm@5820
   343
wenzelm@6848
   344
datatype method = Method of thm list -> tactic;
wenzelm@5820
   345
val method = Method;
wenzelm@5820
   346
wenzelm@5820
   347
wenzelm@5820
   348
(* refine goal *)
wenzelm@5820
   349
wenzelm@8234
   350
local
wenzelm@8234
   351
wenzelm@5820
   352
fun check_sign sg state =
wenzelm@5820
   353
  if Sign.subsig (sg, sign_of state) then state
wenzelm@5820
   354
  else raise STATE ("Bad signature of result: " ^ Sign.str_of_sg sg, state);
wenzelm@5820
   355
wenzelm@8234
   356
fun gen_refine current_context meth_fun state =
wenzelm@6848
   357
  let
wenzelm@8234
   358
    val (goal_ctxt, (_, ((result, (facts, thm)), f))) = find_goal 0 state;
wenzelm@8234
   359
    val Method meth = meth_fun (if current_context then context_of state else goal_ctxt);
wenzelm@5820
   360
wenzelm@6848
   361
    fun refn thm' =
wenzelm@6848
   362
      state
wenzelm@6848
   363
      |> check_sign (Thm.sign_of_thm thm')
wenzelm@7176
   364
      |> map_goal (K ((result, (facts, thm')), f));
wenzelm@6848
   365
  in Seq.map refn (meth facts thm) end;
wenzelm@5820
   366
wenzelm@8234
   367
in
wenzelm@8234
   368
wenzelm@8234
   369
val refine = gen_refine true;
wenzelm@8234
   370
val refine_end = gen_refine false;
wenzelm@8234
   371
wenzelm@8234
   372
end;
wenzelm@8234
   373
wenzelm@5820
   374
wenzelm@6932
   375
(* export *)
wenzelm@5820
   376
wenzelm@7669
   377
fun get_free x (None, t as Free (y, _)) = if x = y then Some t else None
wenzelm@7669
   378
  | get_free _ (opt, _) = opt;
wenzelm@7669
   379
wenzelm@7669
   380
fun find_free t x = foldl_aterms (get_free x) (None, t);
wenzelm@7669
   381
wenzelm@7669
   382
wenzelm@6932
   383
local
wenzelm@6932
   384
wenzelm@6932
   385
fun varify_frees fixes thm =
wenzelm@5820
   386
  let
wenzelm@6887
   387
    val {sign, prop, ...} = Thm.rep_thm thm;
wenzelm@6932
   388
    val frees = map (Thm.cterm_of sign) (mapfilter (find_free prop) fixes);
wenzelm@5820
   389
  in
wenzelm@5820
   390
    thm
wenzelm@5820
   391
    |> Drule.forall_intr_list frees
wenzelm@6887
   392
    |> Drule.forall_elim_vars 0
wenzelm@5820
   393
  end;
wenzelm@5820
   394
wenzelm@8152
   395
fun export fixes casms thm =
wenzelm@8152
   396
  thm
wenzelm@8152
   397
  |> Drule.implies_intr_list casms
wenzelm@8152
   398
  |> varify_frees fixes
wenzelm@8186
   399
  |> ProofContext.most_general_varify_tfrees;
wenzelm@8152
   400
wenzelm@6932
   401
fun diff_context inner None = (ProofContext.fixed_names inner, ProofContext.assumptions inner)
wenzelm@6932
   402
  | diff_context inner (Some outer) =
wenzelm@6932
   403
      (ProofContext.fixed_names inner \\ ProofContext.fixed_names outer,
wenzelm@6932
   404
        Library.drop (length (ProofContext.assumptions outer), ProofContext.assumptions inner));
wenzelm@6932
   405
wenzelm@6932
   406
in
wenzelm@6932
   407
wenzelm@6932
   408
fun export_wrt inner opt_outer =
wenzelm@6932
   409
  let
wenzelm@6932
   410
    val (fixes, asms) = diff_context inner opt_outer;
wenzelm@6996
   411
    val casms = map (Drule.mk_cgoal o #1) asms;
wenzelm@6932
   412
    val tacs = map #2 asms;
wenzelm@6932
   413
  in (export fixes casms, tacs) end;
wenzelm@6932
   414
wenzelm@6932
   415
end;
wenzelm@5820
   416
wenzelm@6848
   417
wenzelm@6932
   418
(* export results *)
wenzelm@6932
   419
wenzelm@6932
   420
fun RANGE [] _ = all_tac
wenzelm@6932
   421
  | RANGE (tac :: tacs) i = RANGE tacs (i + 1) THEN tac i;
wenzelm@6932
   422
wenzelm@8152
   423
fun FINDGOAL tac st =
wenzelm@8152
   424
  let fun find (i, n) = if i > n then no_tac else tac i APPEND find (i + 1, n)
wenzelm@8152
   425
  in find (1, nprems_of st) st end;
wenzelm@8152
   426
wenzelm@8166
   427
fun HEADGOAL tac = tac 1;
wenzelm@8166
   428
wenzelm@6982
   429
fun export_goal print_rule raw_rule inner state =
wenzelm@6932
   430
  let
wenzelm@7176
   431
    val (outer, (_, ((result, (facts, thm)), f))) = find_goal 0 state;
wenzelm@6950
   432
    val (exp, tacs) = export_wrt inner (Some outer);
wenzelm@6932
   433
    val rule = exp raw_rule;
wenzelm@6982
   434
    val _ = print_rule rule;
wenzelm@8152
   435
    val thmq = FINDGOAL (Tactic.rtac rule THEN' RANGE (map #1 tacs)) thm;
wenzelm@7176
   436
  in Seq.map (fn th => map_goal (K ((result, (facts, th)), f)) state) thmq end;
wenzelm@6932
   437
wenzelm@6932
   438
wenzelm@6932
   439
fun export_thm inner thm =
wenzelm@6932
   440
  let val (exp, tacs) = export_wrt inner None in
wenzelm@6932
   441
    (case Seq.chop (2, RANGE (map #2 tacs) 1 (exp thm)) of
wenzelm@6932
   442
      ([thm'], _) => thm'
wenzelm@6932
   443
    | ([], _) => raise THM ("export: failed", 0, [thm])
wenzelm@6932
   444
    | _ => raise THM ("export: more than one result", 0, [thm]))
wenzelm@6932
   445
  end;
wenzelm@6932
   446
wenzelm@6932
   447
wenzelm@6932
   448
fun export_facts inner_state opt_outer_state state =
wenzelm@6932
   449
  let
wenzelm@6932
   450
    val thms = the_facts inner_state;
wenzelm@6932
   451
    val (exp, tacs) = export_wrt (context_of inner_state) (apsome context_of opt_outer_state);
wenzelm@6932
   452
    val thmqs = map (RANGE (map #2 tacs) 1 o exp) thms;
wenzelm@6932
   453
  in Seq.map (fn ths => put_facts (Some ths) state) (Seq.commute thmqs) end;
wenzelm@6932
   454
wenzelm@6932
   455
fun transfer_facts inner_state state =
wenzelm@6932
   456
  (case get_facts inner_state of
wenzelm@6932
   457
    None => Seq.single (reset_facts state)
wenzelm@6932
   458
  | Some ths => export_facts inner_state (Some state) state);
wenzelm@6932
   459
wenzelm@6932
   460
wenzelm@6932
   461
(* prepare result *)
wenzelm@6932
   462
wenzelm@6932
   463
fun prep_result state t raw_thm =
wenzelm@5820
   464
  let
wenzelm@5820
   465
    val ctxt = context_of state;
wenzelm@5820
   466
    fun err msg = raise STATE (msg, state);
wenzelm@5820
   467
wenzelm@5820
   468
    val ngoals = Thm.nprems_of raw_thm;
wenzelm@5820
   469
    val _ =
wenzelm@5820
   470
      if ngoals = 0 then ()
wenzelm@5820
   471
      else (Locale.print_goals ngoals raw_thm; err (string_of_int ngoals ^ " unsolved goal(s)!"));
wenzelm@5820
   472
wenzelm@6932
   473
    val thm = raw_thm RS Drule.rev_triv_goal;
wenzelm@6932
   474
    val {hyps, prop, sign, maxidx, ...} = Thm.rep_thm thm;
wenzelm@5820
   475
    val tsig = Sign.tsig_of sign;
wenzelm@6932
   476
wenzelm@6932
   477
    val casms = map #1 (assumptions state);
wenzelm@6996
   478
    val bad_hyps = Library.gen_rems Term.aconv (hyps, map (Thm.term_of o Drule.mk_cgoal) casms);
wenzelm@5820
   479
  in
wenzelm@6932
   480
    if not (null bad_hyps) then
wenzelm@6932
   481
      err ("Additional hypotheses:\n" ^ cat_lines (map (Sign.string_of_term sign) bad_hyps))
wenzelm@7605
   482
    else if not (t aconv prop) then
wenzelm@7605
   483
      err ("Proved a different theorem: " ^ Sign.string_of_term sign prop)
wenzelm@8186
   484
    else thm |> Drule.forall_elim_vars (maxidx + 1) |> ProofContext.most_general_varify_tfrees
wenzelm@5820
   485
  end;
wenzelm@5820
   486
wenzelm@5820
   487
wenzelm@5820
   488
wenzelm@5820
   489
(*** structured proof commands ***)
wenzelm@5820
   490
wenzelm@5820
   491
(** context **)
wenzelm@5820
   492
wenzelm@5820
   493
(* bind *)
wenzelm@5820
   494
wenzelm@5820
   495
fun gen_bind f x state =
wenzelm@5820
   496
  state
wenzelm@5820
   497
  |> assert_forward
wenzelm@5820
   498
  |> map_context (f x)
wenzelm@5820
   499
  |> reset_facts;
wenzelm@5820
   500
wenzelm@8095
   501
val match_bind = gen_bind ProofContext.match_bind;
wenzelm@8095
   502
val match_bind_i = gen_bind ProofContext.match_bind_i;
wenzelm@5820
   503
wenzelm@5820
   504
wenzelm@6091
   505
(* have_thmss *)
wenzelm@5820
   506
wenzelm@6876
   507
fun have_thmss ths name atts ths_atts state =
wenzelm@5820
   508
  state
wenzelm@5820
   509
  |> assert_forward
wenzelm@7478
   510
  |> map_context_result (ProofContext.have_thmss ths name atts ths_atts)
wenzelm@5820
   511
  |> these_facts;
wenzelm@5820
   512
wenzelm@6876
   513
fun simple_have_thms name thms = have_thmss [] name [] [(thms, [])];
wenzelm@6876
   514
wenzelm@5820
   515
wenzelm@5820
   516
(* fix *)
wenzelm@5820
   517
wenzelm@5820
   518
fun gen_fix f xs state =
wenzelm@5820
   519
  state
wenzelm@5820
   520
  |> assert_forward
wenzelm@5820
   521
  |> map_context (f xs)
wenzelm@5820
   522
  |> reset_facts;
wenzelm@5820
   523
wenzelm@5820
   524
val fix = gen_fix ProofContext.fix;
wenzelm@5820
   525
val fix_i = gen_fix ProofContext.fix_i;
wenzelm@5820
   526
wenzelm@5820
   527
wenzelm@5820
   528
(* assume *)
wenzelm@5820
   529
wenzelm@7271
   530
local
wenzelm@7271
   531
wenzelm@7271
   532
fun gen_assume f tac args state =
wenzelm@5820
   533
  state
wenzelm@5820
   534
  |> assert_forward
wenzelm@7478
   535
  |> map_context_result (f tac args)
wenzelm@7271
   536
  |> (fn (st, (factss, prems)) =>
wenzelm@7271
   537
    foldl these_facts (st, factss)
wenzelm@7271
   538
    |> put_thms ("prems", prems)
wenzelm@7271
   539
    |> put_facts (Some (flat (map #2 factss))));
wenzelm@6932
   540
wenzelm@6996
   541
val hard_asm_tac = Tactic.etac Drule.triv_goal;
wenzelm@6996
   542
val soft_asm_tac = Tactic.rtac Drule.triv_goal;
wenzelm@6996
   543
wenzelm@7271
   544
in
wenzelm@7271
   545
wenzelm@7271
   546
val assm = gen_assume ProofContext.assume;
wenzelm@7271
   547
val assm_i = gen_assume ProofContext.assume_i;
wenzelm@6996
   548
val assume = assm (hard_asm_tac, soft_asm_tac);
wenzelm@6996
   549
val assume_i = assm_i (hard_asm_tac, soft_asm_tac);
wenzelm@6996
   550
val presume = assm (soft_asm_tac, soft_asm_tac);
wenzelm@6996
   551
val presume_i = assm_i (soft_asm_tac, soft_asm_tac);
wenzelm@5820
   552
wenzelm@7271
   553
end;
wenzelm@7271
   554
wenzelm@5820
   555
wenzelm@5820
   556
wenzelm@5820
   557
(** goals **)
wenzelm@5820
   558
wenzelm@5820
   559
(* forward chaining *)
wenzelm@5820
   560
wenzelm@5820
   561
fun chain state =
wenzelm@5820
   562
  state
wenzelm@5820
   563
  |> assert_forward
wenzelm@6848
   564
  |> assert_facts
wenzelm@5820
   565
  |> enter_forward_chain;
wenzelm@5820
   566
wenzelm@6932
   567
fun export_chain state =
wenzelm@6932
   568
  state
wenzelm@6932
   569
  |> assert_forward
wenzelm@6932
   570
  |> export_facts state None
wenzelm@6932
   571
  |> Seq.map chain;
wenzelm@6932
   572
wenzelm@5820
   573
fun from_facts facts state =
wenzelm@5820
   574
  state
wenzelm@5820
   575
  |> put_facts (Some facts)
wenzelm@5820
   576
  |> chain;
wenzelm@5820
   577
wenzelm@5820
   578
wenzelm@5820
   579
(* setup goals *)
wenzelm@5820
   580
wenzelm@7176
   581
fun setup_goal opt_block prepp kind after_qed name atts raw_propp state =
wenzelm@5820
   582
  let
wenzelm@7928
   583
    val (state', prop) =
wenzelm@5936
   584
      state
wenzelm@5936
   585
      |> assert_forward_or_chain
wenzelm@5936
   586
      |> enter_forward
wenzelm@8095
   587
      |> map_context_result (fn ct => prepp (ct, raw_propp));
wenzelm@6932
   588
    val cprop = Thm.cterm_of (sign_of state') prop;
wenzelm@7556
   589
    val goal = Drule.mk_triv_goal cprop;
wenzelm@5820
   590
  in
wenzelm@5936
   591
    state'
wenzelm@7412
   592
    |> opt_block
wenzelm@7478
   593
    |> put_goal (Some (((kind atts, name, prop), ([], goal)), after_qed))
wenzelm@6790
   594
    |> auto_bind_goal prop
wenzelm@5820
   595
    |> (if is_chain state then use_facts else reset_facts)
wenzelm@5820
   596
    |> new_block
wenzelm@5820
   597
    |> enter_backward
wenzelm@5820
   598
  end;
wenzelm@5820
   599
wenzelm@5820
   600
wenzelm@5820
   601
(*global goals*)
wenzelm@8095
   602
fun global_goal prepp kind name atts x thy =
wenzelm@8095
   603
  setup_goal I prepp kind Seq.single name atts x (init_state thy);
wenzelm@5820
   604
wenzelm@7928
   605
val theorem = global_goal ProofContext.bind_propp Theorem;
wenzelm@7928
   606
val theorem_i = global_goal ProofContext.bind_propp_i Theorem;
wenzelm@7928
   607
val lemma = global_goal ProofContext.bind_propp Lemma;
wenzelm@7928
   608
val lemma_i = global_goal ProofContext.bind_propp_i Lemma;
wenzelm@5820
   609
wenzelm@5820
   610
wenzelm@5820
   611
(*local goals*)
wenzelm@8095
   612
fun local_goal prepp kind f name atts args state =
wenzelm@7928
   613
  state
wenzelm@8095
   614
  |> setup_goal open_block prepp kind f name atts args
wenzelm@7928
   615
  |> warn_extra_tfrees state;
wenzelm@5820
   616
wenzelm@7928
   617
val show   = local_goal ProofContext.bind_propp Goal;
wenzelm@7928
   618
val show_i = local_goal ProofContext.bind_propp_i Goal;
wenzelm@7928
   619
val have   = local_goal ProofContext.bind_propp Aux;
wenzelm@7928
   620
val have_i = local_goal ProofContext.bind_propp_i Aux;
wenzelm@5820
   621
wenzelm@5820
   622
wenzelm@5820
   623
wenzelm@5820
   624
(** conclusions **)
wenzelm@5820
   625
wenzelm@5820
   626
(* current goal *)
wenzelm@5820
   627
wenzelm@7176
   628
fun current_goal (State (Node {context, goal = Some goal, ...}, _)) = (context, goal)
wenzelm@5820
   629
  | current_goal state = raise STATE ("No current goal!", state);
wenzelm@5820
   630
wenzelm@7176
   631
fun assert_current_goal true (state as State (Node {goal = None, ...}, _)) =
wenzelm@6404
   632
      raise STATE ("No goal in this block!", state)
wenzelm@7176
   633
  | assert_current_goal false (state as State (Node {goal = Some _, ...}, _)) =
wenzelm@6404
   634
      raise STATE ("Goal present in this block!", state)
wenzelm@6404
   635
  | assert_current_goal _ state = state;
wenzelm@5820
   636
wenzelm@5820
   637
fun assert_bottom true (state as State (_, _ :: _)) =
wenzelm@5820
   638
      raise STATE ("Not at bottom of proof!", state)
wenzelm@5820
   639
  | assert_bottom false (state as State (_, [])) =
wenzelm@5820
   640
      raise STATE ("Already at bottom of proof!", state)
wenzelm@5820
   641
  | assert_bottom _ state = state;
wenzelm@5820
   642
wenzelm@6404
   643
val at_bottom = can (assert_bottom true o close_block);
wenzelm@6404
   644
wenzelm@6932
   645
fun end_proof bot state =
wenzelm@5820
   646
  state
wenzelm@5820
   647
  |> assert_forward
wenzelm@5820
   648
  |> close_block
wenzelm@5820
   649
  |> assert_bottom bot
wenzelm@7011
   650
  |> assert_current_goal true
wenzelm@7011
   651
  |> goal_facts (K []);
wenzelm@5820
   652
wenzelm@5820
   653
wenzelm@6404
   654
(* local_qed *)
wenzelm@5820
   655
wenzelm@6982
   656
fun finish_local (print_result, print_rule) state =
wenzelm@5820
   657
  let
wenzelm@7176
   658
    val (ctxt, (((kind, name, t), (_, raw_thm)), after_qed)) = current_goal state;
wenzelm@6932
   659
    val result = prep_result state t raw_thm;
wenzelm@5820
   660
    val (atts, opt_solve) =
wenzelm@5820
   661
      (case kind of
wenzelm@6982
   662
        Goal atts => (atts, export_goal print_rule result ctxt)
wenzelm@5820
   663
      | Aux atts => (atts, Seq.single)
wenzelm@6932
   664
      | _ => err_malformed "finish_local" state);
wenzelm@5820
   665
  in
wenzelm@6731
   666
    print_result {kind = kind_name kind, name = name, thm = result};
wenzelm@5820
   667
    state
wenzelm@5820
   668
    |> close_block
wenzelm@6871
   669
    |> auto_bind_facts name [t]
wenzelm@6876
   670
    |> have_thmss [] name atts [Thm.no_attributes [result]]
wenzelm@5820
   671
    |> opt_solve
wenzelm@7176
   672
    |> (Seq.flat o Seq.map after_qed)
wenzelm@5820
   673
  end;
wenzelm@5820
   674
wenzelm@6982
   675
fun local_qed finalize print state =
wenzelm@6404
   676
  state
wenzelm@6932
   677
  |> end_proof false
wenzelm@6871
   678
  |> finalize
wenzelm@6982
   679
  |> (Seq.flat o Seq.map (finish_local print));
wenzelm@5820
   680
wenzelm@5820
   681
wenzelm@6404
   682
(* global_qed *)
wenzelm@5820
   683
wenzelm@6950
   684
fun finish_global state =
wenzelm@5820
   685
  let
wenzelm@7176
   686
    val (_, (((kind, name, t), (_, raw_thm)), _)) = current_goal state;   (*ignores after_qed!*)
wenzelm@8152
   687
    val result = Drule.standard (prep_result state t raw_thm);
wenzelm@5820
   688
wenzelm@5820
   689
    val atts =
wenzelm@5820
   690
      (case kind of
wenzelm@6950
   691
        Theorem atts => atts
wenzelm@6950
   692
      | Lemma atts => atts @ [Drule.tag_lemma]
wenzelm@6932
   693
      | _ => err_malformed "finish_global" state);
wenzelm@5820
   694
wenzelm@6091
   695
    val (thy', result') = PureThy.store_thm ((name, result), atts) (theory_of state);
wenzelm@6529
   696
  in (thy', {kind = kind_name kind, name = name, thm = result'}) end;
wenzelm@5820
   697
wenzelm@6871
   698
(*Note: should inspect first result only, backtracking may destroy theory*)
wenzelm@6950
   699
fun global_qed finalize state =
wenzelm@5820
   700
  state
wenzelm@6932
   701
  |> end_proof true
wenzelm@6871
   702
  |> finalize
wenzelm@6950
   703
  |> Seq.map finish_global;
wenzelm@5820
   704
wenzelm@5820
   705
wenzelm@6896
   706
wenzelm@6896
   707
(** blocks **)
wenzelm@6896
   708
wenzelm@6896
   709
(* begin_block *)
wenzelm@6896
   710
wenzelm@6896
   711
fun begin_block state =
wenzelm@6896
   712
  state
wenzelm@6896
   713
  |> assert_forward
wenzelm@6896
   714
  |> new_block
wenzelm@6896
   715
  |> open_block;
wenzelm@6896
   716
wenzelm@6896
   717
wenzelm@6896
   718
(* end_block *)
wenzelm@6896
   719
wenzelm@6896
   720
fun end_block state =
wenzelm@6896
   721
  state
wenzelm@6896
   722
  |> assert_forward
wenzelm@6896
   723
  |> close_block
wenzelm@6896
   724
  |> assert_current_goal false
wenzelm@6896
   725
  |> close_block
wenzelm@6932
   726
  |> transfer_facts state;
wenzelm@6896
   727
wenzelm@6896
   728
wenzelm@6896
   729
(* next_block *)
wenzelm@6896
   730
wenzelm@6896
   731
fun next_block state =
wenzelm@6896
   732
  state
wenzelm@6896
   733
  |> assert_forward
wenzelm@6896
   734
  |> close_block
wenzelm@6932
   735
  |> assert_current_goal true
wenzelm@6896
   736
  |> new_block;
wenzelm@6896
   737
wenzelm@6896
   738
wenzelm@5820
   739
end;
wenzelm@8152
   740
wenzelm@8152
   741
wenzelm@8152
   742
structure BasicProof: BASIC_PROOF = Proof;
wenzelm@8152
   743
open BasicProof;