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