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