src/Pure/Isar/proof.ML
author wenzelm
Mon Nov 09 15:31:29 1998 +0100 (1998-11-09)
changeset 5820 ff3c82b47603
child 5875 6aae55ae3473
permissions -rw-r--r--
Proof states and methods.
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@5820
     9
  - bind: check rhs (extra (T)Vars etc.) (How to handle these anyway?);
wenzelm@5820
    10
  - prep_result: avoid duplicate asms;
wenzelm@5820
    11
  - goal: need asms field? (may take from goal context!?);
wenzelm@5820
    12
  - finish: filter results (!??) (no?);
wenzelm@5820
    13
  - finish_tac: make a parameter (method) (!!?);
wenzelm@5820
    14
  - prep_result error: use error channel (!);
wenzelm@5820
    15
  - next_block: fetch_facts (!?);
wenzelm@5820
    16
  - warn_vars;
wenzelm@5820
    17
  - string constants in one place (e.g. "it", "thesis") (??!);
wenzelm@5820
    18
  - check_finished: trace results (!?);
wenzelm@5820
    19
*)
wenzelm@5820
    20
wenzelm@5820
    21
signature PROOF =
wenzelm@5820
    22
sig
wenzelm@5820
    23
  type context
wenzelm@5820
    24
  type state
wenzelm@5820
    25
  exception STATE of string * state
wenzelm@5820
    26
  val context_of: state -> context
wenzelm@5820
    27
  val theory_of: state -> theory
wenzelm@5820
    28
  val sign_of: state -> Sign.sg
wenzelm@5820
    29
  val the_facts: state -> tthm list
wenzelm@5820
    30
  val goal_facts: (state -> tthm list) -> state -> state
wenzelm@5820
    31
  val use_facts: state -> state
wenzelm@5820
    32
  val reset_facts: state -> state
wenzelm@5820
    33
  val assert_backward: state -> state
wenzelm@5820
    34
  val enter_forward: state -> state
wenzelm@5820
    35
  val print_state: state -> unit
wenzelm@5820
    36
  type method
wenzelm@5820
    37
  val method: (tthm list -> thm ->
wenzelm@5820
    38
    (thm * (indexname * term) list * (string * tthm list) list) Seq.seq) -> method
wenzelm@5820
    39
  val refine: (context -> method) -> state -> state Seq.seq
wenzelm@5820
    40
  val bind: (indexname * string) list -> state -> state
wenzelm@5820
    41
  val bind_i: (indexname * term) list -> state -> state
wenzelm@5820
    42
  val match_bind: (string * string) list -> state -> state
wenzelm@5820
    43
  val match_bind_i: (term * term) list -> state -> state
wenzelm@5820
    44
  val have_tthms: string -> context attribute list ->
wenzelm@5820
    45
    (tthm * context attribute list) list -> state -> state
wenzelm@5820
    46
  val assume: string -> context attribute list -> string list -> state -> state
wenzelm@5820
    47
  val assume_i: string -> context attribute list -> term list -> state -> state
wenzelm@5820
    48
  val fix: (string * string option) list -> state -> state
wenzelm@5820
    49
  val fix_i: (string * typ) list -> state -> state
wenzelm@5820
    50
  val theorem: bstring -> theory attribute list -> string -> theory -> state
wenzelm@5820
    51
  val theorem_i: bstring -> theory attribute list -> term -> theory -> state
wenzelm@5820
    52
  val lemma: bstring -> theory attribute list -> string -> theory -> state
wenzelm@5820
    53
  val lemma_i: bstring -> theory attribute list -> term -> theory -> state
wenzelm@5820
    54
  val chain: state -> state
wenzelm@5820
    55
  val from_facts: tthm list -> state -> state
wenzelm@5820
    56
  val show: string -> context attribute list -> string -> state -> state
wenzelm@5820
    57
  val show_i: string -> context attribute list -> term -> state -> state
wenzelm@5820
    58
  val have: string -> context attribute list -> string -> state -> state
wenzelm@5820
    59
  val have_i: string -> context attribute list -> term -> state -> state
wenzelm@5820
    60
  val begin_block: state -> state
wenzelm@5820
    61
  val next_block: state -> state
wenzelm@5820
    62
  val end_block: (context -> method) -> state -> state Seq.seq
wenzelm@5820
    63
  val qed: (context -> method) -> bstring option -> theory attribute list option -> state
wenzelm@5820
    64
    -> theory * (string * string * tthm)
wenzelm@5820
    65
end;
wenzelm@5820
    66
wenzelm@5820
    67
signature PROOF_PRIVATE =
wenzelm@5820
    68
sig
wenzelm@5820
    69
  include PROOF
wenzelm@5820
    70
  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> state -> state
wenzelm@5820
    71
end;
wenzelm@5820
    72
wenzelm@5820
    73
wenzelm@5820
    74
structure Proof: PROOF_PRIVATE =
wenzelm@5820
    75
struct
wenzelm@5820
    76
wenzelm@5820
    77
wenzelm@5820
    78
(** proof state **)
wenzelm@5820
    79
wenzelm@5820
    80
type context = ProofContext.context;
wenzelm@5820
    81
wenzelm@5820
    82
wenzelm@5820
    83
(* type goal *)
wenzelm@5820
    84
wenzelm@5820
    85
datatype kind =
wenzelm@5820
    86
  Theorem of theory attribute list |    (*top-level theorem*)
wenzelm@5820
    87
  Lemma of theory attribute list |      (*top-level lemma*)
wenzelm@5820
    88
  Goal of context attribute list |      (*intermediate result, solving subgoal*)
wenzelm@5820
    89
  Aux of context attribute list ;       (*intermediate result*)
wenzelm@5820
    90
wenzelm@5820
    91
val kind_name =
wenzelm@5820
    92
  fn Theorem _ => "theorem" | Lemma _ => "lemma" | Goal _ => "show" | Aux _ => "have";
wenzelm@5820
    93
wenzelm@5820
    94
type goal =
wenzelm@5820
    95
 (kind *		(*result kind*)
wenzelm@5820
    96
  string *              (*result name*)
wenzelm@5820
    97
  cterm list *          (*result assumptions*)
wenzelm@5820
    98
  term) *               (*result statement*)
wenzelm@5820
    99
 (tthm list *           (*use facts*)
wenzelm@5820
   100
  thm);                 (*goal: subgoals ==> statement*)
wenzelm@5820
   101
wenzelm@5820
   102
wenzelm@5820
   103
(* type mode *)
wenzelm@5820
   104
wenzelm@5820
   105
datatype mode = Forward | ForwardChain | Backward;
wenzelm@5820
   106
wenzelm@5820
   107
val mode_name =
wenzelm@5820
   108
  fn Forward => "forward" | ForwardChain => "forward chain" | Backward => "backward";
wenzelm@5820
   109
wenzelm@5820
   110
wenzelm@5820
   111
(* type node *)
wenzelm@5820
   112
wenzelm@5820
   113
type node =
wenzelm@5820
   114
 {context: context,
wenzelm@5820
   115
  facts: tthm list option,
wenzelm@5820
   116
  mode: mode,
wenzelm@5820
   117
  goal: goal option};
wenzelm@5820
   118
wenzelm@5820
   119
fun make_node (context, facts, mode, goal) =
wenzelm@5820
   120
  {context = context, facts = facts, mode = mode, goal = goal}: node;
wenzelm@5820
   121
wenzelm@5820
   122
wenzelm@5820
   123
(* datatype state *)
wenzelm@5820
   124
wenzelm@5820
   125
datatype state =
wenzelm@5820
   126
  State of
wenzelm@5820
   127
    node *              (*current*)
wenzelm@5820
   128
    node list;          (*parents wrt. block structure*)
wenzelm@5820
   129
wenzelm@5820
   130
exception STATE of string * state;
wenzelm@5820
   131
wenzelm@5820
   132
fun err_malformed name state =
wenzelm@5820
   133
  raise STATE (name ^ ": internal error -- malformed proof state", state);
wenzelm@5820
   134
wenzelm@5820
   135
wenzelm@5820
   136
fun map_current f (State ({context, facts, mode, goal}, nodes)) =
wenzelm@5820
   137
  State (make_node (f (context, facts, mode, goal)), nodes);
wenzelm@5820
   138
wenzelm@5820
   139
fun init_state thy =
wenzelm@5820
   140
  State (make_node (ProofContext.init_context thy, None, Forward, None), []);
wenzelm@5820
   141
wenzelm@5820
   142
wenzelm@5820
   143
wenzelm@5820
   144
(** basic proof state operations **)
wenzelm@5820
   145
wenzelm@5820
   146
(* context *)
wenzelm@5820
   147
wenzelm@5820
   148
fun context_of (State ({context, ...}, _)) = context;
wenzelm@5820
   149
val theory_of = ProofContext.theory_of o context_of;
wenzelm@5820
   150
val sign_of = ProofContext.sign_of o context_of;
wenzelm@5820
   151
wenzelm@5820
   152
fun map_context f = map_current (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@5820
   153
wenzelm@5820
   154
fun map_context_result f (state as State ({context, facts, mode, goal}, nodes)) =
wenzelm@5820
   155
  let val (context', result) = f context
wenzelm@5820
   156
  in (State (make_node (context', facts, mode, goal), nodes), result) end;
wenzelm@5820
   157
wenzelm@5820
   158
wenzelm@5820
   159
fun put_data kind f = map_context o ProofContext.put_data kind f;
wenzelm@5820
   160
val declare_term = map_context o ProofContext.declare_term;
wenzelm@5820
   161
val add_binds = map_context o ProofContext.add_binds_i;
wenzelm@5820
   162
val put_tthms = map_context o ProofContext.put_tthms;
wenzelm@5820
   163
val put_tthmss = map_context o ProofContext.put_tthmss;
wenzelm@5820
   164
wenzelm@5820
   165
wenzelm@5820
   166
(* bind statements *)
wenzelm@5820
   167
wenzelm@5820
   168
fun bind_props bs state =
wenzelm@5820
   169
  let val mk_bind = map (fn (x, t) => ((Syntax.binding x, 0), t)) o ObjectLogic.dest_statement
wenzelm@5820
   170
  in state |> add_binds (flat (map mk_bind bs)) end;
wenzelm@5820
   171
wenzelm@5820
   172
fun bind_tthms (name, tthms) state =
wenzelm@5820
   173
  let
wenzelm@5820
   174
    val props = map (#prop o Thm.rep_thm o Attribute.thm_of) tthms;
wenzelm@5820
   175
    val named_props =
wenzelm@5820
   176
      (case props of
wenzelm@5820
   177
        [prop] => [(name, prop)]
wenzelm@5820
   178
      | props => map2 (fn (i, t) => (name ^ string_of_int i, t)) (1 upto length props, props));
wenzelm@5820
   179
  in state |> bind_props named_props end;
wenzelm@5820
   180
wenzelm@5820
   181
fun let_tthms name_tthms state =
wenzelm@5820
   182
  state
wenzelm@5820
   183
  |> put_tthms name_tthms
wenzelm@5820
   184
  |> bind_tthms name_tthms;
wenzelm@5820
   185
wenzelm@5820
   186
wenzelm@5820
   187
(* facts *)
wenzelm@5820
   188
wenzelm@5820
   189
fun the_facts (State ({facts = Some facts, ...}, _)) = facts
wenzelm@5820
   190
  | the_facts state = raise STATE ("No current facts available", state);
wenzelm@5820
   191
wenzelm@5820
   192
fun put_facts facts state =
wenzelm@5820
   193
  state
wenzelm@5820
   194
  |> map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal))
wenzelm@5820
   195
  |> let_tthms ("facts", if_none facts []);
wenzelm@5820
   196
wenzelm@5820
   197
val reset_facts = put_facts None;
wenzelm@5820
   198
wenzelm@5820
   199
fun have_facts (name, facts) state =
wenzelm@5820
   200
  state
wenzelm@5820
   201
  |> put_facts (Some facts)
wenzelm@5820
   202
  |> let_tthms (name, facts);
wenzelm@5820
   203
wenzelm@5820
   204
fun these_facts (state, ths) = have_facts ths state;
wenzelm@5820
   205
fun fetch_facts (State ({facts, ...}, _)) = put_facts facts;
wenzelm@5820
   206
wenzelm@5820
   207
wenzelm@5820
   208
(* goal *)
wenzelm@5820
   209
wenzelm@5820
   210
fun find_goal i (State ({goal = Some goal, ...}, _)) = (i, goal)
wenzelm@5820
   211
  | find_goal i (State ({goal = None, ...}, node :: nodes)) =
wenzelm@5820
   212
      find_goal (i + 1) (State (node, nodes))
wenzelm@5820
   213
  | find_goal _ (state as State (_, [])) = err_malformed "find_goal" state;
wenzelm@5820
   214
wenzelm@5820
   215
fun put_goal goal = map_current (fn (ctxt, facts, mode, _) => (ctxt, facts, mode, goal));
wenzelm@5820
   216
wenzelm@5820
   217
fun map_goal f (State ({context, facts, mode, goal = Some goal}, nodes)) =
wenzelm@5820
   218
      State (make_node (context, facts, mode, Some (f goal)), nodes)
wenzelm@5820
   219
  | map_goal f (State (nd, node :: nodes)) =
wenzelm@5820
   220
      let val State (node', nodes') = map_goal f (State (node, nodes))
wenzelm@5820
   221
      in State (nd, node' :: nodes') end
wenzelm@5820
   222
  | map_goal _ state = state;
wenzelm@5820
   223
wenzelm@5820
   224
fun goal_facts get state =
wenzelm@5820
   225
  state
wenzelm@5820
   226
  |> map_goal (fn (result, (_, thm)) => (result, (get state, thm)));
wenzelm@5820
   227
wenzelm@5820
   228
fun use_facts state =
wenzelm@5820
   229
  state
wenzelm@5820
   230
  |> goal_facts the_facts
wenzelm@5820
   231
  |> reset_facts;
wenzelm@5820
   232
wenzelm@5820
   233
wenzelm@5820
   234
(* mode *)
wenzelm@5820
   235
wenzelm@5820
   236
fun get_mode (State ({mode, ...}, _)) = mode;
wenzelm@5820
   237
fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal));
wenzelm@5820
   238
wenzelm@5820
   239
val enter_forward = put_mode Forward;
wenzelm@5820
   240
val enter_forward_chain = put_mode ForwardChain;
wenzelm@5820
   241
val enter_backward = put_mode Backward;
wenzelm@5820
   242
wenzelm@5820
   243
fun assert_mode pred state =
wenzelm@5820
   244
  let val mode = get_mode state in
wenzelm@5820
   245
    if pred mode then state
wenzelm@5820
   246
    else raise STATE ("Illegal application of command in " ^ mode_name mode ^ " mode", state)
wenzelm@5820
   247
  end;
wenzelm@5820
   248
wenzelm@5820
   249
fun is_chain state = get_mode state = ForwardChain;
wenzelm@5820
   250
val assert_forward = assert_mode (equal Forward);
wenzelm@5820
   251
val assert_forward_or_chain = assert_mode (equal Forward orf equal ForwardChain);
wenzelm@5820
   252
val assert_backward = assert_mode (equal Backward);
wenzelm@5820
   253
wenzelm@5820
   254
wenzelm@5820
   255
(* blocks *)
wenzelm@5820
   256
wenzelm@5820
   257
fun open_block (State (node, nodes)) = State (node, node :: nodes);
wenzelm@5820
   258
wenzelm@5820
   259
fun new_block state =
wenzelm@5820
   260
  state
wenzelm@5820
   261
  |> open_block
wenzelm@5820
   262
  |> put_goal None;
wenzelm@5820
   263
wenzelm@5820
   264
fun close_block (State (_, node :: nodes)) = State (node, nodes)
wenzelm@5820
   265
  | close_block state = raise STATE ("Unbalanced block parentheses", state);
wenzelm@5820
   266
wenzelm@5820
   267
wenzelm@5820
   268
wenzelm@5820
   269
(** print_state **)
wenzelm@5820
   270
wenzelm@5820
   271
fun print_state (state as State ({context, facts, mode, goal = _}, nodes)) =
wenzelm@5820
   272
  let
wenzelm@5820
   273
    val prt_tthm = Attribute.pretty_tthm;
wenzelm@5820
   274
wenzelm@5820
   275
    fun print_facts None = ()
wenzelm@5820
   276
      | print_facts (Some ths) =
wenzelm@5820
   277
          Pretty.writeln (Pretty.big_list "Current facts:" (map prt_tthm 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@5820
   283
      (writeln (kind_name kind ^ " " ^ quote name ^ levels_up (i div 1) ^ ":");	(* FIXME *)
wenzelm@5820
   284
        Locale.print_goals (! goals_limit) thm);
wenzelm@5820
   285
  in
wenzelm@5820
   286
    writeln ("Nesting level: " ^ string_of_int (length nodes div 1));	(* FIXME *)
wenzelm@5820
   287
    writeln "";
wenzelm@5820
   288
    ProofContext.print_context context;
wenzelm@5820
   289
    writeln "";
wenzelm@5820
   290
    print_facts facts;
wenzelm@5820
   291
    print_goal (find_goal 0 state);
wenzelm@5820
   292
    writeln "";
wenzelm@5820
   293
    writeln (mode_name mode ^ " mode")
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@5820
   302
datatype method = Method of
wenzelm@5820
   303
  tthm list ->                   	(*use facts*)
wenzelm@5820
   304
  thm                                   (*goal: subgoals ==> statement*)
wenzelm@5820
   305
    -> (thm *                           (*refined goal*)
wenzelm@5820
   306
       (indexname * term) list *        (*new bindings*)
wenzelm@5820
   307
       (string * tthm list) list)       (*new thms*)
wenzelm@5820
   308
         Seq.seq;
wenzelm@5820
   309
wenzelm@5820
   310
val method = Method;
wenzelm@5820
   311
wenzelm@5820
   312
wenzelm@5820
   313
(* refine goal *)
wenzelm@5820
   314
wenzelm@5820
   315
fun check_sign sg state =
wenzelm@5820
   316
  if Sign.subsig (sg, sign_of state) then state
wenzelm@5820
   317
  else raise STATE ("Bad signature of result: " ^ Sign.str_of_sg sg, state);
wenzelm@5820
   318
wenzelm@5820
   319
fun refine meth_fun (state as State ({context, ...}, _)) =
wenzelm@5820
   320
      let
wenzelm@5820
   321
        val Method meth = meth_fun context;
wenzelm@5820
   322
        val (_, (result, (facts, thm))) = find_goal 0 state;
wenzelm@5820
   323
wenzelm@5820
   324
        fun refn (thm', new_binds, new_thms) =
wenzelm@5820
   325
          state
wenzelm@5820
   326
          |> check_sign (sign_of_thm thm')
wenzelm@5820
   327
          |> map_goal (K (result, (facts, thm')))
wenzelm@5820
   328
          |> add_binds new_binds
wenzelm@5820
   329
          |> put_tthmss new_thms;
wenzelm@5820
   330
      in Seq.map refn (meth facts thm) end;
wenzelm@5820
   331
wenzelm@5820
   332
wenzelm@5820
   333
(* prepare result *)
wenzelm@5820
   334
wenzelm@5820
   335
fun varify_frees names thm =
wenzelm@5820
   336
  let
wenzelm@5820
   337
    fun get_free x (None, t as Free (y, _)) = if x = y then Some t else None
wenzelm@5820
   338
      | get_free _ (opt, _) = opt;
wenzelm@5820
   339
wenzelm@5820
   340
    fun find_free t x = foldl_aterms (get_free x) (None, t);
wenzelm@5820
   341
wenzelm@5820
   342
    val {sign, maxidx, prop, ...} = Thm.rep_thm thm;
wenzelm@5820
   343
    val frees = map (Thm.cterm_of sign) (mapfilter (find_free prop) names);
wenzelm@5820
   344
  in
wenzelm@5820
   345
    thm
wenzelm@5820
   346
    |> Drule.forall_intr_list frees
wenzelm@5820
   347
    |> Drule.forall_elim_vars (maxidx + 1)
wenzelm@5820
   348
  end;
wenzelm@5820
   349
wenzelm@5820
   350
fun implies_elim_hyps thm =
wenzelm@5820
   351
  foldl (uncurry Thm.implies_elim) (thm, map Thm.assume (Drule.cprems_of thm));
wenzelm@5820
   352
wenzelm@5820
   353
fun prep_result state asms t raw_thm =
wenzelm@5820
   354
  let
wenzelm@5820
   355
    val ctxt = context_of state;
wenzelm@5820
   356
    fun err msg = raise STATE (msg, state);
wenzelm@5820
   357
wenzelm@5820
   358
    val ngoals = Thm.nprems_of raw_thm;
wenzelm@5820
   359
    val _ =
wenzelm@5820
   360
      if ngoals = 0 then ()
wenzelm@5820
   361
      else (Locale.print_goals ngoals raw_thm; err (string_of_int ngoals ^ " unsolved goal(s)!"));
wenzelm@5820
   362
wenzelm@5820
   363
    val thm =
wenzelm@5820
   364
      raw_thm RS Drule.rev_triv_goal
wenzelm@5820
   365
      |> implies_elim_hyps
wenzelm@5820
   366
      |> Drule.implies_intr_list asms
wenzelm@5820
   367
      |> varify_frees (ProofContext.fixed_names ctxt);
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@5820
   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@5820
   409
fun solve_goal rule state =
wenzelm@5820
   410
  let
wenzelm@5820
   411
    val (_, (result, (facts, thm))) = find_goal 0 state;
wenzelm@5820
   412
    val thms' = FIRSTGOAL (solve_tac [rule]) thm;
wenzelm@5820
   413
  in Seq.map (fn thm' => map_goal (K (result, (facts, thm'))) state) thms' end;
wenzelm@5820
   414
wenzelm@5820
   415
wenzelm@5820
   416
wenzelm@5820
   417
(*** structured proof commands ***)
wenzelm@5820
   418
wenzelm@5820
   419
(** context **)
wenzelm@5820
   420
wenzelm@5820
   421
(* bind *)
wenzelm@5820
   422
wenzelm@5820
   423
fun gen_bind f x state =
wenzelm@5820
   424
  state
wenzelm@5820
   425
  |> assert_forward
wenzelm@5820
   426
  |> map_context (f x)
wenzelm@5820
   427
  |> reset_facts;
wenzelm@5820
   428
wenzelm@5820
   429
val bind = gen_bind ProofContext.add_binds;
wenzelm@5820
   430
val bind_i = gen_bind ProofContext.add_binds_i;
wenzelm@5820
   431
wenzelm@5820
   432
val match_bind = gen_bind ProofContext.match_binds;
wenzelm@5820
   433
val match_bind_i = gen_bind ProofContext.match_binds_i;
wenzelm@5820
   434
wenzelm@5820
   435
wenzelm@5820
   436
(* have_tthms *)
wenzelm@5820
   437
wenzelm@5820
   438
val def_name = fn "" => "it" | name => name;
wenzelm@5820
   439
wenzelm@5820
   440
fun have_tthms name atts ths_atts state =
wenzelm@5820
   441
  state
wenzelm@5820
   442
  |> assert_forward
wenzelm@5820
   443
  |> map_context_result (ProofContext.have_tthms (def_name name) atts ths_atts)
wenzelm@5820
   444
  |> these_facts;
wenzelm@5820
   445
wenzelm@5820
   446
wenzelm@5820
   447
(* fix *)
wenzelm@5820
   448
wenzelm@5820
   449
fun gen_fix f xs state =
wenzelm@5820
   450
  state
wenzelm@5820
   451
  |> assert_forward
wenzelm@5820
   452
  |> map_context (f xs)
wenzelm@5820
   453
  |> reset_facts;
wenzelm@5820
   454
wenzelm@5820
   455
val fix = gen_fix ProofContext.fix;
wenzelm@5820
   456
val fix_i = gen_fix ProofContext.fix_i;
wenzelm@5820
   457
wenzelm@5820
   458
wenzelm@5820
   459
(* assume *)
wenzelm@5820
   460
wenzelm@5820
   461
fun gen_assume f name atts props state =
wenzelm@5820
   462
  state
wenzelm@5820
   463
  |> assert_forward
wenzelm@5820
   464
  |> map_context_result (f (def_name name) atts props)
wenzelm@5820
   465
  |> these_facts
wenzelm@5820
   466
  |> (fn st => let_tthms ("prems", ProofContext.assumptions (context_of st)) st);
wenzelm@5820
   467
wenzelm@5820
   468
val assume = gen_assume ProofContext.assume;
wenzelm@5820
   469
val assume_i = gen_assume ProofContext.assume_i;
wenzelm@5820
   470
wenzelm@5820
   471
wenzelm@5820
   472
wenzelm@5820
   473
(** goals **)
wenzelm@5820
   474
wenzelm@5820
   475
(* forward chaining *)
wenzelm@5820
   476
wenzelm@5820
   477
fun chain state =
wenzelm@5820
   478
  state
wenzelm@5820
   479
  |> assert_forward
wenzelm@5820
   480
  |> enter_forward_chain;
wenzelm@5820
   481
wenzelm@5820
   482
fun from_facts facts state =
wenzelm@5820
   483
  state
wenzelm@5820
   484
  |> put_facts (Some facts)
wenzelm@5820
   485
  |> chain;
wenzelm@5820
   486
wenzelm@5820
   487
wenzelm@5820
   488
(* setup goals *)
wenzelm@5820
   489
wenzelm@5820
   490
val read_prop = ProofContext.read_prop o context_of;
wenzelm@5820
   491
val cert_prop = ProofContext.cert_prop o context_of;
wenzelm@5820
   492
wenzelm@5820
   493
fun setup_goal opt_block prep kind name atts raw_prop state =
wenzelm@5820
   494
  let
wenzelm@5820
   495
    val sign = sign_of state;
wenzelm@5820
   496
    val ctxt = context_of state;
wenzelm@5820
   497
wenzelm@5820
   498
    val casms = map (#prop o Thm.crep_thm o Attribute.thm_of) (ProofContext.assumptions ctxt);
wenzelm@5820
   499
    val asms = map Thm.term_of casms;
wenzelm@5820
   500
wenzelm@5820
   501
    val prop = Logic.list_implies (asms, prep state raw_prop);
wenzelm@5820
   502
    val cprop = Thm.cterm_of sign prop;
wenzelm@5820
   503
    val thm = Drule.mk_triv_goal cprop;
wenzelm@5820
   504
  in
wenzelm@5820
   505
    state
wenzelm@5820
   506
    |> assert_forward_or_chain
wenzelm@5820
   507
    |> enter_forward
wenzelm@5820
   508
    |> opt_block
wenzelm@5820
   509
wenzelm@5820
   510
    |> declare_term prop
wenzelm@5820
   511
    |> put_goal (Some ((kind atts, (def_name name), casms, prop), ([], thm)))
wenzelm@5820
   512
    |> bind_props [("thesis", prop)]
wenzelm@5820
   513
    |> (if is_chain state then use_facts else reset_facts)
wenzelm@5820
   514
wenzelm@5820
   515
    |> new_block
wenzelm@5820
   516
    |> enter_backward
wenzelm@5820
   517
  end;
wenzelm@5820
   518
wenzelm@5820
   519
wenzelm@5820
   520
(*global goals*)
wenzelm@5820
   521
fun global_goal prep kind name atts x thy =
wenzelm@5820
   522
  setup_goal I prep kind name atts x (init_state thy);
wenzelm@5820
   523
wenzelm@5820
   524
val theorem = global_goal read_prop Theorem;
wenzelm@5820
   525
val theorem_i = global_goal cert_prop Theorem;
wenzelm@5820
   526
val lemma = global_goal read_prop Lemma;
wenzelm@5820
   527
val lemma_i = global_goal cert_prop Lemma;
wenzelm@5820
   528
wenzelm@5820
   529
wenzelm@5820
   530
(*local goals*)
wenzelm@5820
   531
fun local_goal prep kind name atts x =
wenzelm@5820
   532
  setup_goal open_block prep kind name atts x;
wenzelm@5820
   533
wenzelm@5820
   534
val show   = local_goal read_prop Goal;
wenzelm@5820
   535
val show_i = local_goal cert_prop Goal;
wenzelm@5820
   536
val have   = local_goal read_prop Aux;
wenzelm@5820
   537
val have_i = local_goal cert_prop Aux;
wenzelm@5820
   538
wenzelm@5820
   539
wenzelm@5820
   540
wenzelm@5820
   541
(** blocks **)
wenzelm@5820
   542
wenzelm@5820
   543
(* begin_block *)
wenzelm@5820
   544
wenzelm@5820
   545
fun begin_block state =
wenzelm@5820
   546
  state
wenzelm@5820
   547
  |> assert_forward
wenzelm@5820
   548
  |> new_block
wenzelm@5820
   549
  |> reset_facts
wenzelm@5820
   550
  |> open_block;
wenzelm@5820
   551
wenzelm@5820
   552
wenzelm@5820
   553
(* next_block *)
wenzelm@5820
   554
wenzelm@5820
   555
fun next_block state =
wenzelm@5820
   556
  state
wenzelm@5820
   557
  |> assert_forward
wenzelm@5820
   558
  |> close_block
wenzelm@5820
   559
  |> new_block;
wenzelm@5820
   560
wenzelm@5820
   561
wenzelm@5820
   562
wenzelm@5820
   563
(** conclusions **)
wenzelm@5820
   564
wenzelm@5820
   565
(* current goal *)
wenzelm@5820
   566
wenzelm@5820
   567
fun current_goal (State ({goal = Some goal, ...}, _)) = goal
wenzelm@5820
   568
  | current_goal state = raise STATE ("No current goal!", state);
wenzelm@5820
   569
wenzelm@5820
   570
fun assert_current_goal state = (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@5820
   578
wenzelm@5820
   579
(* finish proof *)
wenzelm@5820
   580
wenzelm@5820
   581
fun check_finished state states =
wenzelm@5820
   582
  (case Seq.pull states of
wenzelm@5820
   583
    None => raise STATE ("Failed to finish proof", state)
wenzelm@5820
   584
  | Some s_sq => Seq.cons s_sq);
wenzelm@5820
   585
wenzelm@5820
   586
fun finish_proof bot meth_fun state =
wenzelm@5820
   587
  state
wenzelm@5820
   588
  |> assert_forward
wenzelm@5820
   589
  |> close_block
wenzelm@5820
   590
  |> assert_bottom bot
wenzelm@5820
   591
  |> assert_current_goal
wenzelm@5820
   592
  |> refine meth_fun
wenzelm@5820
   593
  |> check_finished state;
wenzelm@5820
   594
wenzelm@5820
   595
wenzelm@5820
   596
(* conclude local proof *)
wenzelm@5820
   597
wenzelm@5820
   598
fun finish_local state =
wenzelm@5820
   599
  let
wenzelm@5820
   600
    val ((kind, name, asms, t), (_, raw_thm)) = current_goal state;
wenzelm@5820
   601
    val result = prep_result state asms t raw_thm;
wenzelm@5820
   602
    val (atts, opt_solve) =
wenzelm@5820
   603
      (case kind of
wenzelm@5820
   604
        Goal atts => (atts, solve_goal result)
wenzelm@5820
   605
      | Aux atts => (atts, Seq.single)
wenzelm@5820
   606
      | _ => raise STATE ("No local goal!", state));
wenzelm@5820
   607
  in
wenzelm@5820
   608
    state
wenzelm@5820
   609
    |> close_block
wenzelm@5820
   610
    |> have_tthms name atts [((result, []), [])]
wenzelm@5820
   611
    |> opt_solve
wenzelm@5820
   612
  end;
wenzelm@5820
   613
wenzelm@5820
   614
fun end_block meth_fun state =
wenzelm@5820
   615
  if can assert_current_goal (close_block state) then
wenzelm@5820
   616
    state
wenzelm@5820
   617
    |> finish_proof false meth_fun
wenzelm@5820
   618
    |> (Seq.flat o Seq.map finish_local)
wenzelm@5820
   619
  else
wenzelm@5820
   620
    state
wenzelm@5820
   621
    |> assert_forward
wenzelm@5820
   622
    |> close_block
wenzelm@5820
   623
    |> close_block
wenzelm@5820
   624
    |> fetch_facts state
wenzelm@5820
   625
    |> Seq.single;
wenzelm@5820
   626
wenzelm@5820
   627
wenzelm@5820
   628
(* conclude global proof *)
wenzelm@5820
   629
wenzelm@5820
   630
fun finish_global alt_name alt_atts state =
wenzelm@5820
   631
  let
wenzelm@5820
   632
    val ((kind, def_name, asms, t), (_, raw_thm)) = current_goal state;
wenzelm@5820
   633
    val result = final_result state (prep_result state asms t raw_thm);
wenzelm@5820
   634
wenzelm@5820
   635
    val name = if_none alt_name def_name;
wenzelm@5820
   636
    val atts =
wenzelm@5820
   637
      (case kind of
wenzelm@5820
   638
        Theorem atts => if_none alt_atts atts
wenzelm@5820
   639
      | Lemma atts => Attribute.tag_lemma :: if_none alt_atts atts
wenzelm@5820
   640
      | _ => raise STATE ("No global goal!", state));
wenzelm@5820
   641
wenzelm@5820
   642
    val (thy', result') = PureThy.store_tthm ((name, (result, [])), atts) (theory_of state);
wenzelm@5820
   643
  in (thy', (kind_name kind, name, result')) end;
wenzelm@5820
   644
wenzelm@5820
   645
fun qed meth_fun alt_name alt_atts state =
wenzelm@5820
   646
  state
wenzelm@5820
   647
  |> finish_proof true meth_fun
wenzelm@5820
   648
  |> Seq.hd
wenzelm@5820
   649
  |> finish_global alt_name alt_atts;
wenzelm@5820
   650
wenzelm@5820
   651
wenzelm@5820
   652
end;