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