src/Pure/Isar/proof.ML
author wenzelm
Thu Jan 10 01:13:41 2002 +0100 (2002-01-10)
changeset 12698 b87b41ade3b2
parent 12549 65f03a3f7998
child 12703 af5fec8a418f
permissions -rw-r--r--
locales: hide base name of exported version;
wenzelm@5820
     1
(*  Title:      Pure/Isar/proof.ML
wenzelm@5820
     2
    ID:         $Id$
wenzelm@5820
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8807
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@5820
     5
wenzelm@5820
     6
Proof states and methods.
wenzelm@5820
     7
*)
wenzelm@5820
     8
wenzelm@8152
     9
signature BASIC_PROOF =
wenzelm@8152
    10
sig
wenzelm@8374
    11
  val FINDGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq
wenzelm@8374
    12
  val HEADGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq
wenzelm@8152
    13
end;
wenzelm@8152
    14
wenzelm@5820
    15
signature PROOF =
wenzelm@5820
    16
sig
wenzelm@8152
    17
  include BASIC_PROOF
wenzelm@5820
    18
  type context
wenzelm@5820
    19
  type state
wenzelm@5820
    20
  exception STATE of string * state
wenzelm@6871
    21
  val check_result: string -> state -> 'a Seq.seq -> 'a Seq.seq
wenzelm@12045
    22
  val init_state: theory -> state
wenzelm@5820
    23
  val context_of: state -> context
wenzelm@5820
    24
  val theory_of: state -> theory
wenzelm@5820
    25
  val sign_of: state -> Sign.sg
wenzelm@7924
    26
  val warn_extra_tfrees: state -> state -> state
wenzelm@7605
    27
  val reset_thms: string -> state -> state
wenzelm@6091
    28
  val the_facts: state -> thm list
wenzelm@9469
    29
  val the_fact: state -> thm
wenzelm@11079
    30
  val get_goal: state -> context * (thm list * thm)
wenzelm@6091
    31
  val goal_facts: (state -> thm list) -> state -> state
wenzelm@5820
    32
  val use_facts: state -> state
wenzelm@5820
    33
  val reset_facts: state -> state
wenzelm@9469
    34
  val is_chain: state -> bool
wenzelm@6891
    35
  val assert_forward: state -> state
wenzelm@9469
    36
  val assert_forward_or_chain: state -> state
wenzelm@5820
    37
  val assert_backward: state -> state
wenzelm@8206
    38
  val assert_no_chain: state -> state
wenzelm@5820
    39
  val enter_forward: state -> state
wenzelm@6529
    40
  val verbose: bool ref
wenzelm@12423
    41
  val pretty_state: int -> state -> Pretty.T list
wenzelm@10360
    42
  val pretty_goals: bool -> state -> Pretty.T list
wenzelm@6776
    43
  val level: state -> int
wenzelm@5820
    44
  type method
wenzelm@6848
    45
  val method: (thm list -> tactic) -> method
wenzelm@8374
    46
  val method_cases: (thm list -> thm -> (thm * (string * RuleCases.T) list) Seq.seq) -> method
wenzelm@5820
    47
  val refine: (context -> method) -> state -> state Seq.seq
wenzelm@8234
    48
  val refine_end: (context -> method) -> state -> state Seq.seq
wenzelm@5936
    49
  val match_bind: (string list * string) list -> state -> state
wenzelm@5936
    50
  val match_bind_i: (term list * term) list -> state -> state
wenzelm@8617
    51
  val let_bind: (string list * string) list -> state -> state
wenzelm@8617
    52
  val let_bind_i: (term list * term) list -> state -> state
wenzelm@6876
    53
  val simple_have_thms: string -> thm list -> state -> state
wenzelm@9196
    54
  val have_thmss: ((string * context attribute list) *
wenzelm@9196
    55
    (thm list * context attribute list) list) list -> state -> state
wenzelm@9196
    56
  val with_thmss: ((string * context attribute list) *
wenzelm@9196
    57
    (thm list * context attribute list) list) list -> state -> state
wenzelm@7412
    58
  val fix: (string list * string option) list -> state -> state
wenzelm@7665
    59
  val fix_i: (string list * typ option) list -> state -> state
wenzelm@9469
    60
  val assm: ProofContext.exporter
wenzelm@10464
    61
    -> ((string * context attribute list) * (string * (string list * string list)) list) list
wenzelm@7271
    62
    -> state -> state
wenzelm@9469
    63
  val assm_i: ProofContext.exporter
wenzelm@10464
    64
    -> ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@6932
    65
    -> state -> state
wenzelm@10464
    66
  val assume:
wenzelm@10464
    67
    ((string * context attribute list) * (string * (string list * string list)) list) list
wenzelm@7271
    68
    -> state -> state
wenzelm@10464
    69
  val assume_i: ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@6932
    70
    -> state -> state
wenzelm@10464
    71
  val presume:
wenzelm@10464
    72
    ((string * context attribute list) * (string * (string list * string list)) list) list
wenzelm@6932
    73
    -> state -> state
wenzelm@10464
    74
  val presume_i: ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@6932
    75
    -> state -> state
wenzelm@11891
    76
  val def: string -> context attribute list -> string *  (string * string list) -> state -> state
wenzelm@11891
    77
  val def_i: string -> context attribute list -> string * (term * term list) -> state -> state
wenzelm@11793
    78
  val invoke_case: string * string option list * context attribute list -> state -> state
wenzelm@12242
    79
  val multi_theorem: string -> bstring * theory attribute list
wenzelm@12698
    80
    -> (xstring * context attribute list list) option -> context attribute Locale.element list
wenzelm@12146
    81
    -> ((bstring * theory attribute list) * (string * (string list * string list)) list) list
wenzelm@12146
    82
    -> theory -> state
wenzelm@12242
    83
  val multi_theorem_i: string -> bstring * theory attribute list
wenzelm@12698
    84
    -> (string * context attribute list list) option -> context attribute Locale.element_i list
wenzelm@12146
    85
    -> ((bstring * theory attribute list) * (term * (term list * term list)) list) list
wenzelm@12146
    86
    -> theory -> state
wenzelm@5820
    87
  val chain: state -> state
wenzelm@6091
    88
  val from_facts: thm list -> state -> state
wenzelm@12146
    89
  val show: (bool -> state -> state) -> (state -> state Seq.seq)
wenzelm@12146
    90
    -> ((string * context attribute list) * (string * (string list * string list)) list) list
wenzelm@12146
    91
    -> bool -> state -> state
wenzelm@12146
    92
  val show_i: (bool -> state -> state) -> (state -> state Seq.seq)
wenzelm@12146
    93
    -> ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@12146
    94
    -> bool -> state -> state
wenzelm@12146
    95
  val have: (state -> state Seq.seq)
wenzelm@12146
    96
    -> ((string * context attribute list) * (string * (string list * string list)) list) list
wenzelm@12146
    97
    -> state -> state
wenzelm@12146
    98
  val have_i: (state -> state Seq.seq)
wenzelm@12146
    99
    -> ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@12146
   100
    -> state -> state
wenzelm@6404
   101
  val at_bottom: state -> bool
wenzelm@6982
   102
  val local_qed: (state -> state Seq.seq)
wenzelm@12146
   103
    -> (context -> string * (string * thm list) list -> unit) * (context -> thm -> unit)
wenzelm@12146
   104
    -> state -> state Seq.seq
wenzelm@6950
   105
  val global_qed: (state -> state Seq.seq) -> state
wenzelm@12244
   106
    -> (theory * ((string * string) * (string * thm list) list)) Seq.seq
wenzelm@6896
   107
  val begin_block: state -> state
wenzelm@6932
   108
  val end_block: state -> state Seq.seq
wenzelm@6896
   109
  val next_block: state -> state
wenzelm@5820
   110
end;
wenzelm@5820
   111
wenzelm@8152
   112
signature PRIVATE_PROOF =
wenzelm@5820
   113
sig
wenzelm@5820
   114
  include PROOF
wenzelm@5820
   115
  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> state -> state
wenzelm@5820
   116
end;
wenzelm@5820
   117
wenzelm@8152
   118
structure Proof: PRIVATE_PROOF =
wenzelm@5820
   119
struct
wenzelm@5820
   120
wenzelm@5820
   121
wenzelm@5820
   122
(** proof state **)
wenzelm@5820
   123
wenzelm@5820
   124
type context = ProofContext.context;
wenzelm@5820
   125
wenzelm@5820
   126
wenzelm@5820
   127
(* type goal *)
wenzelm@5820
   128
wenzelm@5820
   129
datatype kind =
wenzelm@12698
   130
  Theorem of string *                                (*theorem kind*)
wenzelm@12698
   131
    (bstring * theory attribute list) *              (*common result binding*)
wenzelm@12698
   132
    (string * context attribute list list) option *  (*target locale and attributes*)
wenzelm@12698
   133
    theory attribute list list |                     (*attributes of individual result binding*)
wenzelm@12698
   134
  Show of context attribute list list |              (*intermediate result, solving subgoal*)
wenzelm@12698
   135
  Have of context attribute list list;               (*intermediate result*)
wenzelm@5820
   136
wenzelm@12242
   137
fun common_name "" = "" | common_name a = " " ^ a;
wenzelm@12242
   138
wenzelm@12242
   139
fun kind_name _ (Theorem (s, (a, _), None, _)) = s ^ common_name a
wenzelm@12242
   140
  | kind_name sg (Theorem (s, (a, _), Some (name, _), _)) =
wenzelm@12242
   141
      s ^ common_name a ^ " (in " ^ Locale.cond_extern sg name ^ ")"
wenzelm@12015
   142
  | kind_name _ (Show _) = "show"
wenzelm@12015
   143
  | kind_name _ (Have _) = "have";
wenzelm@5820
   144
wenzelm@5820
   145
type goal =
wenzelm@12146
   146
 (kind *             (*result kind*)
wenzelm@12146
   147
  string list *      (*result names*)
wenzelm@12146
   148
  term list list) *  (*result statements*)
wenzelm@12146
   149
 (thm list *         (*use facts*)
wenzelm@12146
   150
  thm);              (*goal: subgoals ==> statement*)
wenzelm@5820
   151
wenzelm@5820
   152
wenzelm@5820
   153
(* type mode *)
wenzelm@5820
   154
wenzelm@5820
   155
datatype mode = Forward | ForwardChain | Backward;
wenzelm@7201
   156
val mode_name = (fn Forward => "state" | ForwardChain => "chain" | Backward => "prove");
wenzelm@5820
   157
wenzelm@5820
   158
wenzelm@5820
   159
(* datatype state *)
wenzelm@5820
   160
wenzelm@7176
   161
datatype node =
wenzelm@7176
   162
  Node of
wenzelm@7176
   163
   {context: context,
wenzelm@7176
   164
    facts: thm list option,
wenzelm@7176
   165
    mode: mode,
wenzelm@7176
   166
    goal: (goal * (state -> state Seq.seq)) option}
wenzelm@7176
   167
and state =
wenzelm@5820
   168
  State of
wenzelm@5820
   169
    node *              (*current*)
wenzelm@5820
   170
    node list;          (*parents wrt. block structure*)
wenzelm@5820
   171
wenzelm@7176
   172
fun make_node (context, facts, mode, goal) =
wenzelm@7176
   173
  Node {context = context, facts = facts, mode = mode, goal = goal};
wenzelm@7176
   174
wenzelm@7176
   175
wenzelm@5820
   176
exception STATE of string * state;
wenzelm@5820
   177
wenzelm@5820
   178
fun err_malformed name state =
wenzelm@5820
   179
  raise STATE (name ^ ": internal error -- malformed proof state", state);
wenzelm@5820
   180
wenzelm@6871
   181
fun check_result msg state sq =
wenzelm@6871
   182
  (case Seq.pull sq of
wenzelm@6871
   183
    None => raise STATE (msg, state)
wenzelm@6871
   184
  | Some s_sq => Seq.cons s_sq);
wenzelm@6871
   185
wenzelm@5820
   186
wenzelm@7176
   187
fun map_current f (State (Node {context, facts, mode, goal}, nodes)) =
wenzelm@5820
   188
  State (make_node (f (context, facts, mode, goal)), nodes);
wenzelm@5820
   189
wenzelm@12045
   190
fun init_state thy =
wenzelm@12045
   191
  State (make_node (ProofContext.init thy, None, Forward, None), []);
wenzelm@12045
   192
wenzelm@5820
   193
wenzelm@5820
   194
wenzelm@5820
   195
(** basic proof state operations **)
wenzelm@5820
   196
wenzelm@5820
   197
(* context *)
wenzelm@5820
   198
wenzelm@7176
   199
fun context_of (State (Node {context, ...}, _)) = context;
wenzelm@5820
   200
val theory_of = ProofContext.theory_of o context_of;
wenzelm@5820
   201
val sign_of = ProofContext.sign_of o context_of;
wenzelm@5820
   202
wenzelm@5820
   203
fun map_context f = map_current (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@5820
   204
wenzelm@7176
   205
fun map_context_result f (state as State (Node {context, facts, mode, goal}, nodes)) =
wenzelm@5820
   206
  let val (context', result) = f context
wenzelm@5820
   207
  in (State (make_node (context', facts, mode, goal), nodes), result) end;
wenzelm@5820
   208
wenzelm@5820
   209
wenzelm@5820
   210
fun put_data kind f = map_context o ProofContext.put_data kind f;
wenzelm@7924
   211
val warn_extra_tfrees = map_context o ProofContext.warn_extra_tfrees o context_of;
wenzelm@10809
   212
val add_binds_i = map_context o ProofContext.add_binds_i;
wenzelm@6790
   213
val auto_bind_goal = map_context o ProofContext.auto_bind_goal;
wenzelm@12146
   214
val auto_bind_facts = map_context o ProofContext.auto_bind_facts;
wenzelm@6091
   215
val put_thms = map_context o ProofContext.put_thms;
wenzelm@6091
   216
val put_thmss = map_context o ProofContext.put_thmss;
wenzelm@7605
   217
val reset_thms = map_context o ProofContext.reset_thms;
wenzelm@8374
   218
val get_case = ProofContext.get_case o context_of;
wenzelm@5820
   219
wenzelm@5820
   220
wenzelm@5820
   221
(* facts *)
wenzelm@5820
   222
wenzelm@7176
   223
fun the_facts (State (Node {facts = Some facts, ...}, _)) = facts
wenzelm@5820
   224
  | the_facts state = raise STATE ("No current facts available", state);
wenzelm@5820
   225
wenzelm@9469
   226
fun the_fact state =
wenzelm@9469
   227
  (case the_facts state of
wenzelm@9469
   228
    [thm] => thm
wenzelm@9469
   229
  | _ => raise STATE ("Single fact expected", state));
wenzelm@9469
   230
wenzelm@6848
   231
fun assert_facts state = (the_facts state; state);
wenzelm@7176
   232
fun get_facts (State (Node {facts, ...}, _)) = facts;
wenzelm@6848
   233
wenzelm@7605
   234
wenzelm@7605
   235
val thisN = "this";
wenzelm@7605
   236
wenzelm@5820
   237
fun put_facts facts state =
wenzelm@5820
   238
  state
wenzelm@5820
   239
  |> map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal))
wenzelm@7605
   240
  |> (case facts of None => reset_thms thisN | Some ths => put_thms (thisN, ths));
wenzelm@5820
   241
wenzelm@5820
   242
val reset_facts = put_facts None;
wenzelm@5820
   243
wenzelm@9196
   244
fun these_factss (state, named_factss) =
wenzelm@5820
   245
  state
wenzelm@9196
   246
  |> put_thmss named_factss
wenzelm@9196
   247
  |> put_facts (Some (flat (map #2 named_factss)));
wenzelm@5820
   248
wenzelm@5820
   249
wenzelm@5820
   250
(* goal *)
wenzelm@5820
   251
wenzelm@10553
   252
local
wenzelm@10553
   253
  fun find i (state as State (Node {goal = Some goal, ...}, _)) = (context_of state, (i, goal))
wenzelm@10553
   254
    | find i (State (Node {goal = None, ...}, node :: nodes)) = find (i + 1) (State (node, nodes))
wenzelm@10553
   255
    | find _ (state as State (_, [])) = err_malformed "find_goal" state;
wenzelm@10553
   256
in val find_goal = find 0 end;
wenzelm@7176
   257
wenzelm@11079
   258
fun get_goal state =
wenzelm@11079
   259
  let val (ctxt, (_, ((_, x), _))) = find_goal state
wenzelm@11079
   260
  in (ctxt, x) end;
wenzelm@11079
   261
wenzelm@5820
   262
fun put_goal goal = map_current (fn (ctxt, facts, mode, _) => (ctxt, facts, mode, goal));
wenzelm@5820
   263
wenzelm@8374
   264
fun map_goal f g (State (Node {context, facts, mode, goal = Some goal}, nodes)) =
wenzelm@8374
   265
      State (make_node (f context, facts, mode, Some (g goal)), nodes)
wenzelm@8374
   266
  | map_goal f g (State (nd, node :: nodes)) =
wenzelm@8374
   267
      let val State (node', nodes') = map_goal f g (State (node, nodes))
wenzelm@8374
   268
      in map_context f (State (nd, node' :: nodes')) end
wenzelm@8374
   269
  | map_goal _ _ state = state;
wenzelm@5820
   270
wenzelm@5820
   271
fun goal_facts get state =
wenzelm@5820
   272
  state
wenzelm@8374
   273
  |> map_goal I (fn ((result, (_, thm)), f) => ((result, (get state, thm)), f));
wenzelm@5820
   274
wenzelm@5820
   275
fun use_facts state =
wenzelm@5820
   276
  state
wenzelm@5820
   277
  |> goal_facts the_facts
wenzelm@5820
   278
  |> reset_facts;
wenzelm@5820
   279
wenzelm@5820
   280
wenzelm@5820
   281
(* mode *)
wenzelm@5820
   282
wenzelm@7176
   283
fun get_mode (State (Node {mode, ...}, _)) = mode;
wenzelm@5820
   284
fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal));
wenzelm@5820
   285
wenzelm@5820
   286
val enter_forward = put_mode Forward;
wenzelm@5820
   287
val enter_forward_chain = put_mode ForwardChain;
wenzelm@5820
   288
val enter_backward = put_mode Backward;
wenzelm@5820
   289
wenzelm@5820
   290
fun assert_mode pred state =
wenzelm@5820
   291
  let val mode = get_mode state in
wenzelm@5820
   292
    if pred mode then state
wenzelm@12010
   293
    else raise STATE ("Illegal application of proof command in "
wenzelm@12010
   294
      ^ quote (mode_name mode) ^ " mode", state)
wenzelm@5820
   295
  end;
wenzelm@5820
   296
wenzelm@5820
   297
fun is_chain state = get_mode state = ForwardChain;
wenzelm@5820
   298
val assert_forward = assert_mode (equal Forward);
wenzelm@5820
   299
val assert_forward_or_chain = assert_mode (equal Forward orf equal ForwardChain);
wenzelm@5820
   300
val assert_backward = assert_mode (equal Backward);
wenzelm@8206
   301
val assert_no_chain = assert_mode (not_equal ForwardChain);
wenzelm@5820
   302
wenzelm@5820
   303
wenzelm@5820
   304
(* blocks *)
wenzelm@5820
   305
wenzelm@6776
   306
fun level (State (_, nodes)) = length nodes;
wenzelm@6776
   307
wenzelm@5820
   308
fun open_block (State (node, nodes)) = State (node, node :: nodes);
wenzelm@5820
   309
wenzelm@5820
   310
fun new_block state =
wenzelm@5820
   311
  state
wenzelm@5820
   312
  |> open_block
wenzelm@5820
   313
  |> put_goal None;
wenzelm@5820
   314
wenzelm@7487
   315
fun close_block (state as State (_, node :: nodes)) = State (node, nodes)
wenzelm@5820
   316
  | close_block state = raise STATE ("Unbalanced block parentheses", state);
wenzelm@5820
   317
wenzelm@5820
   318
wenzelm@5820
   319
wenzelm@12423
   320
(** pretty_state **)
wenzelm@5820
   321
wenzelm@6529
   322
val verbose = ProofContext.verbose;
wenzelm@6529
   323
wenzelm@12085
   324
fun pretty_goals_local ctxt = Display.pretty_goals_aux
wenzelm@12085
   325
  (ProofContext.pretty_sort ctxt, ProofContext.pretty_typ ctxt, ProofContext.pretty_term ctxt);
wenzelm@12085
   326
wenzelm@12055
   327
fun pretty_facts _ _ None = []
wenzelm@12055
   328
  | pretty_facts s ctxt (Some ths) =
wenzelm@12055
   329
      [Pretty.big_list (s ^ "this:") (map (ProofContext.pretty_thm ctxt) ths), Pretty.str ""];
wenzelm@6756
   330
wenzelm@12423
   331
fun pretty_state nr (state as State (Node {context = ctxt, facts, mode, goal = _}, nodes)) =
wenzelm@5820
   332
  let
wenzelm@11891
   333
    val ref (_, _, begin_goal) = Display.current_goals_markers;
wenzelm@5820
   334
wenzelm@5820
   335
    fun levels_up 0 = ""
wenzelm@7575
   336
      | levels_up 1 = ", 1 level up"
wenzelm@7575
   337
      | levels_up i = ", " ^ string_of_int i ^ " levels up";
wenzelm@5820
   338
wenzelm@11556
   339
    fun subgoals 0 = ""
wenzelm@11556
   340
      | subgoals 1 = ", 1 subgoal"
wenzelm@11556
   341
      | subgoals n = ", " ^ string_of_int n ^ " subgoals";
wenzelm@12146
   342
wenzelm@12146
   343
    fun prt_names names =
wenzelm@12242
   344
      (case filter_out (equal "") names of [] => ""
wenzelm@12308
   345
      | nms => " " ^ enclose "(" ")" (space_implode " and " (take (7, nms) @
wenzelm@12308
   346
          (if length nms > 7 then ["..."] else []))));
wenzelm@12146
   347
wenzelm@12146
   348
    fun prt_goal (_, (i, (((kind, names, _), (goal_facts, thm)), _))) =
wenzelm@12085
   349
      pretty_facts "using " ctxt
wenzelm@8462
   350
        (if mode <> Backward orelse null goal_facts then None else Some goal_facts) @
wenzelm@12146
   351
      [Pretty.str ("goal (" ^ kind_name (sign_of state) kind ^ prt_names names ^
wenzelm@12146
   352
          levels_up (i div 2) ^ subgoals (Thm.nprems_of thm) ^ "):")] @
wenzelm@12085
   353
      pretty_goals_local ctxt begin_goal (true, true) (! Display.goals_limit) thm;
wenzelm@6848
   354
wenzelm@8462
   355
    val ctxt_prts =
wenzelm@12085
   356
      if ! verbose orelse mode = Forward then ProofContext.pretty_context ctxt
wenzelm@12085
   357
      else if mode = Backward then ProofContext.pretty_asms ctxt
wenzelm@7575
   358
      else [];
wenzelm@8462
   359
wenzelm@8462
   360
    val prts =
wenzelm@8582
   361
     [Pretty.str ("proof (" ^ mode_name mode ^ "): step " ^ string_of_int nr ^
wenzelm@12010
   362
        (if ! verbose then ", depth " ^ string_of_int (length nodes div 2 - 1)
wenzelm@8561
   363
        else "")), Pretty.str ""] @
wenzelm@8462
   364
     (if null ctxt_prts then [] else ctxt_prts @ [Pretty.str ""]) @
wenzelm@8462
   365
     (if ! verbose orelse mode = Forward then
wenzelm@12146
   366
       (pretty_facts "" ctxt facts @ prt_goal (find_goal state))
wenzelm@12085
   367
      else if mode = ForwardChain then pretty_facts "picking " ctxt facts
wenzelm@12146
   368
      else prt_goal (find_goal state))
wenzelm@12423
   369
  in prts end;
wenzelm@5820
   370
wenzelm@12085
   371
fun pretty_goals main state =
wenzelm@12085
   372
  let val (ctxt, (_, ((_, (_, thm)), _))) = find_goal state
wenzelm@12085
   373
  in pretty_goals_local ctxt "" (false, main) (! Display.goals_limit) thm end;
wenzelm@10320
   374
wenzelm@5820
   375
wenzelm@5820
   376
wenzelm@5820
   377
(** proof steps **)
wenzelm@5820
   378
wenzelm@5820
   379
(* datatype method *)
wenzelm@5820
   380
wenzelm@8374
   381
datatype method =
wenzelm@8374
   382
  Method of thm list -> thm -> (thm * (string * RuleCases.T) list) Seq.seq;
wenzelm@8374
   383
wenzelm@8374
   384
fun method tac = Method (fn facts => fn st => Seq.map (rpair []) (tac facts st));
wenzelm@8374
   385
val method_cases = Method;
wenzelm@5820
   386
wenzelm@5820
   387
wenzelm@5820
   388
(* refine goal *)
wenzelm@5820
   389
wenzelm@8234
   390
local
wenzelm@8234
   391
wenzelm@5820
   392
fun check_sign sg state =
wenzelm@5820
   393
  if Sign.subsig (sg, sign_of state) then state
wenzelm@5820
   394
  else raise STATE ("Bad signature of result: " ^ Sign.str_of_sg sg, state);
wenzelm@5820
   395
wenzelm@8234
   396
fun gen_refine current_context meth_fun state =
wenzelm@6848
   397
  let
wenzelm@10553
   398
    val (goal_ctxt, (_, ((result, (facts, thm)), f))) = find_goal state;
wenzelm@8234
   399
    val Method meth = meth_fun (if current_context then context_of state else goal_ctxt);
wenzelm@5820
   400
wenzelm@8374
   401
    fun refn (thm', cases) =
wenzelm@6848
   402
      state
wenzelm@6848
   403
      |> check_sign (Thm.sign_of_thm thm')
wenzelm@8374
   404
      |> map_goal (ProofContext.add_cases cases) (K ((result, (facts, thm')), f));
wenzelm@6848
   405
  in Seq.map refn (meth facts thm) end;
wenzelm@5820
   406
wenzelm@8234
   407
in
wenzelm@8234
   408
wenzelm@8234
   409
val refine = gen_refine true;
wenzelm@8234
   410
val refine_end = gen_refine false;
wenzelm@8234
   411
wenzelm@8234
   412
end;
wenzelm@8234
   413
wenzelm@5820
   414
wenzelm@11917
   415
(* goal addressing *)
wenzelm@6932
   416
wenzelm@8152
   417
fun FINDGOAL tac st =
wenzelm@8374
   418
  let fun find (i, n) = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1, n))
wenzelm@8374
   419
  in find (1, Thm.nprems_of st) st end;
wenzelm@8152
   420
wenzelm@8166
   421
fun HEADGOAL tac = tac 1;
wenzelm@8166
   422
wenzelm@9469
   423
wenzelm@9469
   424
(* export results *)
wenzelm@9469
   425
wenzelm@11816
   426
fun refine_tac rule =
wenzelm@11816
   427
  Tactic.rtac rule THEN_ALL_NEW (SUBGOAL (fn (goal, i) =>
wenzelm@11816
   428
    if can Logic.dest_goal (Logic.strip_assums_concl goal) then
wenzelm@11816
   429
      Tactic.etac Drule.triv_goal i
wenzelm@11816
   430
    else all_tac));
wenzelm@11816
   431
wenzelm@12146
   432
fun export_goal inner print_rule raw_rule state =
wenzelm@6932
   433
  let
wenzelm@10553
   434
    val (outer, (_, ((result, (facts, thm)), f))) = find_goal state;
wenzelm@12010
   435
    val exp_tac = ProofContext.export true inner outer;
wenzelm@9469
   436
    fun exp_rule rule =
wenzelm@9469
   437
      let
wenzelm@12055
   438
        val _ = print_rule outer rule;
wenzelm@11816
   439
        val thmq = FINDGOAL (refine_tac rule) thm;
wenzelm@9469
   440
      in Seq.map (fn th => map_goal I (K ((result, (facts, th)), f)) state) thmq end;
wenzelm@9469
   441
  in raw_rule |> exp_tac |> (Seq.flat o Seq.map exp_rule) end;
wenzelm@6932
   442
wenzelm@12146
   443
fun export_goals inner print_rule raw_rules =
wenzelm@12146
   444
  Seq.EVERY (map (export_goal inner print_rule) raw_rules);
wenzelm@12146
   445
wenzelm@6932
   446
fun transfer_facts inner_state state =
wenzelm@6932
   447
  (case get_facts inner_state of
wenzelm@6932
   448
    None => Seq.single (reset_facts state)
wenzelm@8617
   449
  | Some thms =>
wenzelm@8617
   450
      let
wenzelm@12010
   451
        val exp_tac = ProofContext.export false (context_of inner_state) (context_of state);
wenzelm@11816
   452
        val thmqs = map exp_tac thms;
wenzelm@8617
   453
      in Seq.map (fn ths => put_facts (Some ths) state) (Seq.commute thmqs) end);
wenzelm@6932
   454
wenzelm@6932
   455
wenzelm@6932
   456
(* prepare result *)
wenzelm@6932
   457
wenzelm@12146
   458
fun prep_result state ts raw_th =
wenzelm@5820
   459
  let
wenzelm@5820
   460
    val ctxt = context_of state;
wenzelm@5820
   461
    fun err msg = raise STATE (msg, state);
wenzelm@5820
   462
wenzelm@12146
   463
    val ngoals = Thm.nprems_of raw_th;
wenzelm@5820
   464
    val _ =
wenzelm@5820
   465
      if ngoals = 0 then ()
wenzelm@12085
   466
      else (err (Pretty.string_of (Pretty.chunks (pretty_goals_local ctxt "" (true, true)
wenzelm@12146
   467
            (! Display.goals_limit) raw_th @
wenzelm@12085
   468
          [Pretty.str (string_of_int ngoals ^ " unsolved goal(s)!")]))));
wenzelm@5820
   469
wenzelm@12146
   470
    val {hyps, sign, ...} = Thm.rep_thm raw_th;
wenzelm@5820
   471
    val tsig = Sign.tsig_of sign;
wenzelm@12055
   472
    val casms = flat (map #1 (ProofContext.assumptions_of (context_of state)));
wenzelm@11816
   473
    val bad_hyps = Library.gen_rems Term.aconv (hyps, map Thm.term_of casms);
wenzelm@12146
   474
wenzelm@12146
   475
    val th = raw_th RS Drule.rev_triv_goal;
wenzelm@12146
   476
    val ths = Drule.conj_elim_precise (length ts) th
wenzelm@12146
   477
      handle THM _ => err "Main goal structure corrupted";
wenzelm@12146
   478
    val props = map (#prop o Thm.rep_thm) ths;
wenzelm@5820
   479
  in
wenzelm@12146
   480
    conditional (not (null bad_hyps)) (fn () => err ("Additional hypotheses:\n" ^
wenzelm@12146
   481
        cat_lines (map (ProofContext.string_of_term ctxt) bad_hyps)));
wenzelm@12146
   482
    conditional (exists (not o Pattern.matches tsig) (ts ~~ props)) (fn () =>
wenzelm@12146
   483
      err ("Proved a different theorem: " ^ Pretty.string_of (ProofContext.pretty_thm ctxt th)));
wenzelm@12146
   484
    ths
wenzelm@5820
   485
  end;
wenzelm@5820
   486
wenzelm@5820
   487
wenzelm@5820
   488
wenzelm@5820
   489
(*** structured proof commands ***)
wenzelm@5820
   490
wenzelm@5820
   491
(** context **)
wenzelm@5820
   492
wenzelm@5820
   493
(* bind *)
wenzelm@5820
   494
wenzelm@5820
   495
fun gen_bind f x state =
wenzelm@5820
   496
  state
wenzelm@5820
   497
  |> assert_forward
wenzelm@5820
   498
  |> map_context (f x)
wenzelm@5820
   499
  |> reset_facts;
wenzelm@5820
   500
wenzelm@8617
   501
val match_bind = gen_bind (ProofContext.match_bind false);
wenzelm@8617
   502
val match_bind_i = gen_bind (ProofContext.match_bind_i false);
wenzelm@8617
   503
val let_bind = gen_bind (ProofContext.match_bind true);
wenzelm@8617
   504
val let_bind_i = gen_bind (ProofContext.match_bind_i true);
wenzelm@5820
   505
wenzelm@5820
   506
wenzelm@9196
   507
(* have_thmss etc. *)
wenzelm@5820
   508
wenzelm@9196
   509
fun have_thmss args state =
wenzelm@5820
   510
  state
wenzelm@5820
   511
  |> assert_forward
wenzelm@9196
   512
  |> map_context_result (ProofContext.have_thmss args)
wenzelm@9196
   513
  |> these_factss;
wenzelm@5820
   514
wenzelm@9196
   515
fun simple_have_thms name thms = have_thmss [((name, []), [(thms, [])])];
wenzelm@9196
   516
wenzelm@9196
   517
fun with_thmss args state =
wenzelm@9196
   518
  let val state' = state |> have_thmss args
wenzelm@9196
   519
  in state' |> simple_have_thms "" (the_facts state' @ the_facts state) end;
wenzelm@6876
   520
wenzelm@5820
   521
wenzelm@5820
   522
(* fix *)
wenzelm@5820
   523
wenzelm@5820
   524
fun gen_fix f xs state =
wenzelm@5820
   525
  state
wenzelm@5820
   526
  |> assert_forward
wenzelm@5820
   527
  |> map_context (f xs)
wenzelm@5820
   528
  |> reset_facts;
wenzelm@5820
   529
wenzelm@5820
   530
val fix = gen_fix ProofContext.fix;
wenzelm@5820
   531
val fix_i = gen_fix ProofContext.fix_i;
wenzelm@5820
   532
wenzelm@5820
   533
wenzelm@11891
   534
(* assume and presume *)
wenzelm@5820
   535
wenzelm@9469
   536
fun gen_assume f exp args state =
wenzelm@5820
   537
  state
wenzelm@5820
   538
  |> assert_forward
wenzelm@9469
   539
  |> map_context_result (f exp args)
wenzelm@11924
   540
  |> these_factss;
wenzelm@6932
   541
wenzelm@7271
   542
val assm = gen_assume ProofContext.assume;
wenzelm@7271
   543
val assm_i = gen_assume ProofContext.assume_i;
wenzelm@11917
   544
val assume = assm ProofContext.export_assume;
wenzelm@11917
   545
val assume_i = assm_i ProofContext.export_assume;
wenzelm@11917
   546
val presume = assm ProofContext.export_presume;
wenzelm@11917
   547
val presume_i = assm_i ProofContext.export_presume;
wenzelm@5820
   548
wenzelm@7271
   549
wenzelm@5820
   550
wenzelm@11891
   551
(** local definitions **)
wenzelm@11891
   552
wenzelm@11891
   553
fun gen_def fix prep_term prep_pats raw_name atts (x, (raw_rhs, raw_pats)) state =
wenzelm@11891
   554
  let
wenzelm@11891
   555
    val _ = assert_forward state;
wenzelm@11891
   556
    val ctxt = context_of state;
wenzelm@11891
   557
wenzelm@11891
   558
    (*rhs*)
wenzelm@11891
   559
    val name = if raw_name = "" then Thm.def_name x else raw_name;
wenzelm@11891
   560
    val rhs = prep_term ctxt raw_rhs;
wenzelm@11891
   561
    val T = Term.fastype_of rhs;
wenzelm@11891
   562
    val pats = prep_pats T (ProofContext.declare_term rhs ctxt) raw_pats;
wenzelm@11891
   563
wenzelm@11891
   564
    (*lhs*)
wenzelm@11891
   565
    val lhs = ProofContext.bind_skolem ctxt [x] (Free (x, T));
wenzelm@11891
   566
  in
wenzelm@11891
   567
    state
wenzelm@11891
   568
    |> fix [([x], None)]
wenzelm@11891
   569
    |> match_bind_i [(pats, rhs)]
wenzelm@11917
   570
    |> assm_i ProofContext.export_def [((name, atts), [(Logic.mk_equals (lhs, rhs), ([], []))])]
wenzelm@11891
   571
  end;
wenzelm@11891
   572
wenzelm@11891
   573
val def = gen_def fix ProofContext.read_term ProofContext.read_term_pats;
wenzelm@11891
   574
val def_i = gen_def fix_i ProofContext.cert_term ProofContext.cert_term_pats;
wenzelm@11891
   575
wenzelm@11891
   576
wenzelm@8374
   577
(* invoke_case *)
wenzelm@8374
   578
wenzelm@11793
   579
fun invoke_case (name, xs, atts) state =
wenzelm@9292
   580
  let
wenzelm@10830
   581
    val (state', (lets, asms)) =
wenzelm@11793
   582
      map_context_result (ProofContext.apply_case (get_case state name xs)) state;
wenzelm@9292
   583
  in
wenzelm@10830
   584
    state'
wenzelm@10809
   585
    |> add_binds_i lets
wenzelm@10830
   586
    |> assume_i [((name, atts), map (rpair ([], [])) asms)]
wenzelm@8374
   587
  end;
wenzelm@8374
   588
wenzelm@8374
   589
wenzelm@5820
   590
wenzelm@5820
   591
(** goals **)
wenzelm@5820
   592
wenzelm@5820
   593
(* forward chaining *)
wenzelm@5820
   594
wenzelm@5820
   595
fun chain state =
wenzelm@5820
   596
  state
wenzelm@5820
   597
  |> assert_forward
wenzelm@6848
   598
  |> assert_facts
wenzelm@5820
   599
  |> enter_forward_chain;
wenzelm@5820
   600
wenzelm@5820
   601
fun from_facts facts state =
wenzelm@5820
   602
  state
wenzelm@5820
   603
  |> put_facts (Some facts)
wenzelm@5820
   604
  |> chain;
wenzelm@5820
   605
wenzelm@5820
   606
wenzelm@5820
   607
(* setup goals *)
wenzelm@5820
   608
wenzelm@11549
   609
val rule_contextN = "rule_context";
wenzelm@8991
   610
wenzelm@12534
   611
fun setup_goal opt_block prepp autofix kind after_qed names raw_propp state =
wenzelm@5820
   612
  let
wenzelm@12146
   613
    val (state', (propss, gen_binds)) =
wenzelm@5936
   614
      state
wenzelm@5936
   615
      |> assert_forward_or_chain
wenzelm@5936
   616
      |> enter_forward
wenzelm@8617
   617
      |> opt_block
wenzelm@12534
   618
      |> map_context_result (fn ctxt => prepp (ctxt, raw_propp));
wenzelm@12146
   619
wenzelm@12146
   620
    val props = flat propss;
wenzelm@12146
   621
    val cprop = Thm.cterm_of (sign_of state') (foldr1 Logic.mk_conjunction props);
wenzelm@7556
   622
    val goal = Drule.mk_triv_goal cprop;
wenzelm@10553
   623
wenzelm@12146
   624
    val tvars = foldr Term.add_term_tvars (props, []);
wenzelm@12146
   625
    val vars = foldr Term.add_term_vars (props, []);
wenzelm@5820
   626
  in
wenzelm@10553
   627
    if null tvars then ()
wenzelm@10553
   628
    else raise STATE ("Goal statement contains illegal schematic type variable(s): " ^
wenzelm@10553
   629
      commas (map (Syntax.string_of_vname o #1) tvars), state);
wenzelm@10553
   630
    if null vars then ()
wenzelm@10553
   631
    else warning ("Goal statement contains unbound schematic variable(s): " ^
wenzelm@12055
   632
      commas (map (ProofContext.string_of_term (context_of state')) vars));
wenzelm@5936
   633
    state'
wenzelm@12146
   634
    |> map_context (autofix props)
wenzelm@12146
   635
    |> put_goal (Some (((kind, names, propss), ([], goal)), after_qed o map_context gen_binds))
wenzelm@12167
   636
    |> map_context (ProofContext.add_cases
wenzelm@12167
   637
     (if length props = 1 then RuleCases.make true goal [rule_contextN]
wenzelm@12167
   638
      else [(rule_contextN, RuleCases.empty)]))
wenzelm@12146
   639
    |> auto_bind_goal props
wenzelm@5820
   640
    |> (if is_chain state then use_facts else reset_facts)
wenzelm@5820
   641
    |> new_block
wenzelm@5820
   642
    |> enter_backward
wenzelm@5820
   643
  end;
wenzelm@5820
   644
wenzelm@5820
   645
(*global goals*)
wenzelm@12534
   646
fun global_goal prep kind a raw_locale elems args thy =
wenzelm@12503
   647
  let
wenzelm@12549
   648
    val init = init_state thy;
wenzelm@12534
   649
    val (opt_name, locale_ctxt, elems_ctxt, propp) =
wenzelm@12549
   650
      prep (apsome fst raw_locale) elems (map snd args) (context_of init);
wenzelm@12534
   651
    val locale = (case raw_locale of None => None | Some (_, atts) => Some (the opt_name, atts));
wenzelm@12503
   652
  in
wenzelm@12549
   653
    init
wenzelm@12549
   654
    |> open_block
wenzelm@12511
   655
    |> map_context (K locale_ctxt)
wenzelm@12065
   656
    |> open_block
wenzelm@12511
   657
    |> map_context (K elems_ctxt)
wenzelm@12534
   658
    |> setup_goal I ProofContext.bind_propp_schematic_i
wenzelm@12534
   659
      ProofContext.fix_frees (Theorem (kind, a, locale, map (snd o fst) args))
wenzelm@12534
   660
      Seq.single (map (fst o fst) args) propp
wenzelm@12065
   661
  end;
wenzelm@5820
   662
wenzelm@12534
   663
val multi_theorem = global_goal Locale.read_context_statement;
wenzelm@12534
   664
val multi_theorem_i = global_goal Locale.cert_context_statement;
wenzelm@12146
   665
wenzelm@5820
   666
wenzelm@5820
   667
(*local goals*)
wenzelm@12146
   668
fun local_goal' prepp kind (check: bool -> state -> state) f args int state =
wenzelm@7928
   669
  state
wenzelm@12146
   670
  |> setup_goal open_block prepp (K I) (kind (map (snd o fst) args))
wenzelm@12146
   671
    f (map (fst o fst) args) (map snd args)
wenzelm@10936
   672
  |> warn_extra_tfrees state
wenzelm@10936
   673
  |> check int;
wenzelm@5820
   674
wenzelm@12146
   675
fun local_goal prepp kind f args = local_goal' prepp kind (K I) f args false;
wenzelm@10936
   676
wenzelm@10936
   677
val show = local_goal' ProofContext.bind_propp Show;
wenzelm@10936
   678
val show_i = local_goal' ProofContext.bind_propp_i Show;
wenzelm@10936
   679
val have = local_goal ProofContext.bind_propp Have;
wenzelm@10380
   680
val have_i = local_goal ProofContext.bind_propp_i Have;
wenzelm@5820
   681
wenzelm@5820
   682
wenzelm@5820
   683
wenzelm@5820
   684
(** conclusions **)
wenzelm@5820
   685
wenzelm@5820
   686
(* current goal *)
wenzelm@5820
   687
wenzelm@7176
   688
fun current_goal (State (Node {context, goal = Some goal, ...}, _)) = (context, goal)
wenzelm@5820
   689
  | current_goal state = raise STATE ("No current goal!", state);
wenzelm@5820
   690
wenzelm@7176
   691
fun assert_current_goal true (state as State (Node {goal = None, ...}, _)) =
wenzelm@6404
   692
      raise STATE ("No goal in this block!", state)
wenzelm@7176
   693
  | assert_current_goal false (state as State (Node {goal = Some _, ...}, _)) =
wenzelm@6404
   694
      raise STATE ("Goal present in this block!", state)
wenzelm@6404
   695
  | assert_current_goal _ state = state;
wenzelm@5820
   696
wenzelm@12010
   697
fun assert_bottom b (state as State (_, nodes)) =
wenzelm@12010
   698
  let val bot = (length nodes <= 2) in
wenzelm@12010
   699
    if b andalso not bot then raise STATE ("Not at bottom of proof!", state)
wenzelm@12010
   700
    else if not b andalso bot then raise STATE ("Already at bottom of proof!", state)
wenzelm@12010
   701
    else state
wenzelm@12010
   702
  end;
wenzelm@5820
   703
wenzelm@6404
   704
val at_bottom = can (assert_bottom true o close_block);
wenzelm@6404
   705
wenzelm@6932
   706
fun end_proof bot state =
wenzelm@5820
   707
  state
wenzelm@5820
   708
  |> assert_forward
wenzelm@5820
   709
  |> close_block
wenzelm@5820
   710
  |> assert_bottom bot
wenzelm@7011
   711
  |> assert_current_goal true
wenzelm@7011
   712
  |> goal_facts (K []);
wenzelm@5820
   713
wenzelm@5820
   714
wenzelm@6404
   715
(* local_qed *)
wenzelm@5820
   716
wenzelm@6982
   717
fun finish_local (print_result, print_rule) state =
wenzelm@5820
   718
  let
wenzelm@12146
   719
    val (goal_ctxt, (((kind, names, tss), (_, raw_thm)), after_qed)) = current_goal state;
wenzelm@8617
   720
    val outer_state = state |> close_block;
wenzelm@8617
   721
    val outer_ctxt = context_of outer_state;
wenzelm@8617
   722
wenzelm@12146
   723
    val ts = flat tss;
wenzelm@12146
   724
    val results = prep_result state ts raw_thm;
wenzelm@12146
   725
    val resultq =
wenzelm@12146
   726
      results |> map (ProofContext.export false goal_ctxt outer_ctxt)
wenzelm@12146
   727
      |> Seq.commute |> Seq.map (Library.unflat tss);
wenzelm@9469
   728
wenzelm@12146
   729
    val (attss, opt_solve) =
wenzelm@5820
   730
      (case kind of
wenzelm@12146
   731
        Show attss => (attss, export_goals goal_ctxt print_rule results)
wenzelm@12146
   732
      | Have attss => (attss, Seq.single)
wenzelm@6932
   733
      | _ => err_malformed "finish_local" state);
wenzelm@5820
   734
  in
wenzelm@12146
   735
    print_result goal_ctxt (kind_name (sign_of state) kind, names ~~ Library.unflat tss results);
wenzelm@8617
   736
    outer_state
wenzelm@12549
   737
    |> auto_bind_facts (ProofContext.generalize goal_ctxt outer_ctxt ts)
wenzelm@9469
   738
    |> (fn st => Seq.map (fn res =>
wenzelm@12146
   739
      have_thmss ((names ~~ attss) ~~ map (single o Thm.no_attributes) res) st) resultq)
wenzelm@9469
   740
    |> (Seq.flat o Seq.map opt_solve)
wenzelm@7176
   741
    |> (Seq.flat o Seq.map after_qed)
wenzelm@5820
   742
  end;
wenzelm@5820
   743
wenzelm@6982
   744
fun local_qed finalize print state =
wenzelm@6404
   745
  state
wenzelm@6932
   746
  |> end_proof false
wenzelm@6871
   747
  |> finalize
wenzelm@6982
   748
  |> (Seq.flat o Seq.map (finish_local print));
wenzelm@5820
   749
wenzelm@5820
   750
wenzelm@12698
   751
(* global_qed *)  (* FIXME tune *)
wenzelm@12065
   752
wenzelm@6950
   753
fun finish_global state =
wenzelm@5820
   754
  let
wenzelm@12065
   755
    fun export inner outer th =
wenzelm@12065
   756
      (case Seq.pull (ProofContext.export false inner outer th) of
wenzelm@12065
   757
        Some (th', _) => th' |> Drule.local_standard
wenzelm@12065
   758
      | None => raise STATE ("Internal failure of theorem export", state));
wenzelm@12010
   759
wenzelm@12698
   760
    fun add_thmss None _ _ add_global thy = add_global thy
wenzelm@12698
   761
      | add_thmss (Some (loc, atts)) names ths add_global thy =
wenzelm@12698
   762
          let val n = length names - length atts in
wenzelm@12698
   763
            if n < 0 then raise STATE ("Bad number of local attributes", state)
wenzelm@12698
   764
            else
wenzelm@12698
   765
              thy
wenzelm@12698
   766
              |> Locale.add_thmss loc ((names ~~ ths) ~~ (atts @ replicate n []))
wenzelm@12698
   767
              |> Theory.add_path (Sign.base_name loc)
wenzelm@12698
   768
              |> add_global
wenzelm@12698
   769
              |>> (fn thy' => thy' |> PureThy.hide_thms false
wenzelm@12698
   770
                (map (Sign.full_name (Theory.sign_of thy')) (filter_out (equal "") names)))
wenzelm@12698
   771
              |>> Theory.parent_path
wenzelm@12698
   772
          end;
wenzelm@12698
   773
wenzelm@12146
   774
    val (goal_ctxt, (((kind, names, tss), (_, raw_thm)), _)) = current_goal state;
wenzelm@12065
   775
    val locale_ctxt = context_of (state |> close_block);
wenzelm@12065
   776
    val theory_ctxt = context_of (state |> close_block |> close_block);
wenzelm@5820
   777
wenzelm@12146
   778
    val ts = flat tss;
wenzelm@12146
   779
    val locale_results =
wenzelm@12146
   780
      prep_result state ts raw_thm |> map (export goal_ctxt locale_ctxt);
wenzelm@12223
   781
    val results = locale_results
wenzelm@12223
   782
      |> map (Drule.strip_shyps_warning o export locale_ctxt theory_ctxt);
wenzelm@12065
   783
wenzelm@12242
   784
    val (k, (cname, catts), locale, attss) =
wenzelm@12146
   785
      (case kind of Theorem x => x | _ => err_malformed "finish_global" state);
wenzelm@12242
   786
    val (thy1, res') =
wenzelm@12698
   787
      theory_of state |>
wenzelm@12698
   788
      add_thmss locale names (Library.unflat tss locale_results)
wenzelm@12698
   789
        (PureThy.have_thmss [Drule.kind k]
wenzelm@12698
   790
          ((names ~~ attss) ~~ map (single o Thm.no_attributes) (Library.unflat tss results)));
wenzelm@12242
   791
    val thy2 = thy1 |> PureThy.add_thmss [((cname, flat (map #2 res')), catts)] |> #1;
wenzelm@12244
   792
  in (thy2, ((k, cname), res')) end;
wenzelm@5820
   793
wenzelm@10553
   794
(*note: clients should inspect first result only, as backtracking may destroy theory*)
wenzelm@6950
   795
fun global_qed finalize state =
wenzelm@5820
   796
  state
wenzelm@6932
   797
  |> end_proof true
wenzelm@6871
   798
  |> finalize
wenzelm@12065
   799
  |> Seq.map finish_global;
wenzelm@5820
   800
wenzelm@5820
   801
wenzelm@6896
   802
wenzelm@6896
   803
(** blocks **)
wenzelm@6896
   804
wenzelm@6896
   805
(* begin_block *)
wenzelm@6896
   806
wenzelm@6896
   807
fun begin_block state =
wenzelm@6896
   808
  state
wenzelm@6896
   809
  |> assert_forward
wenzelm@6896
   810
  |> new_block
wenzelm@6896
   811
  |> open_block;
wenzelm@6896
   812
wenzelm@6896
   813
wenzelm@6896
   814
(* end_block *)
wenzelm@6896
   815
wenzelm@6896
   816
fun end_block state =
wenzelm@6896
   817
  state
wenzelm@6896
   818
  |> assert_forward
wenzelm@6896
   819
  |> close_block
wenzelm@6896
   820
  |> assert_current_goal false
wenzelm@6896
   821
  |> close_block
wenzelm@6932
   822
  |> transfer_facts state;
wenzelm@6896
   823
wenzelm@6896
   824
wenzelm@6896
   825
(* next_block *)
wenzelm@6896
   826
wenzelm@6896
   827
fun next_block state =
wenzelm@6896
   828
  state
wenzelm@6896
   829
  |> assert_forward
wenzelm@6896
   830
  |> close_block
wenzelm@8718
   831
  |> new_block
wenzelm@8718
   832
  |> reset_facts;
wenzelm@6896
   833
wenzelm@6896
   834
wenzelm@5820
   835
end;
wenzelm@8152
   836
wenzelm@8152
   837
structure BasicProof: BASIC_PROOF = Proof;
wenzelm@8152
   838
open BasicProof;