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