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