src/Pure/Isar/proof.ML
author wenzelm
Tue Nov 06 23:55:19 2001 +0100 (2001-11-06)
changeset 12085 235576bea937
parent 12065 5f7f44d5e3dd
child 12096 8945a021f375
permissions -rw-r--r--
pretty_goals_local: observes context syntax;
PureThy.store_thm: locale_prefix;
wenzelm@5820
     1
(*  Title:      Pure/Isar/proof.ML
wenzelm@5820
     2
    ID:         $Id$
wenzelm@5820
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8807
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@5820
     5
wenzelm@5820
     6
Proof states and methods.
wenzelm@5820
     7
*)
wenzelm@5820
     8
wenzelm@8152
     9
signature BASIC_PROOF =
wenzelm@8152
    10
sig
wenzelm@8374
    11
  val FINDGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq
wenzelm@8374
    12
  val HEADGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq
wenzelm@8152
    13
end;
wenzelm@8152
    14
wenzelm@5820
    15
signature PROOF =
wenzelm@5820
    16
sig
wenzelm@8152
    17
  include BASIC_PROOF
wenzelm@5820
    18
  type context
wenzelm@5820
    19
  type state
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@5820
    25
  val sign_of: state -> Sign.sg
wenzelm@7924
    26
  val warn_extra_tfrees: state -> state -> state
wenzelm@7605
    27
  val reset_thms: string -> state -> state
wenzelm@6091
    28
  val the_facts: state -> thm list
wenzelm@9469
    29
  val the_fact: state -> thm
wenzelm@11079
    30
  val get_goal: state -> context * (thm list * thm)
wenzelm@6091
    31
  val goal_facts: (state -> thm list) -> state -> state
wenzelm@5820
    32
  val use_facts: state -> state
wenzelm@5820
    33
  val reset_facts: state -> state
wenzelm@9469
    34
  val is_chain: state -> bool
wenzelm@6891
    35
  val assert_forward: state -> state
wenzelm@9469
    36
  val assert_forward_or_chain: state -> state
wenzelm@5820
    37
  val assert_backward: state -> state
wenzelm@8206
    38
  val assert_no_chain: state -> state
wenzelm@5820
    39
  val enter_forward: state -> state
wenzelm@6529
    40
  val verbose: bool ref
wenzelm@7201
    41
  val print_state: int -> state -> unit
wenzelm@10360
    42
  val pretty_goals: bool -> state -> Pretty.T list
wenzelm@6776
    43
  val level: state -> int
wenzelm@5820
    44
  type method
wenzelm@6848
    45
  val method: (thm list -> tactic) -> method
wenzelm@8374
    46
  val method_cases: (thm list -> thm -> (thm * (string * RuleCases.T) list) Seq.seq) -> method
wenzelm@5820
    47
  val refine: (context -> method) -> state -> state Seq.seq
wenzelm@8234
    48
  val refine_end: (context -> method) -> state -> state Seq.seq
wenzelm@5936
    49
  val match_bind: (string list * string) list -> state -> state
wenzelm@5936
    50
  val match_bind_i: (term list * term) list -> state -> state
wenzelm@8617
    51
  val let_bind: (string list * string) list -> state -> state
wenzelm@8617
    52
  val let_bind_i: (term list * term) list -> state -> state
wenzelm@6876
    53
  val simple_have_thms: string -> thm list -> state -> state
wenzelm@9196
    54
  val have_thmss: ((string * context attribute list) *
wenzelm@9196
    55
    (thm list * context attribute list) list) list -> state -> state
wenzelm@9196
    56
  val with_thmss: ((string * context attribute list) *
wenzelm@9196
    57
    (thm list * context attribute list) list) list -> state -> state
wenzelm@7412
    58
  val fix: (string list * string option) list -> state -> state
wenzelm@7665
    59
  val fix_i: (string list * typ option) list -> state -> state
wenzelm@9469
    60
  val assm: ProofContext.exporter
wenzelm@10464
    61
    -> ((string * context attribute list) * (string * (string list * string list)) list) list
wenzelm@7271
    62
    -> state -> state
wenzelm@9469
    63
  val assm_i: ProofContext.exporter
wenzelm@10464
    64
    -> ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@6932
    65
    -> state -> state
wenzelm@10464
    66
  val assume:
wenzelm@10464
    67
    ((string * context attribute list) * (string * (string list * string list)) list) list
wenzelm@7271
    68
    -> state -> state
wenzelm@10464
    69
  val assume_i: ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@6932
    70
    -> state -> state
wenzelm@10464
    71
  val presume:
wenzelm@10464
    72
    ((string * context attribute list) * (string * (string list * string list)) list) list
wenzelm@6932
    73
    -> state -> state
wenzelm@10464
    74
  val presume_i: ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@6932
    75
    -> state -> state
wenzelm@11891
    76
  val def: string -> context attribute list -> string *  (string * string list) -> state -> state
wenzelm@11891
    77
  val def_i: string -> context attribute list -> string * (term * term list) -> state -> state
wenzelm@11793
    78
  val invoke_case: string * string option list * context attribute list -> state -> state
wenzelm@12065
    79
  val theorem: string
wenzelm@12065
    80
    -> (xstring * context attribute list) option -> context attribute Locale.element list
wenzelm@12065
    81
    -> bstring -> theory attribute list -> string * (string list * string list) -> theory -> state
wenzelm@12065
    82
  val theorem_i: string
wenzelm@12065
    83
    -> (string * context attribute list) option -> context attribute Locale.element_i list
wenzelm@12065
    84
    -> bstring -> theory attribute list -> term * (term list * term list) -> theory -> state
wenzelm@5820
    85
  val chain: state -> state
wenzelm@6091
    86
  val from_facts: thm list -> state -> state
wenzelm@10936
    87
  val show: (bool -> state -> state) -> (state -> state Seq.seq) -> string
wenzelm@10936
    88
    -> context attribute list -> string * (string list * string list) -> bool -> state -> state
wenzelm@10936
    89
  val show_i: (bool -> state -> state) -> (state -> state Seq.seq) -> string
wenzelm@10936
    90
    -> context attribute list -> term * (term list * term list) -> bool -> state -> state
wenzelm@7176
    91
  val have: (state -> state Seq.seq) -> string -> context attribute list
wenzelm@7176
    92
    -> string * (string list * string list) -> state -> state
wenzelm@7176
    93
  val have_i: (state -> state Seq.seq) -> string -> context attribute list
wenzelm@7176
    94
    -> term * (term list * term list) -> state -> state
wenzelm@6404
    95
  val at_bottom: state -> bool
wenzelm@6982
    96
  val local_qed: (state -> state Seq.seq)
wenzelm@12055
    97
    -> (context -> {kind: string, name: string, thm: thm} -> unit) *
wenzelm@12055
    98
      (context -> thm -> unit) -> state -> state Seq.seq
wenzelm@6950
    99
  val global_qed: (state -> state Seq.seq) -> state
wenzelm@6950
   100
    -> (theory * {kind: string, name: string, thm: thm}) Seq.seq
wenzelm@6896
   101
  val begin_block: state -> state
wenzelm@6932
   102
  val end_block: state -> state Seq.seq
wenzelm@6896
   103
  val next_block: state -> state
wenzelm@5820
   104
end;
wenzelm@5820
   105
wenzelm@8152
   106
signature PRIVATE_PROOF =
wenzelm@5820
   107
sig
wenzelm@5820
   108
  include PROOF
wenzelm@5820
   109
  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> state -> state
wenzelm@5820
   110
end;
wenzelm@5820
   111
wenzelm@8152
   112
structure Proof: PRIVATE_PROOF =
wenzelm@5820
   113
struct
wenzelm@5820
   114
wenzelm@5820
   115
wenzelm@5820
   116
(** proof state **)
wenzelm@5820
   117
wenzelm@5820
   118
type context = ProofContext.context;
wenzelm@5820
   119
wenzelm@5820
   120
wenzelm@5820
   121
(* type goal *)
wenzelm@5820
   122
wenzelm@5820
   123
datatype kind =
wenzelm@12065
   124
  Theorem of string * (string * context attribute list) option * theory attribute list |
wenzelm@12065
   125
    (*theorem with kind, locale target, attributes*)
wenzelm@12065
   126
  Show of context attribute list |  (*intermediate result, solving subgoal*)
wenzelm@12065
   127
  Have of context attribute list;   (*intermediate result*)
wenzelm@5820
   128
wenzelm@12015
   129
fun kind_name _ (Theorem (s, None, _)) = s
wenzelm@12065
   130
  | kind_name sg (Theorem (s, Some (name, _), _)) = s ^ " (in " ^ Locale.cond_extern sg name ^ ")"
wenzelm@12015
   131
  | kind_name _ (Show _) = "show"
wenzelm@12015
   132
  | kind_name _ (Have _) = "have";
wenzelm@5820
   133
wenzelm@5820
   134
type goal =
wenzelm@6932
   135
 (kind *        (*result kind*)
wenzelm@6932
   136
  string *      (*result name*)
wenzelm@6932
   137
  term) *       (*result statement*)
wenzelm@6932
   138
 (thm list *    (*use facts*)
wenzelm@6932
   139
  thm);         (*goal: subgoals ==> statement*)
wenzelm@5820
   140
wenzelm@5820
   141
wenzelm@5820
   142
(* type mode *)
wenzelm@5820
   143
wenzelm@5820
   144
datatype mode = Forward | ForwardChain | Backward;
wenzelm@7201
   145
val mode_name = (fn Forward => "state" | ForwardChain => "chain" | Backward => "prove");
wenzelm@5820
   146
wenzelm@5820
   147
wenzelm@5820
   148
(* datatype state *)
wenzelm@5820
   149
wenzelm@7176
   150
datatype node =
wenzelm@7176
   151
  Node of
wenzelm@7176
   152
   {context: context,
wenzelm@7176
   153
    facts: thm list option,
wenzelm@7176
   154
    mode: mode,
wenzelm@7176
   155
    goal: (goal * (state -> state Seq.seq)) option}
wenzelm@7176
   156
and state =
wenzelm@5820
   157
  State of
wenzelm@5820
   158
    node *              (*current*)
wenzelm@5820
   159
    node list;          (*parents wrt. block structure*)
wenzelm@5820
   160
wenzelm@7176
   161
fun make_node (context, facts, mode, goal) =
wenzelm@7176
   162
  Node {context = context, facts = facts, mode = mode, goal = goal};
wenzelm@7176
   163
wenzelm@7176
   164
wenzelm@5820
   165
exception STATE of string * state;
wenzelm@5820
   166
wenzelm@5820
   167
fun err_malformed name state =
wenzelm@5820
   168
  raise STATE (name ^ ": internal error -- malformed proof state", state);
wenzelm@5820
   169
wenzelm@6871
   170
fun check_result msg state sq =
wenzelm@6871
   171
  (case Seq.pull sq of
wenzelm@6871
   172
    None => raise STATE (msg, state)
wenzelm@6871
   173
  | Some s_sq => Seq.cons s_sq);
wenzelm@6871
   174
wenzelm@5820
   175
wenzelm@7176
   176
fun map_current f (State (Node {context, facts, mode, goal}, nodes)) =
wenzelm@5820
   177
  State (make_node (f (context, facts, mode, goal)), nodes);
wenzelm@5820
   178
wenzelm@12045
   179
fun init_state thy =
wenzelm@12045
   180
  State (make_node (ProofContext.init thy, None, Forward, None), []);
wenzelm@12045
   181
wenzelm@5820
   182
wenzelm@5820
   183
wenzelm@5820
   184
(** basic proof state operations **)
wenzelm@5820
   185
wenzelm@5820
   186
(* context *)
wenzelm@5820
   187
wenzelm@7176
   188
fun context_of (State (Node {context, ...}, _)) = context;
wenzelm@5820
   189
val theory_of = ProofContext.theory_of o context_of;
wenzelm@5820
   190
val sign_of = ProofContext.sign_of o context_of;
wenzelm@5820
   191
wenzelm@5820
   192
fun map_context f = map_current (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@5820
   193
wenzelm@7176
   194
fun map_context_result f (state as State (Node {context, facts, mode, goal}, nodes)) =
wenzelm@5820
   195
  let val (context', result) = f context
wenzelm@5820
   196
  in (State (make_node (context', facts, mode, goal), nodes), result) end;
wenzelm@5820
   197
wenzelm@5820
   198
wenzelm@5820
   199
fun put_data kind f = map_context o ProofContext.put_data kind f;
wenzelm@7924
   200
val warn_extra_tfrees = map_context o ProofContext.warn_extra_tfrees o context_of;
wenzelm@10809
   201
val add_binds_i = map_context o ProofContext.add_binds_i;
wenzelm@6790
   202
val auto_bind_goal = map_context o ProofContext.auto_bind_goal;
wenzelm@6798
   203
val auto_bind_facts = map_context oo ProofContext.auto_bind_facts;
wenzelm@6091
   204
val put_thms = map_context o ProofContext.put_thms;
wenzelm@6091
   205
val put_thmss = map_context o ProofContext.put_thmss;
wenzelm@7605
   206
val reset_thms = map_context o ProofContext.reset_thms;
wenzelm@8374
   207
val get_case = ProofContext.get_case o context_of;
wenzelm@5820
   208
wenzelm@5820
   209
wenzelm@5820
   210
(* facts *)
wenzelm@5820
   211
wenzelm@7176
   212
fun the_facts (State (Node {facts = Some facts, ...}, _)) = facts
wenzelm@5820
   213
  | the_facts state = raise STATE ("No current facts available", state);
wenzelm@5820
   214
wenzelm@9469
   215
fun the_fact state =
wenzelm@9469
   216
  (case the_facts state of
wenzelm@9469
   217
    [thm] => thm
wenzelm@9469
   218
  | _ => raise STATE ("Single fact expected", state));
wenzelm@9469
   219
wenzelm@6848
   220
fun assert_facts state = (the_facts state; state);
wenzelm@7176
   221
fun get_facts (State (Node {facts, ...}, _)) = facts;
wenzelm@6848
   222
wenzelm@7605
   223
wenzelm@7605
   224
val thisN = "this";
wenzelm@7605
   225
wenzelm@5820
   226
fun put_facts facts state =
wenzelm@5820
   227
  state
wenzelm@5820
   228
  |> map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal))
wenzelm@7605
   229
  |> (case facts of None => reset_thms thisN | Some ths => put_thms (thisN, ths));
wenzelm@5820
   230
wenzelm@5820
   231
val reset_facts = put_facts None;
wenzelm@5820
   232
wenzelm@9196
   233
fun these_factss (state, named_factss) =
wenzelm@5820
   234
  state
wenzelm@9196
   235
  |> put_thmss named_factss
wenzelm@9196
   236
  |> put_facts (Some (flat (map #2 named_factss)));
wenzelm@5820
   237
wenzelm@5820
   238
wenzelm@5820
   239
(* goal *)
wenzelm@5820
   240
wenzelm@10553
   241
local
wenzelm@10553
   242
  fun find i (state as State (Node {goal = Some goal, ...}, _)) = (context_of state, (i, goal))
wenzelm@10553
   243
    | find i (State (Node {goal = None, ...}, node :: nodes)) = find (i + 1) (State (node, nodes))
wenzelm@10553
   244
    | find _ (state as State (_, [])) = err_malformed "find_goal" state;
wenzelm@10553
   245
in val find_goal = find 0 end;
wenzelm@7176
   246
wenzelm@11079
   247
fun get_goal state =
wenzelm@11079
   248
  let val (ctxt, (_, ((_, x), _))) = find_goal state
wenzelm@11079
   249
  in (ctxt, x) end;
wenzelm@11079
   250
wenzelm@5820
   251
fun put_goal goal = map_current (fn (ctxt, facts, mode, _) => (ctxt, facts, mode, goal));
wenzelm@5820
   252
wenzelm@8374
   253
fun map_goal f g (State (Node {context, facts, mode, goal = Some goal}, nodes)) =
wenzelm@8374
   254
      State (make_node (f context, facts, mode, Some (g goal)), nodes)
wenzelm@8374
   255
  | map_goal f g (State (nd, node :: nodes)) =
wenzelm@8374
   256
      let val State (node', nodes') = map_goal f g (State (node, nodes))
wenzelm@8374
   257
      in map_context f (State (nd, node' :: nodes')) end
wenzelm@8374
   258
  | map_goal _ _ state = state;
wenzelm@5820
   259
wenzelm@5820
   260
fun goal_facts get state =
wenzelm@5820
   261
  state
wenzelm@8374
   262
  |> map_goal I (fn ((result, (_, thm)), f) => ((result, (get state, thm)), f));
wenzelm@5820
   263
wenzelm@5820
   264
fun use_facts state =
wenzelm@5820
   265
  state
wenzelm@5820
   266
  |> goal_facts the_facts
wenzelm@5820
   267
  |> reset_facts;
wenzelm@5820
   268
wenzelm@5820
   269
wenzelm@5820
   270
(* mode *)
wenzelm@5820
   271
wenzelm@7176
   272
fun get_mode (State (Node {mode, ...}, _)) = mode;
wenzelm@5820
   273
fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal));
wenzelm@5820
   274
wenzelm@5820
   275
val enter_forward = put_mode Forward;
wenzelm@5820
   276
val enter_forward_chain = put_mode ForwardChain;
wenzelm@5820
   277
val enter_backward = put_mode Backward;
wenzelm@5820
   278
wenzelm@5820
   279
fun assert_mode pred state =
wenzelm@5820
   280
  let val mode = get_mode state in
wenzelm@5820
   281
    if pred mode then state
wenzelm@12010
   282
    else raise STATE ("Illegal application of proof command in "
wenzelm@12010
   283
      ^ quote (mode_name mode) ^ " mode", state)
wenzelm@5820
   284
  end;
wenzelm@5820
   285
wenzelm@5820
   286
fun is_chain state = get_mode state = ForwardChain;
wenzelm@5820
   287
val assert_forward = assert_mode (equal Forward);
wenzelm@5820
   288
val assert_forward_or_chain = assert_mode (equal Forward orf equal ForwardChain);
wenzelm@5820
   289
val assert_backward = assert_mode (equal Backward);
wenzelm@8206
   290
val assert_no_chain = assert_mode (not_equal ForwardChain);
wenzelm@5820
   291
wenzelm@5820
   292
wenzelm@5820
   293
(* blocks *)
wenzelm@5820
   294
wenzelm@6776
   295
fun level (State (_, nodes)) = length nodes;
wenzelm@6776
   296
wenzelm@5820
   297
fun open_block (State (node, nodes)) = State (node, node :: nodes);
wenzelm@5820
   298
wenzelm@5820
   299
fun new_block state =
wenzelm@5820
   300
  state
wenzelm@5820
   301
  |> open_block
wenzelm@5820
   302
  |> put_goal None;
wenzelm@5820
   303
wenzelm@7487
   304
fun close_block (state as State (_, node :: nodes)) = State (node, nodes)
wenzelm@5820
   305
  | close_block state = raise STATE ("Unbalanced block parentheses", state);
wenzelm@5820
   306
wenzelm@5820
   307
wenzelm@5820
   308
wenzelm@5820
   309
(** print_state **)
wenzelm@5820
   310
wenzelm@6529
   311
val verbose = ProofContext.verbose;
wenzelm@6529
   312
wenzelm@12085
   313
fun pretty_goals_local ctxt = Display.pretty_goals_aux
wenzelm@12085
   314
  (ProofContext.pretty_sort ctxt, ProofContext.pretty_typ ctxt, ProofContext.pretty_term ctxt);
wenzelm@12085
   315
wenzelm@12055
   316
fun pretty_facts _ _ None = []
wenzelm@12055
   317
  | pretty_facts s ctxt (Some ths) =
wenzelm@12055
   318
      [Pretty.big_list (s ^ "this:") (map (ProofContext.pretty_thm ctxt) ths), Pretty.str ""];
wenzelm@6756
   319
wenzelm@12085
   320
fun print_state nr (state as State (Node {context = ctxt, facts, mode, goal = _}, nodes)) =
wenzelm@5820
   321
  let
wenzelm@11891
   322
    val ref (_, _, begin_goal) = Display.current_goals_markers;
wenzelm@5820
   323
wenzelm@5820
   324
    fun levels_up 0 = ""
wenzelm@7575
   325
      | levels_up 1 = ", 1 level up"
wenzelm@7575
   326
      | levels_up i = ", " ^ string_of_int i ^ " levels up";
wenzelm@5820
   327
wenzelm@11556
   328
    fun subgoals 0 = ""
wenzelm@11556
   329
      | subgoals 1 = ", 1 subgoal"
wenzelm@11556
   330
      | subgoals n = ", " ^ string_of_int n ^ " subgoals";
wenzelm@11556
   331
      
wenzelm@8462
   332
    fun pretty_goal (_, (i, (((kind, name, _), (goal_facts, thm)), _))) =
wenzelm@12085
   333
      pretty_facts "using " ctxt
wenzelm@8462
   334
        (if mode <> Backward orelse null goal_facts then None else Some goal_facts) @
wenzelm@12015
   335
      [Pretty.str ("goal (" ^ kind_name (sign_of state) kind ^
wenzelm@11556
   336
          (if name = "" then "" else " " ^ name) ^
wenzelm@11556
   337
            levels_up (i div 2) ^ subgoals (Thm.nprems_of thm) ^ "):")] @
wenzelm@12085
   338
      pretty_goals_local ctxt begin_goal (true, true) (! Display.goals_limit) thm;
wenzelm@6848
   339
wenzelm@8462
   340
    val ctxt_prts =
wenzelm@12085
   341
      if ! verbose orelse mode = Forward then ProofContext.pretty_context ctxt
wenzelm@12085
   342
      else if mode = Backward then ProofContext.pretty_asms ctxt
wenzelm@7575
   343
      else [];
wenzelm@8462
   344
wenzelm@8462
   345
    val prts =
wenzelm@8582
   346
     [Pretty.str ("proof (" ^ mode_name mode ^ "): step " ^ string_of_int nr ^
wenzelm@12010
   347
        (if ! verbose then ", depth " ^ string_of_int (length nodes div 2 - 1)
wenzelm@8561
   348
        else "")), Pretty.str ""] @
wenzelm@8462
   349
     (if null ctxt_prts then [] else ctxt_prts @ [Pretty.str ""]) @
wenzelm@8462
   350
     (if ! verbose orelse mode = Forward then
wenzelm@12085
   351
       (pretty_facts "" ctxt facts @ pretty_goal (find_goal state))
wenzelm@12085
   352
      else if mode = ForwardChain then pretty_facts "picking " ctxt facts
wenzelm@10553
   353
      else pretty_goal (find_goal state))
wenzelm@8462
   354
  in Pretty.writeln (Pretty.chunks prts) end;
wenzelm@5820
   355
wenzelm@12085
   356
fun pretty_goals main state =
wenzelm@12085
   357
  let val (ctxt, (_, ((_, (_, thm)), _))) = find_goal state
wenzelm@12085
   358
  in pretty_goals_local ctxt "" (false, main) (! Display.goals_limit) thm end;
wenzelm@10320
   359
wenzelm@5820
   360
wenzelm@5820
   361
wenzelm@5820
   362
(** proof steps **)
wenzelm@5820
   363
wenzelm@5820
   364
(* datatype method *)
wenzelm@5820
   365
wenzelm@8374
   366
datatype method =
wenzelm@8374
   367
  Method of thm list -> thm -> (thm * (string * RuleCases.T) list) Seq.seq;
wenzelm@8374
   368
wenzelm@8374
   369
fun method tac = Method (fn facts => fn st => Seq.map (rpair []) (tac facts st));
wenzelm@8374
   370
val method_cases = Method;
wenzelm@5820
   371
wenzelm@5820
   372
wenzelm@5820
   373
(* refine goal *)
wenzelm@5820
   374
wenzelm@8234
   375
local
wenzelm@8234
   376
wenzelm@5820
   377
fun check_sign sg state =
wenzelm@5820
   378
  if Sign.subsig (sg, sign_of state) then state
wenzelm@5820
   379
  else raise STATE ("Bad signature of result: " ^ Sign.str_of_sg sg, state);
wenzelm@5820
   380
wenzelm@8234
   381
fun gen_refine current_context meth_fun state =
wenzelm@6848
   382
  let
wenzelm@10553
   383
    val (goal_ctxt, (_, ((result, (facts, thm)), f))) = find_goal state;
wenzelm@8234
   384
    val Method meth = meth_fun (if current_context then context_of state else goal_ctxt);
wenzelm@5820
   385
wenzelm@8374
   386
    fun refn (thm', cases) =
wenzelm@6848
   387
      state
wenzelm@6848
   388
      |> check_sign (Thm.sign_of_thm thm')
wenzelm@8374
   389
      |> map_goal (ProofContext.add_cases cases) (K ((result, (facts, thm')), f));
wenzelm@6848
   390
  in Seq.map refn (meth facts thm) end;
wenzelm@5820
   391
wenzelm@8234
   392
in
wenzelm@8234
   393
wenzelm@8234
   394
val refine = gen_refine true;
wenzelm@8234
   395
val refine_end = gen_refine false;
wenzelm@8234
   396
wenzelm@8234
   397
end;
wenzelm@8234
   398
wenzelm@5820
   399
wenzelm@11917
   400
(* goal addressing *)
wenzelm@6932
   401
wenzelm@8152
   402
fun FINDGOAL tac st =
wenzelm@8374
   403
  let fun find (i, n) = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1, n))
wenzelm@8374
   404
  in find (1, Thm.nprems_of st) st end;
wenzelm@8152
   405
wenzelm@8166
   406
fun HEADGOAL tac = tac 1;
wenzelm@8166
   407
wenzelm@9469
   408
wenzelm@9469
   409
(* export results *)
wenzelm@9469
   410
wenzelm@11816
   411
fun refine_tac rule =
wenzelm@11816
   412
  Tactic.rtac rule THEN_ALL_NEW (SUBGOAL (fn (goal, i) =>
wenzelm@11816
   413
    if can Logic.dest_goal (Logic.strip_assums_concl goal) then
wenzelm@11816
   414
      Tactic.etac Drule.triv_goal i
wenzelm@11816
   415
    else all_tac));
wenzelm@11816
   416
wenzelm@6982
   417
fun export_goal print_rule raw_rule inner state =
wenzelm@6932
   418
  let
wenzelm@10553
   419
    val (outer, (_, ((result, (facts, thm)), f))) = find_goal state;
wenzelm@12010
   420
    val exp_tac = ProofContext.export true inner outer;
wenzelm@9469
   421
    fun exp_rule rule =
wenzelm@9469
   422
      let
wenzelm@12055
   423
        val _ = print_rule outer rule;
wenzelm@11816
   424
        val thmq = FINDGOAL (refine_tac rule) thm;
wenzelm@9469
   425
      in Seq.map (fn th => map_goal I (K ((result, (facts, th)), f)) state) thmq end;
wenzelm@9469
   426
  in raw_rule |> exp_tac |> (Seq.flat o Seq.map exp_rule) end;
wenzelm@6932
   427
wenzelm@6932
   428
fun transfer_facts inner_state state =
wenzelm@6932
   429
  (case get_facts inner_state of
wenzelm@6932
   430
    None => Seq.single (reset_facts state)
wenzelm@8617
   431
  | Some thms =>
wenzelm@8617
   432
      let
wenzelm@12010
   433
        val exp_tac = ProofContext.export false (context_of inner_state) (context_of state);
wenzelm@11816
   434
        val thmqs = map exp_tac thms;
wenzelm@8617
   435
      in Seq.map (fn ths => put_facts (Some ths) state) (Seq.commute thmqs) end);
wenzelm@6932
   436
wenzelm@6932
   437
wenzelm@6932
   438
(* prepare result *)
wenzelm@6932
   439
wenzelm@6932
   440
fun prep_result state t raw_thm =
wenzelm@5820
   441
  let
wenzelm@5820
   442
    val ctxt = context_of state;
wenzelm@5820
   443
    fun err msg = raise STATE (msg, state);
wenzelm@5820
   444
wenzelm@5820
   445
    val ngoals = Thm.nprems_of raw_thm;
wenzelm@5820
   446
    val _ =
wenzelm@5820
   447
      if ngoals = 0 then ()
wenzelm@12085
   448
      else (err (Pretty.string_of (Pretty.chunks (pretty_goals_local ctxt "" (true, true)
wenzelm@12085
   449
            (! Display.goals_limit) raw_thm @
wenzelm@12085
   450
          [Pretty.str (string_of_int ngoals ^ " unsolved goal(s)!")]))));
wenzelm@5820
   451
wenzelm@6932
   452
    val thm = raw_thm RS Drule.rev_triv_goal;
wenzelm@8617
   453
    val {hyps, prop, sign, ...} = Thm.rep_thm thm;
wenzelm@5820
   454
    val tsig = Sign.tsig_of sign;
wenzelm@6932
   455
wenzelm@12055
   456
    val casms = flat (map #1 (ProofContext.assumptions_of (context_of state)));
wenzelm@11816
   457
    val bad_hyps = Library.gen_rems Term.aconv (hyps, map Thm.term_of casms);
wenzelm@5820
   458
  in
wenzelm@12055
   459
    if not (null bad_hyps) then err ("Additional hypotheses:\n" ^
wenzelm@12055
   460
        cat_lines (map (ProofContext.string_of_term ctxt) bad_hyps))
wenzelm@10553
   461
    else if not (Pattern.matches (Sign.tsig_of (sign_of state)) (t, prop)) then
wenzelm@12055
   462
      err ("Proved a different theorem: " ^ ProofContext.string_of_term ctxt prop)
wenzelm@8617
   463
    else thm
wenzelm@5820
   464
  end;
wenzelm@5820
   465
wenzelm@5820
   466
wenzelm@5820
   467
wenzelm@5820
   468
(*** structured proof commands ***)
wenzelm@5820
   469
wenzelm@5820
   470
(** context **)
wenzelm@5820
   471
wenzelm@5820
   472
(* bind *)
wenzelm@5820
   473
wenzelm@5820
   474
fun gen_bind f x state =
wenzelm@5820
   475
  state
wenzelm@5820
   476
  |> assert_forward
wenzelm@5820
   477
  |> map_context (f x)
wenzelm@5820
   478
  |> reset_facts;
wenzelm@5820
   479
wenzelm@8617
   480
val match_bind = gen_bind (ProofContext.match_bind false);
wenzelm@8617
   481
val match_bind_i = gen_bind (ProofContext.match_bind_i false);
wenzelm@8617
   482
val let_bind = gen_bind (ProofContext.match_bind true);
wenzelm@8617
   483
val let_bind_i = gen_bind (ProofContext.match_bind_i true);
wenzelm@5820
   484
wenzelm@5820
   485
wenzelm@9196
   486
(* have_thmss etc. *)
wenzelm@5820
   487
wenzelm@9196
   488
fun have_thmss args state =
wenzelm@5820
   489
  state
wenzelm@5820
   490
  |> assert_forward
wenzelm@9196
   491
  |> map_context_result (ProofContext.have_thmss args)
wenzelm@9196
   492
  |> these_factss;
wenzelm@5820
   493
wenzelm@9196
   494
fun simple_have_thms name thms = have_thmss [((name, []), [(thms, [])])];
wenzelm@9196
   495
wenzelm@9196
   496
fun with_thmss args state =
wenzelm@9196
   497
  let val state' = state |> have_thmss args
wenzelm@9196
   498
  in state' |> simple_have_thms "" (the_facts state' @ the_facts state) end;
wenzelm@6876
   499
wenzelm@5820
   500
wenzelm@5820
   501
(* fix *)
wenzelm@5820
   502
wenzelm@5820
   503
fun gen_fix f xs state =
wenzelm@5820
   504
  state
wenzelm@5820
   505
  |> assert_forward
wenzelm@5820
   506
  |> map_context (f xs)
wenzelm@5820
   507
  |> reset_facts;
wenzelm@5820
   508
wenzelm@5820
   509
val fix = gen_fix ProofContext.fix;
wenzelm@5820
   510
val fix_i = gen_fix ProofContext.fix_i;
wenzelm@5820
   511
wenzelm@5820
   512
wenzelm@11891
   513
(* assume and presume *)
wenzelm@5820
   514
wenzelm@9469
   515
fun gen_assume f exp args state =
wenzelm@5820
   516
  state
wenzelm@5820
   517
  |> assert_forward
wenzelm@9469
   518
  |> map_context_result (f exp args)
wenzelm@11924
   519
  |> these_factss;
wenzelm@6932
   520
wenzelm@7271
   521
val assm = gen_assume ProofContext.assume;
wenzelm@7271
   522
val assm_i = gen_assume ProofContext.assume_i;
wenzelm@11917
   523
val assume = assm ProofContext.export_assume;
wenzelm@11917
   524
val assume_i = assm_i ProofContext.export_assume;
wenzelm@11917
   525
val presume = assm ProofContext.export_presume;
wenzelm@11917
   526
val presume_i = assm_i ProofContext.export_presume;
wenzelm@5820
   527
wenzelm@7271
   528
wenzelm@5820
   529
wenzelm@11891
   530
(** local definitions **)
wenzelm@11891
   531
wenzelm@11891
   532
fun gen_def fix prep_term prep_pats raw_name atts (x, (raw_rhs, raw_pats)) state =
wenzelm@11891
   533
  let
wenzelm@11891
   534
    val _ = assert_forward state;
wenzelm@11891
   535
    val ctxt = context_of state;
wenzelm@11891
   536
wenzelm@11891
   537
    (*rhs*)
wenzelm@11891
   538
    val name = if raw_name = "" then Thm.def_name x else raw_name;
wenzelm@11891
   539
    val rhs = prep_term ctxt raw_rhs;
wenzelm@11891
   540
    val T = Term.fastype_of rhs;
wenzelm@11891
   541
    val pats = prep_pats T (ProofContext.declare_term rhs ctxt) raw_pats;
wenzelm@11891
   542
wenzelm@11891
   543
    (*lhs*)
wenzelm@11891
   544
    val lhs = ProofContext.bind_skolem ctxt [x] (Free (x, T));
wenzelm@11891
   545
  in
wenzelm@11891
   546
    state
wenzelm@11891
   547
    |> fix [([x], None)]
wenzelm@11891
   548
    |> match_bind_i [(pats, rhs)]
wenzelm@11917
   549
    |> assm_i ProofContext.export_def [((name, atts), [(Logic.mk_equals (lhs, rhs), ([], []))])]
wenzelm@11891
   550
  end;
wenzelm@11891
   551
wenzelm@11891
   552
val def = gen_def fix ProofContext.read_term ProofContext.read_term_pats;
wenzelm@11891
   553
val def_i = gen_def fix_i ProofContext.cert_term ProofContext.cert_term_pats;
wenzelm@11891
   554
wenzelm@11891
   555
wenzelm@8374
   556
(* invoke_case *)
wenzelm@8374
   557
wenzelm@11793
   558
fun invoke_case (name, xs, atts) state =
wenzelm@9292
   559
  let
wenzelm@10830
   560
    val (state', (lets, asms)) =
wenzelm@11793
   561
      map_context_result (ProofContext.apply_case (get_case state name xs)) state;
wenzelm@9292
   562
  in
wenzelm@10830
   563
    state'
wenzelm@10809
   564
    |> add_binds_i lets
wenzelm@10830
   565
    |> assume_i [((name, atts), map (rpair ([], [])) asms)]
wenzelm@8374
   566
  end;
wenzelm@8374
   567
wenzelm@8374
   568
wenzelm@5820
   569
wenzelm@5820
   570
(** goals **)
wenzelm@5820
   571
wenzelm@5820
   572
(* forward chaining *)
wenzelm@5820
   573
wenzelm@5820
   574
fun chain state =
wenzelm@5820
   575
  state
wenzelm@5820
   576
  |> assert_forward
wenzelm@6848
   577
  |> assert_facts
wenzelm@5820
   578
  |> enter_forward_chain;
wenzelm@5820
   579
wenzelm@5820
   580
fun from_facts facts state =
wenzelm@5820
   581
  state
wenzelm@5820
   582
  |> put_facts (Some facts)
wenzelm@5820
   583
  |> chain;
wenzelm@5820
   584
wenzelm@5820
   585
wenzelm@5820
   586
(* setup goals *)
wenzelm@5820
   587
wenzelm@11549
   588
val rule_contextN = "rule_context";
wenzelm@8991
   589
wenzelm@12010
   590
fun setup_goal opt_block prepp autofix kind after_qed name raw_propp state =
wenzelm@5820
   591
  let
wenzelm@10464
   592
    val (state', ([[prop]], gen_binds)) =
wenzelm@5936
   593
      state
wenzelm@5936
   594
      |> assert_forward_or_chain
wenzelm@5936
   595
      |> enter_forward
wenzelm@8617
   596
      |> opt_block
wenzelm@10464
   597
      |> map_context_result (fn ct => prepp (ct, [[raw_propp]]));
wenzelm@6932
   598
    val cprop = Thm.cterm_of (sign_of state') prop;
wenzelm@7556
   599
    val goal = Drule.mk_triv_goal cprop;
wenzelm@10553
   600
wenzelm@10553
   601
    val tvars = Term.term_tvars prop;
wenzelm@10553
   602
    val vars = Term.term_vars prop;
wenzelm@5820
   603
  in
wenzelm@10553
   604
    if null tvars then ()
wenzelm@10553
   605
    else raise STATE ("Goal statement contains illegal schematic type variable(s): " ^
wenzelm@10553
   606
      commas (map (Syntax.string_of_vname o #1) tvars), state);
wenzelm@10553
   607
    if null vars then ()
wenzelm@10553
   608
    else warning ("Goal statement contains unbound schematic variable(s): " ^
wenzelm@12055
   609
      commas (map (ProofContext.string_of_term (context_of state')) vars));
wenzelm@5936
   610
    state'
wenzelm@12010
   611
    |> map_context (autofix prop)
wenzelm@12010
   612
    |> put_goal (Some (((kind, name, prop), ([], goal)), after_qed o map_context gen_binds))
wenzelm@11549
   613
    |> map_context (ProofContext.add_cases (RuleCases.make true goal [rule_contextN]))
wenzelm@6790
   614
    |> auto_bind_goal prop
wenzelm@5820
   615
    |> (if is_chain state then use_facts else reset_facts)
wenzelm@5820
   616
    |> new_block
wenzelm@5820
   617
    |> enter_backward
wenzelm@5820
   618
  end;
wenzelm@5820
   619
wenzelm@5820
   620
(*global goals*)
wenzelm@12065
   621
fun global_goal prepp prep_locale activate kind raw_locale elems name atts x thy =
wenzelm@12065
   622
  let val locale = apsome (apfst (prep_locale (Theory.sign_of thy))) raw_locale in
wenzelm@12065
   623
    thy
wenzelm@12065
   624
    |> init_state
wenzelm@12065
   625
    |> open_block
wenzelm@12065
   626
    |> (case locale of Some (name, _) => map_context (Locale.activate_locale_i name) | None => I)
wenzelm@12065
   627
    |> open_block
wenzelm@12065
   628
    |> map_context (activate elems)
wenzelm@12065
   629
    |> setup_goal I prepp (ProofContext.fix_frees o single) (Theorem (kind, locale, atts))
wenzelm@12065
   630
      Seq.single name x
wenzelm@12065
   631
  end;
wenzelm@5820
   632
wenzelm@12065
   633
val theorem = global_goal ProofContext.bind_propp_schematic Locale.intern Locale.activate_elements;
wenzelm@12065
   634
val theorem_i = global_goal ProofContext.bind_propp_schematic_i (K I) Locale.activate_elements_i;
wenzelm@5820
   635
wenzelm@5820
   636
wenzelm@5820
   637
(*local goals*)
wenzelm@10938
   638
fun local_goal' prepp kind (check: bool -> state -> state)
wenzelm@10938
   639
    f name atts args int state =
wenzelm@7928
   640
  state
wenzelm@12010
   641
  |> setup_goal open_block prepp (K I) (kind atts) f name args
wenzelm@10936
   642
  |> warn_extra_tfrees state
wenzelm@10936
   643
  |> check int;
wenzelm@5820
   644
wenzelm@10936
   645
fun local_goal prepp kind f name atts args =
wenzelm@10936
   646
  local_goal' prepp kind (K I) f name atts args false;
wenzelm@10936
   647
wenzelm@10936
   648
val show = local_goal' ProofContext.bind_propp Show;
wenzelm@10936
   649
val show_i = local_goal' ProofContext.bind_propp_i Show;
wenzelm@10936
   650
val have = local_goal ProofContext.bind_propp Have;
wenzelm@10380
   651
val have_i = local_goal ProofContext.bind_propp_i Have;
wenzelm@5820
   652
wenzelm@5820
   653
wenzelm@5820
   654
wenzelm@5820
   655
(** conclusions **)
wenzelm@5820
   656
wenzelm@5820
   657
(* current goal *)
wenzelm@5820
   658
wenzelm@7176
   659
fun current_goal (State (Node {context, goal = Some goal, ...}, _)) = (context, goal)
wenzelm@5820
   660
  | current_goal state = raise STATE ("No current goal!", state);
wenzelm@5820
   661
wenzelm@7176
   662
fun assert_current_goal true (state as State (Node {goal = None, ...}, _)) =
wenzelm@6404
   663
      raise STATE ("No goal in this block!", state)
wenzelm@7176
   664
  | assert_current_goal false (state as State (Node {goal = Some _, ...}, _)) =
wenzelm@6404
   665
      raise STATE ("Goal present in this block!", state)
wenzelm@6404
   666
  | assert_current_goal _ state = state;
wenzelm@5820
   667
wenzelm@12010
   668
fun assert_bottom b (state as State (_, nodes)) =
wenzelm@12010
   669
  let val bot = (length nodes <= 2) in
wenzelm@12010
   670
    if b andalso not bot then raise STATE ("Not at bottom of proof!", state)
wenzelm@12010
   671
    else if not b andalso bot then raise STATE ("Already at bottom of proof!", state)
wenzelm@12010
   672
    else state
wenzelm@12010
   673
  end;
wenzelm@5820
   674
wenzelm@6404
   675
val at_bottom = can (assert_bottom true o close_block);
wenzelm@6404
   676
wenzelm@6932
   677
fun end_proof bot state =
wenzelm@5820
   678
  state
wenzelm@5820
   679
  |> assert_forward
wenzelm@5820
   680
  |> close_block
wenzelm@5820
   681
  |> assert_bottom bot
wenzelm@7011
   682
  |> assert_current_goal true
wenzelm@7011
   683
  |> goal_facts (K []);
wenzelm@5820
   684
wenzelm@5820
   685
wenzelm@6404
   686
(* local_qed *)
wenzelm@5820
   687
wenzelm@6982
   688
fun finish_local (print_result, print_rule) state =
wenzelm@5820
   689
  let
wenzelm@8617
   690
    val (goal_ctxt, (((kind, name, t), (_, raw_thm)), after_qed)) = current_goal state;
wenzelm@8617
   691
    val outer_state = state |> close_block;
wenzelm@8617
   692
    val outer_ctxt = context_of outer_state;
wenzelm@8617
   693
wenzelm@6932
   694
    val result = prep_result state t raw_thm;
wenzelm@12010
   695
    val resultq = ProofContext.export false goal_ctxt outer_ctxt result;
wenzelm@9469
   696
wenzelm@5820
   697
    val (atts, opt_solve) =
wenzelm@5820
   698
      (case kind of
wenzelm@10380
   699
        Show atts => (atts, export_goal print_rule result goal_ctxt)
wenzelm@10380
   700
      | Have atts => (atts, Seq.single)
wenzelm@6932
   701
      | _ => err_malformed "finish_local" state);
wenzelm@5820
   702
  in
wenzelm@12055
   703
    print_result goal_ctxt {kind = kind_name (sign_of state) kind, name = name, thm = result};
wenzelm@8617
   704
    outer_state
wenzelm@8617
   705
    |> auto_bind_facts name [ProofContext.generalize goal_ctxt outer_ctxt t]
wenzelm@9469
   706
    |> (fn st => Seq.map (fn res =>
wenzelm@9469
   707
      have_thmss [((name, atts), [Thm.no_attributes [res]])] st) resultq)
wenzelm@9469
   708
    |> (Seq.flat o Seq.map opt_solve)
wenzelm@7176
   709
    |> (Seq.flat o Seq.map after_qed)
wenzelm@5820
   710
  end;
wenzelm@5820
   711
wenzelm@6982
   712
fun local_qed finalize print state =
wenzelm@6404
   713
  state
wenzelm@6932
   714
  |> end_proof false
wenzelm@6871
   715
  |> finalize
wenzelm@6982
   716
  |> (Seq.flat o Seq.map (finish_local print));
wenzelm@5820
   717
wenzelm@5820
   718
wenzelm@6404
   719
(* global_qed *)
wenzelm@5820
   720
wenzelm@12085
   721
fun locale_prefix None f thy = f thy
wenzelm@12085
   722
  | locale_prefix (Some (loc, _)) f thy = thy |> Theory.add_path loc |> f |>> Theory.parent_path;
wenzelm@12085
   723
wenzelm@12065
   724
fun locale_store_thm _ None _ = I
wenzelm@12065
   725
  | locale_store_thm name (Some (loc, atts)) th = Locale.store_thm loc ((name, th), atts);
wenzelm@12065
   726
wenzelm@6950
   727
fun finish_global state =
wenzelm@5820
   728
  let
wenzelm@12065
   729
    fun export inner outer th =
wenzelm@12065
   730
      (case Seq.pull (ProofContext.export false inner outer th) of
wenzelm@12065
   731
        Some (th', _) => th' |> Drule.local_standard
wenzelm@12065
   732
      | None => raise STATE ("Internal failure of theorem export", state));
wenzelm@12010
   733
wenzelm@12065
   734
    val (goal_ctxt, (((kind, name, t), (_, raw_thm)), _)) = current_goal state;
wenzelm@12065
   735
    val locale_ctxt = context_of (state |> close_block);
wenzelm@12065
   736
    val theory_ctxt = context_of (state |> close_block |> close_block);
wenzelm@5820
   737
wenzelm@12065
   738
    val locale_result = prep_result state t raw_thm |> export goal_ctxt locale_ctxt;
wenzelm@12065
   739
    val result = locale_result |> export locale_ctxt theory_ctxt;
wenzelm@12065
   740
wenzelm@12065
   741
    val (kname, atts, locale) =
wenzelm@12065
   742
      (case kind of Theorem (s, locale, atts) => (s, atts @ [Drule.kind s], locale)
wenzelm@6932
   743
      | _ => err_malformed "finish_global" state);
wenzelm@12065
   744
    val (thy', result') =
wenzelm@12065
   745
      theory_of state
wenzelm@12085
   746
      |> locale_prefix locale (PureThy.store_thm ((name, result), atts))
wenzelm@12065
   747
      |>> locale_store_thm name locale locale_result;
wenzelm@12065
   748
  in (thy', {kind = kname, name = name, thm = result'}) end;
wenzelm@5820
   749
wenzelm@10553
   750
(*note: clients should inspect first result only, as backtracking may destroy theory*)
wenzelm@6950
   751
fun global_qed finalize state =
wenzelm@5820
   752
  state
wenzelm@6932
   753
  |> end_proof true
wenzelm@6871
   754
  |> finalize
wenzelm@12065
   755
  |> Seq.map finish_global;
wenzelm@5820
   756
wenzelm@5820
   757
wenzelm@6896
   758
wenzelm@6896
   759
(** blocks **)
wenzelm@6896
   760
wenzelm@6896
   761
(* begin_block *)
wenzelm@6896
   762
wenzelm@6896
   763
fun begin_block state =
wenzelm@6896
   764
  state
wenzelm@6896
   765
  |> assert_forward
wenzelm@6896
   766
  |> new_block
wenzelm@6896
   767
  |> open_block;
wenzelm@6896
   768
wenzelm@6896
   769
wenzelm@6896
   770
(* end_block *)
wenzelm@6896
   771
wenzelm@6896
   772
fun end_block state =
wenzelm@6896
   773
  state
wenzelm@6896
   774
  |> assert_forward
wenzelm@6896
   775
  |> close_block
wenzelm@6896
   776
  |> assert_current_goal false
wenzelm@6896
   777
  |> close_block
wenzelm@6932
   778
  |> transfer_facts state;
wenzelm@6896
   779
wenzelm@6896
   780
wenzelm@6896
   781
(* next_block *)
wenzelm@6896
   782
wenzelm@6896
   783
fun next_block state =
wenzelm@6896
   784
  state
wenzelm@6896
   785
  |> assert_forward
wenzelm@6896
   786
  |> close_block
wenzelm@8718
   787
  |> new_block
wenzelm@8718
   788
  |> reset_facts;
wenzelm@6896
   789
wenzelm@6896
   790
wenzelm@5820
   791
end;
wenzelm@8152
   792
wenzelm@8152
   793
structure BasicProof: BASIC_PROOF = Proof;
wenzelm@8152
   794
open BasicProof;