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