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