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