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