src/Pure/Isar/proof.ML
author wenzelm
Fri, 02 Jul 1999 15:05:16 +0200
changeset 6887 12b5fb35a688
parent 6876 4ae9c47f2b6b
child 6891 7bb02d03035d
permissions -rw-r--r--
generalized fixes get index 0;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/proof.ML
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
     4
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
     5
Proof states and methods.
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
     6
*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
     7
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
     8
signature PROOF =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
     9
sig
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    10
  type context
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    11
  type state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    12
  exception STATE of string * state
6871
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
    13
  val check_result: string -> state -> 'a Seq.seq -> 'a Seq.seq
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    14
  val context_of: state -> context
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    15
  val theory_of: state -> theory
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    16
  val sign_of: state -> Sign.sg
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 6030
diff changeset
    17
  val the_facts: state -> thm list
6776
55f1e6b639a4 added the_fact, level;
wenzelm
parents: 6756
diff changeset
    18
  val the_fact: state -> thm
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 6030
diff changeset
    19
  val goal_facts: (state -> thm list) -> state -> state
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    20
  val use_facts: state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    21
  val reset_facts: state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    22
  val assert_backward: state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    23
  val enter_forward: state -> state
6529
0f4c2ebc5018 improved print_state;
wenzelm
parents: 6404
diff changeset
    24
  val verbose: bool ref
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    25
  val print_state: state -> unit
6776
55f1e6b639a4 added the_fact, level;
wenzelm
parents: 6756
diff changeset
    26
  val level: state -> int
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    27
  type method
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
    28
  val method: (thm list -> tactic) -> method
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    29
  val refine: (context -> method) -> state -> state Seq.seq
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    30
  val bind: (indexname * string) list -> state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    31
  val bind_i: (indexname * term) list -> state -> state
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    32
  val match_bind: (string list * string) list -> state -> state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    33
  val match_bind_i: (term list * term) list -> state -> state
6876
4ae9c47f2b6b have_thmss: more_ths;
wenzelm
parents: 6871
diff changeset
    34
  val have_thmss: thm list -> string -> context attribute list ->
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 6030
diff changeset
    35
    (thm list * context attribute list) list -> state -> state
6876
4ae9c47f2b6b have_thmss: more_ths;
wenzelm
parents: 6871
diff changeset
    36
  val simple_have_thms: string -> thm list -> state -> state
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
    37
  val assume: (int -> tactic) -> string -> context attribute list -> (string * string list) list
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
    38
    -> state -> state
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
    39
  val assume_i: (int -> tactic) -> string -> context attribute list -> (term * term list) list
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
    40
    -> state -> state
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    41
  val fix: (string * string option) list -> state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    42
  val fix_i: (string * typ) list -> state -> state
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    43
  val theorem: bstring -> theory attribute list -> string * string list -> theory -> state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    44
  val theorem_i: bstring -> theory attribute list -> term * term list -> theory -> state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    45
  val lemma: bstring -> theory attribute list -> string * string list -> theory -> state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    46
  val lemma_i: bstring -> theory attribute list -> term * term list -> theory -> state
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    47
  val chain: state -> state
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 6030
diff changeset
    48
  val from_facts: thm list -> state -> state
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    49
  val show: string -> context attribute list -> string * string list -> state -> state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    50
  val show_i: string -> context attribute list -> term * term list -> state -> state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    51
  val have: string -> context attribute list -> string * string list -> state -> state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    52
  val have_i: string -> context attribute list -> term * term list -> state -> state
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    53
  val begin_block: state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    54
  val next_block: state -> state
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
    55
  val end_block: state -> state
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
    56
  val at_bottom: state -> bool
6731
57e761134c85 cleaned comments;
wenzelm
parents: 6683
diff changeset
    57
  val local_qed: (state -> state Seq.seq) -> ({kind: string, name: string, thm: thm} -> unit)
57e761134c85 cleaned comments;
wenzelm
parents: 6683
diff changeset
    58
    -> state -> state Seq.seq
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
    59
  val global_qed: (state -> state Seq.seq) -> bstring option
6871
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
    60
    -> theory attribute list option -> state -> (theory * {kind: string, name: string, thm: thm}) Seq.seq
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    61
end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    62
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    63
signature PROOF_PRIVATE =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    64
sig
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    65
  include PROOF
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    66
  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    67
end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    68
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    69
structure Proof: PROOF_PRIVATE =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    70
struct
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    71
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    72
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    73
(** proof state **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    74
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    75
type context = ProofContext.context;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    76
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    77
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    78
(* type goal *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    79
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    80
datatype kind =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    81
  Theorem of theory attribute list |    (*top-level theorem*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    82
  Lemma of theory attribute list |      (*top-level lemma*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    83
  Goal of context attribute list |      (*intermediate result, solving subgoal*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    84
  Aux of context attribute list ;       (*intermediate result*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    85
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    86
val kind_name =
6001
28b0a9891852 qed: kind_name (again);
wenzelm
parents: 5993
diff changeset
    87
  fn Theorem _ => "theorem" | Lemma _ => "lemma" | Goal _ => "show" | Aux _ => "have";
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    88
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    89
type goal =
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
    90
 (kind *               		(*result kind*)
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
    91
  string *                      (*result name*)
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
    92
  cterm list *			(*result assumptions*)
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
    93
  (int -> tactic) list *      	(*tactics to solve result assumptions*)
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
    94
  term) *                       (*result statement*)
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
    95
 (thm list *                    (*use facts*)
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
    96
  thm);                         (*goal: subgoals ==> statement*)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    97
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    98
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    99
(* type mode *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   100
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   101
datatype mode = Forward | ForwardChain | Backward;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   102
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   103
val mode_name =
6798
f6bc583a5776 tuned mode_name;
wenzelm
parents: 6790
diff changeset
   104
  enclose "`" "'" o
f6bc583a5776 tuned mode_name;
wenzelm
parents: 6790
diff changeset
   105
    (fn Forward => "state" | ForwardChain => "chain" | Backward => "prove");
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   106
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   107
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   108
(* type node *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   109
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   110
type node =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   111
 {context: context,
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 6030
diff changeset
   112
  facts: thm list option,
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   113
  mode: mode,
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   114
  goal: goal option};
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   115
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   116
fun make_node (context, facts, mode, goal) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   117
  {context = context, facts = facts, mode = mode, goal = goal}: node;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   118
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   119
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   120
(* datatype state *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   121
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   122
datatype state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   123
  State of
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   124
    node *              (*current*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   125
    node list;          (*parents wrt. block structure*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   126
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   127
exception STATE of string * state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   128
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   129
fun err_malformed name state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   130
  raise STATE (name ^ ": internal error -- malformed proof state", state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   131
6871
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   132
fun check_result msg state sq =
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   133
  (case Seq.pull sq of
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   134
    None => raise STATE (msg, state)
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   135
  | Some s_sq => Seq.cons s_sq);
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   136
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   137
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   138
fun map_current f (State ({context, facts, mode, goal}, nodes)) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   139
  State (make_node (f (context, facts, mode, goal)), nodes);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   140
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   141
fun init_state thy =
5875
6aae55ae3473 renamed init_context to init;
wenzelm
parents: 5820
diff changeset
   142
  State (make_node (ProofContext.init thy, None, Forward, None), []);
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   143
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   144
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   145
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   146
(** basic proof state operations **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   147
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   148
(* context *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   149
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   150
fun context_of (State ({context, ...}, _)) = context;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   151
val theory_of = ProofContext.theory_of o context_of;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   152
val sign_of = ProofContext.sign_of o context_of;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   153
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   154
fun map_context f = map_current (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   155
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   156
fun map_context_result f (state as State ({context, facts, mode, goal}, nodes)) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   157
  let val (context', result) = f context
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   158
  in (State (make_node (context', facts, mode, goal), nodes), result) end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   159
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   160
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   161
fun put_data kind f = map_context o ProofContext.put_data kind f;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   162
val declare_term = map_context o ProofContext.declare_term;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   163
val add_binds = map_context o ProofContext.add_binds_i;
6790
0a39f22f847a auto_bind_goal, auto_bind_facts;
wenzelm
parents: 6776
diff changeset
   164
val auto_bind_goal = map_context o ProofContext.auto_bind_goal;
6798
f6bc583a5776 tuned mode_name;
wenzelm
parents: 6790
diff changeset
   165
val auto_bind_facts = map_context oo ProofContext.auto_bind_facts;
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 6030
diff changeset
   166
val put_thms = map_context o ProofContext.put_thms;
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 6030
diff changeset
   167
val put_thmss = map_context o ProofContext.put_thmss;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   168
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   169
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   170
(* facts *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   171
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   172
fun the_facts (State ({facts = Some facts, ...}, _)) = facts
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   173
  | the_facts state = raise STATE ("No current facts available", state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   174
6776
55f1e6b639a4 added the_fact, level;
wenzelm
parents: 6756
diff changeset
   175
fun the_fact state =
55f1e6b639a4 added the_fact, level;
wenzelm
parents: 6756
diff changeset
   176
  (case the_facts state of
55f1e6b639a4 added the_fact, level;
wenzelm
parents: 6756
diff changeset
   177
    [fact] => fact
55f1e6b639a4 added the_fact, level;
wenzelm
parents: 6756
diff changeset
   178
  | _ => raise STATE ("Single fact expected", state));
55f1e6b639a4 added the_fact, level;
wenzelm
parents: 6756
diff changeset
   179
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   180
fun assert_facts state = (the_facts state; state);
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   181
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   182
fun put_facts facts state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   183
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   184
  |> map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal))
6798
f6bc583a5776 tuned mode_name;
wenzelm
parents: 6790
diff changeset
   185
  |> put_thms ("facts", if_none facts []);
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   186
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   187
val reset_facts = put_facts None;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   188
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   189
fun have_facts (name, facts) state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   190
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   191
  |> put_facts (Some facts)
6798
f6bc583a5776 tuned mode_name;
wenzelm
parents: 6790
diff changeset
   192
  |> put_thms (name, facts);
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   193
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   194
fun these_facts (state, ths) = have_facts ths state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   195
fun fetch_facts (State ({facts, ...}, _)) = put_facts facts;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   196
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   197
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   198
(* goal *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   199
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   200
fun find_goal i (State ({goal = Some goal, ...}, _)) = (i, goal)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   201
  | find_goal i (State ({goal = None, ...}, node :: nodes)) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   202
      find_goal (i + 1) (State (node, nodes))
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   203
  | find_goal _ (state as State (_, [])) = err_malformed "find_goal" state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   204
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   205
fun put_goal goal = map_current (fn (ctxt, facts, mode, _) => (ctxt, facts, mode, goal));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   206
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   207
fun map_goal f (State ({context, facts, mode, goal = Some goal}, nodes)) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   208
      State (make_node (context, facts, mode, Some (f goal)), nodes)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   209
  | map_goal f (State (nd, node :: nodes)) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   210
      let val State (node', nodes') = map_goal f (State (node, nodes))
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   211
      in State (nd, node' :: nodes') end
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   212
  | map_goal _ state = state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   213
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   214
fun goal_facts get state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   215
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   216
  |> map_goal (fn (result, (_, thm)) => (result, (get state, thm)));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   217
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   218
fun use_facts state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   219
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   220
  |> goal_facts the_facts
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   221
  |> reset_facts;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   222
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   223
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   224
(* mode *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   225
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   226
fun get_mode (State ({mode, ...}, _)) = mode;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   227
fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   228
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   229
val enter_forward = put_mode Forward;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   230
val enter_forward_chain = put_mode ForwardChain;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   231
val enter_backward = put_mode Backward;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   232
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   233
fun assert_mode pred state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   234
  let val mode = get_mode state in
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   235
    if pred mode then state
6731
57e761134c85 cleaned comments;
wenzelm
parents: 6683
diff changeset
   236
    else raise STATE ("Illegal application of proof command in " ^ mode_name mode ^ " mode", state)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   237
  end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   238
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   239
fun is_chain state = get_mode state = ForwardChain;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   240
val assert_forward = assert_mode (equal Forward);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   241
val assert_forward_or_chain = assert_mode (equal Forward orf equal ForwardChain);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   242
val assert_backward = assert_mode (equal Backward);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   243
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   244
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   245
(* blocks *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   246
6776
55f1e6b639a4 added the_fact, level;
wenzelm
parents: 6756
diff changeset
   247
fun level (State (_, nodes)) = length nodes;
55f1e6b639a4 added the_fact, level;
wenzelm
parents: 6756
diff changeset
   248
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   249
fun open_block (State (node, nodes)) = State (node, node :: nodes);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   250
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   251
fun new_block state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   252
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   253
  |> open_block
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   254
  |> put_goal None;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   255
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   256
fun close_block (State (_, node :: nodes)) = State (node, nodes)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   257
  | close_block state = raise STATE ("Unbalanced block parentheses", state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   258
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   259
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   260
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   261
(** print_state **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   262
6529
0f4c2ebc5018 improved print_state;
wenzelm
parents: 6404
diff changeset
   263
val verbose = ProofContext.verbose;
0f4c2ebc5018 improved print_state;
wenzelm
parents: 6404
diff changeset
   264
6756
fe6eb161df3e improved print_state;
wenzelm
parents: 6752
diff changeset
   265
fun print_facts _ None = ()
6871
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   266
  | print_facts s (Some ths) = Pretty.writeln (Pretty.big_list (s ^ " facts:")
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   267
      (map (setmp Display.show_hyps false Display.pretty_thm) ths));
6756
fe6eb161df3e improved print_state;
wenzelm
parents: 6752
diff changeset
   268
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   269
fun print_state (state as State ({context, facts, mode, goal = _}, nodes)) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   270
  let
5945
63184e276c1d print_state: use begin_goal from Goals.current_goals_markers;
wenzelm
parents: 5936
diff changeset
   271
    val ref (_, _, begin_goal) = Goals.current_goals_markers;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   272
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   273
    fun levels_up 0 = ""
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   274
      | levels_up i = " (" ^ string_of_int i ^ " levels up)";
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   275
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   276
    fun print_goal (i, ((kind, name, _, _, _), (goal_facts, thm))) =
6756
fe6eb161df3e improved print_state;
wenzelm
parents: 6752
diff changeset
   277
      (print_facts "Using" (if null goal_facts then None else Some goal_facts);
fe6eb161df3e improved print_state;
wenzelm
parents: 6752
diff changeset
   278
        writeln (kind_name kind ^ " " ^ quote name ^ levels_up (i div 2) ^ ":");
5945
63184e276c1d print_state: use begin_goal from Goals.current_goals_markers;
wenzelm
parents: 5936
diff changeset
   279
        Locale.print_goals_marker begin_goal (! goals_limit) thm);
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   280
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   281
    val ctxt_strings = ProofContext.strings_of_context context;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   282
  in
6262
0ebfcf181d84 tuned msg;
wenzelm
parents: 6091
diff changeset
   283
    writeln ("Nesting level: " ^ string_of_int (length nodes div 2));
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   284
    writeln "";
6798
f6bc583a5776 tuned mode_name;
wenzelm
parents: 6790
diff changeset
   285
    writeln (mode_name mode ^ " mode");
5993
d03fbef54c62 tuned print_state;
wenzelm
parents: 5945
diff changeset
   286
    writeln "";
6529
0f4c2ebc5018 improved print_state;
wenzelm
parents: 6404
diff changeset
   287
    if ! verbose orelse mode = Forward then
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   288
      (if null ctxt_strings then () else (seq writeln ctxt_strings; writeln "");
6756
fe6eb161df3e improved print_state;
wenzelm
parents: 6752
diff changeset
   289
        print_facts "Current" facts;
6529
0f4c2ebc5018 improved print_state;
wenzelm
parents: 6404
diff changeset
   290
        print_goal (find_goal 0 state))
6756
fe6eb161df3e improved print_state;
wenzelm
parents: 6752
diff changeset
   291
    else if mode = ForwardChain then print_facts "Picking" facts
6529
0f4c2ebc5018 improved print_state;
wenzelm
parents: 6404
diff changeset
   292
    else print_goal (find_goal 0 state)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   293
  end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   294
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   295
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   296
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   297
(** proof steps **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   298
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   299
(* datatype method *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   300
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   301
datatype method = Method of thm list -> tactic;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   302
val method = Method;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   303
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   304
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   305
(* refine goal *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   306
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   307
fun check_sign sg state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   308
  if Sign.subsig (sg, sign_of state) then state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   309
  else raise STATE ("Bad signature of result: " ^ Sign.str_of_sg sg, state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   310
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   311
fun refine meth_fun (state as State ({context, ...}, _)) =
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   312
  let
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   313
    val Method meth = meth_fun context;
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   314
    val (_, (result, (facts, thm))) = find_goal 0 state;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   315
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   316
    fun refn thm' =
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   317
      state
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   318
      |> check_sign (Thm.sign_of_thm thm')
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   319
      |> map_goal (K (result, (facts, thm')));
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   320
  in Seq.map refn (meth facts thm) end;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   321
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   322
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   323
(* prepare result *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   324
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   325
fun varify_frees names thm =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   326
  let
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   327
    fun get_free x (None, t as Free (y, _)) = if x = y then Some t else None
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   328
      | get_free _ (opt, _) = opt;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   329
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   330
    fun find_free t x = foldl_aterms (get_free x) (None, t);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   331
6887
12b5fb35a688 generalized fixes get index 0;
wenzelm
parents: 6876
diff changeset
   332
    val {sign, prop, ...} = Thm.rep_thm thm;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   333
    val frees = map (Thm.cterm_of sign) (mapfilter (find_free prop) names);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   334
  in
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   335
    thm
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   336
    |> Drule.forall_intr_list frees
6887
12b5fb35a688 generalized fixes get index 0;
wenzelm
parents: 6876
diff changeset
   337
    |> Drule.forall_elim_vars 0
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   338
  end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   339
6790
0a39f22f847a auto_bind_goal, auto_bind_facts;
wenzelm
parents: 6776
diff changeset
   340
fun varify_tfrees thm =
0a39f22f847a auto_bind_goal, auto_bind_facts;
wenzelm
parents: 6776
diff changeset
   341
  let
0a39f22f847a auto_bind_goal, auto_bind_facts;
wenzelm
parents: 6776
diff changeset
   342
    val {hyps, prop, ...} = Thm.rep_thm thm;
0a39f22f847a auto_bind_goal, auto_bind_facts;
wenzelm
parents: 6776
diff changeset
   343
    val frees = foldr Term.add_term_frees (prop :: hyps, []);
0a39f22f847a auto_bind_goal, auto_bind_facts;
wenzelm
parents: 6776
diff changeset
   344
    val leave_tfrees = foldr Term.add_term_tfree_names (frees, []);
0a39f22f847a auto_bind_goal, auto_bind_facts;
wenzelm
parents: 6776
diff changeset
   345
  in thm |> Thm.varifyT' leave_tfrees end;
0a39f22f847a auto_bind_goal, auto_bind_facts;
wenzelm
parents: 6776
diff changeset
   346
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   347
fun implies_elim_hyps thm =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   348
  foldl (uncurry Thm.implies_elim) (thm, map Thm.assume (Drule.cprems_of thm));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   349
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   350
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   351
fun prep_result state asms t raw_thm =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   352
  let
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   353
    val ctxt = context_of state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   354
    fun err msg = raise STATE (msg, state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   355
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   356
    val ngoals = Thm.nprems_of raw_thm;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   357
    val _ =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   358
      if ngoals = 0 then ()
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   359
      else (Locale.print_goals ngoals raw_thm; err (string_of_int ngoals ^ " unsolved goal(s)!"));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   360
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   361
    val thm =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   362
      raw_thm RS Drule.rev_triv_goal
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   363
      |> implies_elim_hyps
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   364
      |> Drule.implies_intr_list asms
6731
57e761134c85 cleaned comments;
wenzelm
parents: 6683
diff changeset
   365
      |> varify_frees (ProofContext.fixed_names ctxt)
6790
0a39f22f847a auto_bind_goal, auto_bind_facts;
wenzelm
parents: 6776
diff changeset
   366
      |> varify_tfrees;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   367
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   368
    val {hyps, prop, sign, ...} = Thm.rep_thm thm;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   369
    val tsig = Sign.tsig_of sign;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   370
  in
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   371
(* FIXME
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   372
    if not (Pattern.matches tsig (t, Logic.skip_flexpairs prop)) then
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   373
      warning ("Proved a different theorem: " ^ Sign.string_of_term sign prop)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   374
    else ();
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   375
*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   376
    if not (null hyps) then
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   377
      err ("Additional hypotheses:\n" ^ cat_lines (map (Sign.string_of_term sign) hyps))
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   378
(* FIXME    else if not (Pattern.matches tsig (t, Logic.skip_flexpairs prop)) then
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   379
      err ("Proved a different theorem: " ^ Sign.string_of_term sign prop) *)
6871
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   380
    else thm
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   381
  end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   382
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   383
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   384
(* prepare final result *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   385
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   386
fun strip_flexflex thm =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   387
  Seq.hd (Thm.flexflex_rule thm) handle THM _ => thm;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   388
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   389
fun final_result state pre_thm =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   390
  let
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   391
    val thm =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   392
      pre_thm
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   393
      |> strip_flexflex
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   394
      |> Thm.strip_shyps
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   395
      |> Drule.standard;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   396
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   397
    val str_of_sort = Sign.str_of_sort (Thm.sign_of_thm thm);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   398
    val xshyps = Thm.extra_shyps thm;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   399
  in
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   400
    if not (null xshyps) then
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   401
      raise STATE ("Extra sort hypotheses: " ^ commas (map str_of_sort xshyps), state)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   402
    else thm
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   403
  end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   404
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   405
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   406
(* solve goal *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   407
6853
80f88b762816 improved RANGE;
wenzelm
parents: 6848
diff changeset
   408
fun RANGE [] _ = all_tac
80f88b762816 improved RANGE;
wenzelm
parents: 6848
diff changeset
   409
  | RANGE (tac :: tacs) i = RANGE tacs (i + 1) THEN tac i;
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   410
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   411
fun solve_goal rule tacs state =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   412
  let
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   413
    val (_, (result, (facts, thm))) = find_goal 0 state;
6853
80f88b762816 improved RANGE;
wenzelm
parents: 6848
diff changeset
   414
    val thms' = FIRSTGOAL (rtac rule THEN' RANGE tacs) thm;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   415
  in Seq.map (fn thm' => map_goal (K (result, (facts, thm'))) state) thms' end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   416
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   417
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   418
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   419
(*** structured proof commands ***)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   420
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   421
(** context **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   422
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   423
(* bind *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   424
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   425
fun gen_bind f x state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   426
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   427
  |> assert_forward
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   428
  |> map_context (f x)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   429
  |> reset_facts;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   430
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   431
val bind = gen_bind ProofContext.add_binds;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   432
val bind_i = gen_bind ProofContext.add_binds_i;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   433
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   434
val match_bind = gen_bind ProofContext.match_binds;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   435
val match_bind_i = gen_bind ProofContext.match_binds_i;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   436
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   437
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 6030
diff changeset
   438
(* have_thmss *)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   439
6876
4ae9c47f2b6b have_thmss: more_ths;
wenzelm
parents: 6871
diff changeset
   440
fun have_thmss ths name atts ths_atts state =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   441
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   442
  |> assert_forward
6876
4ae9c47f2b6b have_thmss: more_ths;
wenzelm
parents: 6871
diff changeset
   443
  |> map_context_result (ProofContext.have_thmss ths (PureThy.default_name name) atts ths_atts)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   444
  |> these_facts;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   445
6876
4ae9c47f2b6b have_thmss: more_ths;
wenzelm
parents: 6871
diff changeset
   446
fun simple_have_thms name thms = have_thmss [] name [] [(thms, [])];
4ae9c47f2b6b have_thmss: more_ths;
wenzelm
parents: 6871
diff changeset
   447
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   448
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   449
(* fix *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   450
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   451
fun gen_fix f xs state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   452
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   453
  |> assert_forward
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   454
  |> map_context (f xs)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   455
  |> reset_facts;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   456
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   457
val fix = gen_fix ProofContext.fix;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   458
val fix_i = gen_fix ProofContext.fix_i;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   459
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   460
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   461
(* assume *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   462
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   463
fun gen_assume f tac name atts props state =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   464
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   465
  |> assert_forward
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   466
  |> map_context_result (f tac (PureThy.default_name name) atts props)
6798
f6bc583a5776 tuned mode_name;
wenzelm
parents: 6790
diff changeset
   467
  |> (fn (st, (facts, prems)) =>
f6bc583a5776 tuned mode_name;
wenzelm
parents: 6790
diff changeset
   468
    (st, facts)
f6bc583a5776 tuned mode_name;
wenzelm
parents: 6790
diff changeset
   469
    |> these_facts
f6bc583a5776 tuned mode_name;
wenzelm
parents: 6790
diff changeset
   470
    |> put_thms ("prems", prems));
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   471
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   472
val assume = gen_assume ProofContext.assume;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   473
val assume_i = gen_assume ProofContext.assume_i;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   474
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   475
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   476
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   477
(** goals **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   478
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   479
(* forward chaining *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   480
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   481
fun chain state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   482
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   483
  |> assert_forward
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   484
  |> assert_facts
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   485
  |> enter_forward_chain;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   486
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   487
fun from_facts facts state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   488
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   489
  |> put_facts (Some facts)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   490
  |> chain;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   491
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   492
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   493
(* setup goals *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   494
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   495
fun setup_goal opt_block prepp kind name atts raw_propp state =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   496
  let
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   497
    val (state', concl) =
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   498
      state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   499
      |> assert_forward_or_chain
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   500
      |> enter_forward
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   501
      |> opt_block
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   502
      |> map_context_result (fn c => prepp (c, raw_propp));
6752
0545b77f864e setup_goal: proper handling of non-atomic goals (include cprems into asms);
wenzelm
parents: 6731
diff changeset
   503
    val cterm_of = Thm.cterm_of (sign_of state);
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   504
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   505
    val (casms, asm_tacs) = Library.split_list (ProofContext.assumptions (context_of state'));
6752
0545b77f864e setup_goal: proper handling of non-atomic goals (include cprems into asms);
wenzelm
parents: 6731
diff changeset
   506
    val cprems = map cterm_of (Logic.strip_imp_prems concl);
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   507
    val prem_tacs = replicate (length cprems) (K all_tac);
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   508
6752
0545b77f864e setup_goal: proper handling of non-atomic goals (include cprems into asms);
wenzelm
parents: 6731
diff changeset
   509
    val prop = Logic.list_implies (map Thm.term_of casms, concl);
0545b77f864e setup_goal: proper handling of non-atomic goals (include cprems into asms);
wenzelm
parents: 6731
diff changeset
   510
    val cprop = cterm_of prop;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   511
    val thm = Drule.mk_triv_goal cprop;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   512
  in
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   513
    state'
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   514
    |> put_goal (Some ((kind atts, (PureThy.default_name name), casms @ cprems,
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   515
        asm_tacs @ prem_tacs, prop), ([], thm)))
6790
0a39f22f847a auto_bind_goal, auto_bind_facts;
wenzelm
parents: 6776
diff changeset
   516
    |> auto_bind_goal prop
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   517
    |> (if is_chain state then use_facts else reset_facts)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   518
    |> new_block
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   519
    |> enter_backward
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   520
  end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   521
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   522
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   523
(*global goals*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   524
fun global_goal prep kind name atts x thy =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   525
  setup_goal I prep kind name atts x (init_state thy);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   526
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   527
val theorem = global_goal ProofContext.bind_propp Theorem;
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   528
val theorem_i = global_goal ProofContext.bind_propp_i Theorem;
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   529
val lemma = global_goal ProofContext.bind_propp Lemma;
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   530
val lemma_i = global_goal ProofContext.bind_propp_i Lemma;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   531
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   532
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   533
(*local goals*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   534
fun local_goal prep kind name atts x =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   535
  setup_goal open_block prep kind name atts x;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   536
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   537
val show   = local_goal ProofContext.bind_propp Goal;
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   538
val show_i = local_goal ProofContext.bind_propp_i Goal;
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   539
val have   = local_goal ProofContext.bind_propp Aux;
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   540
val have_i = local_goal ProofContext.bind_propp_i Aux;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   541
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   542
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   543
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   544
(** blocks **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   545
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   546
(* begin_block *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   547
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   548
fun begin_block state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   549
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   550
  |> assert_forward
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   551
  |> new_block
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   552
  |> reset_facts
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   553
  |> open_block;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   554
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   555
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   556
(* next_block *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   557
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   558
fun next_block state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   559
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   560
  |> assert_forward
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   561
  |> close_block
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   562
  |> new_block;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   563
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   564
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   565
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   566
(** conclusions **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   567
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   568
(* current goal *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   569
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   570
fun current_goal (State ({goal = Some goal, ...}, _)) = goal
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   571
  | current_goal state = raise STATE ("No current goal!", state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   572
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   573
fun assert_current_goal true (state as State ({goal = None, ...}, _)) =
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   574
      raise STATE ("No goal in this block!", state)
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   575
  | assert_current_goal false (state as State ({goal = Some _, ...}, _)) =
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   576
      raise STATE ("Goal present in this block!", state)
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   577
  | assert_current_goal _ state = state;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   578
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   579
fun assert_bottom true (state as State (_, _ :: _)) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   580
      raise STATE ("Not at bottom of proof!", state)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   581
  | assert_bottom false (state as State (_, [])) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   582
      raise STATE ("Already at bottom of proof!", state)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   583
  | assert_bottom _ state = state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   584
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   585
val at_bottom = can (assert_bottom true o close_block);
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   586
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   587
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   588
(* finish proof *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   589
6871
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   590
fun finish_proof bot state =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   591
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   592
  |> assert_forward
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   593
  |> close_block
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   594
  |> assert_bottom bot
6871
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   595
  |> assert_current_goal true;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   596
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   597
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   598
(* end_block *)
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   599
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   600
fun end_block state =
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   601
  state
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   602
  |> assert_forward
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   603
  |> close_block
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   604
  |> assert_current_goal false
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   605
  |> close_block
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   606
  |> fetch_facts state;
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   607
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   608
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   609
(* local_qed *)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   610
6731
57e761134c85 cleaned comments;
wenzelm
parents: 6683
diff changeset
   611
fun finish_local print_result state =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   612
  let
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   613
    val ((kind, name, asms, tacs, t), (_, raw_thm)) = current_goal state;
6871
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   614
    val result = prep_result state asms t raw_thm;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   615
    val (atts, opt_solve) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   616
      (case kind of
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   617
        Goal atts => (atts, solve_goal result tacs)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   618
      | Aux atts => (atts, Seq.single)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   619
      | _ => raise STATE ("No local goal!", state));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   620
  in
6731
57e761134c85 cleaned comments;
wenzelm
parents: 6683
diff changeset
   621
    print_result {kind = kind_name kind, name = name, thm = result};
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   622
    state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   623
    |> close_block
6871
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   624
    |> auto_bind_facts name [t]
6876
4ae9c47f2b6b have_thmss: more_ths;
wenzelm
parents: 6871
diff changeset
   625
    |> have_thmss [] name atts [Thm.no_attributes [result]]
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   626
    |> opt_solve
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   627
  end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   628
6731
57e761134c85 cleaned comments;
wenzelm
parents: 6683
diff changeset
   629
fun local_qed finalize print_result state =
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   630
  state
6871
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   631
  |> finish_proof false
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   632
  |> finalize
6731
57e761134c85 cleaned comments;
wenzelm
parents: 6683
diff changeset
   633
  |> (Seq.flat o Seq.map (finish_local print_result));
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   634
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   635
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   636
(* global_qed *)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   637
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   638
fun finish_global alt_name alt_atts state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   639
  let
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   640
    val ((kind, def_name, asms, _, t), (_, raw_thm)) = current_goal state;
6871
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   641
    val result = final_result state (prep_result state asms t raw_thm);
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   642
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   643
    val name = if_none alt_name def_name;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   644
    val atts =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   645
      (case kind of
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   646
        Theorem atts => if_none alt_atts atts
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 6030
diff changeset
   647
      | Lemma atts => (if_none alt_atts atts) @ [Drule.tag_lemma]
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   648
      | _ => raise STATE ("No global goal!", state));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   649
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 6030
diff changeset
   650
    val (thy', result') = PureThy.store_thm ((name, result), atts) (theory_of state);
6529
0f4c2ebc5018 improved print_state;
wenzelm
parents: 6404
diff changeset
   651
  in (thy', {kind = kind_name kind, name = name, thm = result'}) end;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   652
6871
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   653
(*Note: should inspect first result only, backtracking may destroy theory*)
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   654
fun global_qed finalize alt_name alt_atts state =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   655
  state
6871
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   656
  |> finish_proof true
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   657
  |> finalize
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   658
  |> Seq.map (finish_global alt_name alt_atts);
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   659
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   660
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   661
end;