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