src/Pure/Isar/proof.ML
author wenzelm
Thu, 19 Nov 1998 11:47:22 +0100
changeset 5936 406eb27fe53c
parent 5918 4cbd726577f7
child 5945 63184e276c1d
permissions -rw-r--r--
match_bind(_i): 'as' patterns; assume, theorem, show etc.: propp; tuned qed msg;
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
TODO:
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
     8
  - assume: improve schematic Vars handling (!?);
5918
4cbd726577f7 PureThy.default_name;
wenzelm
parents: 5875
diff changeset
     9
  - warn_vars;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    10
  - bind: check rhs (extra (T)Vars etc.) (How to handle these anyway?);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    11
  - prep_result: avoid duplicate asms;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    12
  - prep_result error: use error channel (!);
5918
4cbd726577f7 PureThy.default_name;
wenzelm
parents: 5875
diff changeset
    13
  - check_finished: trace results (!?);
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    14
  - next_block: fetch_facts (!?);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    15
*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    16
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    17
signature PROOF =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    18
sig
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    19
  type context
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    20
  type state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    21
  exception STATE of string * state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    22
  val context_of: state -> context
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    23
  val theory_of: state -> theory
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    24
  val sign_of: state -> Sign.sg
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    25
  val the_facts: state -> tthm list
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    26
  val goal_facts: (state -> tthm list) -> state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    27
  val use_facts: state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    28
  val reset_facts: state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    29
  val assert_backward: state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    30
  val enter_forward: state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    31
  val print_state: state -> unit
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    32
  type method
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    33
  val method: (tthm list -> thm ->
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    34
    (thm * (indexname * term) list * (string * tthm list) list) Seq.seq) -> method
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    35
  val refine: (context -> method) -> state -> state Seq.seq
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    36
  val bind: (indexname * string) list -> state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    37
  val bind_i: (indexname * term) list -> state -> state
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    38
  val match_bind: (string list * string) list -> state -> state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    39
  val match_bind_i: (term list * term) list -> state -> state
5918
4cbd726577f7 PureThy.default_name;
wenzelm
parents: 5875
diff changeset
    40
  val have_tthmss: string -> context attribute list ->
4cbd726577f7 PureThy.default_name;
wenzelm
parents: 5875
diff changeset
    41
    (tthm list * context attribute list) list -> state -> state
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    42
  val assume: string -> context attribute list -> (string * string list) list -> state -> state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    43
  val assume_i: string -> context attribute list -> (term * term list) list -> state -> state
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    44
  val fix: (string * string option) list -> state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    45
  val fix_i: (string * typ) list -> state -> state
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    46
  val theorem: bstring -> theory attribute list -> string * string list -> theory -> state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    47
  val theorem_i: bstring -> theory attribute list -> term * term list -> theory -> state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    48
  val lemma: bstring -> theory attribute list -> string * string list -> theory -> state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    49
  val lemma_i: bstring -> theory attribute list -> term * term list -> theory -> state
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    50
  val chain: state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    51
  val from_facts: tthm list -> state -> state
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    52
  val show: string -> context attribute list -> string * string list -> state -> state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    53
  val show_i: string -> context attribute list -> term * term list -> state -> state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    54
  val have: string -> context attribute list -> string * string list -> state -> state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    55
  val have_i: string -> context attribute list -> term * term list -> state -> state
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    56
  val begin_block: state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    57
  val next_block: state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    58
  val end_block: (context -> method) -> state -> state Seq.seq
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    59
  val qed: (context -> method) -> bstring option -> theory attribute list option -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    60
    -> theory * (string * string * tthm)
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
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    86
val theoremN = "theorem";
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    87
val kind_name =
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    88
  fn Theorem _ => theoremN | Lemma _ => "lemma" | Goal _ => "show" | Aux _ => "have";
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    89
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    90
type goal =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    91
 (kind *		(*result kind*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    92
  string *              (*result name*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    93
  cterm list *          (*result assumptions*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    94
  term) *               (*result statement*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    95
 (tthm list *           (*use facts*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    96
  thm);                 (*goal: subgoals ==> statement*)
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 =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   104
  fn Forward => "forward" | ForwardChain => "forward chain" | Backward => "backward";
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   105
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   106
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   107
(* type node *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   108
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   109
type node =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   110
 {context: context,
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   111
  facts: tthm list option,
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   112
  mode: mode,
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   113
  goal: goal option};
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   114
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   115
fun make_node (context, facts, mode, goal) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   116
  {context = context, facts = facts, mode = mode, goal = goal}: node;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   117
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   118
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   119
(* datatype state *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   120
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   121
datatype state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   122
  State of
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   123
    node *              (*current*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   124
    node list;          (*parents wrt. block structure*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   125
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   126
exception STATE of string * state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   127
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   128
fun err_malformed name state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   129
  raise STATE (name ^ ": internal error -- malformed proof state", state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   130
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   131
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   132
fun map_current f (State ({context, facts, mode, goal}, nodes)) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   133
  State (make_node (f (context, facts, mode, goal)), nodes);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   134
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   135
fun init_state thy =
5875
6aae55ae3473 renamed init_context to init;
wenzelm
parents: 5820
diff changeset
   136
  State (make_node (ProofContext.init thy, None, Forward, None), []);
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   137
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   138
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   139
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   140
(** basic proof state operations **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   141
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   142
(* context *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   143
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   144
fun context_of (State ({context, ...}, _)) = context;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   145
val theory_of = ProofContext.theory_of o context_of;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   146
val sign_of = ProofContext.sign_of o context_of;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   147
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   148
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
   149
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   150
fun map_context_result f (state as State ({context, facts, mode, goal}, nodes)) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   151
  let val (context', result) = f context
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   152
  in (State (make_node (context', facts, mode, goal), nodes), result) end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   153
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   154
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   155
fun put_data kind f = map_context o ProofContext.put_data kind f;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   156
val declare_term = map_context o ProofContext.declare_term;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   157
val add_binds = map_context o ProofContext.add_binds_i;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   158
val put_tthms = map_context o ProofContext.put_tthms;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   159
val put_tthmss = map_context o ProofContext.put_tthmss;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   160
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   161
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   162
(* bind statements *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   163
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   164
fun bind_props bs state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   165
  let val mk_bind = map (fn (x, t) => ((Syntax.binding x, 0), t)) o ObjectLogic.dest_statement
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   166
  in state |> add_binds (flat (map mk_bind bs)) end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   167
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   168
fun bind_tthms (name, tthms) state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   169
  let
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   170
    val props = map (#prop o Thm.rep_thm o Attribute.thm_of) tthms;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   171
    val named_props =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   172
      (case props of
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   173
        [prop] => [(name, prop)]
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   174
      | props => map2 (fn (i, t) => (name ^ string_of_int i, t)) (1 upto length props, props));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   175
  in state |> bind_props named_props end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   176
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   177
fun let_tthms name_tthms state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   178
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   179
  |> put_tthms name_tthms
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   180
  |> bind_tthms name_tthms;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   181
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   182
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   183
(* facts *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   184
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   185
fun the_facts (State ({facts = Some facts, ...}, _)) = facts
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   186
  | the_facts state = raise STATE ("No current facts available", state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   187
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   188
fun put_facts facts state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   189
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   190
  |> map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal))
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   191
  |> let_tthms ("facts", if_none facts []);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   192
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   193
val reset_facts = put_facts None;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   194
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   195
fun have_facts (name, facts) state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   196
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   197
  |> put_facts (Some facts)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   198
  |> let_tthms (name, facts);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   199
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   200
fun these_facts (state, ths) = have_facts ths state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   201
fun fetch_facts (State ({facts, ...}, _)) = put_facts facts;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   202
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   203
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   204
(* goal *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   205
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   206
fun find_goal i (State ({goal = Some goal, ...}, _)) = (i, goal)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   207
  | find_goal i (State ({goal = None, ...}, node :: nodes)) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   208
      find_goal (i + 1) (State (node, nodes))
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   209
  | find_goal _ (state as State (_, [])) = err_malformed "find_goal" state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   210
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   211
fun put_goal goal = map_current (fn (ctxt, facts, mode, _) => (ctxt, facts, mode, goal));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   212
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   213
fun map_goal f (State ({context, facts, mode, goal = Some goal}, nodes)) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   214
      State (make_node (context, facts, mode, Some (f goal)), nodes)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   215
  | map_goal f (State (nd, node :: nodes)) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   216
      let val State (node', nodes') = map_goal f (State (node, nodes))
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   217
      in State (nd, node' :: nodes') end
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   218
  | map_goal _ state = state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   219
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   220
fun goal_facts get state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   221
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   222
  |> map_goal (fn (result, (_, thm)) => (result, (get state, thm)));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   223
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   224
fun use_facts state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   225
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   226
  |> goal_facts the_facts
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   227
  |> reset_facts;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   228
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   229
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   230
(* mode *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   231
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   232
fun get_mode (State ({mode, ...}, _)) = mode;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   233
fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   234
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   235
val enter_forward = put_mode Forward;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   236
val enter_forward_chain = put_mode ForwardChain;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   237
val enter_backward = put_mode Backward;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   238
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   239
fun assert_mode pred state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   240
  let val mode = get_mode state in
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   241
    if pred mode then state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   242
    else raise STATE ("Illegal application of command in " ^ mode_name mode ^ " mode", state)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   243
  end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   244
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   245
fun is_chain state = get_mode state = ForwardChain;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   246
val assert_forward = assert_mode (equal Forward);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   247
val assert_forward_or_chain = assert_mode (equal Forward orf equal ForwardChain);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   248
val assert_backward = assert_mode (equal Backward);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   249
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   250
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   251
(* blocks *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   252
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   253
fun open_block (State (node, nodes)) = State (node, node :: nodes);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   254
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   255
fun new_block state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   256
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   257
  |> open_block
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   258
  |> put_goal None;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   259
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   260
fun close_block (State (_, node :: nodes)) = State (node, nodes)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   261
  | close_block state = raise STATE ("Unbalanced block parentheses", state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   262
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   263
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   264
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   265
(** print_state **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   266
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   267
fun print_state (state as State ({context, facts, mode, goal = _}, nodes)) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   268
  let
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   269
    val prt_tthm = Attribute.pretty_tthm;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   270
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   271
    fun print_facts None = ()
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   272
      | print_facts (Some ths) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   273
          Pretty.writeln (Pretty.big_list "Current facts:" (map prt_tthm ths));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   274
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   275
    fun levels_up 0 = ""
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   276
      | levels_up i = " (" ^ string_of_int i ^ " levels up)";
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   277
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   278
    fun print_goal (i, ((kind, name, _, _), (_, thm))) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   279
      (writeln (kind_name kind ^ " " ^ quote name ^ levels_up (i div 1) ^ ":");	(* FIXME *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   280
        Locale.print_goals (! goals_limit) thm);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   281
  in
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   282
    writeln ("Nesting level: " ^ string_of_int (length nodes div 1));	(* FIXME *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   283
    writeln "";
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   284
    ProofContext.print_context context;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   285
    writeln "";
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   286
    print_facts facts;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   287
    print_goal (find_goal 0 state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   288
    writeln "";
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   289
    writeln (mode_name mode ^ " mode")
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   290
  end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   291
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   292
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   293
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   294
(** proof steps **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   295
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   296
(* datatype method *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   297
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   298
datatype method = Method of
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   299
  tthm list ->                   	(*use facts*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   300
  thm                                   (*goal: subgoals ==> statement*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   301
    -> (thm *                           (*refined goal*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   302
       (indexname * term) list *        (*new bindings*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   303
       (string * tthm list) list)       (*new thms*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   304
         Seq.seq;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   305
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   306
val method = Method;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   307
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   308
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   309
(* refine goal *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   310
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   311
fun check_sign sg state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   312
  if Sign.subsig (sg, sign_of state) then state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   313
  else raise STATE ("Bad signature of result: " ^ Sign.str_of_sg sg, state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   314
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   315
fun refine meth_fun (state as State ({context, ...}, _)) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   316
      let
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   317
        val Method meth = meth_fun context;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   318
        val (_, (result, (facts, thm))) = find_goal 0 state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   319
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   320
        fun refn (thm', new_binds, new_thms) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   321
          state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   322
          |> check_sign (sign_of_thm thm')
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   323
          |> map_goal (K (result, (facts, thm')))
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   324
          |> add_binds new_binds
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   325
          |> put_tthmss new_thms;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   326
      in Seq.map refn (meth facts thm) end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   327
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   328
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   329
(* prepare result *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   330
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   331
fun varify_frees names thm =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   332
  let
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   333
    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
   334
      | get_free _ (opt, _) = opt;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   335
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   336
    fun find_free t x = foldl_aterms (get_free x) (None, t);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   337
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   338
    val {sign, maxidx, prop, ...} = Thm.rep_thm thm;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   339
    val frees = map (Thm.cterm_of sign) (mapfilter (find_free prop) names);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   340
  in
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   341
    thm
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   342
    |> Drule.forall_intr_list frees
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   343
    |> Drule.forall_elim_vars (maxidx + 1)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   344
  end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   345
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   346
fun implies_elim_hyps thm =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   347
  foldl (uncurry Thm.implies_elim) (thm, map Thm.assume (Drule.cprems_of thm));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   348
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   349
fun prep_result state asms t raw_thm =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   350
  let
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   351
    val ctxt = context_of state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   352
    fun err msg = raise STATE (msg, state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   353
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   354
    val ngoals = Thm.nprems_of raw_thm;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   355
    val _ =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   356
      if ngoals = 0 then ()
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   357
      else (Locale.print_goals ngoals raw_thm; err (string_of_int ngoals ^ " unsolved goal(s)!"));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   358
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   359
    val thm =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   360
      raw_thm RS Drule.rev_triv_goal
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   361
      |> implies_elim_hyps
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   362
      |> Drule.implies_intr_list asms
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   363
      |> varify_frees (ProofContext.fixed_names ctxt);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   364
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   365
    val {hyps, prop, sign, ...} = Thm.rep_thm thm;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   366
    val tsig = Sign.tsig_of sign;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   367
  in
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   368
(* FIXME
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   369
    if not (Pattern.matches tsig (t, Logic.skip_flexpairs prop)) then
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   370
      warning ("Proved a different theorem: " ^ Sign.string_of_term sign prop)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   371
    else ();
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   372
*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   373
    if not (null hyps) then
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   374
      err ("Additional hypotheses:\n" ^ cat_lines (map (Sign.string_of_term sign) hyps))
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   375
(* FIXME    else if not (Pattern.matches tsig (t, Logic.skip_flexpairs prop)) then
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   376
      err ("Proved a different theorem: " ^ Sign.string_of_term sign prop) *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   377
    else thm
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   378
  end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   379
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   380
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   381
(* prepare final result *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   382
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   383
fun strip_flexflex thm =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   384
  Seq.hd (Thm.flexflex_rule thm) handle THM _ => thm;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   385
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   386
fun final_result state pre_thm =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   387
  let
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   388
    val thm =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   389
      pre_thm
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   390
      |> strip_flexflex
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   391
      |> Thm.strip_shyps
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   392
      |> Drule.standard;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   393
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   394
    val str_of_sort = Sign.str_of_sort (Thm.sign_of_thm thm);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   395
    val xshyps = Thm.extra_shyps thm;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   396
  in
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   397
    if not (null xshyps) then
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   398
      raise STATE ("Extra sort hypotheses: " ^ commas (map str_of_sort xshyps), state)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   399
    else thm
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   400
  end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   401
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   402
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   403
(* solve goal *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   404
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   405
fun solve_goal rule state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   406
  let
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   407
    val (_, (result, (facts, thm))) = find_goal 0 state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   408
    val thms' = FIRSTGOAL (solve_tac [rule]) thm;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   409
  in Seq.map (fn thm' => map_goal (K (result, (facts, thm'))) state) thms' end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   410
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   411
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   412
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   413
(*** structured proof commands ***)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   414
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   415
(** context **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   416
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   417
(* bind *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   418
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   419
fun gen_bind f x state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   420
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   421
  |> assert_forward
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   422
  |> map_context (f x)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   423
  |> reset_facts;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   424
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   425
val bind = gen_bind ProofContext.add_binds;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   426
val bind_i = gen_bind ProofContext.add_binds_i;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   427
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   428
val match_bind = gen_bind ProofContext.match_binds;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   429
val match_bind_i = gen_bind ProofContext.match_binds_i;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   430
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   431
5918
4cbd726577f7 PureThy.default_name;
wenzelm
parents: 5875
diff changeset
   432
(* have_tthmss *)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   433
5918
4cbd726577f7 PureThy.default_name;
wenzelm
parents: 5875
diff changeset
   434
fun have_tthmss name atts ths_atts state =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   435
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   436
  |> assert_forward
5918
4cbd726577f7 PureThy.default_name;
wenzelm
parents: 5875
diff changeset
   437
  |> map_context_result (ProofContext.have_tthmss (PureThy.default_name name) atts ths_atts)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   438
  |> these_facts;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   439
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   440
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   441
(* fix *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   442
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   443
fun gen_fix f xs state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   444
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   445
  |> assert_forward
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   446
  |> map_context (f xs)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   447
  |> reset_facts;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   448
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   449
val fix = gen_fix ProofContext.fix;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   450
val fix_i = gen_fix ProofContext.fix_i;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   451
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   452
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   453
(* assume *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   454
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   455
fun gen_assume f name atts props state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   456
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   457
  |> assert_forward
5918
4cbd726577f7 PureThy.default_name;
wenzelm
parents: 5875
diff changeset
   458
  |> map_context_result (f (PureThy.default_name name) atts props)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   459
  |> these_facts
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   460
  |> (fn st => let_tthms ("prems", ProofContext.assumptions (context_of st)) st);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   461
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   462
val assume = gen_assume ProofContext.assume;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   463
val assume_i = gen_assume ProofContext.assume_i;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   464
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   465
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   466
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   467
(** goals **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   468
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   469
(* forward chaining *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   470
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   471
fun chain state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   472
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   473
  |> assert_forward
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   474
  |> enter_forward_chain;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   475
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   476
fun from_facts facts state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   477
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   478
  |> put_facts (Some facts)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   479
  |> chain;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   480
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   481
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   482
(* setup goals *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   483
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   484
fun setup_goal opt_block prepp kind name atts raw_propp state =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   485
  let
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   486
    val (state', concl) =
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   487
      state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   488
      |> assert_forward_or_chain
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   489
      |> enter_forward
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   490
      |> opt_block
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   491
      |> map_context_result (fn c => prepp (c, raw_propp));
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   492
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   493
    val casms = map (#prop o Thm.crep_thm o Attribute.thm_of)
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   494
      (ProofContext.assumptions (context_of state'));
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   495
    val asms = map Thm.term_of casms;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   496
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   497
    val prop = Logic.list_implies (asms, concl);
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   498
    val cprop = Thm.cterm_of (sign_of state') prop;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   499
    val thm = Drule.mk_triv_goal cprop;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   500
  in
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   501
    state'
5918
4cbd726577f7 PureThy.default_name;
wenzelm
parents: 5875
diff changeset
   502
    |> put_goal (Some ((kind atts, (PureThy.default_name name), casms, prop), ([], thm)))
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   503
    |> bind_props [("thesis", prop)]
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   504
    |> (if is_chain state then use_facts else reset_facts)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   505
    |> new_block
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   506
    |> enter_backward
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   507
  end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   508
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   509
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   510
(*global goals*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   511
fun global_goal prep kind name atts x thy =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   512
  setup_goal I prep kind name atts x (init_state thy);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   513
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   514
val theorem = global_goal ProofContext.bind_propp Theorem;
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   515
val theorem_i = global_goal ProofContext.bind_propp_i Theorem;
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   516
val lemma = global_goal ProofContext.bind_propp Lemma;
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   517
val lemma_i = global_goal ProofContext.bind_propp_i Lemma;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   518
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   519
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   520
(*local goals*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   521
fun local_goal prep kind name atts x =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   522
  setup_goal open_block prep kind name atts x;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   523
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   524
val show   = local_goal ProofContext.bind_propp Goal;
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   525
val show_i = local_goal ProofContext.bind_propp_i Goal;
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   526
val have   = local_goal ProofContext.bind_propp Aux;
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   527
val have_i = local_goal ProofContext.bind_propp_i Aux;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   528
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   529
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   530
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   531
(** blocks **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   532
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   533
(* begin_block *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   534
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   535
fun begin_block state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   536
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   537
  |> assert_forward
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   538
  |> new_block
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   539
  |> reset_facts
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   540
  |> open_block;
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
(* next_block *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   544
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   545
fun next_block state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   546
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   547
  |> assert_forward
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   548
  |> close_block
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   549
  |> new_block;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   550
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   551
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   552
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   553
(** conclusions **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   554
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   555
(* current goal *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   556
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   557
fun current_goal (State ({goal = Some goal, ...}, _)) = goal
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   558
  | current_goal state = raise STATE ("No current goal!", state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   559
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   560
fun assert_current_goal state = (current_goal state; state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   561
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   562
fun assert_bottom true (state as State (_, _ :: _)) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   563
      raise STATE ("Not at bottom of proof!", state)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   564
  | assert_bottom false (state as State (_, [])) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   565
      raise STATE ("Already at bottom of proof!", state)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   566
  | assert_bottom _ state = state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   567
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   568
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   569
(* finish proof *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   570
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   571
fun check_finished state states =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   572
  (case Seq.pull states of
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   573
    None => raise STATE ("Failed to finish proof", state)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   574
  | Some s_sq => Seq.cons s_sq);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   575
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   576
fun finish_proof bot meth_fun state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   577
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   578
  |> assert_forward
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   579
  |> close_block
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   580
  |> assert_bottom bot
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   581
  |> assert_current_goal
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   582
  |> refine meth_fun
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   583
  |> check_finished state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   584
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   585
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   586
(* conclude local proof *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   587
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   588
fun finish_local state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   589
  let
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   590
    val ((kind, name, asms, t), (_, raw_thm)) = current_goal state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   591
    val result = prep_result state asms t raw_thm;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   592
    val (atts, opt_solve) =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   593
      (case kind of
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   594
        Goal atts => (atts, solve_goal result)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   595
      | Aux atts => (atts, Seq.single)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   596
      | _ => raise STATE ("No local goal!", state));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   597
  in
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   598
    state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   599
    |> close_block
5918
4cbd726577f7 PureThy.default_name;
wenzelm
parents: 5875
diff changeset
   600
    |> have_tthmss name atts [([(result, [])], [])]
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   601
    |> opt_solve
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   602
  end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   603
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   604
fun end_block meth_fun state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   605
  if can assert_current_goal (close_block state) then
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   606
    state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   607
    |> finish_proof false meth_fun
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   608
    |> (Seq.flat o Seq.map finish_local)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   609
  else
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   610
    state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   611
    |> assert_forward
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   612
    |> close_block
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   613
    |> close_block
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   614
    |> fetch_facts state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   615
    |> Seq.single;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   616
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   617
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   618
(* conclude global proof *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   619
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   620
fun finish_global alt_name alt_atts state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   621
  let
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   622
    val ((kind, def_name, asms, t), (_, raw_thm)) = current_goal state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   623
    val result = final_result state (prep_result state asms t raw_thm);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   624
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   625
    val name = if_none alt_name def_name;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   626
    val atts =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   627
      (case kind of
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   628
        Theorem atts => if_none alt_atts atts
5918
4cbd726577f7 PureThy.default_name;
wenzelm
parents: 5875
diff changeset
   629
      | Lemma atts => (if_none alt_atts atts) @ [Attribute.tag_lemma]
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   630
      | _ => raise STATE ("No global goal!", state));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   631
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   632
    val (thy', result') = PureThy.store_tthm ((name, (result, [])), atts) (theory_of state);
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   633
  in (thy', (theoremN, name, result')) end;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   634
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   635
fun qed meth_fun alt_name alt_atts state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   636
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   637
  |> finish_proof true meth_fun
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   638
  |> Seq.hd
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   639
  |> finish_global alt_name alt_atts;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   640
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   641
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   642
end;