src/Pure/Isar/proof.ML
author wenzelm
Thu, 17 Jan 2002 21:06:23 +0100
changeset 12808 a529b4b91888
parent 12760 2356740225ce
child 12930 c1f3ff5feff1
permissions -rw-r--r--
RuleCases.make interface based on term instead of thm; tuned;
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
8807
wenzelm
parents: 8722
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
     5
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
     6
Proof states and methods.
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
     7
*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
     8
8152
ce3387fafebb replaced FIRSTGOAL by FINDGOAL (backtracking!);
wenzelm
parents: 8095
diff changeset
     9
signature BASIC_PROOF =
ce3387fafebb replaced FIRSTGOAL by FINDGOAL (backtracking!);
wenzelm
parents: 8095
diff changeset
    10
sig
8374
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
    11
  val FINDGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
    12
  val HEADGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq
8152
ce3387fafebb replaced FIRSTGOAL by FINDGOAL (backtracking!);
wenzelm
parents: 8095
diff changeset
    13
end;
ce3387fafebb replaced FIRSTGOAL by FINDGOAL (backtracking!);
wenzelm
parents: 8095
diff changeset
    14
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    15
signature PROOF =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    16
sig
8152
ce3387fafebb replaced FIRSTGOAL by FINDGOAL (backtracking!);
wenzelm
parents: 8095
diff changeset
    17
  include BASIC_PROOF
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    18
  type context
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    19
  type state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    20
  exception STATE of string * state
6871
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
    21
  val check_result: string -> state -> 'a Seq.seq -> 'a Seq.seq
12045
bfaa23b19f47 theorem(_i): locale elements;
wenzelm
parents: 12015
diff changeset
    22
  val init_state: theory -> state
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    23
  val context_of: state -> context
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    24
  val theory_of: state -> theory
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    25
  val sign_of: state -> Sign.sg
7924
5fee69b1f5fe warn_extra_tfrees;
wenzelm
parents: 7669
diff changeset
    26
  val warn_extra_tfrees: state -> state -> state
7605
8bbfcb54054e added reset_thms;
wenzelm
parents: 7580
diff changeset
    27
  val reset_thms: string -> state -> state
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 6030
diff changeset
    28
  val the_facts: state -> thm list
9469
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
    29
  val the_fact: state -> thm
11079
a378479996f7 val get_goal: state -> context * (thm list * thm);
wenzelm
parents: 10938
diff changeset
    30
  val get_goal: state -> context * (thm list * thm)
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 6030
diff changeset
    31
  val goal_facts: (state -> thm list) -> state -> state
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    32
  val use_facts: state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    33
  val reset_facts: state -> state
9469
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
    34
  val is_chain: state -> bool
6891
7bb02d03035d tuned print_state;
wenzelm
parents: 6887
diff changeset
    35
  val assert_forward: state -> state
9469
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
    36
  val assert_forward_or_chain: state -> state
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    37
  val assert_backward: state -> state
8206
e50a130ec9af assert_no_chain;
wenzelm
parents: 8186
diff changeset
    38
  val assert_no_chain: state -> state
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    39
  val enter_forward: state -> state
6529
0f4c2ebc5018 improved print_state;
wenzelm
parents: 6404
diff changeset
    40
  val verbose: bool ref
12423
7fd447422dee tuned print_state interfaces;
wenzelm
parents: 12308
diff changeset
    41
  val pretty_state: int -> state -> Pretty.T list
10360
807992b67edd tuned goals output;
wenzelm
parents: 10350
diff changeset
    42
  val pretty_goals: bool -> state -> Pretty.T list
6776
55f1e6b639a4 added the_fact, level;
wenzelm
parents: 6756
diff changeset
    43
  val level: state -> int
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    44
  type method
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
    45
  val method: (thm list -> tactic) -> method
8374
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
    46
  val method_cases: (thm list -> thm -> (thm * (string * RuleCases.T) list) Seq.seq) -> method
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    47
  val refine: (context -> method) -> state -> state Seq.seq
8234
36a85a6c4852 refine_end;
wenzelm
parents: 8206
diff changeset
    48
  val refine_end: (context -> method) -> state -> state Seq.seq
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    49
  val match_bind: (string list * string) list -> state -> state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
    50
  val match_bind_i: (term list * term) list -> state -> state
8617
33e2bd53aec3 support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents: 8582
diff changeset
    51
  val let_bind: (string list * string) list -> state -> state
33e2bd53aec3 support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents: 8582
diff changeset
    52
  val let_bind_i: (term list * term) list -> state -> state
6876
4ae9c47f2b6b have_thmss: more_ths;
wenzelm
parents: 6871
diff changeset
    53
  val simple_have_thms: string -> thm list -> state -> state
12714
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
    54
  val have_thmss: ((bstring * context attribute list) *
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
    55
    (xstring * context attribute list) list) list -> state -> state
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
    56
  val have_thmss_i: ((bstring * context attribute list) *
9196
1f6f66fe777a facts: handle multiple lists of arguments;
wenzelm
parents: 8991
diff changeset
    57
    (thm list * context attribute list) list) list -> state -> state
12714
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
    58
  val with_thmss: ((bstring * context attribute list) *
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
    59
    (xstring * context attribute list) list) list -> state -> state
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
    60
  val with_thmss_i: ((bstring * context attribute list) *
9196
1f6f66fe777a facts: handle multiple lists of arguments;
wenzelm
parents: 8991
diff changeset
    61
    (thm list * context attribute list) list) list -> state -> state
7412
35ebe1452c10 removed the_fact;
wenzelm
parents: 7271
diff changeset
    62
  val fix: (string list * string option) list -> state -> state
7665
1ab0c74cd748 get_goal: prop;
wenzelm
parents: 7632
diff changeset
    63
  val fix_i: (string list * typ option) list -> state -> state
9469
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
    64
  val assm: ProofContext.exporter
10464
b7b916a82dca tuned statement args;
wenzelm
parents: 10446
diff changeset
    65
    -> ((string * context attribute list) * (string * (string list * string list)) list) list
7271
442456b2a8bb assume: multiple args;
wenzelm
parents: 7201
diff changeset
    66
    -> state -> state
9469
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
    67
  val assm_i: ProofContext.exporter
10464
b7b916a82dca tuned statement args;
wenzelm
parents: 10446
diff changeset
    68
    -> ((string * context attribute list) * (term * (term list * term list)) list) list
6932
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
    69
    -> state -> state
10464
b7b916a82dca tuned statement args;
wenzelm
parents: 10446
diff changeset
    70
  val assume:
b7b916a82dca tuned statement args;
wenzelm
parents: 10446
diff changeset
    71
    ((string * context attribute list) * (string * (string list * string list)) list) list
7271
442456b2a8bb assume: multiple args;
wenzelm
parents: 7201
diff changeset
    72
    -> state -> state
10464
b7b916a82dca tuned statement args;
wenzelm
parents: 10446
diff changeset
    73
  val assume_i: ((string * context attribute list) * (term * (term list * term list)) list) list
6932
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
    74
    -> state -> state
10464
b7b916a82dca tuned statement args;
wenzelm
parents: 10446
diff changeset
    75
  val presume:
b7b916a82dca tuned statement args;
wenzelm
parents: 10446
diff changeset
    76
    ((string * context attribute list) * (string * (string list * string list)) list) list
6932
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
    77
    -> state -> state
10464
b7b916a82dca tuned statement args;
wenzelm
parents: 10446
diff changeset
    78
  val presume_i: ((string * context attribute list) * (term * (term list * term list)) list) list
6932
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
    79
    -> state -> state
11891
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
    80
  val def: string -> context attribute list -> string *  (string * string list) -> state -> state
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
    81
  val def_i: string -> context attribute list -> string * (term * term list) -> state -> state
11793
5f0ab6f5c280 support impromptu terminology of cases parameters;
wenzelm
parents: 11742
diff changeset
    82
  val invoke_case: string * string option list * context attribute list -> state -> state
12242
282a92bc5655 multi_theorem: common statement header (covers *all* results);
wenzelm
parents: 12223
diff changeset
    83
  val multi_theorem: string -> bstring * theory attribute list
12698
b87b41ade3b2 locales: hide base name of exported version;
wenzelm
parents: 12549
diff changeset
    84
    -> (xstring * context attribute list list) option -> context attribute Locale.element list
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
    85
    -> ((bstring * theory attribute list) * (string * (string list * string list)) list) list
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
    86
    -> theory -> state
12242
282a92bc5655 multi_theorem: common statement header (covers *all* results);
wenzelm
parents: 12223
diff changeset
    87
  val multi_theorem_i: string -> bstring * theory attribute list
12698
b87b41ade3b2 locales: hide base name of exported version;
wenzelm
parents: 12549
diff changeset
    88
    -> (string * context attribute list list) option -> context attribute Locale.element_i list
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
    89
    -> ((bstring * theory attribute list) * (term * (term list * term list)) list) list
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
    90
    -> theory -> state
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
    91
  val chain: state -> state
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 6030
diff changeset
    92
  val from_facts: thm list -> state -> state
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
    93
  val show: (bool -> state -> state) -> (state -> state Seq.seq)
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
    94
    -> ((string * context attribute list) * (string * (string list * string list)) list) list
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
    95
    -> bool -> state -> state
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
    96
  val show_i: (bool -> state -> state) -> (state -> state Seq.seq)
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
    97
    -> ((string * context attribute list) * (term * (term list * term list)) list) list
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
    98
    -> bool -> state -> state
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
    99
  val have: (state -> state Seq.seq)
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   100
    -> ((string * context attribute list) * (string * (string list * string list)) list) list
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   101
    -> state -> state
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   102
  val have_i: (state -> state Seq.seq)
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   103
    -> ((string * context attribute list) * (term * (term list * term list)) list) list
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   104
    -> state -> state
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   105
  val at_bottom: state -> bool
6982
4d2a3f35af93 added show_hyps flag;
wenzelm
parents: 6950
diff changeset
   106
  val local_qed: (state -> state Seq.seq)
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   107
    -> (context -> string * (string * thm list) list -> unit) * (context -> thm -> unit)
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   108
    -> state -> state Seq.seq
6950
ab6d35b7283f global_qed: removed alt_name, alt_att;
wenzelm
parents: 6932
diff changeset
   109
  val global_qed: (state -> state Seq.seq) -> state
12244
179f142ffb03 improved treatment of common result name;
wenzelm
parents: 12242
diff changeset
   110
    -> (theory * ((string * string) * (string * thm list) list)) Seq.seq
6896
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   111
  val begin_block: state -> state
6932
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
   112
  val end_block: state -> state Seq.seq
6896
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   113
  val next_block: state -> state
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   114
end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   115
8152
ce3387fafebb replaced FIRSTGOAL by FINDGOAL (backtracking!);
wenzelm
parents: 8095
diff changeset
   116
signature PRIVATE_PROOF =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   117
sig
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   118
  include PROOF
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   119
  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> state -> state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   120
end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   121
8152
ce3387fafebb replaced FIRSTGOAL by FINDGOAL (backtracking!);
wenzelm
parents: 8095
diff changeset
   122
structure Proof: PRIVATE_PROOF =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   123
struct
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   124
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   125
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   126
(** proof state **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   127
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   128
type context = ProofContext.context;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   129
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   130
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   131
(* type goal *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   132
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   133
datatype kind =
12698
b87b41ade3b2 locales: hide base name of exported version;
wenzelm
parents: 12549
diff changeset
   134
  Theorem of string *                                (*theorem kind*)
b87b41ade3b2 locales: hide base name of exported version;
wenzelm
parents: 12549
diff changeset
   135
    (bstring * theory attribute list) *              (*common result binding*)
b87b41ade3b2 locales: hide base name of exported version;
wenzelm
parents: 12549
diff changeset
   136
    (string * context attribute list list) option *  (*target locale and attributes*)
b87b41ade3b2 locales: hide base name of exported version;
wenzelm
parents: 12549
diff changeset
   137
    theory attribute list list |                     (*attributes of individual result binding*)
b87b41ade3b2 locales: hide base name of exported version;
wenzelm
parents: 12549
diff changeset
   138
  Show of context attribute list list |              (*intermediate result, solving subgoal*)
b87b41ade3b2 locales: hide base name of exported version;
wenzelm
parents: 12549
diff changeset
   139
  Have of context attribute list list;               (*intermediate result*)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   140
12242
282a92bc5655 multi_theorem: common statement header (covers *all* results);
wenzelm
parents: 12223
diff changeset
   141
fun common_name "" = "" | common_name a = " " ^ a;
282a92bc5655 multi_theorem: common statement header (covers *all* results);
wenzelm
parents: 12223
diff changeset
   142
282a92bc5655 multi_theorem: common statement header (covers *all* results);
wenzelm
parents: 12223
diff changeset
   143
fun kind_name _ (Theorem (s, (a, _), None, _)) = s ^ common_name a
282a92bc5655 multi_theorem: common statement header (covers *all* results);
wenzelm
parents: 12223
diff changeset
   144
  | kind_name sg (Theorem (s, (a, _), Some (name, _), _)) =
282a92bc5655 multi_theorem: common statement header (covers *all* results);
wenzelm
parents: 12223
diff changeset
   145
      s ^ common_name a ^ " (in " ^ Locale.cond_extern sg name ^ ")"
12015
68b2a53161e6 theorem: locale argument;
wenzelm
parents: 12010
diff changeset
   146
  | kind_name _ (Show _) = "show"
68b2a53161e6 theorem: locale argument;
wenzelm
parents: 12010
diff changeset
   147
  | kind_name _ (Have _) = "have";
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   148
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   149
type goal =
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   150
 (kind *             (*result kind*)
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   151
  string list *      (*result names*)
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   152
  term list list) *  (*result statements*)
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   153
 (thm list *         (*use facts*)
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   154
  thm);              (*goal: subgoals ==> statement*)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   155
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   156
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   157
(* type mode *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   158
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   159
datatype mode = Forward | ForwardChain | Backward;
7201
59b9b7aec3c5 tuned print_state;
wenzelm
parents: 7176
diff changeset
   160
val mode_name = (fn Forward => "state" | ForwardChain => "chain" | Backward => "prove");
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
(* datatype state *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   164
7176
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   165
datatype node =
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   166
  Node of
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   167
   {context: context,
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   168
    facts: thm list option,
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   169
    mode: mode,
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   170
    goal: (goal * (state -> state Seq.seq)) option}
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   171
and state =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   172
  State of
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   173
    node *              (*current*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   174
    node list;          (*parents wrt. block structure*)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   175
7176
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   176
fun make_node (context, facts, mode, goal) =
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   177
  Node {context = context, facts = facts, mode = mode, goal = goal};
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   178
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   179
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   180
exception STATE of string * state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   181
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   182
fun err_malformed name state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   183
  raise STATE (name ^ ": internal error -- malformed proof state", state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   184
6871
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   185
fun check_result msg state sq =
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   186
  (case Seq.pull sq of
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   187
    None => raise STATE (msg, state)
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   188
  | Some s_sq => Seq.cons s_sq);
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   189
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   190
7176
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   191
fun map_current f (State (Node {context, facts, mode, goal}, nodes)) =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   192
  State (make_node (f (context, facts, mode, goal)), nodes);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   193
12045
bfaa23b19f47 theorem(_i): locale elements;
wenzelm
parents: 12015
diff changeset
   194
fun init_state thy =
bfaa23b19f47 theorem(_i): locale elements;
wenzelm
parents: 12015
diff changeset
   195
  State (make_node (ProofContext.init thy, None, Forward, None), []);
bfaa23b19f47 theorem(_i): locale elements;
wenzelm
parents: 12015
diff changeset
   196
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   197
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   198
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   199
(** basic proof state operations **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   200
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   201
(* context *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   202
7176
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   203
fun context_of (State (Node {context, ...}, _)) = context;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   204
val theory_of = ProofContext.theory_of o context_of;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   205
val sign_of = ProofContext.sign_of o context_of;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   206
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   207
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
   208
7176
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   209
fun map_context_result f (state as State (Node {context, facts, mode, goal}, nodes)) =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   210
  let val (context', result) = f context
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   211
  in (State (make_node (context', facts, mode, goal), nodes), result) end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   212
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   213
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   214
fun put_data kind f = map_context o ProofContext.put_data kind f;
7924
5fee69b1f5fe warn_extra_tfrees;
wenzelm
parents: 7669
diff changeset
   215
val warn_extra_tfrees = map_context o ProofContext.warn_extra_tfrees o context_of;
10809
e827c779ae2e moved norm_hhf_tac to Pure/tactic.ML;
wenzelm
parents: 10553
diff changeset
   216
val add_binds_i = map_context o ProofContext.add_binds_i;
6790
0a39f22f847a auto_bind_goal, auto_bind_facts;
wenzelm
parents: 6776
diff changeset
   217
val auto_bind_goal = map_context o ProofContext.auto_bind_goal;
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   218
val auto_bind_facts = map_context o ProofContext.auto_bind_facts;
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 6030
diff changeset
   219
val put_thms = map_context o ProofContext.put_thms;
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 6030
diff changeset
   220
val put_thmss = map_context o ProofContext.put_thmss;
7605
8bbfcb54054e added reset_thms;
wenzelm
parents: 7580
diff changeset
   221
val reset_thms = map_context o ProofContext.reset_thms;
8374
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   222
val get_case = ProofContext.get_case o context_of;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   223
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   224
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   225
(* facts *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   226
7176
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   227
fun the_facts (State (Node {facts = Some facts, ...}, _)) = facts
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   228
  | the_facts state = raise STATE ("No current facts available", state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   229
9469
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
   230
fun the_fact state =
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
   231
  (case the_facts state of
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
   232
    [thm] => thm
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
   233
  | _ => raise STATE ("Single fact expected", state));
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
   234
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   235
fun assert_facts state = (the_facts state; state);
7176
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   236
fun get_facts (State (Node {facts, ...}, _)) = facts;
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   237
7605
8bbfcb54054e added reset_thms;
wenzelm
parents: 7580
diff changeset
   238
8bbfcb54054e added reset_thms;
wenzelm
parents: 7580
diff changeset
   239
val thisN = "this";
8bbfcb54054e added reset_thms;
wenzelm
parents: 7580
diff changeset
   240
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   241
fun put_facts facts state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   242
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   243
  |> map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal))
7605
8bbfcb54054e added reset_thms;
wenzelm
parents: 7580
diff changeset
   244
  |> (case facts of None => reset_thms thisN | Some ths => put_thms (thisN, ths));
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   245
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   246
val reset_facts = put_facts None;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   247
9196
1f6f66fe777a facts: handle multiple lists of arguments;
wenzelm
parents: 8991
diff changeset
   248
fun these_factss (state, named_factss) =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   249
  state
9196
1f6f66fe777a facts: handle multiple lists of arguments;
wenzelm
parents: 8991
diff changeset
   250
  |> put_thmss named_factss
1f6f66fe777a facts: handle multiple lists of arguments;
wenzelm
parents: 8991
diff changeset
   251
  |> put_facts (Some (flat (map #2 named_factss)));
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   252
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   253
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   254
(* goal *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   255
10553
7f239ef89c50 removed get_goal;
wenzelm
parents: 10508
diff changeset
   256
local
7f239ef89c50 removed get_goal;
wenzelm
parents: 10508
diff changeset
   257
  fun find i (state as State (Node {goal = Some goal, ...}, _)) = (context_of state, (i, goal))
7f239ef89c50 removed get_goal;
wenzelm
parents: 10508
diff changeset
   258
    | find i (State (Node {goal = None, ...}, node :: nodes)) = find (i + 1) (State (node, nodes))
7f239ef89c50 removed get_goal;
wenzelm
parents: 10508
diff changeset
   259
    | find _ (state as State (_, [])) = err_malformed "find_goal" state;
7f239ef89c50 removed get_goal;
wenzelm
parents: 10508
diff changeset
   260
in val find_goal = find 0 end;
7176
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   261
11079
a378479996f7 val get_goal: state -> context * (thm list * thm);
wenzelm
parents: 10938
diff changeset
   262
fun get_goal state =
a378479996f7 val get_goal: state -> context * (thm list * thm);
wenzelm
parents: 10938
diff changeset
   263
  let val (ctxt, (_, ((_, x), _))) = find_goal state
a378479996f7 val get_goal: state -> context * (thm list * thm);
wenzelm
parents: 10938
diff changeset
   264
  in (ctxt, x) end;
a378479996f7 val get_goal: state -> context * (thm list * thm);
wenzelm
parents: 10938
diff changeset
   265
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   266
fun put_goal goal = map_current (fn (ctxt, facts, mode, _) => (ctxt, facts, mode, goal));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   267
8374
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   268
fun map_goal f g (State (Node {context, facts, mode, goal = Some goal}, nodes)) =
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   269
      State (make_node (f context, facts, mode, Some (g goal)), nodes)
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   270
  | map_goal f g (State (nd, node :: nodes)) =
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   271
      let val State (node', nodes') = map_goal f g (State (node, nodes))
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   272
      in map_context f (State (nd, node' :: nodes')) end
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   273
  | map_goal _ _ state = state;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   274
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   275
fun goal_facts get state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   276
  state
8374
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   277
  |> map_goal I (fn ((result, (_, thm)), f) => ((result, (get state, thm)), f));
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   278
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   279
fun use_facts state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   280
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   281
  |> goal_facts the_facts
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   282
  |> reset_facts;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   283
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   284
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   285
(* mode *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   286
7176
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   287
fun get_mode (State (Node {mode, ...}, _)) = mode;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   288
fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal));
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   289
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   290
val enter_forward = put_mode Forward;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   291
val enter_forward_chain = put_mode ForwardChain;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   292
val enter_backward = put_mode Backward;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   293
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   294
fun assert_mode pred state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   295
  let val mode = get_mode state in
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   296
    if pred mode then state
12010
e1d4df962ac9 theorem(_i): locale argument;
wenzelm
parents: 12000
diff changeset
   297
    else raise STATE ("Illegal application of proof command in "
e1d4df962ac9 theorem(_i): locale argument;
wenzelm
parents: 12000
diff changeset
   298
      ^ quote (mode_name mode) ^ " mode", state)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   299
  end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   300
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   301
fun is_chain state = get_mode state = ForwardChain;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   302
val assert_forward = assert_mode (equal Forward);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   303
val assert_forward_or_chain = assert_mode (equal Forward orf equal ForwardChain);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   304
val assert_backward = assert_mode (equal Backward);
8206
e50a130ec9af assert_no_chain;
wenzelm
parents: 8186
diff changeset
   305
val assert_no_chain = assert_mode (not_equal ForwardChain);
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   306
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   307
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   308
(* blocks *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   309
6776
55f1e6b639a4 added the_fact, level;
wenzelm
parents: 6756
diff changeset
   310
fun level (State (_, nodes)) = length nodes;
55f1e6b639a4 added the_fact, level;
wenzelm
parents: 6756
diff changeset
   311
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   312
fun open_block (State (node, nodes)) = State (node, node :: nodes);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   313
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   314
fun new_block state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   315
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   316
  |> open_block
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   317
  |> put_goal None;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   318
7487
c0f9b956a3e7 close_block: removed ProofContext.transfer_used_names;
wenzelm
parents: 7478
diff changeset
   319
fun close_block (state as State (_, node :: nodes)) = State (node, nodes)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   320
  | close_block state = raise STATE ("Unbalanced block parentheses", state);
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
12423
7fd447422dee tuned print_state interfaces;
wenzelm
parents: 12308
diff changeset
   324
(** pretty_state **)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   325
6529
0f4c2ebc5018 improved print_state;
wenzelm
parents: 6404
diff changeset
   326
val verbose = ProofContext.verbose;
0f4c2ebc5018 improved print_state;
wenzelm
parents: 6404
diff changeset
   327
12085
235576bea937 pretty_goals_local: observes context syntax;
wenzelm
parents: 12065
diff changeset
   328
fun pretty_goals_local ctxt = Display.pretty_goals_aux
235576bea937 pretty_goals_local: observes context syntax;
wenzelm
parents: 12065
diff changeset
   329
  (ProofContext.pretty_sort ctxt, ProofContext.pretty_typ ctxt, ProofContext.pretty_term ctxt);
235576bea937 pretty_goals_local: observes context syntax;
wenzelm
parents: 12065
diff changeset
   330
12055
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 12045
diff changeset
   331
fun pretty_facts _ _ None = []
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 12045
diff changeset
   332
  | pretty_facts s ctxt (Some ths) =
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 12045
diff changeset
   333
      [Pretty.big_list (s ^ "this:") (map (ProofContext.pretty_thm ctxt) ths), Pretty.str ""];
6756
fe6eb161df3e improved print_state;
wenzelm
parents: 6752
diff changeset
   334
12423
7fd447422dee tuned print_state interfaces;
wenzelm
parents: 12308
diff changeset
   335
fun pretty_state nr (state as State (Node {context = ctxt, facts, mode, goal = _}, nodes)) =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   336
  let
11891
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   337
    val ref (_, _, begin_goal) = Display.current_goals_markers;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   338
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   339
    fun levels_up 0 = ""
7575
e1e2d07287d8 improved output;
wenzelm
parents: 7556
diff changeset
   340
      | levels_up 1 = ", 1 level up"
e1e2d07287d8 improved output;
wenzelm
parents: 7556
diff changeset
   341
      | levels_up i = ", " ^ string_of_int i ^ " levels up";
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   342
11556
8e0768929662 print_state: subgoals;
wenzelm
parents: 11549
diff changeset
   343
    fun subgoals 0 = ""
8e0768929662 print_state: subgoals;
wenzelm
parents: 11549
diff changeset
   344
      | subgoals 1 = ", 1 subgoal"
8e0768929662 print_state: subgoals;
wenzelm
parents: 11549
diff changeset
   345
      | subgoals n = ", " ^ string_of_int n ^ " subgoals";
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   346
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   347
    fun prt_names names =
12242
282a92bc5655 multi_theorem: common statement header (covers *all* results);
wenzelm
parents: 12223
diff changeset
   348
      (case filter_out (equal "") names of [] => ""
12308
4e7c3f45a083 print_state: up to 7 result names;
wenzelm
parents: 12244
diff changeset
   349
      | nms => " " ^ enclose "(" ")" (space_implode " and " (take (7, nms) @
4e7c3f45a083 print_state: up to 7 result names;
wenzelm
parents: 12244
diff changeset
   350
          (if length nms > 7 then ["..."] else []))));
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   351
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   352
    fun prt_goal (_, (i, (((kind, names, _), (goal_facts, thm)), _))) =
12085
235576bea937 pretty_goals_local: observes context syntax;
wenzelm
parents: 12065
diff changeset
   353
      pretty_facts "using " ctxt
8462
7f4e4e875c13 pretty chunks;
wenzelm
parents: 8450
diff changeset
   354
        (if mode <> Backward orelse null goal_facts then None else Some goal_facts) @
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   355
      [Pretty.str ("goal (" ^ kind_name (sign_of state) kind ^ prt_names names ^
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   356
          levels_up (i div 2) ^ subgoals (Thm.nprems_of thm) ^ "):")] @
12085
235576bea937 pretty_goals_local: observes context syntax;
wenzelm
parents: 12065
diff changeset
   357
      pretty_goals_local ctxt begin_goal (true, true) (! Display.goals_limit) thm;
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   358
8462
7f4e4e875c13 pretty chunks;
wenzelm
parents: 8450
diff changeset
   359
    val ctxt_prts =
12085
235576bea937 pretty_goals_local: observes context syntax;
wenzelm
parents: 12065
diff changeset
   360
      if ! verbose orelse mode = Forward then ProofContext.pretty_context ctxt
235576bea937 pretty_goals_local: observes context syntax;
wenzelm
parents: 12065
diff changeset
   361
      else if mode = Backward then ProofContext.pretty_asms ctxt
7575
e1e2d07287d8 improved output;
wenzelm
parents: 7556
diff changeset
   362
      else [];
8462
7f4e4e875c13 pretty chunks;
wenzelm
parents: 8450
diff changeset
   363
7f4e4e875c13 pretty chunks;
wenzelm
parents: 8450
diff changeset
   364
    val prts =
8582
3051aa8aa412 tuned output;
wenzelm
parents: 8561
diff changeset
   365
     [Pretty.str ("proof (" ^ mode_name mode ^ "): step " ^ string_of_int nr ^
12010
e1d4df962ac9 theorem(_i): locale argument;
wenzelm
parents: 12000
diff changeset
   366
        (if ! verbose then ", depth " ^ string_of_int (length nodes div 2 - 1)
8561
2675e2f4dc61 tuned output;
wenzelm
parents: 8542
diff changeset
   367
        else "")), Pretty.str ""] @
8462
7f4e4e875c13 pretty chunks;
wenzelm
parents: 8450
diff changeset
   368
     (if null ctxt_prts then [] else ctxt_prts @ [Pretty.str ""]) @
7f4e4e875c13 pretty chunks;
wenzelm
parents: 8450
diff changeset
   369
     (if ! verbose orelse mode = Forward then
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   370
       (pretty_facts "" ctxt facts @ prt_goal (find_goal state))
12085
235576bea937 pretty_goals_local: observes context syntax;
wenzelm
parents: 12065
diff changeset
   371
      else if mode = ForwardChain then pretty_facts "picking " ctxt facts
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   372
      else prt_goal (find_goal state))
12423
7fd447422dee tuned print_state interfaces;
wenzelm
parents: 12308
diff changeset
   373
  in prts end;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   374
12085
235576bea937 pretty_goals_local: observes context syntax;
wenzelm
parents: 12065
diff changeset
   375
fun pretty_goals main state =
235576bea937 pretty_goals_local: observes context syntax;
wenzelm
parents: 12065
diff changeset
   376
  let val (ctxt, (_, ((_, (_, thm)), _))) = find_goal state
235576bea937 pretty_goals_local: observes context syntax;
wenzelm
parents: 12065
diff changeset
   377
  in pretty_goals_local ctxt "" (false, main) (! Display.goals_limit) thm end;
10320
26d4b84eb047 added pretty_goals;
wenzelm
parents: 9482
diff changeset
   378
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   379
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   380
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   381
(** proof steps **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   382
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   383
(* datatype method *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   384
8374
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   385
datatype method =
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   386
  Method of thm list -> thm -> (thm * (string * RuleCases.T) list) Seq.seq;
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   387
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   388
fun method tac = Method (fn facts => fn st => Seq.map (rpair []) (tac facts st));
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   389
val method_cases = Method;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   390
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   391
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   392
(* refine goal *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   393
8234
36a85a6c4852 refine_end;
wenzelm
parents: 8206
diff changeset
   394
local
36a85a6c4852 refine_end;
wenzelm
parents: 8206
diff changeset
   395
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   396
fun check_sign sg state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   397
  if Sign.subsig (sg, sign_of state) then state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   398
  else raise STATE ("Bad signature of result: " ^ Sign.str_of_sg sg, state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   399
8234
36a85a6c4852 refine_end;
wenzelm
parents: 8206
diff changeset
   400
fun gen_refine current_context meth_fun state =
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   401
  let
10553
7f239ef89c50 removed get_goal;
wenzelm
parents: 10508
diff changeset
   402
    val (goal_ctxt, (_, ((result, (facts, thm)), f))) = find_goal state;
8234
36a85a6c4852 refine_end;
wenzelm
parents: 8206
diff changeset
   403
    val Method meth = meth_fun (if current_context then context_of state else goal_ctxt);
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   404
8374
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   405
    fun refn (thm', cases) =
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   406
      state
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   407
      |> check_sign (Thm.sign_of_thm thm')
8374
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   408
      |> map_goal (ProofContext.add_cases cases) (K ((result, (facts, thm')), f));
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   409
  in Seq.map refn (meth facts thm) end;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   410
8234
36a85a6c4852 refine_end;
wenzelm
parents: 8206
diff changeset
   411
in
36a85a6c4852 refine_end;
wenzelm
parents: 8206
diff changeset
   412
36a85a6c4852 refine_end;
wenzelm
parents: 8206
diff changeset
   413
val refine = gen_refine true;
36a85a6c4852 refine_end;
wenzelm
parents: 8206
diff changeset
   414
val refine_end = gen_refine false;
36a85a6c4852 refine_end;
wenzelm
parents: 8206
diff changeset
   415
36a85a6c4852 refine_end;
wenzelm
parents: 8206
diff changeset
   416
end;
36a85a6c4852 refine_end;
wenzelm
parents: 8206
diff changeset
   417
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   418
11917
9a0a736d820b moved RANGE to tctical.ML;
wenzelm
parents: 11906
diff changeset
   419
(* goal addressing *)
6932
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
   420
8152
ce3387fafebb replaced FIRSTGOAL by FINDGOAL (backtracking!);
wenzelm
parents: 8095
diff changeset
   421
fun FINDGOAL tac st =
8374
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   422
  let fun find (i, n) = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1, n))
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   423
  in find (1, Thm.nprems_of st) st end;
8152
ce3387fafebb replaced FIRSTGOAL by FINDGOAL (backtracking!);
wenzelm
parents: 8095
diff changeset
   424
8166
97414b447a02 added HEADGOAL;
wenzelm
parents: 8152
diff changeset
   425
fun HEADGOAL tac = tac 1;
97414b447a02 added HEADGOAL;
wenzelm
parents: 8152
diff changeset
   426
9469
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
   427
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
   428
(* export results *)
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
   429
11816
545aab7410ac simplified exporter interface;
wenzelm
parents: 11793
diff changeset
   430
fun refine_tac rule =
12703
af5fec8a418f refine_tac: Tactic.norm_hhf_tac before trying rule;
wenzelm
parents: 12698
diff changeset
   431
  Tactic.norm_hhf_tac THEN' Tactic.rtac rule THEN_ALL_NEW (SUBGOAL (fn (goal, i) =>
11816
545aab7410ac simplified exporter interface;
wenzelm
parents: 11793
diff changeset
   432
    if can Logic.dest_goal (Logic.strip_assums_concl goal) then
545aab7410ac simplified exporter interface;
wenzelm
parents: 11793
diff changeset
   433
      Tactic.etac Drule.triv_goal i
545aab7410ac simplified exporter interface;
wenzelm
parents: 11793
diff changeset
   434
    else all_tac));
545aab7410ac simplified exporter interface;
wenzelm
parents: 11793
diff changeset
   435
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   436
fun export_goal inner print_rule raw_rule state =
6932
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
   437
  let
10553
7f239ef89c50 removed get_goal;
wenzelm
parents: 10508
diff changeset
   438
    val (outer, (_, ((result, (facts, thm)), f))) = find_goal state;
12010
e1d4df962ac9 theorem(_i): locale argument;
wenzelm
parents: 12000
diff changeset
   439
    val exp_tac = ProofContext.export true inner outer;
9469
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
   440
    fun exp_rule rule =
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
   441
      let
12055
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 12045
diff changeset
   442
        val _ = print_rule outer rule;
11816
545aab7410ac simplified exporter interface;
wenzelm
parents: 11793
diff changeset
   443
        val thmq = FINDGOAL (refine_tac rule) thm;
9469
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
   444
      in Seq.map (fn th => map_goal I (K ((result, (facts, th)), f)) state) thmq end;
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
   445
  in raw_rule |> exp_tac |> (Seq.flat o Seq.map exp_rule) end;
6932
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
   446
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   447
fun export_goals inner print_rule raw_rules =
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   448
  Seq.EVERY (map (export_goal inner print_rule) raw_rules);
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   449
6932
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
   450
fun transfer_facts inner_state state =
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
   451
  (case get_facts inner_state of
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
   452
    None => Seq.single (reset_facts state)
8617
33e2bd53aec3 support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents: 8582
diff changeset
   453
  | Some thms =>
33e2bd53aec3 support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents: 8582
diff changeset
   454
      let
12010
e1d4df962ac9 theorem(_i): locale argument;
wenzelm
parents: 12000
diff changeset
   455
        val exp_tac = ProofContext.export false (context_of inner_state) (context_of state);
11816
545aab7410ac simplified exporter interface;
wenzelm
parents: 11793
diff changeset
   456
        val thmqs = map exp_tac thms;
8617
33e2bd53aec3 support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents: 8582
diff changeset
   457
      in Seq.map (fn ths => put_facts (Some ths) state) (Seq.commute thmqs) end);
6932
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
   458
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
   459
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
   460
(* prepare result *)
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
   461
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   462
fun prep_result state ts raw_th =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   463
  let
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   464
    val ctxt = context_of state;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   465
    fun err msg = raise STATE (msg, state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   466
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   467
    val ngoals = Thm.nprems_of raw_th;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   468
    val _ =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   469
      if ngoals = 0 then ()
12085
235576bea937 pretty_goals_local: observes context syntax;
wenzelm
parents: 12065
diff changeset
   470
      else (err (Pretty.string_of (Pretty.chunks (pretty_goals_local ctxt "" (true, true)
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   471
            (! Display.goals_limit) raw_th @
12085
235576bea937 pretty_goals_local: observes context syntax;
wenzelm
parents: 12065
diff changeset
   472
          [Pretty.str (string_of_int ngoals ^ " unsolved goal(s)!")]))));
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   473
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   474
    val {hyps, sign, ...} = Thm.rep_thm raw_th;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   475
    val tsig = Sign.tsig_of sign;
12055
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 12045
diff changeset
   476
    val casms = flat (map #1 (ProofContext.assumptions_of (context_of state)));
11816
545aab7410ac simplified exporter interface;
wenzelm
parents: 11793
diff changeset
   477
    val bad_hyps = Library.gen_rems Term.aconv (hyps, map Thm.term_of casms);
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   478
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   479
    val th = raw_th RS Drule.rev_triv_goal;
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   480
    val ths = Drule.conj_elim_precise (length ts) th
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   481
      handle THM _ => err "Main goal structure corrupted";
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   482
  in
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   483
    conditional (not (null bad_hyps)) (fn () => err ("Additional hypotheses:\n" ^
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   484
        cat_lines (map (ProofContext.string_of_term ctxt) bad_hyps)));
12808
a529b4b91888 RuleCases.make interface based on term instead of thm;
wenzelm
parents: 12760
diff changeset
   485
    conditional (exists (not o Pattern.matches tsig) (ts ~~ map Thm.prop_of ths)) (fn () =>
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   486
      err ("Proved a different theorem: " ^ Pretty.string_of (ProofContext.pretty_thm ctxt th)));
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   487
    ths
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   488
  end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   489
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   490
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   491
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   492
(*** structured proof commands ***)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   493
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   494
(** context **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   495
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   496
(* bind *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   497
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   498
fun gen_bind f x state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   499
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   500
  |> assert_forward
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   501
  |> map_context (f x)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   502
  |> reset_facts;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   503
8617
33e2bd53aec3 support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents: 8582
diff changeset
   504
val match_bind = gen_bind (ProofContext.match_bind false);
33e2bd53aec3 support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents: 8582
diff changeset
   505
val match_bind_i = gen_bind (ProofContext.match_bind_i false);
33e2bd53aec3 support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents: 8582
diff changeset
   506
val let_bind = gen_bind (ProofContext.match_bind true);
33e2bd53aec3 support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents: 8582
diff changeset
   507
val let_bind_i = gen_bind (ProofContext.match_bind_i true);
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   508
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   509
12714
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   510
(* have_thmss *)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   511
12714
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   512
local
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   513
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   514
fun gen_have_thmss f args state =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   515
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   516
  |> assert_forward
12714
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   517
  |> map_context_result (f args)
9196
1f6f66fe777a facts: handle multiple lists of arguments;
wenzelm
parents: 8991
diff changeset
   518
  |> these_factss;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   519
12714
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   520
in
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   521
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   522
val have_thmss = gen_have_thmss ProofContext.have_thmss;
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   523
val have_thmss_i = gen_have_thmss ProofContext.have_thmss_i;
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   524
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   525
fun simple_have_thms name thms = have_thmss_i [((name, []), [(thms, [])])];
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   526
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   527
end;
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   528
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   529
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   530
(* with_thmss *)
9196
1f6f66fe777a facts: handle multiple lists of arguments;
wenzelm
parents: 8991
diff changeset
   531
12714
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   532
local
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   533
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   534
fun gen_with_thmss f args state =
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   535
  let val state' = state |> f args
9196
1f6f66fe777a facts: handle multiple lists of arguments;
wenzelm
parents: 8991
diff changeset
   536
  in state' |> simple_have_thms "" (the_facts state' @ the_facts state) end;
6876
4ae9c47f2b6b have_thmss: more_ths;
wenzelm
parents: 6871
diff changeset
   537
12714
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   538
in
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   539
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   540
val with_thmss = gen_with_thmss have_thmss;
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   541
val with_thmss_i = gen_with_thmss have_thmss_i;
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   542
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   543
end;
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   544
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   545
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   546
(* fix *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   547
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   548
fun gen_fix f xs state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   549
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   550
  |> assert_forward
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   551
  |> map_context (f xs)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   552
  |> reset_facts;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   553
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   554
val fix = gen_fix ProofContext.fix;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   555
val fix_i = gen_fix ProofContext.fix_i;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   556
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   557
11891
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   558
(* assume and presume *)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   559
9469
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
   560
fun gen_assume f exp args state =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   561
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   562
  |> assert_forward
9469
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
   563
  |> map_context_result (f exp args)
11924
b6def266cbb9 simplified ProofContext.assume interface;
wenzelm
parents: 11917
diff changeset
   564
  |> these_factss;
6932
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
   565
7271
442456b2a8bb assume: multiple args;
wenzelm
parents: 7201
diff changeset
   566
val assm = gen_assume ProofContext.assume;
442456b2a8bb assume: multiple args;
wenzelm
parents: 7201
diff changeset
   567
val assm_i = gen_assume ProofContext.assume_i;
11917
9a0a736d820b moved RANGE to tctical.ML;
wenzelm
parents: 11906
diff changeset
   568
val assume = assm ProofContext.export_assume;
9a0a736d820b moved RANGE to tctical.ML;
wenzelm
parents: 11906
diff changeset
   569
val assume_i = assm_i ProofContext.export_assume;
9a0a736d820b moved RANGE to tctical.ML;
wenzelm
parents: 11906
diff changeset
   570
val presume = assm ProofContext.export_presume;
9a0a736d820b moved RANGE to tctical.ML;
wenzelm
parents: 11906
diff changeset
   571
val presume_i = assm_i ProofContext.export_presume;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   572
7271
442456b2a8bb assume: multiple args;
wenzelm
parents: 7201
diff changeset
   573
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   574
11891
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   575
(** local definitions **)
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   576
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   577
fun gen_def fix prep_term prep_pats raw_name atts (x, (raw_rhs, raw_pats)) state =
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   578
  let
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   579
    val _ = assert_forward state;
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   580
    val ctxt = context_of state;
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   581
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   582
    (*rhs*)
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   583
    val name = if raw_name = "" then Thm.def_name x else raw_name;
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   584
    val rhs = prep_term ctxt raw_rhs;
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   585
    val T = Term.fastype_of rhs;
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   586
    val pats = prep_pats T (ProofContext.declare_term rhs ctxt) raw_pats;
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   587
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   588
    (*lhs*)
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   589
    val lhs = ProofContext.bind_skolem ctxt [x] (Free (x, T));
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   590
  in
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   591
    state
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   592
    |> fix [([x], None)]
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   593
    |> match_bind_i [(pats, rhs)]
11917
9a0a736d820b moved RANGE to tctical.ML;
wenzelm
parents: 11906
diff changeset
   594
    |> assm_i ProofContext.export_def [((name, atts), [(Logic.mk_equals (lhs, rhs), ([], []))])]
11891
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   595
  end;
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   596
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   597
val def = gen_def fix ProofContext.read_term ProofContext.read_term_pats;
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   598
val def_i = gen_def fix_i ProofContext.cert_term ProofContext.cert_term_pats;
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   599
99569e5f0ab5 moved local defs to proof.ML (for locales);
wenzelm
parents: 11816
diff changeset
   600
8374
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   601
(* invoke_case *)
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   602
11793
5f0ab6f5c280 support impromptu terminology of cases parameters;
wenzelm
parents: 11742
diff changeset
   603
fun invoke_case (name, xs, atts) state =
9292
c5875175751f invoke_case: bind_skolem;
wenzelm
parents: 9196
diff changeset
   604
  let
10830
d19f9f4c35ee avoid renaming of params in cases;
wenzelm
parents: 10809
diff changeset
   605
    val (state', (lets, asms)) =
11793
5f0ab6f5c280 support impromptu terminology of cases parameters;
wenzelm
parents: 11742
diff changeset
   606
      map_context_result (ProofContext.apply_case (get_case state name xs)) state;
9292
c5875175751f invoke_case: bind_skolem;
wenzelm
parents: 9196
diff changeset
   607
  in
10830
d19f9f4c35ee avoid renaming of params in cases;
wenzelm
parents: 10809
diff changeset
   608
    state'
10809
e827c779ae2e moved norm_hhf_tac to Pure/tactic.ML;
wenzelm
parents: 10553
diff changeset
   609
    |> add_binds_i lets
10830
d19f9f4c35ee avoid renaming of params in cases;
wenzelm
parents: 10809
diff changeset
   610
    |> assume_i [((name, atts), map (rpair ([], [])) asms)]
8374
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   611
  end;
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   612
ffb2eb084078 generalized FINDGOAL, HEADGOAL;
wenzelm
parents: 8239
diff changeset
   613
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   614
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   615
(** goals **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   616
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   617
(* forward chaining *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   618
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   619
fun chain state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   620
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   621
  |> assert_forward
6848
3d82756e1af5 tuned print_state;
wenzelm
parents: 6798
diff changeset
   622
  |> assert_facts
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   623
  |> enter_forward_chain;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   624
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   625
fun from_facts facts state =
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   626
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   627
  |> put_facts (Some facts)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   628
  |> chain;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   629
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   630
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   631
(* setup goals *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   632
11549
e7265e70fd7c renamed "antecedent" case to "rule_context";
wenzelm
parents: 11525
diff changeset
   633
val rule_contextN = "rule_context";
8991
dc70b797827f case 'antecedent';
wenzelm
parents: 8807
diff changeset
   634
12534
5729b8cac4e1 use Locale.read/cert_context_statement;
wenzelm
parents: 12511
diff changeset
   635
fun setup_goal opt_block prepp autofix kind after_qed names raw_propp state =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   636
  let
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   637
    val (state', (propss, gen_binds)) =
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   638
      state
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   639
      |> assert_forward_or_chain
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   640
      |> enter_forward
8617
33e2bd53aec3 support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents: 8582
diff changeset
   641
      |> opt_block
12534
5729b8cac4e1 use Locale.read/cert_context_statement;
wenzelm
parents: 12511
diff changeset
   642
      |> map_context_result (fn ctxt => prepp (ctxt, raw_propp));
12808
a529b4b91888 RuleCases.make interface based on term instead of thm;
wenzelm
parents: 12760
diff changeset
   643
    val sign' = sign_of state';
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   644
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   645
    val props = flat propss;
12808
a529b4b91888 RuleCases.make interface based on term instead of thm;
wenzelm
parents: 12760
diff changeset
   646
    val cprop = Thm.cterm_of sign' (Logic.mk_conjunction_list props);
7556
f3e78ebcf6ba setup_goal: do not insert assumptions;
wenzelm
parents: 7487
diff changeset
   647
    val goal = Drule.mk_triv_goal cprop;
10553
7f239ef89c50 removed get_goal;
wenzelm
parents: 10508
diff changeset
   648
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   649
    val tvars = foldr Term.add_term_tvars (props, []);
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   650
    val vars = foldr Term.add_term_vars (props, []);
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   651
  in
10553
7f239ef89c50 removed get_goal;
wenzelm
parents: 10508
diff changeset
   652
    if null tvars then ()
7f239ef89c50 removed get_goal;
wenzelm
parents: 10508
diff changeset
   653
    else raise STATE ("Goal statement contains illegal schematic type variable(s): " ^
7f239ef89c50 removed get_goal;
wenzelm
parents: 10508
diff changeset
   654
      commas (map (Syntax.string_of_vname o #1) tvars), state);
7f239ef89c50 removed get_goal;
wenzelm
parents: 10508
diff changeset
   655
    if null vars then ()
7f239ef89c50 removed get_goal;
wenzelm
parents: 10508
diff changeset
   656
    else warning ("Goal statement contains unbound schematic variable(s): " ^
12055
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 12045
diff changeset
   657
      commas (map (ProofContext.string_of_term (context_of state')) vars));
5936
406eb27fe53c match_bind(_i): 'as' patterns;
wenzelm
parents: 5918
diff changeset
   658
    state'
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   659
    |> map_context (autofix props)
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   660
    |> put_goal (Some (((kind, names, propss), ([], goal)), after_qed o map_context gen_binds))
12167
74f92a43bac3 empty rule_context for multiple goals;
wenzelm
parents: 12146
diff changeset
   661
    |> map_context (ProofContext.add_cases
12808
a529b4b91888 RuleCases.make interface based on term instead of thm;
wenzelm
parents: 12760
diff changeset
   662
     (if length props = 1 then
a529b4b91888 RuleCases.make interface based on term instead of thm;
wenzelm
parents: 12760
diff changeset
   663
      RuleCases.make true (Thm.sign_of_thm goal, Thm.prop_of goal) [rule_contextN]
12167
74f92a43bac3 empty rule_context for multiple goals;
wenzelm
parents: 12146
diff changeset
   664
      else [(rule_contextN, RuleCases.empty)]))
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   665
    |> auto_bind_goal props
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   666
    |> (if is_chain state then use_facts else reset_facts)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   667
    |> new_block
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   668
    |> enter_backward
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   669
  end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   670
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   671
(*global goals*)
12534
5729b8cac4e1 use Locale.read/cert_context_statement;
wenzelm
parents: 12511
diff changeset
   672
fun global_goal prep kind a raw_locale elems args thy =
12503
52994bfef01b Locale.activate_context;
wenzelm
parents: 12423
diff changeset
   673
  let
12549
65f03a3f7998 tuned interface of ProofContext.generalize;
wenzelm
parents: 12534
diff changeset
   674
    val init = init_state thy;
12534
5729b8cac4e1 use Locale.read/cert_context_statement;
wenzelm
parents: 12511
diff changeset
   675
    val (opt_name, locale_ctxt, elems_ctxt, propp) =
12549
65f03a3f7998 tuned interface of ProofContext.generalize;
wenzelm
parents: 12534
diff changeset
   676
      prep (apsome fst raw_locale) elems (map snd args) (context_of init);
12534
5729b8cac4e1 use Locale.read/cert_context_statement;
wenzelm
parents: 12511
diff changeset
   677
    val locale = (case raw_locale of None => None | Some (_, atts) => Some (the opt_name, atts));
12503
52994bfef01b Locale.activate_context;
wenzelm
parents: 12423
diff changeset
   678
  in
12549
65f03a3f7998 tuned interface of ProofContext.generalize;
wenzelm
parents: 12534
diff changeset
   679
    init
65f03a3f7998 tuned interface of ProofContext.generalize;
wenzelm
parents: 12534
diff changeset
   680
    |> open_block
12511
901c6c477907 tuned locale interface;
wenzelm
parents: 12503
diff changeset
   681
    |> map_context (K locale_ctxt)
12065
5f7f44d5e3dd theorem(_i): locale atts;
wenzelm
parents: 12055
diff changeset
   682
    |> open_block
12511
901c6c477907 tuned locale interface;
wenzelm
parents: 12503
diff changeset
   683
    |> map_context (K elems_ctxt)
12534
5729b8cac4e1 use Locale.read/cert_context_statement;
wenzelm
parents: 12511
diff changeset
   684
    |> setup_goal I ProofContext.bind_propp_schematic_i
5729b8cac4e1 use Locale.read/cert_context_statement;
wenzelm
parents: 12511
diff changeset
   685
      ProofContext.fix_frees (Theorem (kind, a, locale, map (snd o fst) args))
5729b8cac4e1 use Locale.read/cert_context_statement;
wenzelm
parents: 12511
diff changeset
   686
      Seq.single (map (fst o fst) args) propp
12065
5f7f44d5e3dd theorem(_i): locale atts;
wenzelm
parents: 12055
diff changeset
   687
  end;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   688
12534
5729b8cac4e1 use Locale.read/cert_context_statement;
wenzelm
parents: 12511
diff changeset
   689
val multi_theorem = global_goal Locale.read_context_statement;
5729b8cac4e1 use Locale.read/cert_context_statement;
wenzelm
parents: 12511
diff changeset
   690
val multi_theorem_i = global_goal Locale.cert_context_statement;
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   691
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   692
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   693
(*local goals*)
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   694
fun local_goal' prepp kind (check: bool -> state -> state) f args int state =
7928
06a11f8cf573 improved handling of warn_extra_tfrees;
wenzelm
parents: 7924
diff changeset
   695
  state
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   696
  |> setup_goal open_block prepp (K I) (kind (map (snd o fst) args))
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   697
    f (map (fst o fst) args) (map snd args)
10936
a0dc597d9173 show(_i): check goal;
wenzelm
parents: 10830
diff changeset
   698
  |> warn_extra_tfrees state
a0dc597d9173 show(_i): check goal;
wenzelm
parents: 10830
diff changeset
   699
  |> check int;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   700
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   701
fun local_goal prepp kind f args = local_goal' prepp kind (K I) f args false;
10936
a0dc597d9173 show(_i): check goal;
wenzelm
parents: 10830
diff changeset
   702
a0dc597d9173 show(_i): check goal;
wenzelm
parents: 10830
diff changeset
   703
val show = local_goal' ProofContext.bind_propp Show;
a0dc597d9173 show(_i): check goal;
wenzelm
parents: 10830
diff changeset
   704
val show_i = local_goal' ProofContext.bind_propp_i Show;
a0dc597d9173 show(_i): check goal;
wenzelm
parents: 10830
diff changeset
   705
val have = local_goal ProofContext.bind_propp Have;
10380
e5370304d893 tuned names;
wenzelm
parents: 10360
diff changeset
   706
val have_i = local_goal ProofContext.bind_propp_i Have;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   707
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   708
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   709
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   710
(** conclusions **)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   711
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   712
(* current goal *)
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   713
7176
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   714
fun current_goal (State (Node {context, goal = Some goal, ...}, _)) = (context, goal)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   715
  | current_goal state = raise STATE ("No current goal!", state);
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   716
7176
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   717
fun assert_current_goal true (state as State (Node {goal = None, ...}, _)) =
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   718
      raise STATE ("No goal in this block!", state)
7176
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   719
  | assert_current_goal false (state as State (Node {goal = Some _, ...}, _)) =
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   720
      raise STATE ("Goal present in this block!", state)
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   721
  | assert_current_goal _ state = state;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   722
12010
e1d4df962ac9 theorem(_i): locale argument;
wenzelm
parents: 12000
diff changeset
   723
fun assert_bottom b (state as State (_, nodes)) =
e1d4df962ac9 theorem(_i): locale argument;
wenzelm
parents: 12000
diff changeset
   724
  let val bot = (length nodes <= 2) in
e1d4df962ac9 theorem(_i): locale argument;
wenzelm
parents: 12000
diff changeset
   725
    if b andalso not bot then raise STATE ("Not at bottom of proof!", state)
e1d4df962ac9 theorem(_i): locale argument;
wenzelm
parents: 12000
diff changeset
   726
    else if not b andalso bot then raise STATE ("Already at bottom of proof!", state)
e1d4df962ac9 theorem(_i): locale argument;
wenzelm
parents: 12000
diff changeset
   727
    else state
e1d4df962ac9 theorem(_i): locale argument;
wenzelm
parents: 12000
diff changeset
   728
  end;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   729
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   730
val at_bottom = can (assert_bottom true o close_block);
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   731
6932
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
   732
fun end_proof bot state =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   733
  state
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   734
  |> assert_forward
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   735
  |> close_block
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   736
  |> assert_bottom bot
7011
7e8e9a26ba2c export init_state;
wenzelm
parents: 6996
diff changeset
   737
  |> assert_current_goal true
7e8e9a26ba2c export init_state;
wenzelm
parents: 6996
diff changeset
   738
  |> goal_facts (K []);
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   739
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   740
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   741
(* local_qed *)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   742
6982
4d2a3f35af93 added show_hyps flag;
wenzelm
parents: 6950
diff changeset
   743
fun finish_local (print_result, print_rule) state =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   744
  let
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   745
    val (goal_ctxt, (((kind, names, tss), (_, raw_thm)), after_qed)) = current_goal state;
8617
33e2bd53aec3 support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents: 8582
diff changeset
   746
    val outer_state = state |> close_block;
33e2bd53aec3 support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents: 8582
diff changeset
   747
    val outer_ctxt = context_of outer_state;
33e2bd53aec3 support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents: 8582
diff changeset
   748
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   749
    val ts = flat tss;
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   750
    val results = prep_result state ts raw_thm;
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   751
    val resultq =
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   752
      results |> map (ProofContext.export false goal_ctxt outer_ctxt)
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   753
      |> Seq.commute |> Seq.map (Library.unflat tss);
9469
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
   754
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   755
    val (attss, opt_solve) =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   756
      (case kind of
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   757
        Show attss => (attss, export_goals goal_ctxt print_rule results)
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   758
      | Have attss => (attss, Seq.single)
6932
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
   759
      | _ => err_malformed "finish_local" state);
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   760
  in
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   761
    print_result goal_ctxt (kind_name (sign_of state) kind, names ~~ Library.unflat tss results);
8617
33e2bd53aec3 support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents: 8582
diff changeset
   762
    outer_state
12549
65f03a3f7998 tuned interface of ProofContext.generalize;
wenzelm
parents: 12534
diff changeset
   763
    |> auto_bind_facts (ProofContext.generalize goal_ctxt outer_ctxt ts)
9469
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
   764
    |> (fn st => Seq.map (fn res =>
12714
61af28328417 have_thmss vs. have_thmss_i;
wenzelm
parents: 12703
diff changeset
   765
      have_thmss_i ((names ~~ attss) ~~ map (single o Thm.no_attributes) res) st) resultq)
9469
8058d285b1cd export RANGE, hard_asm_tac, soft_asm_tac;
wenzelm
parents: 9292
diff changeset
   766
    |> (Seq.flat o Seq.map opt_solve)
7176
a329a37ed91a local goals: after_qed;
wenzelm
parents: 7011
diff changeset
   767
    |> (Seq.flat o Seq.map after_qed)
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   768
  end;
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   769
6982
4d2a3f35af93 added show_hyps flag;
wenzelm
parents: 6950
diff changeset
   770
fun local_qed finalize print state =
6404
2daaf2943c79 common qed and end of proofs;
wenzelm
parents: 6262
diff changeset
   771
  state
6932
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
   772
  |> end_proof false
6871
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   773
  |> finalize
6982
4d2a3f35af93 added show_hyps flag;
wenzelm
parents: 6950
diff changeset
   774
  |> (Seq.flat o Seq.map (finish_local print));
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   775
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   776
12703
af5fec8a418f refine_tac: Tactic.norm_hhf_tac before trying rule;
wenzelm
parents: 12698
diff changeset
   777
(* global_qed *)
12065
5f7f44d5e3dd theorem(_i): locale atts;
wenzelm
parents: 12055
diff changeset
   778
6950
ab6d35b7283f global_qed: removed alt_name, alt_att;
wenzelm
parents: 6932
diff changeset
   779
fun finish_global state =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   780
  let
12703
af5fec8a418f refine_tac: Tactic.norm_hhf_tac before trying rule;
wenzelm
parents: 12698
diff changeset
   781
    val export = Drule.local_standard ooo ProofContext.export_single;
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   782
    val (goal_ctxt, (((kind, names, tss), (_, raw_thm)), _)) = current_goal state;
12065
5f7f44d5e3dd theorem(_i): locale atts;
wenzelm
parents: 12055
diff changeset
   783
    val locale_ctxt = context_of (state |> close_block);
5f7f44d5e3dd theorem(_i): locale atts;
wenzelm
parents: 12055
diff changeset
   784
    val theory_ctxt = context_of (state |> close_block |> close_block);
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   785
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   786
    val ts = flat tss;
12703
af5fec8a418f refine_tac: Tactic.norm_hhf_tac before trying rule;
wenzelm
parents: 12698
diff changeset
   787
    val locale_results = map (export goal_ctxt locale_ctxt) (prep_result state ts raw_thm);
af5fec8a418f refine_tac: Tactic.norm_hhf_tac before trying rule;
wenzelm
parents: 12698
diff changeset
   788
    val results = map (Drule.strip_shyps_warning o export locale_ctxt theory_ctxt) locale_results;
12242
282a92bc5655 multi_theorem: common statement header (covers *all* results);
wenzelm
parents: 12223
diff changeset
   789
    val (k, (cname, catts), locale, attss) =
12146
8e02a45f77e2 added multi_theorem(_i);
wenzelm
parents: 12096
diff changeset
   790
      (case kind of Theorem x => x | _ => err_malformed "finish_global" state);
12703
af5fec8a418f refine_tac: Tactic.norm_hhf_tac before trying rule;
wenzelm
parents: 12698
diff changeset
   791
12242
282a92bc5655 multi_theorem: common statement header (covers *all* results);
wenzelm
parents: 12223
diff changeset
   792
    val (thy1, res') =
12703
af5fec8a418f refine_tac: Tactic.norm_hhf_tac before trying rule;
wenzelm
parents: 12698
diff changeset
   793
      theory_of state |> Locale.add_thmss_hybrid k
af5fec8a418f refine_tac: Tactic.norm_hhf_tac before trying rule;
wenzelm
parents: 12698
diff changeset
   794
        ((names ~~ attss) ~~ map (single o Thm.no_attributes) (Library.unflat tss results))
af5fec8a418f refine_tac: Tactic.norm_hhf_tac before trying rule;
wenzelm
parents: 12698
diff changeset
   795
        locale (Library.unflat tss locale_results);
12242
282a92bc5655 multi_theorem: common statement header (covers *all* results);
wenzelm
parents: 12223
diff changeset
   796
    val thy2 = thy1 |> PureThy.add_thmss [((cname, flat (map #2 res')), catts)] |> #1;
12244
179f142ffb03 improved treatment of common result name;
wenzelm
parents: 12242
diff changeset
   797
  in (thy2, ((k, cname), res')) end;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   798
10553
7f239ef89c50 removed get_goal;
wenzelm
parents: 10508
diff changeset
   799
(*note: clients should inspect first result only, as backtracking may destroy theory*)
6950
ab6d35b7283f global_qed: removed alt_name, alt_att;
wenzelm
parents: 6932
diff changeset
   800
fun global_qed finalize state =
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   801
  state
6932
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
   802
  |> end_proof true
6871
01866edde713 added check_result;
wenzelm
parents: 6853
diff changeset
   803
  |> finalize
12065
5f7f44d5e3dd theorem(_i): locale atts;
wenzelm
parents: 12055
diff changeset
   804
  |> Seq.map finish_global;
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   805
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   806
6896
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   807
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   808
(** blocks **)
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   809
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   810
(* begin_block *)
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   811
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   812
fun begin_block state =
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   813
  state
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   814
  |> assert_forward
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   815
  |> new_block
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   816
  |> open_block;
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   817
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   818
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   819
(* end_block *)
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   820
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   821
fun end_block state =
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   822
  state
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   823
  |> assert_forward
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   824
  |> close_block
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   825
  |> assert_current_goal false
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   826
  |> close_block
6932
77c14313af51 propp: 'concl' patterns;
wenzelm
parents: 6896
diff changeset
   827
  |> transfer_facts state;
6896
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   828
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   829
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   830
(* next_block *)
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   831
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   832
fun next_block state =
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   833
  state
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   834
  |> assert_forward
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   835
  |> close_block
8718
3ba75b7e1168 next_block: reset_facts;
wenzelm
parents: 8670
diff changeset
   836
  |> new_block
3ba75b7e1168 next_block: reset_facts;
wenzelm
parents: 8670
diff changeset
   837
  |> reset_facts;
6896
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   838
4bdc3600ddae close_block: transfer_used_names;
wenzelm
parents: 6891
diff changeset
   839
5820
ff3c82b47603 Proof states and methods.
wenzelm
parents:
diff changeset
   840
end;
8152
ce3387fafebb replaced FIRSTGOAL by FINDGOAL (backtracking!);
wenzelm
parents: 8095
diff changeset
   841
ce3387fafebb replaced FIRSTGOAL by FINDGOAL (backtracking!);
wenzelm
parents: 8095
diff changeset
   842
structure BasicProof: BASIC_PROOF = Proof;
ce3387fafebb replaced FIRSTGOAL by FINDGOAL (backtracking!);
wenzelm
parents: 8095
diff changeset
   843
open BasicProof;