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