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