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