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