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