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