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