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