src/Pure/Isar/proof.ML
author wenzelm
Thu Jul 13 11:39:03 2000 +0200 (2000-07-13)
changeset 9292 c5875175751f
parent 9196 1f6f66fe777a
child 9469 8058d285b1cd
permissions -rw-r--r--
invoke_case: bind_skolem;
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@7011
    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@7665
    29
  val get_goal: state -> term * (thm list * thm)
wenzelm@6091
    30
  val goal_facts: (state -> thm list) -> state -> state
wenzelm@5820
    31
  val use_facts: state -> state
wenzelm@5820
    32
  val reset_facts: state -> state
wenzelm@6891
    33
  val assert_forward: state -> state
wenzelm@5820
    34
  val assert_backward: state -> state
wenzelm@8206
    35
  val assert_no_chain: state -> state
wenzelm@5820
    36
  val enter_forward: state -> state
wenzelm@6982
    37
  val show_hyps: bool ref
wenzelm@6982
    38
  val pretty_thm: thm -> Pretty.T
wenzelm@7412
    39
  val pretty_thms: thm list -> Pretty.T
wenzelm@6529
    40
  val verbose: bool ref
wenzelm@7201
    41
  val print_state: int -> state -> unit
wenzelm@6776
    42
  val level: state -> int
wenzelm@5820
    43
  type method
wenzelm@6848
    44
  val method: (thm list -> tactic) -> method
wenzelm@8374
    45
  val method_cases: (thm list -> thm -> (thm * (string * RuleCases.T) list) Seq.seq) -> method
wenzelm@5820
    46
  val refine: (context -> method) -> state -> state Seq.seq
wenzelm@8234
    47
  val refine_end: (context -> method) -> state -> state Seq.seq
wenzelm@6932
    48
  val export_thm: context -> thm -> thm
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@7271
    60
  val assm: (int -> tactic) * (int -> tactic)
wenzelm@7271
    61
    -> (string * context attribute list * (string * (string list * string list)) list) list
wenzelm@7271
    62
    -> state -> state
wenzelm@7271
    63
  val assm_i: (int -> tactic) * (int -> tactic)
wenzelm@7271
    64
    -> (string * context attribute list * (term * (term list * term list)) list) list
wenzelm@6932
    65
    -> state -> state
wenzelm@7271
    66
  val assume: (string * context attribute list * (string * (string list * string list)) list) list
wenzelm@7271
    67
    -> state -> state
wenzelm@7271
    68
  val assume_i: (string * context attribute list * (term * (term list * term list)) list) list
wenzelm@6932
    69
    -> state -> state
wenzelm@7271
    70
  val presume: (string * context attribute list * (string * (string list * string list)) list) list
wenzelm@6932
    71
    -> state -> state
wenzelm@7271
    72
  val presume_i: (string * context attribute list * (term * (term list * term list)) list) list
wenzelm@6932
    73
    -> state -> state
wenzelm@8450
    74
  val invoke_case: string * context attribute list -> state -> state
wenzelm@6932
    75
  val theorem: bstring -> theory attribute list -> string * (string list * string list)
wenzelm@6932
    76
    -> theory -> state
wenzelm@6932
    77
  val theorem_i: bstring -> theory attribute list -> term * (term list * term list)
wenzelm@6932
    78
    -> theory -> state
wenzelm@6932
    79
  val lemma: bstring -> theory attribute list -> string * (string list * string list)
wenzelm@6932
    80
    -> theory -> state
wenzelm@6932
    81
  val lemma_i: bstring -> theory attribute list -> term * (term list * term list)
wenzelm@6932
    82
    -> theory -> state
wenzelm@5820
    83
  val chain: state -> state
wenzelm@6091
    84
  val from_facts: thm list -> state -> state
wenzelm@7176
    85
  val show: (state -> state Seq.seq) -> string -> context attribute list
wenzelm@7176
    86
    -> string * (string list * string list) -> state -> state
wenzelm@7176
    87
  val show_i: (state -> state Seq.seq) -> string -> context attribute list
wenzelm@7176
    88
    -> term * (term list * term list) -> state -> state
wenzelm@7176
    89
  val have: (state -> state Seq.seq) -> string -> context attribute list
wenzelm@7176
    90
    -> string * (string list * string list) -> state -> state
wenzelm@7176
    91
  val have_i: (state -> state Seq.seq) -> string -> context attribute list
wenzelm@7176
    92
    -> term * (term list * term list) -> state -> state
wenzelm@6404
    93
  val at_bottom: state -> bool
wenzelm@6982
    94
  val local_qed: (state -> state Seq.seq)
wenzelm@6982
    95
    -> ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit) -> state -> state Seq.seq
wenzelm@6950
    96
  val global_qed: (state -> state Seq.seq) -> state
wenzelm@6950
    97
    -> (theory * {kind: string, name: string, thm: thm}) Seq.seq
wenzelm@6896
    98
  val begin_block: state -> state
wenzelm@6932
    99
  val end_block: state -> state Seq.seq
wenzelm@6896
   100
  val next_block: state -> state
wenzelm@5820
   101
end;
wenzelm@5820
   102
wenzelm@8152
   103
signature PRIVATE_PROOF =
wenzelm@5820
   104
sig
wenzelm@5820
   105
  include PROOF
wenzelm@5820
   106
  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> state -> state
wenzelm@5820
   107
end;
wenzelm@5820
   108
wenzelm@8152
   109
structure Proof: PRIVATE_PROOF =
wenzelm@5820
   110
struct
wenzelm@5820
   111
wenzelm@5820
   112
wenzelm@5820
   113
(** proof state **)
wenzelm@5820
   114
wenzelm@5820
   115
type context = ProofContext.context;
wenzelm@5820
   116
wenzelm@5820
   117
wenzelm@5820
   118
(* type goal *)
wenzelm@5820
   119
wenzelm@5820
   120
datatype kind =
wenzelm@5820
   121
  Theorem of theory attribute list |    (*top-level theorem*)
wenzelm@5820
   122
  Lemma of theory attribute list |      (*top-level lemma*)
wenzelm@5820
   123
  Goal of context attribute list |      (*intermediate result, solving subgoal*)
wenzelm@5820
   124
  Aux of context attribute list ;       (*intermediate result*)
wenzelm@5820
   125
wenzelm@5820
   126
val kind_name =
wenzelm@6001
   127
  fn Theorem _ => "theorem" | Lemma _ => "lemma" | Goal _ => "show" | Aux _ => "have";
wenzelm@5820
   128
wenzelm@5820
   129
type goal =
wenzelm@6932
   130
 (kind *        (*result kind*)
wenzelm@6932
   131
  string *      (*result name*)
wenzelm@6932
   132
  term) *       (*result statement*)
wenzelm@6932
   133
 (thm list *    (*use facts*)
wenzelm@6932
   134
  thm);         (*goal: subgoals ==> statement*)
wenzelm@5820
   135
wenzelm@5820
   136
wenzelm@5820
   137
(* type mode *)
wenzelm@5820
   138
wenzelm@5820
   139
datatype mode = Forward | ForwardChain | Backward;
wenzelm@7201
   140
val mode_name = (fn Forward => "state" | ForwardChain => "chain" | Backward => "prove");
wenzelm@5820
   141
wenzelm@5820
   142
wenzelm@5820
   143
(* datatype state *)
wenzelm@5820
   144
wenzelm@7176
   145
datatype node =
wenzelm@7176
   146
  Node of
wenzelm@7176
   147
   {context: context,
wenzelm@7176
   148
    facts: thm list option,
wenzelm@7176
   149
    mode: mode,
wenzelm@7176
   150
    goal: (goal * (state -> state Seq.seq)) option}
wenzelm@7176
   151
and state =
wenzelm@5820
   152
  State of
wenzelm@5820
   153
    node *              (*current*)
wenzelm@5820
   154
    node list;          (*parents wrt. block structure*)
wenzelm@5820
   155
wenzelm@7176
   156
fun make_node (context, facts, mode, goal) =
wenzelm@7176
   157
  Node {context = context, facts = facts, mode = mode, goal = goal};
wenzelm@7176
   158
wenzelm@7176
   159
wenzelm@5820
   160
exception STATE of string * state;
wenzelm@5820
   161
wenzelm@5820
   162
fun err_malformed name state =
wenzelm@5820
   163
  raise STATE (name ^ ": internal error -- malformed proof state", state);
wenzelm@5820
   164
wenzelm@6871
   165
fun check_result msg state sq =
wenzelm@6871
   166
  (case Seq.pull sq of
wenzelm@6871
   167
    None => raise STATE (msg, state)
wenzelm@6871
   168
  | Some s_sq => Seq.cons s_sq);
wenzelm@6871
   169
wenzelm@5820
   170
wenzelm@7176
   171
fun map_current f (State (Node {context, facts, mode, goal}, nodes)) =
wenzelm@5820
   172
  State (make_node (f (context, facts, mode, goal)), nodes);
wenzelm@5820
   173
wenzelm@5820
   174
fun init_state thy =
wenzelm@5875
   175
  State (make_node (ProofContext.init thy, None, Forward, None), []);
wenzelm@5820
   176
wenzelm@5820
   177
wenzelm@5820
   178
wenzelm@5820
   179
(** basic proof state operations **)
wenzelm@5820
   180
wenzelm@5820
   181
(* context *)
wenzelm@5820
   182
wenzelm@7176
   183
fun context_of (State (Node {context, ...}, _)) = context;
wenzelm@5820
   184
val theory_of = ProofContext.theory_of o context_of;
wenzelm@5820
   185
val sign_of = ProofContext.sign_of o context_of;
wenzelm@5820
   186
wenzelm@5820
   187
fun map_context f = map_current (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@5820
   188
wenzelm@7176
   189
fun map_context_result f (state as State (Node {context, facts, mode, goal}, nodes)) =
wenzelm@5820
   190
  let val (context', result) = f context
wenzelm@5820
   191
  in (State (make_node (context', facts, mode, goal), nodes), result) end;
wenzelm@5820
   192
wenzelm@5820
   193
wenzelm@5820
   194
fun put_data kind f = map_context o ProofContext.put_data kind f;
wenzelm@7924
   195
val warn_extra_tfrees = map_context o ProofContext.warn_extra_tfrees o context_of;
wenzelm@6790
   196
val auto_bind_goal = map_context o ProofContext.auto_bind_goal;
wenzelm@6798
   197
val auto_bind_facts = map_context oo ProofContext.auto_bind_facts;
wenzelm@6091
   198
val put_thms = map_context o ProofContext.put_thms;
wenzelm@6091
   199
val put_thmss = map_context o ProofContext.put_thmss;
wenzelm@7605
   200
val reset_thms = map_context o ProofContext.reset_thms;
wenzelm@6932
   201
val assumptions = ProofContext.assumptions o context_of;
wenzelm@8374
   202
val get_case = ProofContext.get_case o context_of;
wenzelm@5820
   203
wenzelm@5820
   204
wenzelm@5820
   205
(* facts *)
wenzelm@5820
   206
wenzelm@7176
   207
fun the_facts (State (Node {facts = Some facts, ...}, _)) = facts
wenzelm@5820
   208
  | the_facts state = raise STATE ("No current facts available", state);
wenzelm@5820
   209
wenzelm@6848
   210
fun assert_facts state = (the_facts state; state);
wenzelm@7176
   211
fun get_facts (State (Node {facts, ...}, _)) = facts;
wenzelm@6848
   212
wenzelm@7605
   213
wenzelm@7605
   214
val thisN = "this";
wenzelm@7605
   215
wenzelm@5820
   216
fun put_facts facts state =
wenzelm@5820
   217
  state
wenzelm@5820
   218
  |> map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal))
wenzelm@7605
   219
  |> (case facts of None => reset_thms thisN | Some ths => put_thms (thisN, ths));
wenzelm@5820
   220
wenzelm@5820
   221
val reset_facts = put_facts None;
wenzelm@5820
   222
wenzelm@9196
   223
fun these_factss (state, named_factss) =
wenzelm@5820
   224
  state
wenzelm@9196
   225
  |> put_thmss named_factss
wenzelm@9196
   226
  |> put_facts (Some (flat (map #2 named_factss)));
wenzelm@5820
   227
wenzelm@5820
   228
wenzelm@5820
   229
(* goal *)
wenzelm@5820
   230
wenzelm@7176
   231
fun find_goal i (state as State (Node {goal = Some goal, ...}, _)) = (context_of state, (i, goal))
wenzelm@7176
   232
  | find_goal i (State (Node {goal = None, ...}, node :: nodes)) =
wenzelm@5820
   233
      find_goal (i + 1) (State (node, nodes))
wenzelm@5820
   234
  | find_goal _ (state as State (_, [])) = err_malformed "find_goal" state;
wenzelm@5820
   235
wenzelm@7176
   236
fun get_goal state =
wenzelm@7665
   237
  let val (_, (_, (((_, _, t), goal), _))) = find_goal 0 state
wenzelm@7665
   238
  in (t, goal) end;
wenzelm@7176
   239
wenzelm@5820
   240
fun put_goal goal = map_current (fn (ctxt, facts, mode, _) => (ctxt, facts, mode, goal));
wenzelm@5820
   241
wenzelm@8374
   242
fun map_goal f g (State (Node {context, facts, mode, goal = Some goal}, nodes)) =
wenzelm@8374
   243
      State (make_node (f context, facts, mode, Some (g goal)), nodes)
wenzelm@8374
   244
  | map_goal f g (State (nd, node :: nodes)) =
wenzelm@8374
   245
      let val State (node', nodes') = map_goal f g (State (node, nodes))
wenzelm@8374
   246
      in map_context f (State (nd, node' :: nodes')) end
wenzelm@8374
   247
  | map_goal _ _ state = state;
wenzelm@5820
   248
wenzelm@5820
   249
fun goal_facts get state =
wenzelm@5820
   250
  state
wenzelm@8374
   251
  |> map_goal I (fn ((result, (_, thm)), f) => ((result, (get state, thm)), f));
wenzelm@5820
   252
wenzelm@5820
   253
fun use_facts state =
wenzelm@5820
   254
  state
wenzelm@5820
   255
  |> goal_facts the_facts
wenzelm@5820
   256
  |> reset_facts;
wenzelm@5820
   257
wenzelm@5820
   258
wenzelm@5820
   259
(* mode *)
wenzelm@5820
   260
wenzelm@7176
   261
fun get_mode (State (Node {mode, ...}, _)) = mode;
wenzelm@5820
   262
fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal));
wenzelm@5820
   263
wenzelm@5820
   264
val enter_forward = put_mode Forward;
wenzelm@5820
   265
val enter_forward_chain = put_mode ForwardChain;
wenzelm@5820
   266
val enter_backward = put_mode Backward;
wenzelm@5820
   267
wenzelm@5820
   268
fun assert_mode pred state =
wenzelm@5820
   269
  let val mode = get_mode state in
wenzelm@5820
   270
    if pred mode then state
wenzelm@9196
   271
    else raise STATE ("Illegal application of proof command in " ^ quote (mode_name mode) ^ " mode", state)
wenzelm@5820
   272
  end;
wenzelm@5820
   273
wenzelm@5820
   274
fun is_chain state = get_mode state = ForwardChain;
wenzelm@5820
   275
val assert_forward = assert_mode (equal Forward);
wenzelm@5820
   276
val assert_forward_or_chain = assert_mode (equal Forward orf equal ForwardChain);
wenzelm@5820
   277
val assert_backward = assert_mode (equal Backward);
wenzelm@8206
   278
val assert_no_chain = assert_mode (not_equal ForwardChain);
wenzelm@5820
   279
wenzelm@5820
   280
wenzelm@5820
   281
(* blocks *)
wenzelm@5820
   282
wenzelm@6776
   283
fun level (State (_, nodes)) = length nodes;
wenzelm@6776
   284
wenzelm@5820
   285
fun open_block (State (node, nodes)) = State (node, node :: nodes);
wenzelm@5820
   286
wenzelm@5820
   287
fun new_block state =
wenzelm@5820
   288
  state
wenzelm@5820
   289
  |> open_block
wenzelm@5820
   290
  |> put_goal None;
wenzelm@5820
   291
wenzelm@7487
   292
fun close_block (state as State (_, node :: nodes)) = State (node, nodes)
wenzelm@5820
   293
  | close_block state = raise STATE ("Unbalanced block parentheses", state);
wenzelm@5820
   294
wenzelm@5820
   295
wenzelm@5820
   296
wenzelm@5820
   297
(** print_state **)
wenzelm@5820
   298
wenzelm@6982
   299
val show_hyps = ProofContext.show_hyps;
wenzelm@6982
   300
val pretty_thm = ProofContext.pretty_thm;
wenzelm@6982
   301
wenzelm@7412
   302
fun pretty_thms [th] = pretty_thm th
wenzelm@7580
   303
  | pretty_thms ths = Pretty.blk (0, Pretty.fbreaks (map pretty_thm ths));
wenzelm@7412
   304
wenzelm@7412
   305
wenzelm@6529
   306
val verbose = ProofContext.verbose;
wenzelm@6529
   307
wenzelm@8462
   308
fun pretty_facts _ None = []
wenzelm@8462
   309
  | pretty_facts s (Some ths) =
wenzelm@8462
   310
      [Pretty.big_list (s ^ "this:") (map pretty_thm ths), Pretty.str ""];
wenzelm@6756
   311
wenzelm@7201
   312
fun print_state nr (state as State (Node {context, facts, mode, goal = _}, nodes)) =
wenzelm@5820
   313
  let
wenzelm@5945
   314
    val ref (_, _, begin_goal) = Goals.current_goals_markers;
wenzelm@5820
   315
wenzelm@5820
   316
    fun levels_up 0 = ""
wenzelm@7575
   317
      | levels_up 1 = ", 1 level up"
wenzelm@7575
   318
      | levels_up i = ", " ^ string_of_int i ^ " levels up";
wenzelm@5820
   319
wenzelm@8462
   320
    fun pretty_goal (_, (i, (((kind, name, _), (goal_facts, thm)), _))) =
wenzelm@8462
   321
      pretty_facts "using "
wenzelm@8462
   322
        (if mode <> Backward orelse null goal_facts then None else Some goal_facts) @
wenzelm@8462
   323
      [Pretty.str ("goal (" ^ kind_name kind ^ (if name = "" then "" else " " ^ name) ^
wenzelm@8462
   324
        levels_up (i div 2) ^ "):")] @
wenzelm@8462
   325
      Locale.pretty_goals_marker begin_goal (! goals_limit) thm;
wenzelm@6848
   326
wenzelm@8462
   327
    val ctxt_prts =
wenzelm@8462
   328
      if ! verbose orelse mode = Forward then ProofContext.pretty_context context
wenzelm@8462
   329
      else if mode = Backward then ProofContext.pretty_prems context
wenzelm@7575
   330
      else [];
wenzelm@8462
   331
wenzelm@8462
   332
    val prts =
wenzelm@8582
   333
     [Pretty.str ("proof (" ^ mode_name mode ^ "): step " ^ string_of_int nr ^
wenzelm@8561
   334
        (if ! verbose then ", depth " ^ string_of_int (length nodes div 2)
wenzelm@8561
   335
        else "")), Pretty.str ""] @
wenzelm@8462
   336
     (if null ctxt_prts then [] else ctxt_prts @ [Pretty.str ""]) @
wenzelm@8462
   337
     (if ! verbose orelse mode = Forward then
wenzelm@8462
   338
       (pretty_facts "" facts @ pretty_goal (find_goal 0 state))
wenzelm@8462
   339
      else if mode = ForwardChain then pretty_facts "picking " facts
wenzelm@8462
   340
      else pretty_goal (find_goal 0 state))
wenzelm@8462
   341
  in Pretty.writeln (Pretty.chunks prts) end;
wenzelm@5820
   342
wenzelm@5820
   343
wenzelm@5820
   344
wenzelm@5820
   345
(** proof steps **)
wenzelm@5820
   346
wenzelm@5820
   347
(* datatype method *)
wenzelm@5820
   348
wenzelm@8374
   349
datatype method =
wenzelm@8374
   350
  Method of thm list -> thm -> (thm * (string * RuleCases.T) list) Seq.seq;
wenzelm@8374
   351
wenzelm@8374
   352
fun method tac = Method (fn facts => fn st => Seq.map (rpair []) (tac facts st));
wenzelm@8374
   353
val method_cases = Method;
wenzelm@5820
   354
wenzelm@5820
   355
wenzelm@5820
   356
(* refine goal *)
wenzelm@5820
   357
wenzelm@8234
   358
local
wenzelm@8234
   359
wenzelm@5820
   360
fun check_sign sg state =
wenzelm@5820
   361
  if Sign.subsig (sg, sign_of state) then state
wenzelm@5820
   362
  else raise STATE ("Bad signature of result: " ^ Sign.str_of_sg sg, state);
wenzelm@5820
   363
wenzelm@8234
   364
fun gen_refine current_context meth_fun state =
wenzelm@6848
   365
  let
wenzelm@8234
   366
    val (goal_ctxt, (_, ((result, (facts, thm)), f))) = find_goal 0 state;
wenzelm@8234
   367
    val Method meth = meth_fun (if current_context then context_of state else goal_ctxt);
wenzelm@5820
   368
wenzelm@8374
   369
    fun refn (thm', cases) =
wenzelm@6848
   370
      state
wenzelm@6848
   371
      |> check_sign (Thm.sign_of_thm thm')
wenzelm@8374
   372
      |> map_goal (ProofContext.add_cases cases) (K ((result, (facts, thm')), f));
wenzelm@6848
   373
  in Seq.map refn (meth facts thm) end;
wenzelm@5820
   374
wenzelm@8234
   375
in
wenzelm@8234
   376
wenzelm@8234
   377
val refine = gen_refine true;
wenzelm@8234
   378
val refine_end = gen_refine false;
wenzelm@8234
   379
wenzelm@8234
   380
end;
wenzelm@8234
   381
wenzelm@5820
   382
wenzelm@6932
   383
(* export results *)
wenzelm@6932
   384
wenzelm@6932
   385
fun RANGE [] _ = all_tac
wenzelm@6932
   386
  | RANGE (tac :: tacs) i = RANGE tacs (i + 1) THEN tac i;
wenzelm@6932
   387
wenzelm@8152
   388
fun FINDGOAL tac st =
wenzelm@8374
   389
  let fun find (i, n) = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1, n))
wenzelm@8374
   390
  in find (1, Thm.nprems_of st) st end;
wenzelm@8152
   391
wenzelm@8166
   392
fun HEADGOAL tac = tac 1;
wenzelm@8166
   393
wenzelm@6982
   394
fun export_goal print_rule raw_rule inner state =
wenzelm@6932
   395
  let
wenzelm@7176
   396
    val (outer, (_, ((result, (facts, thm)), f))) = find_goal 0 state;
wenzelm@8617
   397
    val (exp, tacs) = ProofContext.export_wrt inner (Some outer);
wenzelm@6932
   398
    val rule = exp raw_rule;
wenzelm@6982
   399
    val _ = print_rule rule;
wenzelm@8152
   400
    val thmq = FINDGOAL (Tactic.rtac rule THEN' RANGE (map #1 tacs)) thm;
wenzelm@8374
   401
  in Seq.map (fn th => map_goal I (K ((result, (facts, th)), f)) state) thmq end;
wenzelm@6932
   402
wenzelm@6932
   403
wenzelm@6932
   404
fun export_thm inner thm =
wenzelm@8617
   405
  let val (exp, tacs) = ProofContext.export_wrt inner None in
wenzelm@6932
   406
    (case Seq.chop (2, RANGE (map #2 tacs) 1 (exp thm)) of
wenzelm@6932
   407
      ([thm'], _) => thm'
wenzelm@6932
   408
    | ([], _) => raise THM ("export: failed", 0, [thm])
wenzelm@6932
   409
    | _ => raise THM ("export: more than one result", 0, [thm]))
wenzelm@6932
   410
  end;
wenzelm@6932
   411
wenzelm@6932
   412
fun transfer_facts inner_state state =
wenzelm@6932
   413
  (case get_facts inner_state of
wenzelm@6932
   414
    None => Seq.single (reset_facts state)
wenzelm@8617
   415
  | Some thms =>
wenzelm@8617
   416
      let
wenzelm@8617
   417
        val (exp, tacs) =
wenzelm@8617
   418
          ProofContext.export_wrt (context_of inner_state) (Some (context_of state));
wenzelm@8617
   419
        val thmqs = map (RANGE (map #2 tacs) 1 o exp) thms;
wenzelm@8617
   420
      in Seq.map (fn ths => put_facts (Some ths) state) (Seq.commute thmqs) end);
wenzelm@6932
   421
wenzelm@6932
   422
wenzelm@6932
   423
(* prepare result *)
wenzelm@6932
   424
wenzelm@6932
   425
fun prep_result state t raw_thm =
wenzelm@5820
   426
  let
wenzelm@5820
   427
    val ctxt = context_of state;
wenzelm@5820
   428
    fun err msg = raise STATE (msg, state);
wenzelm@5820
   429
wenzelm@5820
   430
    val ngoals = Thm.nprems_of raw_thm;
wenzelm@5820
   431
    val _ =
wenzelm@5820
   432
      if ngoals = 0 then ()
wenzelm@5820
   433
      else (Locale.print_goals ngoals raw_thm; err (string_of_int ngoals ^ " unsolved goal(s)!"));
wenzelm@5820
   434
wenzelm@6932
   435
    val thm = raw_thm RS Drule.rev_triv_goal;
wenzelm@8617
   436
    val {hyps, prop, sign, ...} = Thm.rep_thm thm;
wenzelm@5820
   437
    val tsig = Sign.tsig_of sign;
wenzelm@6932
   438
wenzelm@6932
   439
    val casms = map #1 (assumptions state);
wenzelm@6996
   440
    val bad_hyps = Library.gen_rems Term.aconv (hyps, map (Thm.term_of o Drule.mk_cgoal) casms);
wenzelm@5820
   441
  in
wenzelm@6932
   442
    if not (null bad_hyps) then
wenzelm@6932
   443
      err ("Additional hypotheses:\n" ^ cat_lines (map (Sign.string_of_term sign) bad_hyps))
wenzelm@7605
   444
    else if not (t aconv prop) then
wenzelm@7605
   445
      err ("Proved a different theorem: " ^ Sign.string_of_term sign prop)
wenzelm@8617
   446
    else thm
wenzelm@5820
   447
  end;
wenzelm@5820
   448
wenzelm@5820
   449
wenzelm@5820
   450
wenzelm@5820
   451
(*** structured proof commands ***)
wenzelm@5820
   452
wenzelm@5820
   453
(** context **)
wenzelm@5820
   454
wenzelm@5820
   455
(* bind *)
wenzelm@5820
   456
wenzelm@5820
   457
fun gen_bind f x state =
wenzelm@5820
   458
  state
wenzelm@5820
   459
  |> assert_forward
wenzelm@5820
   460
  |> map_context (f x)
wenzelm@5820
   461
  |> reset_facts;
wenzelm@5820
   462
wenzelm@8617
   463
val match_bind = gen_bind (ProofContext.match_bind false);
wenzelm@8617
   464
val match_bind_i = gen_bind (ProofContext.match_bind_i false);
wenzelm@8617
   465
val let_bind = gen_bind (ProofContext.match_bind true);
wenzelm@8617
   466
val let_bind_i = gen_bind (ProofContext.match_bind_i true);
wenzelm@5820
   467
wenzelm@5820
   468
wenzelm@9196
   469
(* have_thmss etc. *)
wenzelm@5820
   470
wenzelm@9196
   471
fun have_thmss args state =
wenzelm@5820
   472
  state
wenzelm@5820
   473
  |> assert_forward
wenzelm@9196
   474
  |> map_context_result (ProofContext.have_thmss args)
wenzelm@9196
   475
  |> these_factss;
wenzelm@5820
   476
wenzelm@9196
   477
fun simple_have_thms name thms = have_thmss [((name, []), [(thms, [])])];
wenzelm@9196
   478
wenzelm@9196
   479
fun with_thmss args state =
wenzelm@9196
   480
  let val state' = state |> have_thmss args
wenzelm@9196
   481
  in state' |> simple_have_thms "" (the_facts state' @ the_facts state) end;
wenzelm@6876
   482
wenzelm@5820
   483
wenzelm@5820
   484
(* fix *)
wenzelm@5820
   485
wenzelm@5820
   486
fun gen_fix f xs state =
wenzelm@5820
   487
  state
wenzelm@5820
   488
  |> assert_forward
wenzelm@5820
   489
  |> map_context (f xs)
wenzelm@5820
   490
  |> reset_facts;
wenzelm@5820
   491
wenzelm@5820
   492
val fix = gen_fix ProofContext.fix;
wenzelm@5820
   493
val fix_i = gen_fix ProofContext.fix_i;
wenzelm@5820
   494
wenzelm@5820
   495
wenzelm@5820
   496
(* assume *)
wenzelm@5820
   497
wenzelm@7271
   498
local
wenzelm@7271
   499
wenzelm@7271
   500
fun gen_assume f tac args state =
wenzelm@5820
   501
  state
wenzelm@5820
   502
  |> assert_forward
wenzelm@7478
   503
  |> map_context_result (f tac args)
wenzelm@7271
   504
  |> (fn (st, (factss, prems)) =>
wenzelm@9196
   505
    these_factss (st, factss)
wenzelm@9196
   506
    |> put_thms ("prems", prems));
wenzelm@6932
   507
wenzelm@6996
   508
val hard_asm_tac = Tactic.etac Drule.triv_goal;
wenzelm@8542
   509
val soft_asm_tac = Tactic.rtac Drule.triv_goal
wenzelm@8617
   510
  THEN' Tactic.rtac asm_rl;     (* FIXME hack to norm goal *)
wenzelm@6996
   511
wenzelm@7271
   512
in
wenzelm@7271
   513
wenzelm@7271
   514
val assm = gen_assume ProofContext.assume;
wenzelm@7271
   515
val assm_i = gen_assume ProofContext.assume_i;
wenzelm@6996
   516
val assume = assm (hard_asm_tac, soft_asm_tac);
wenzelm@6996
   517
val assume_i = assm_i (hard_asm_tac, soft_asm_tac);
wenzelm@6996
   518
val presume = assm (soft_asm_tac, soft_asm_tac);
wenzelm@6996
   519
val presume_i = assm_i (soft_asm_tac, soft_asm_tac);
wenzelm@5820
   520
wenzelm@7271
   521
end;
wenzelm@7271
   522
wenzelm@5820
   523
wenzelm@8374
   524
(* invoke_case *)
wenzelm@8374
   525
wenzelm@8450
   526
fun invoke_case (name, atts) state =
wenzelm@9292
   527
  let
wenzelm@9292
   528
    val (vars, props) = get_case state name;
wenzelm@9292
   529
    val bind_skolem = ProofContext.bind_skolem (context_of state) (map #1 vars);
wenzelm@9292
   530
  in
wenzelm@8374
   531
    state
wenzelm@8374
   532
    |> fix_i (map (fn (x, T) => ([x], Some T)) vars)
wenzelm@9292
   533
    |> assume_i [(name, atts, map (fn t => (t, ([], []))) (map bind_skolem props))]
wenzelm@8374
   534
  end;
wenzelm@8374
   535
wenzelm@8374
   536
wenzelm@5820
   537
wenzelm@5820
   538
(** goals **)
wenzelm@5820
   539
wenzelm@5820
   540
(* forward chaining *)
wenzelm@5820
   541
wenzelm@5820
   542
fun chain state =
wenzelm@5820
   543
  state
wenzelm@5820
   544
  |> assert_forward
wenzelm@6848
   545
  |> assert_facts
wenzelm@5820
   546
  |> enter_forward_chain;
wenzelm@5820
   547
wenzelm@5820
   548
fun from_facts facts state =
wenzelm@5820
   549
  state
wenzelm@5820
   550
  |> put_facts (Some facts)
wenzelm@5820
   551
  |> chain;
wenzelm@5820
   552
wenzelm@5820
   553
wenzelm@5820
   554
(* setup goals *)
wenzelm@5820
   555
wenzelm@8991
   556
val antN = "antecedent";
wenzelm@8991
   557
wenzelm@7176
   558
fun setup_goal opt_block prepp kind after_qed name atts raw_propp state =
wenzelm@5820
   559
  let
wenzelm@8617
   560
    val (state', (prop, gen_binds)) =
wenzelm@5936
   561
      state
wenzelm@5936
   562
      |> assert_forward_or_chain
wenzelm@5936
   563
      |> enter_forward
wenzelm@8617
   564
      |> opt_block
wenzelm@8095
   565
      |> map_context_result (fn ct => prepp (ct, raw_propp));
wenzelm@6932
   566
    val cprop = Thm.cterm_of (sign_of state') prop;
wenzelm@7556
   567
    val goal = Drule.mk_triv_goal cprop;
wenzelm@5820
   568
  in
wenzelm@5936
   569
    state'
wenzelm@8617
   570
    |> put_goal (Some (((kind atts, name, prop), ([], goal)), after_qed o map_context gen_binds))
wenzelm@9292
   571
    |> map_context (ProofContext.add_cases (RuleCases.make false goal [antN]))
wenzelm@6790
   572
    |> auto_bind_goal prop
wenzelm@5820
   573
    |> (if is_chain state then use_facts else reset_facts)
wenzelm@5820
   574
    |> new_block
wenzelm@5820
   575
    |> enter_backward
wenzelm@5820
   576
  end;
wenzelm@5820
   577
wenzelm@5820
   578
wenzelm@5820
   579
(*global goals*)
wenzelm@8095
   580
fun global_goal prepp kind name atts x thy =
wenzelm@8095
   581
  setup_goal I prepp kind Seq.single name atts x (init_state thy);
wenzelm@5820
   582
wenzelm@7928
   583
val theorem = global_goal ProofContext.bind_propp Theorem;
wenzelm@7928
   584
val theorem_i = global_goal ProofContext.bind_propp_i Theorem;
wenzelm@7928
   585
val lemma = global_goal ProofContext.bind_propp Lemma;
wenzelm@7928
   586
val lemma_i = global_goal ProofContext.bind_propp_i Lemma;
wenzelm@5820
   587
wenzelm@5820
   588
wenzelm@5820
   589
(*local goals*)
wenzelm@8095
   590
fun local_goal prepp kind f name atts args state =
wenzelm@7928
   591
  state
wenzelm@8095
   592
  |> setup_goal open_block prepp kind f name atts args
wenzelm@7928
   593
  |> warn_extra_tfrees state;
wenzelm@5820
   594
wenzelm@7928
   595
val show   = local_goal ProofContext.bind_propp Goal;
wenzelm@7928
   596
val show_i = local_goal ProofContext.bind_propp_i Goal;
wenzelm@7928
   597
val have   = local_goal ProofContext.bind_propp Aux;
wenzelm@7928
   598
val have_i = local_goal ProofContext.bind_propp_i Aux;
wenzelm@5820
   599
wenzelm@5820
   600
wenzelm@5820
   601
wenzelm@5820
   602
(** conclusions **)
wenzelm@5820
   603
wenzelm@5820
   604
(* current goal *)
wenzelm@5820
   605
wenzelm@7176
   606
fun current_goal (State (Node {context, goal = Some goal, ...}, _)) = (context, goal)
wenzelm@5820
   607
  | current_goal state = raise STATE ("No current goal!", state);
wenzelm@5820
   608
wenzelm@7176
   609
fun assert_current_goal true (state as State (Node {goal = None, ...}, _)) =
wenzelm@6404
   610
      raise STATE ("No goal in this block!", state)
wenzelm@7176
   611
  | assert_current_goal false (state as State (Node {goal = Some _, ...}, _)) =
wenzelm@6404
   612
      raise STATE ("Goal present in this block!", state)
wenzelm@6404
   613
  | assert_current_goal _ state = state;
wenzelm@5820
   614
wenzelm@5820
   615
fun assert_bottom true (state as State (_, _ :: _)) =
wenzelm@5820
   616
      raise STATE ("Not at bottom of proof!", state)
wenzelm@5820
   617
  | assert_bottom false (state as State (_, [])) =
wenzelm@5820
   618
      raise STATE ("Already at bottom of proof!", state)
wenzelm@5820
   619
  | assert_bottom _ state = state;
wenzelm@5820
   620
wenzelm@6404
   621
val at_bottom = can (assert_bottom true o close_block);
wenzelm@6404
   622
wenzelm@6932
   623
fun end_proof bot state =
wenzelm@5820
   624
  state
wenzelm@5820
   625
  |> assert_forward
wenzelm@5820
   626
  |> close_block
wenzelm@5820
   627
  |> assert_bottom bot
wenzelm@7011
   628
  |> assert_current_goal true
wenzelm@7011
   629
  |> goal_facts (K []);
wenzelm@5820
   630
wenzelm@5820
   631
wenzelm@6404
   632
(* local_qed *)
wenzelm@5820
   633
wenzelm@6982
   634
fun finish_local (print_result, print_rule) state =
wenzelm@5820
   635
  let
wenzelm@8617
   636
    val (goal_ctxt, (((kind, name, t), (_, raw_thm)), after_qed)) = current_goal state;
wenzelm@8617
   637
    val outer_state = state |> close_block;
wenzelm@8617
   638
    val outer_ctxt = context_of outer_state;
wenzelm@8617
   639
wenzelm@6932
   640
    val result = prep_result state t raw_thm;
wenzelm@5820
   641
    val (atts, opt_solve) =
wenzelm@5820
   642
      (case kind of
wenzelm@8617
   643
        Goal atts => (atts, export_goal print_rule result goal_ctxt)
wenzelm@5820
   644
      | Aux atts => (atts, Seq.single)
wenzelm@6932
   645
      | _ => err_malformed "finish_local" state);
wenzelm@5820
   646
  in
wenzelm@6731
   647
    print_result {kind = kind_name kind, name = name, thm = result};
wenzelm@8617
   648
    outer_state
wenzelm@8617
   649
    |> auto_bind_facts name [ProofContext.generalize goal_ctxt outer_ctxt t]
wenzelm@9196
   650
    |> have_thmss [((name, atts), [Thm.no_attributes
wenzelm@9196
   651
        [#1 (ProofContext.export_wrt goal_ctxt (Some outer_ctxt)) result]])]
wenzelm@5820
   652
    |> opt_solve
wenzelm@7176
   653
    |> (Seq.flat o Seq.map after_qed)
wenzelm@5820
   654
  end;
wenzelm@5820
   655
wenzelm@6982
   656
fun local_qed finalize print state =
wenzelm@6404
   657
  state
wenzelm@6932
   658
  |> end_proof false
wenzelm@6871
   659
  |> finalize
wenzelm@6982
   660
  |> (Seq.flat o Seq.map (finish_local print));
wenzelm@5820
   661
wenzelm@5820
   662
wenzelm@6404
   663
(* global_qed *)
wenzelm@5820
   664
wenzelm@6950
   665
fun finish_global state =
wenzelm@5820
   666
  let
wenzelm@7176
   667
    val (_, (((kind, name, t), (_, raw_thm)), _)) = current_goal state;   (*ignores after_qed!*)
wenzelm@8152
   668
    val result = Drule.standard (prep_result state t raw_thm);
wenzelm@5820
   669
wenzelm@5820
   670
    val atts =
wenzelm@5820
   671
      (case kind of
wenzelm@6950
   672
        Theorem atts => atts
wenzelm@6950
   673
      | Lemma atts => atts @ [Drule.tag_lemma]
wenzelm@6932
   674
      | _ => err_malformed "finish_global" state);
wenzelm@5820
   675
wenzelm@6091
   676
    val (thy', result') = PureThy.store_thm ((name, result), atts) (theory_of state);
wenzelm@6529
   677
  in (thy', {kind = kind_name kind, name = name, thm = result'}) end;
wenzelm@5820
   678
wenzelm@6871
   679
(*Note: should inspect first result only, backtracking may destroy theory*)
wenzelm@6950
   680
fun global_qed finalize state =
wenzelm@5820
   681
  state
wenzelm@6932
   682
  |> end_proof true
wenzelm@6871
   683
  |> finalize
wenzelm@6950
   684
  |> Seq.map finish_global;
wenzelm@5820
   685
wenzelm@5820
   686
wenzelm@6896
   687
wenzelm@6896
   688
(** blocks **)
wenzelm@6896
   689
wenzelm@6896
   690
(* begin_block *)
wenzelm@6896
   691
wenzelm@6896
   692
fun begin_block state =
wenzelm@6896
   693
  state
wenzelm@6896
   694
  |> assert_forward
wenzelm@6896
   695
  |> new_block
wenzelm@6896
   696
  |> open_block;
wenzelm@6896
   697
wenzelm@6896
   698
wenzelm@6896
   699
(* end_block *)
wenzelm@6896
   700
wenzelm@6896
   701
fun end_block state =
wenzelm@6896
   702
  state
wenzelm@6896
   703
  |> assert_forward
wenzelm@6896
   704
  |> close_block
wenzelm@6896
   705
  |> assert_current_goal false
wenzelm@6896
   706
  |> close_block
wenzelm@6932
   707
  |> transfer_facts state;
wenzelm@6896
   708
wenzelm@6896
   709
wenzelm@6896
   710
(* next_block *)
wenzelm@6896
   711
wenzelm@6896
   712
fun next_block state =
wenzelm@6896
   713
  state
wenzelm@6896
   714
  |> assert_forward
wenzelm@6896
   715
  |> close_block
wenzelm@8718
   716
  |> new_block
wenzelm@8718
   717
  |> reset_facts;
wenzelm@6896
   718
wenzelm@6896
   719
wenzelm@5820
   720
end;
wenzelm@8152
   721
wenzelm@8152
   722
wenzelm@8152
   723
structure BasicProof: BASIC_PROOF = Proof;
wenzelm@8152
   724
open BasicProof;