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