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