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