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