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