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