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