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