author | wenzelm |
Tue, 16 May 2006 21:33:18 +0200 | |
changeset 19663 | 459848022d6e |
parent 19585 | 70a1ce3b23ae |
child 19673 | 853f5a3cc06e |
permissions | -rw-r--r-- |
5819 | 1 |
(* Title: Pure/Isar/proof_context.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
19001 | 5 |
The key concept of Isar proof contexts: elevates primitive local |
6 |
reasoning Gamma |- phi to a structured concept, with generic context |
|
7 |
elements, polymorphic abbreviations, and extra-logical data. |
|
5819 | 8 |
*) |
9 |
||
10 |
signature PROOF_CONTEXT = |
|
11 |
sig |
|
17756 | 12 |
type context (*= Context.proof*) |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
13 |
type export |
5819 | 14 |
val theory_of: context -> theory |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
15 |
val init: theory -> context |
19387 | 16 |
val full_name: context -> bstring -> string |
19663 | 17 |
val consts_of: context -> Consts.T |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
18 |
val set_body: bool -> context -> context |
18743 | 19 |
val restore_body: context -> context -> context |
19543 | 20 |
val set_syntax_mode: string * bool -> context -> context |
21 |
val restore_syntax_mode: context -> context -> context |
|
17451 | 22 |
val assms_of: context -> term list |
7557 | 23 |
val prems_of: context -> thm list |
16031
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
24 |
val fact_index_of: context -> FactIndex.T |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
25 |
val is_fixed: context -> string -> bool |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
26 |
val is_known: context -> string -> bool |
17072 | 27 |
val transfer: theory -> context -> context |
19019 | 28 |
val map_theory: (theory -> theory) -> context -> context |
14828 | 29 |
val pretty_term: context -> term -> Pretty.T |
30 |
val pretty_typ: context -> typ -> Pretty.T |
|
31 |
val pretty_sort: context -> sort -> Pretty.T |
|
19411 | 32 |
val pretty_classrel: context -> class list -> Pretty.T |
33 |
val pretty_arity: context -> arity -> Pretty.T |
|
14828 | 34 |
val pp: context -> Pretty.pp |
35 |
val pretty_thm: context -> thm -> Pretty.T |
|
36 |
val pretty_thms: context -> thm list -> Pretty.T |
|
37 |
val pretty_fact: context -> string * thm list -> Pretty.T |
|
17072 | 38 |
val pretty_proof: context -> Proofterm.proof -> Pretty.T |
39 |
val pretty_proof_of: context -> bool -> thm -> Pretty.T |
|
17360
fa1f262dbc4e
added add_view, export_view (supercedes adhoc view arguments);
wenzelm
parents:
17221
diff
changeset
|
40 |
val string_of_typ: context -> typ -> string |
14828 | 41 |
val string_of_term: context -> term -> string |
17860
b4cf247ea0d2
note_thmss, read/cert_vars etc.: natural argument order;
wenzelm
parents:
17756
diff
changeset
|
42 |
val string_of_thm: context -> thm -> string |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
43 |
val used_types: context -> string list |
12414 | 44 |
val default_type: context -> string -> typ option |
5819 | 45 |
val read_typ: context -> string -> typ |
16348 | 46 |
val read_typ_syntax: context -> string -> typ |
47 |
val read_typ_abbrev: context -> string -> typ |
|
5819 | 48 |
val cert_typ: context -> typ -> typ |
16348 | 49 |
val cert_typ_syntax: context -> typ -> typ |
50 |
val cert_typ_abbrev: context -> typ -> typ |
|
10583 | 51 |
val get_skolem: context -> string -> string |
18255 | 52 |
val revert_skolem: context -> string -> string |
9133 | 53 |
val extern_skolem: context -> term -> term |
14720
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
54 |
val read_termTs: context -> (string -> bool) -> (indexname -> typ option) |
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
55 |
-> (indexname -> sort option) -> string list -> (string * typ) list |
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
56 |
-> term list * (indexname * typ) list |
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
57 |
val read_termTs_schematic: context -> (string -> bool) -> (indexname -> typ option) |
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
58 |
-> (indexname -> sort option) -> string list -> (string * typ) list |
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
59 |
-> term list * (indexname * typ) list |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
60 |
val read_term_legacy: context -> string -> term |
5819 | 61 |
val read_term: context -> string -> term |
62 |
val read_prop: context -> string -> term |
|
11925 | 63 |
val read_prop_schematic: context -> string -> term |
8096 | 64 |
val read_term_pats: typ -> context -> string list -> term list |
65 |
val read_prop_pats: context -> string list -> term list |
|
5819 | 66 |
val cert_term: context -> term -> term |
67 |
val cert_prop: context -> term -> term |
|
8096 | 68 |
val cert_term_pats: typ -> context -> term list -> term list |
69 |
val cert_prop_pats: context -> term list -> term list |
|
5819 | 70 |
val declare_term: term -> context -> context |
18770 | 71 |
val infer_type: context -> string -> typ |
72 |
val inferred_param: string -> context -> (string * typ) * context |
|
73 |
val inferred_fixes: context -> (string * typ) list * context |
|
15703 | 74 |
val read_tyname: context -> string -> typ |
75 |
val read_const: context -> string -> term |
|
19019 | 76 |
val rename_frees: context -> term list -> (string * 'a) list -> (string * 'a) list |
7925 | 77 |
val warn_extra_tfrees: context -> context -> context |
12550
32843ad8160a
generalize type variables properly: start with occurrences in objects
wenzelm
parents:
12530
diff
changeset
|
78 |
val generalize: context -> context -> term list -> term list |
19270 | 79 |
val monomorphic: context -> term list -> term list |
19001 | 80 |
val polymorphic: context -> term list -> term list |
19270 | 81 |
val hidden_polymorphism: term -> typ -> (indexname * sort) list |
18785
5ae1f1c1b764
renamed export to export_standard (again!), because it includes Drule.local_standard';
wenzelm
parents:
18770
diff
changeset
|
82 |
val export_standard: context -> context -> thm -> thm |
18042 | 83 |
val exports: context -> context -> thm -> thm Seq.seq |
84 |
val goal_exports: context -> context -> thm -> thm Seq.seq |
|
10810 | 85 |
val drop_schematic: indexname * term option -> indexname * term option |
86 |
val add_binds: (indexname * string option) list -> context -> context |
|
87 |
val add_binds_i: (indexname * term option) list -> context -> context |
|
12147 | 88 |
val auto_bind_goal: term list -> context -> context |
89 |
val auto_bind_facts: term list -> context -> context |
|
18310 | 90 |
val match_bind: bool -> (string list * string) list -> context -> term list * context |
91 |
val match_bind_i: bool -> (term list * term) list -> context -> term list * context |
|
19585 | 92 |
val read_propp: context * (string * string list) list list |
93 |
-> context * (term * term list) list list |
|
94 |
val cert_propp: context * (term * term list) list list |
|
95 |
-> context * (term * term list) list list |
|
96 |
val read_propp_schematic: context * (string * string list) list list |
|
97 |
-> context * (term * term list) list list |
|
98 |
val cert_propp_schematic: context * (term * term list) list list |
|
99 |
-> context * (term * term list) list list |
|
100 |
val bind_propp: context * (string * string list) list list |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
101 |
-> context * (term list list * (context -> context)) |
19585 | 102 |
val bind_propp_i: context * (term * term list) list list |
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
103 |
-> context * (term list list * (context -> context)) |
19585 | 104 |
val bind_propp_schematic: context * (string * string list) list list |
10554 | 105 |
-> context * (term list list * (context -> context)) |
19585 | 106 |
val bind_propp_schematic_i: context * (term * term list) list list |
10554 | 107 |
-> context * (term list list * (context -> context)) |
18042 | 108 |
val fact_tac: thm list -> int -> tactic |
109 |
val some_fact_tac: context -> int -> tactic |
|
15456
956d6acacf89
Specific theorems in a named list of theorems can now be referred to
berghofe
parents:
15452
diff
changeset
|
110 |
val get_thm: context -> thmref -> thm |
956d6acacf89
Specific theorems in a named list of theorems can now be referred to
berghofe
parents:
15452
diff
changeset
|
111 |
val get_thm_closure: context -> thmref -> thm |
956d6acacf89
Specific theorems in a named list of theorems can now be referred to
berghofe
parents:
15452
diff
changeset
|
112 |
val get_thms: context -> thmref -> thm list |
956d6acacf89
Specific theorems in a named list of theorems can now be referred to
berghofe
parents:
15452
diff
changeset
|
113 |
val get_thms_closure: context -> thmref -> thm list |
16031
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
114 |
val valid_thms: context -> string * thm list -> bool |
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
115 |
val lthms_containing: context -> FactIndex.spec -> (string * thm list) list |
16348 | 116 |
val extern_thm: context -> string -> xstring |
19019 | 117 |
val no_base_names: context -> context |
16147 | 118 |
val qualified_names: context -> context |
19062
0fd52e819c24
replaced qualified_force_prefix to sticky_prefix;
wenzelm
parents:
19033
diff
changeset
|
119 |
val sticky_prefix: string -> context -> context |
16147 | 120 |
val restore_naming: context -> context -> context |
13425
119ae829ad9b
support for split assumptions in cases (hyps vs. prems);
wenzelm
parents:
13415
diff
changeset
|
121 |
val hide_thms: bool -> string list -> context -> context |
19079
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
122 |
val put_thms: bool -> string * thm list option -> context -> context |
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
123 |
val put_thms_internal: string * thm list option -> context -> context |
14564 | 124 |
val note_thmss: |
18728 | 125 |
((bstring * attribute list) * (thmref * attribute list) list) list -> |
17860
b4cf247ea0d2
note_thmss, read/cert_vars etc.: natural argument order;
wenzelm
parents:
17756
diff
changeset
|
126 |
context -> (bstring * thm list) list * context |
14564 | 127 |
val note_thmss_i: |
18728 | 128 |
((bstring * attribute list) * (thm list * attribute list) list) list -> |
17860
b4cf247ea0d2
note_thmss, read/cert_vars etc.: natural argument order;
wenzelm
parents:
17756
diff
changeset
|
129 |
context -> (bstring * thm list) list * context |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
130 |
val read_vars: (string * string option * mixfix) list -> context -> |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
131 |
(string * typ option * mixfix) list * context |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
132 |
val cert_vars: (string * typ option * mixfix) list -> context -> |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
133 |
(string * typ option * mixfix) list * context |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
134 |
val read_vars_legacy: (string * string option * mixfix) list -> context -> |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
135 |
(string * typ option * mixfix) list * context |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
136 |
val cert_vars_legacy: (string * typ option * mixfix) list -> context -> |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
137 |
(string * typ option * mixfix) list * context |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
138 |
val add_fixes: (string * string option * mixfix) list -> context -> string list * context |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
139 |
val add_fixes_i: (string * typ option * mixfix) list -> context -> string list * context |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
140 |
val add_fixes_legacy: (string * typ option * mixfix) list -> context -> string list * context |
18809 | 141 |
val invent_fixes: string list -> context -> string list * context |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
142 |
val fix_frees: term -> context -> context |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
143 |
val auto_fixes: context * (term list list * 'a) -> context * (term list list * 'a) |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
144 |
val bind_fixes: string list -> context -> (term -> term) * context |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
145 |
val add_assms: export -> |
19585 | 146 |
((string * attribute list) * (string * string list) list) list -> |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
147 |
context -> (bstring * thm list) list * context |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
148 |
val add_assms_i: export -> |
19585 | 149 |
((string * attribute list) * (term * term list) list) list -> |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
150 |
context -> (bstring * thm list) list * context |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
151 |
val assume_export: export |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
152 |
val presume_export: export |
17360
fa1f262dbc4e
added add_view, export_view (supercedes adhoc view arguments);
wenzelm
parents:
17221
diff
changeset
|
153 |
val add_view: context -> cterm list -> context -> context |
18042 | 154 |
val export_view: cterm list -> context -> context -> thm -> thm |
18609 | 155 |
val add_cases: bool -> (string * RuleCases.T option) list -> context -> context |
156 |
val apply_case: RuleCases.T -> context -> (string * term list) list * context |
|
16147 | 157 |
val get_case: context -> string -> string option list -> RuleCases.T |
19371 | 158 |
val expand_abbrevs: bool -> context -> context |
19663 | 159 |
val add_const_syntax: string * bool -> (string * mixfix) list -> context -> context |
160 |
val add_abbrevs: string * bool -> (bstring * term * mixfix) list -> context -> context |
|
10810 | 161 |
val verbose: bool ref |
162 |
val setmp_verbose: ('a -> 'b) -> 'a -> 'b |
|
12072
4281198fb8cd
local syntax: add_syntax, proper read/pretty functions;
wenzelm
parents:
12066
diff
changeset
|
163 |
val print_syntax: context -> unit |
10810 | 164 |
val print_binds: context -> unit |
12057 | 165 |
val print_lthms: context -> unit |
10810 | 166 |
val print_cases: context -> unit |
167 |
val prems_limit: int ref |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
168 |
val pretty_ctxt: context -> Pretty.T list |
10810 | 169 |
val pretty_context: context -> Pretty.T list |
18809 | 170 |
val debug: bool ref |
171 |
val pprint_context: context -> pprint_args -> unit |
|
5819 | 172 |
end; |
173 |
||
16540 | 174 |
structure ProofContext: PROOF_CONTEXT = |
5819 | 175 |
struct |
176 |
||
16540 | 177 |
type context = Context.proof; |
5819 | 178 |
|
16540 | 179 |
val theory_of = Context.theory_of_proof; |
19001 | 180 |
val tsig_of = Sign.tsig_of o theory_of; |
181 |
||
16540 | 182 |
val init = Context.init_proof; |
12057 | 183 |
|
7270 | 184 |
|
5819 | 185 |
|
16540 | 186 |
(** Isar proof context information **) |
5819 | 187 |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
188 |
type export = bool -> cterm list -> thm -> thm Seq.seq; |
5819 | 189 |
|
16540 | 190 |
datatype ctxt = |
191 |
Ctxt of |
|
19001 | 192 |
{naming: NameSpace.naming, (*local naming conventions*) |
193 |
syntax: LocalSyntax.T, (*local syntax*) |
|
19033
24e251657e56
consts: maintain thy version for efficient transfer;
wenzelm
parents:
19019
diff
changeset
|
194 |
consts: Consts.T * Consts.T, (*global/local consts*) |
19001 | 195 |
fixes: bool * (string * string) list, (*fixes: !!x. _ with proof body flag*) |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
196 |
assms: |
19001 | 197 |
((cterm list * export) list * (*assumes and views: A ==> _*) |
198 |
(string * thm list) list), (*prems: A |- A*) |
|
199 |
binds: (typ * term) Vartab.table, (*term bindings*) |
|
200 |
thms: thm list NameSpace.table * FactIndex.T, (*local thms*) |
|
201 |
cases: (string * (RuleCases.T * bool)) list, (*local contexts*) |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
202 |
defaults: |
19001 | 203 |
typ Vartab.table * (*type constraints*) |
204 |
sort Vartab.table * (*default sorts*) |
|
205 |
string list * (*used type variables*) |
|
206 |
term list Symtab.table}; (*type variable occurrences*) |
|
5819 | 207 |
|
19001 | 208 |
fun make_ctxt (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) = |
209 |
Ctxt {naming = naming, syntax = syntax, consts = consts, fixes = fixes, assms = assms, |
|
210 |
binds = binds, thms = thms, cases = cases, defaults = defaults}; |
|
5819 | 211 |
|
19079
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
212 |
val local_naming = NameSpace.default_naming |> NameSpace.add_path "local"; |
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
213 |
|
16540 | 214 |
structure ContextData = ProofDataFun |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
215 |
( |
16540 | 216 |
val name = "Isar/context"; |
217 |
type T = ctxt; |
|
218 |
fun init thy = |
|
19079
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
219 |
make_ctxt (local_naming, LocalSyntax.init thy, |
19033
24e251657e56
consts: maintain thy version for efficient transfer;
wenzelm
parents:
19019
diff
changeset
|
220 |
(Sign.consts_of thy, Sign.consts_of thy), (false, []), ([], []), |
24e251657e56
consts: maintain thy version for efficient transfer;
wenzelm
parents:
19019
diff
changeset
|
221 |
Vartab.empty, (NameSpace.empty_table, FactIndex.empty), [], |
16540 | 222 |
(Vartab.empty, Vartab.empty, [], Symtab.empty)); |
223 |
fun print _ _ = (); |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
224 |
); |
5819 | 225 |
|
18708 | 226 |
val _ = Context.add_setup ContextData.init; |
5819 | 227 |
|
16540 | 228 |
fun rep_context ctxt = ContextData.get ctxt |> (fn Ctxt args => args); |
5819 | 229 |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
230 |
fun map_context f = |
19001 | 231 |
ContextData.map (fn Ctxt {naming, syntax, consts, fixes, assms, binds, thms, cases, defaults} => |
232 |
make_ctxt (f (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults))); |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
233 |
|
19001 | 234 |
fun map_naming f = |
235 |
map_context (fn (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) => |
|
236 |
(f naming, syntax, consts, fixes, assms, binds, thms, cases, defaults)); |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
237 |
|
19001 | 238 |
fun map_syntax f = |
239 |
map_context (fn (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) => |
|
240 |
(naming, f syntax, consts, fixes, assms, binds, thms, cases, defaults)); |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
241 |
|
19001 | 242 |
fun map_consts f = |
243 |
map_context (fn (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) => |
|
244 |
(naming, syntax, f consts, fixes, assms, binds, thms, cases, defaults)); |
|
245 |
||
246 |
fun map_fixes f = |
|
247 |
map_context (fn (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) => |
|
248 |
(naming, syntax, consts, f fixes, assms, binds, thms, cases, defaults)); |
|
18971 | 249 |
|
19001 | 250 |
fun map_assms f = |
251 |
map_context (fn (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) => |
|
252 |
(naming, syntax, consts, fixes, f assms, binds, thms, cases, defaults)); |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
253 |
|
19001 | 254 |
fun map_binds f = |
255 |
map_context (fn (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) => |
|
256 |
(naming, syntax, consts, fixes, assms, f binds, thms, cases, defaults)); |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
257 |
|
19001 | 258 |
fun map_thms f = |
259 |
map_context (fn (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) => |
|
260 |
(naming, syntax, consts, fixes, assms, binds, f thms, cases, defaults)); |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
261 |
|
19001 | 262 |
fun map_cases f = |
263 |
map_context (fn (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) => |
|
264 |
(naming, syntax, consts, fixes, assms, binds, thms, f cases, defaults)); |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
265 |
|
19001 | 266 |
fun map_defaults f = |
267 |
map_context (fn (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) => |
|
268 |
(naming, syntax, consts, fixes, assms, binds, thms, cases, f defaults)); |
|
269 |
||
270 |
val naming_of = #naming o rep_context; |
|
19387 | 271 |
val full_name = NameSpace.full o naming_of; |
5819 | 272 |
|
16540 | 273 |
val syntax_of = #syntax o rep_context; |
19001 | 274 |
val syn_of = LocalSyntax.syn_of o syntax_of; |
19543 | 275 |
val set_syntax_mode = map_syntax o LocalSyntax.set_mode; |
276 |
val restore_syntax_mode = map_syntax o LocalSyntax.restore_mode o syntax_of; |
|
19001 | 277 |
|
19033
24e251657e56
consts: maintain thy version for efficient transfer;
wenzelm
parents:
19019
diff
changeset
|
278 |
val consts_of = #2 o #consts o rep_context; |
5819 | 279 |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
280 |
val is_body = #1 o #fixes o rep_context; |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
281 |
fun set_body b = map_fixes (fn (_, fixes) => (b, fixes)); |
18743 | 282 |
fun restore_body ctxt = set_body (is_body ctxt); |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
283 |
|
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
284 |
val fixes_of = #2 o #fixes o rep_context; |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
285 |
val fixed_names_of = map #2 o fixes_of; |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
286 |
|
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
287 |
val assumptions_of = #1 o #assms o rep_context; |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19422
diff
changeset
|
288 |
val assms_of = map Thm.term_of o maps #1 o assumptions_of; |
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19422
diff
changeset
|
289 |
val prems_of = maps #2 o #2 o #assms o rep_context; |
5819 | 290 |
|
17860
b4cf247ea0d2
note_thmss, read/cert_vars etc.: natural argument order;
wenzelm
parents:
17756
diff
changeset
|
291 |
val binds_of = #binds o rep_context; |
5819 | 292 |
|
16540 | 293 |
val thms_of = #thms o rep_context; |
19001 | 294 |
val fact_index_of = #2 o thms_of; |
16540 | 295 |
|
296 |
val cases_of = #cases o rep_context; |
|
5819 | 297 |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
298 |
val defaults_of = #defaults o rep_context; |
16540 | 299 |
val type_occs_of = #4 o defaults_of; |
5819 | 300 |
|
19019 | 301 |
fun is_fixed ctxt x = exists (fn (_, y) => x = y) (fixes_of ctxt); |
16894 | 302 |
fun is_known ctxt x = Vartab.defined (#1 (defaults_of ctxt)) (x, ~1) orelse is_fixed ctxt x; |
5819 | 303 |
|
304 |
||
19001 | 305 |
(* transfer *) |
12093 | 306 |
|
19001 | 307 |
fun transfer_syntax thy = |
308 |
map_syntax (LocalSyntax.rebuild thy) #> |
|
19033
24e251657e56
consts: maintain thy version for efficient transfer;
wenzelm
parents:
19019
diff
changeset
|
309 |
map_consts (fn consts as (global_consts, local_consts) => |
24e251657e56
consts: maintain thy version for efficient transfer;
wenzelm
parents:
19019
diff
changeset
|
310 |
let val thy_consts = Sign.consts_of thy in |
24e251657e56
consts: maintain thy version for efficient transfer;
wenzelm
parents:
19019
diff
changeset
|
311 |
if Consts.eq_consts (thy_consts, global_consts) then consts |
24e251657e56
consts: maintain thy version for efficient transfer;
wenzelm
parents:
19019
diff
changeset
|
312 |
else (thy_consts, Consts.merge (thy_consts, local_consts)) |
24e251657e56
consts: maintain thy version for efficient transfer;
wenzelm
parents:
19019
diff
changeset
|
313 |
end); |
17072 | 314 |
|
19001 | 315 |
fun transfer thy = Context.transfer_proof thy #> transfer_syntax thy; |
17072 | 316 |
|
19019 | 317 |
fun map_theory f ctxt = transfer (f (theory_of ctxt)) ctxt; |
318 |
||
12093 | 319 |
|
320 |
||
14828 | 321 |
(** pretty printing **) |
322 |
||
19310 | 323 |
local |
324 |
||
19371 | 325 |
fun rewrite_term thy rews t = |
326 |
if can Term.type_of t then Pattern.rewrite_term thy rews [] t |
|
327 |
else (warning "Printing ill-typed term -- cannot expand abbreviations"; t); |
|
328 |
||
19001 | 329 |
fun pretty_term' abbrevs ctxt t = |
18971 | 330 |
let |
19001 | 331 |
val thy = theory_of ctxt; |
332 |
val syntax = syntax_of ctxt; |
|
18971 | 333 |
val consts = consts_of ctxt; |
19001 | 334 |
val t' = t |
19371 | 335 |
|> K abbrevs ? rewrite_term thy (Consts.abbrevs_of consts (! print_mode @ [""])) |
336 |
|> Sign.extern_term (Consts.extern_early consts) thy |
|
337 |
|> LocalSyntax.extern_term syntax; |
|
338 |
in |
|
339 |
Sign.pretty_term' (Context.Proof ctxt) (LocalSyntax.syn_of syntax) (Consts.extern consts) t' |
|
340 |
end; |
|
18971 | 341 |
|
19310 | 342 |
in |
343 |
||
19001 | 344 |
val pretty_term = pretty_term' true; |
19310 | 345 |
val pretty_term_no_abbrevs = pretty_term' false; |
346 |
||
347 |
end; |
|
348 |
||
16458 | 349 |
fun pretty_typ ctxt T = Sign.pretty_typ (theory_of ctxt) T; |
350 |
fun pretty_sort ctxt S = Sign.pretty_sort (theory_of ctxt) S; |
|
351 |
fun pretty_classrel ctxt cs = Sign.pretty_classrel (theory_of ctxt) cs; |
|
352 |
fun pretty_arity ctxt ar = Sign.pretty_arity (theory_of ctxt) ar; |
|
14828 | 353 |
|
14974
b1ecb7859c99
avoid premature evaluation of syn_of (wastes time in conjunction with pp);
wenzelm
parents:
14901
diff
changeset
|
354 |
fun pp ctxt = Pretty.pp (pretty_term ctxt, pretty_typ ctxt, pretty_sort ctxt, |
b1ecb7859c99
avoid premature evaluation of syn_of (wastes time in conjunction with pp);
wenzelm
parents:
14901
diff
changeset
|
355 |
pretty_classrel ctxt, pretty_arity ctxt); |
14828 | 356 |
|
17451 | 357 |
fun pretty_thm ctxt th = |
358 |
Display.pretty_thm_aux (pp ctxt) false true (assms_of ctxt) th; |
|
14828 | 359 |
|
360 |
fun pretty_thms ctxt [th] = pretty_thm ctxt th |
|
361 |
| pretty_thms ctxt ths = Pretty.blk (0, Pretty.fbreaks (map (pretty_thm ctxt) ths)); |
|
362 |
||
363 |
fun pretty_fact ctxt ("", ths) = pretty_thms ctxt ths |
|
364 |
| pretty_fact ctxt (a, [th]) = |
|
365 |
Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1, pretty_thm ctxt th] |
|
366 |
| pretty_fact ctxt (a, ths) = |
|
367 |
Pretty.block (Pretty.fbreaks (Pretty.str (a ^ ":") :: map (pretty_thm ctxt) ths)); |
|
368 |
||
17072 | 369 |
fun pretty_proof ctxt prf = |
19310 | 370 |
pretty_term_no_abbrevs (ctxt |> transfer_syntax (ProofSyntax.proof_syntax prf (theory_of ctxt))) |
17072 | 371 |
(ProofSyntax.term_of_proof prf); |
372 |
||
373 |
fun pretty_proof_of ctxt full th = |
|
374 |
pretty_proof ctxt (ProofSyntax.proof_of full th); |
|
375 |
||
17860
b4cf247ea0d2
note_thmss, read/cert_vars etc.: natural argument order;
wenzelm
parents:
17756
diff
changeset
|
376 |
val string_of_typ = Pretty.string_of oo pretty_typ; |
b4cf247ea0d2
note_thmss, read/cert_vars etc.: natural argument order;
wenzelm
parents:
17756
diff
changeset
|
377 |
val string_of_term = Pretty.string_of oo pretty_term; |
b4cf247ea0d2
note_thmss, read/cert_vars etc.: natural argument order;
wenzelm
parents:
17756
diff
changeset
|
378 |
val string_of_thm = Pretty.string_of oo pretty_thm; |
b4cf247ea0d2
note_thmss, read/cert_vars etc.: natural argument order;
wenzelm
parents:
17756
diff
changeset
|
379 |
|
14828 | 380 |
|
381 |
||
7663 | 382 |
(** default sorts and types **) |
383 |
||
17412 | 384 |
val def_sort = Vartab.lookup o #2 o defaults_of; |
7663 | 385 |
|
16540 | 386 |
fun def_type ctxt pattern xi = |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
387 |
let val {binds, defaults = (types, _, _, _), ...} = rep_context ctxt in |
17412 | 388 |
(case Vartab.lookup types xi of |
16540 | 389 |
NONE => |
390 |
if pattern then NONE |
|
18953
93903be7ff66
norm_term: Sign.const_expansion, Envir.expand_atom;
wenzelm
parents:
18928
diff
changeset
|
391 |
else Vartab.lookup binds xi |> Option.map (TypeInfer.polymorphicT o #1) |
16540 | 392 |
| some => some) |
393 |
end; |
|
7663 | 394 |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
395 |
val used_types = #3 o defaults_of; |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
396 |
|
17412 | 397 |
fun default_type ctxt x = Vartab.lookup (#1 (defaults_of ctxt)) (x, ~1); |
12414 | 398 |
|
7663 | 399 |
|
5819 | 400 |
|
401 |
(** prepare types **) |
|
402 |
||
9504 | 403 |
local |
404 |
||
405 |
fun read_typ_aux read ctxt s = |
|
18857 | 406 |
read (syn_of ctxt) (Context.Proof ctxt) (def_sort ctxt) s; |
5819 | 407 |
|
10554 | 408 |
fun cert_typ_aux cert ctxt raw_T = |
18678 | 409 |
cert (theory_of ctxt) raw_T handle TYPE (msg, _, _) => error msg; |
9504 | 410 |
|
411 |
in |
|
412 |
||
16348 | 413 |
val read_typ = read_typ_aux Sign.read_typ'; |
414 |
val read_typ_syntax = read_typ_aux Sign.read_typ_syntax'; |
|
415 |
val read_typ_abbrev = read_typ_aux Sign.read_typ_abbrev'; |
|
416 |
val cert_typ = cert_typ_aux Sign.certify_typ; |
|
417 |
val cert_typ_syntax = cert_typ_aux Sign.certify_typ_syntax; |
|
418 |
val cert_typ_abbrev = cert_typ_aux Sign.certify_typ_abbrev; |
|
9504 | 419 |
|
420 |
end; |
|
421 |
||
5819 | 422 |
|
7679 | 423 |
(* internalize Skolem constants *) |
424 |
||
17184 | 425 |
val lookup_skolem = AList.lookup (op =) o fixes_of; |
18187 | 426 |
fun get_skolem ctxt x = the_default x (lookup_skolem ctxt x); |
7679 | 427 |
|
18678 | 428 |
fun no_skolem internal x = |
9291
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
429 |
if can Syntax.dest_skolem x then |
18678 | 430 |
error ("Illegal reference to internal Skolem constant: " ^ quote x) |
12504 | 431 |
else if not internal andalso can Syntax.dest_internal x then |
18678 | 432 |
error ("Illegal reference to internal variable: " ^ quote x) |
7679 | 433 |
else x; |
434 |
||
14720
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
435 |
fun intern_skolem ctxt internal = |
7679 | 436 |
let |
437 |
fun intern (t as Free (x, T)) = |
|
14720
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
438 |
if internal x then t |
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
439 |
else |
18678 | 440 |
(case lookup_skolem ctxt (no_skolem false x) of |
15531 | 441 |
SOME x' => Free (x', T) |
442 |
| NONE => t) |
|
7679 | 443 |
| intern (t $ u) = intern t $ intern u |
444 |
| intern (Abs (x, T, t)) = Abs (x, T, intern t) |
|
445 |
| intern a = a; |
|
446 |
in intern end; |
|
447 |
||
448 |
||
18187 | 449 |
(* externalize Skolem constants -- approximation only! *) |
450 |
||
18255 | 451 |
fun rev_skolem ctxt = |
18187 | 452 |
let val rev_fixes = map Library.swap (fixes_of ctxt) |
453 |
in AList.lookup (op =) rev_fixes end; |
|
9133 | 454 |
|
18255 | 455 |
fun revert_skolem ctxt x = |
456 |
(case rev_skolem ctxt x of |
|
457 |
SOME x' => x' |
|
18375 | 458 |
| NONE => perhaps (try Syntax.dest_skolem) x); |
18255 | 459 |
|
9133 | 460 |
fun extern_skolem ctxt = |
461 |
let |
|
18255 | 462 |
val revert = rev_skolem ctxt; |
9133 | 463 |
fun extern (t as Free (x, T)) = |
18187 | 464 |
(case revert x of |
465 |
SOME x' => Free (if lookup_skolem ctxt x' = SOME x then x' else NameSpace.hidden x', T) |
|
466 |
| NONE => t) |
|
9133 | 467 |
| extern (t $ u) = extern t $ extern u |
468 |
| extern (Abs (x, T, t)) = Abs (x, T, extern t) |
|
469 |
| extern a = a; |
|
470 |
in extern end |
|
471 |
||
8096 | 472 |
|
18187 | 473 |
|
5819 | 474 |
(** prepare terms and propositions **) |
475 |
||
476 |
(* |
|
16501 | 477 |
(1) read / certify wrt. theory of context |
5819 | 478 |
(2) intern Skolem constants |
479 |
(3) expand term bindings |
|
480 |
*) |
|
481 |
||
482 |
||
16501 | 483 |
(* read wrt. theory *) (*exception ERROR*) |
5819 | 484 |
|
18857 | 485 |
fun read_def_termTs freeze pp syn ctxt (types, sorts, used) sTs = |
18971 | 486 |
Sign.read_def_terms' pp (Sign.is_logtype (theory_of ctxt)) syn (consts_of ctxt) |
18857 | 487 |
(Context.Proof ctxt) (types, sorts) used freeze sTs; |
5874 | 488 |
|
18857 | 489 |
fun read_def_termT freeze pp syn ctxt defaults sT = |
490 |
apfst hd (read_def_termTs freeze pp syn ctxt defaults [sT]); |
|
14828 | 491 |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
492 |
fun read_term_thy freeze pp syn thy defaults s = |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
493 |
#1 (read_def_termT freeze pp syn thy defaults (s, TypeInfer.logicT)); |
5874 | 494 |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
495 |
fun read_prop_thy freeze pp syn thy defaults s = |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
496 |
#1 (read_def_termT freeze pp syn thy defaults (s, propT)); |
12072
4281198fb8cd
local syntax: add_syntax, proper read/pretty functions;
wenzelm
parents:
12066
diff
changeset
|
497 |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
498 |
fun read_terms_thy freeze pp syn thy defaults = |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
499 |
#1 o read_def_termTs freeze pp syn thy defaults o map (rpair TypeInfer.logicT); |
12072
4281198fb8cd
local syntax: add_syntax, proper read/pretty functions;
wenzelm
parents:
12066
diff
changeset
|
500 |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
501 |
fun read_props_thy freeze pp syn thy defaults = |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
502 |
#1 o read_def_termTs freeze pp syn thy defaults o map (rpair propT); |
5819 | 503 |
|
504 |
||
19001 | 505 |
(* local abbreviations *) |
5819 | 506 |
|
19371 | 507 |
fun certify_consts ctxt = |
19001 | 508 |
Consts.certify (pp ctxt) (tsig_of ctxt) (consts_of ctxt); |
509 |
||
510 |
fun expand_binds ctxt schematic = |
|
5819 | 511 |
let |
18971 | 512 |
val binds = binds_of ctxt; |
5819 | 513 |
|
19001 | 514 |
fun expand_var (xi, T) = |
18971 | 515 |
(case Vartab.lookup binds xi of |
19422 | 516 |
SOME t => Envir.expand_atom T t |
18971 | 517 |
| NONE => |
518 |
if schematic then Var (xi, T) |
|
519 |
else error ("Unbound schematic variable: " ^ Syntax.string_of_vname xi)); |
|
19001 | 520 |
in Envir.beta_norm o Term.map_aterms (fn Var v => expand_var v | a => a) end; |
5819 | 521 |
|
522 |
||
6550 | 523 |
(* dummy patterns *) |
524 |
||
18310 | 525 |
val prepare_dummies = |
526 |
let val next = ref 1 in |
|
527 |
fn t => |
|
528 |
let val (i, u) = Term.replace_dummy_patterns (! next, t) |
|
529 |
in next := i; u end |
|
530 |
end; |
|
6762 | 531 |
|
18678 | 532 |
fun reject_dummies t = Term.no_dummy_patterns t |
533 |
handle TERM _ => error "Illegal dummy pattern(s) in term"; |
|
6550 | 534 |
|
535 |
||
5819 | 536 |
(* read terms *) |
537 |
||
10554 | 538 |
local |
539 |
||
15531 | 540 |
fun append_env e1 e2 x = (case e2 x of NONE => e1 x | some => some); |
14720
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
541 |
|
16031
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
542 |
fun gen_read' read app pattern schematic |
14720
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
543 |
ctxt internal more_types more_sorts more_used s = |
14174
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
ballarin
parents:
13629
diff
changeset
|
544 |
let |
14720
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
545 |
val types = append_env (def_type ctxt pattern) more_types; |
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
546 |
val sorts = append_env (def_sort ctxt) more_sorts; |
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
547 |
val used = used_types ctxt @ more_used; |
14174
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
ballarin
parents:
13629
diff
changeset
|
548 |
in |
18857 | 549 |
(read (pp ctxt) (syn_of ctxt) ctxt (types, sorts, used) s |
18678 | 550 |
handle TERM (msg, _) => error msg) |
14720
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
551 |
|> app (intern_skolem ctxt internal) |
19371 | 552 |
|> app (certify_consts ctxt) |
19001 | 553 |
|> app (if pattern then I else expand_binds ctxt schematic) |
18678 | 554 |
|> app (if pattern then prepare_dummies else reject_dummies) |
14720
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
555 |
end; |
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
556 |
|
16031
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
557 |
fun gen_read read app pattern schematic ctxt = |
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
558 |
gen_read' read app pattern schematic ctxt (K false) (K NONE) (K NONE) []; |
14720
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
559 |
|
10554 | 560 |
in |
561 |
||
16031
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
562 |
val read_termTs = gen_read' (read_def_termTs false) (apfst o map) false false; |
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
563 |
val read_termTs_schematic = gen_read' (read_def_termTs false) (apfst o map) false true; |
8096 | 564 |
|
14720
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
565 |
fun read_term_pats T ctxt = |
16031
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
566 |
#1 o gen_read (read_def_termTs false) (apfst o map) true false ctxt o map (rpair T); |
8096 | 567 |
val read_prop_pats = read_term_pats propT; |
568 |
||
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
569 |
fun read_term_legacy ctxt = |
16458 | 570 |
gen_read' (read_term_thy true) I false false ctxt (K true) (K NONE) (K NONE) []; |
14720
ceff6d4fb836
cleanup up read functions, include liberal versions;
wenzelm
parents:
14707
diff
changeset
|
571 |
|
19143 | 572 |
val read_term = gen_read (read_term_thy true) I false false; |
573 |
val read_prop = gen_read (read_prop_thy true) I false false; |
|
574 |
val read_prop_schematic = gen_read (read_prop_thy true) I false true; |
|
575 |
val read_terms = gen_read (read_terms_thy true) map false false; |
|
576 |
fun read_props schematic = gen_read (read_props_thy true) map false schematic; |
|
5819 | 577 |
|
10554 | 578 |
end; |
579 |
||
5819 | 580 |
|
581 |
(* certify terms *) |
|
582 |
||
19371 | 583 |
val test = ref (NONE: (context * term) option); |
584 |
||
10554 | 585 |
local |
586 |
||
18971 | 587 |
fun gen_cert prop pattern schematic ctxt t = t |
19371 | 588 |
|> certify_consts ctxt |
19001 | 589 |
|> (if pattern then I else expand_binds ctxt schematic) |
19371 | 590 |
|> (fn t' => #1 (Sign.certify' false prop (pp ctxt) (consts_of ctxt) (theory_of ctxt) t') |
18678 | 591 |
handle TYPE (msg, _, _) => error msg |
592 |
| TERM (msg, _) => error msg); |
|
16501 | 593 |
|
10554 | 594 |
in |
8096 | 595 |
|
18971 | 596 |
val cert_term = gen_cert false false false; |
597 |
val cert_prop = gen_cert true false false; |
|
598 |
val cert_props = map oo gen_cert true false; |
|
10554 | 599 |
|
18971 | 600 |
fun cert_term_pats _ = map o gen_cert false true false; |
601 |
val cert_prop_pats = map o gen_cert true true false; |
|
10554 | 602 |
|
603 |
end; |
|
5819 | 604 |
|
605 |
||
606 |
(* declare terms *) |
|
607 |
||
10381 | 608 |
local |
609 |
||
16861 | 610 |
val ins_types = fold_aterms |
17412 | 611 |
(fn Free (x, T) => Vartab.update ((x, ~1), T) |
612 |
| Var v => Vartab.update v |
|
16861 | 613 |
| _ => I); |
5819 | 614 |
|
16861 | 615 |
val ins_sorts = fold_types (fold_atyps |
19079
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
616 |
(fn TFree (x, S) => Vartab.update ((x, ~1), S) |
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
617 |
| TVar v => Vartab.update v |
16861 | 618 |
| _ => I)); |
5819 | 619 |
|
16861 | 620 |
val ins_used = fold_term_types (fn t => |
621 |
fold_atyps (fn TFree (x, _) => insert (op =) x | _ => I)); |
|
12291 | 622 |
|
16861 | 623 |
val ins_occs = fold_term_types (fn t => |
18953
93903be7ff66
norm_term: Sign.const_expansion, Envir.expand_atom;
wenzelm
parents:
18928
diff
changeset
|
624 |
fold_atyps (fn TFree (x, _) => Symtab.update_list (x, t) | _ => I)); |
5819 | 625 |
|
16861 | 626 |
fun ins_skolem def_ty = fold_rev (fn (x, x') => |
627 |
(case def_ty x' of |
|
17412 | 628 |
SOME T => Vartab.update ((x, ~1), T) |
16861 | 629 |
| NONE => I)); |
5994 | 630 |
|
16031
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
631 |
in |
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
632 |
|
19079
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
633 |
fun declare_syntax t = map_defaults (fn (types, sorts, used, occ) => |
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
634 |
(ins_types t types, |
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
635 |
ins_sorts t sorts, |
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
636 |
ins_used t used, |
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
637 |
occ)); |
10381 | 638 |
|
19001 | 639 |
fun declare_var (x, opt_T, mx) ctxt = |
640 |
let val T = (case opt_T of SOME T => T | NONE => TypeInfer.mixfixT mx) |
|
641 |
in ((x, T, mx), ctxt |> declare_syntax (Free (x, T))) end; |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
642 |
|
16540 | 643 |
fun declare_term t ctxt = |
16031
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
644 |
ctxt |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
645 |
|> declare_syntax t |
12291 | 646 |
|> map_defaults (fn (types, sorts, used, occ) => |
19079
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
647 |
(ins_skolem (fn x => Vartab.lookup types (x, ~1)) (fixes_of ctxt) types, |
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
648 |
sorts, |
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
649 |
used, |
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
650 |
ins_occs t occ)); |
5819 | 651 |
|
18770 | 652 |
end; |
653 |
||
654 |
||
655 |
(* inferred types of parameters *) |
|
656 |
||
657 |
fun infer_type ctxt x = |
|
658 |
(case try (fn () => |
|
18971 | 659 |
Sign.infer_types (pp ctxt) (theory_of ctxt) (consts_of ctxt) (def_type ctxt false) |
660 |
(def_sort ctxt) (used_types ctxt) true ([Free (x, dummyT)], TypeInfer.logicT)) () of |
|
18770 | 661 |
SOME (Free (_, T), _) => T |
662 |
| _ => error ("Failed to infer type of fixed variable " ^ quote x)); |
|
663 |
||
664 |
fun inferred_param x ctxt = |
|
18619 | 665 |
let val T = infer_type ctxt x |
19019 | 666 |
in ((x, T), ctxt |> declare_syntax (Free (x, T))) end; |
18619 | 667 |
|
18770 | 668 |
fun inferred_fixes ctxt = |
669 |
fold_map inferred_param (rev (fixed_names_of ctxt)) ctxt; |
|
5819 | 670 |
|
671 |
||
15703 | 672 |
(* type and constant names *) |
673 |
||
674 |
fun read_tyname ctxt c = |
|
18340 | 675 |
if member (op =) (used_types ctxt) c then |
18187 | 676 |
TFree (c, the_default (Sign.defaultS (theory_of ctxt)) (def_sort ctxt (c, ~1))) |
16458 | 677 |
else Sign.read_tyname (theory_of ctxt) c; |
15703 | 678 |
|
679 |
fun read_const ctxt c = |
|
680 |
(case lookup_skolem ctxt c of |
|
681 |
SOME c' => Free (c', dummyT) |
|
18971 | 682 |
| NONE => Consts.read_const (consts_of ctxt) c); |
15703 | 683 |
|
684 |
||
19019 | 685 |
(* renaming term/type frees *) |
686 |
||
687 |
fun rename_frees ctxt ts frees = |
|
688 |
let |
|
689 |
val (types, sorts, _, _) = defaults_of (ctxt |> fold declare_syntax ts); |
|
690 |
fun rename (x, X) xs = |
|
691 |
let |
|
19274 | 692 |
fun used y = y = "" orelse y = "'" orelse member (op =) xs y orelse |
19019 | 693 |
Vartab.defined types (y, ~1) orelse Vartab.defined sorts (y, ~1); |
694 |
val x' = Term.variant_name used x; |
|
695 |
in ((x', X), x' :: xs) end; |
|
696 |
in #1 (fold_map rename frees []) end; |
|
697 |
||
698 |
||
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
699 |
|
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
700 |
(** Hindley-Milner polymorphism **) |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
701 |
|
7925 | 702 |
(* warn_extra_tfrees *) |
703 |
||
16540 | 704 |
fun warn_extra_tfrees ctxt1 ctxt2 = |
12130
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
wenzelm
parents:
12123
diff
changeset
|
705 |
let |
17451 | 706 |
fun occs_typ a (Type (_, Ts)) = exists (occs_typ a) Ts |
707 |
| occs_typ a (TFree (b, _)) = a = b |
|
708 |
| occs_typ _ (TVar _) = false; |
|
709 |
fun occs_free a (Free (x, _)) = |
|
710 |
(case def_type ctxt1 false (x, ~1) of |
|
711 |
SOME T => if occs_typ a T then I else cons (a, x) |
|
712 |
| NONE => cons (a, x)) |
|
713 |
| occs_free _ _ = I; |
|
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
714 |
|
17451 | 715 |
val occs1 = type_occs_of ctxt1 and occs2 = type_occs_of ctxt2; |
716 |
val extras = Symtab.fold (fn (a, ts) => |
|
717 |
if Symtab.defined occs1 a then I else fold (occs_free a) ts) occs2 []; |
|
18428 | 718 |
val tfrees = map #1 extras |> sort_distinct string_ord; |
719 |
val frees = map #2 extras |> sort_distinct string_ord; |
|
12130
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
wenzelm
parents:
12123
diff
changeset
|
720 |
in |
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
wenzelm
parents:
12123
diff
changeset
|
721 |
if null extras then () |
17451 | 722 |
else warning ("Introduced fixed type variable(s): " ^ commas tfrees ^ " in " ^ |
17860
b4cf247ea0d2
note_thmss, read/cert_vars etc.: natural argument order;
wenzelm
parents:
17756
diff
changeset
|
723 |
space_implode " or " (map (string_of_term ctxt2 o Syntax.free) frees)); |
12130
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
wenzelm
parents:
12123
diff
changeset
|
724 |
ctxt2 |
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
wenzelm
parents:
12123
diff
changeset
|
725 |
end; |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
726 |
|
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
727 |
|
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
728 |
(* generalize type variables *) |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
729 |
|
12550
32843ad8160a
generalize type variables properly: start with occurrences in objects
wenzelm
parents:
12530
diff
changeset
|
730 |
fun generalize_tfrees inner outer = |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
731 |
let |
19310 | 732 |
val extra_fixes = subtract (op =) (fixed_names_of outer) (fixed_names_of inner); |
18340 | 733 |
fun still_fixed (Free (x, _)) = not (member (op =) extra_fixes x) |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
734 |
| still_fixed _ = false; |
16540 | 735 |
val occs_inner = type_occs_of inner; |
736 |
val occs_outer = type_occs_of outer; |
|
16031
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
737 |
fun add a gen = |
16894 | 738 |
if Symtab.defined occs_outer a orelse |
18953
93903be7ff66
norm_term: Sign.const_expansion, Envir.expand_atom;
wenzelm
parents:
18928
diff
changeset
|
739 |
exists still_fixed (Symtab.lookup_list occs_inner a) |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
740 |
then gen else a :: gen; |
16031
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
741 |
in fn tfrees => fold add tfrees [] end; |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
742 |
|
12550
32843ad8160a
generalize type variables properly: start with occurrences in objects
wenzelm
parents:
12530
diff
changeset
|
743 |
fun generalize inner outer ts = |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
744 |
let |
18187 | 745 |
val tfrees = generalize_tfrees inner outer (map #1 (fold Term.add_tfrees ts [])); |
18340 | 746 |
fun gen (x, S) = if member (op =) tfrees x then TVar ((x, 0), S) else TFree (x, S); |
12550
32843ad8160a
generalize type variables properly: start with occurrences in objects
wenzelm
parents:
12530
diff
changeset
|
747 |
in map (Term.map_term_types (Term.map_type_tfree gen)) ts end; |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
748 |
|
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
749 |
|
19001 | 750 |
(* polymorphic terms *) |
751 |
||
19270 | 752 |
fun monomorphic ctxt ts = |
753 |
let |
|
754 |
val tvars = fold Term.add_tvars ts []; |
|
19274 | 755 |
val tfrees = map TFree (rename_frees ctxt ts (map (pair "'" o #2) tvars)); |
19270 | 756 |
val specialize = Term.instantiate ((tvars ~~ tfrees), []); |
757 |
in map specialize ts end; |
|
758 |
||
19001 | 759 |
fun polymorphic ctxt ts = |
760 |
generalize (ctxt |> fold declare_term ts) ctxt ts; |
|
761 |
||
19270 | 762 |
fun hidden_polymorphism t T = |
763 |
let |
|
764 |
val tvarsT = Term.add_tvarsT T []; |
|
765 |
val extra_tvars = Term.fold_types (Term.fold_atyps |
|
766 |
(fn TVar v => if member (op =) tvarsT v then I else insert (op =) v | _ => I)) t []; |
|
767 |
in extra_tvars end; |
|
19001 | 768 |
|
769 |
||
9553 | 770 |
|
771 |
(** export theorems **) |
|
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
772 |
|
18042 | 773 |
fun common_exports is_goal inner outer = |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
774 |
let |
12550
32843ad8160a
generalize type variables properly: start with occurrences in objects
wenzelm
parents:
12530
diff
changeset
|
775 |
val gen = generalize_tfrees inner outer; |
19310 | 776 |
val fixes = subtract (op =) (fixed_names_of outer) (fixed_names_of inner); |
12057 | 777 |
val asms = Library.drop (length (assumptions_of outer), assumptions_of inner); |
11816 | 778 |
val exp_asms = map (fn (cprops, exp) => exp is_goal cprops) asms; |
16948 | 779 |
in |
18255 | 780 |
Goal.norm_hhf_protect |
16948 | 781 |
#> Seq.EVERY (rev exp_asms) |
782 |
#> Seq.map (fn rule => |
|
11816 | 783 |
let |
16992 | 784 |
val thy = Thm.theory_of_thm rule; |
785 |
val prop = Thm.full_prop_of rule; |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19422
diff
changeset
|
786 |
val frees = map (Thm.cterm_of thy) (map_filter (Term.find_free prop) fixes); |
12550
32843ad8160a
generalize type variables properly: start with occurrences in objects
wenzelm
parents:
12530
diff
changeset
|
787 |
val tfrees = gen (Term.add_term_tfree_names (prop, [])); |
11816 | 788 |
in |
789 |
rule |
|
790 |
|> Drule.forall_intr_list frees |
|
18255 | 791 |
|> Goal.norm_hhf_protect |
18138 | 792 |
|> Drule.tvars_intr_list tfrees |> #2 |
11816 | 793 |
end) |
794 |
end; |
|
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
795 |
|
18785
5ae1f1c1b764
renamed export to export_standard (again!), because it includes Drule.local_standard';
wenzelm
parents:
18770
diff
changeset
|
796 |
fun export_standard inner outer = |
18122 | 797 |
let val exp = common_exports false inner outer in |
13378 | 798 |
fn th => |
18122 | 799 |
(case Seq.pull (exp th) of |
15531 | 800 |
SOME (th', _) => th' |> Drule.local_standard |
16850 | 801 |
| NONE => sys_error "Failed to export theorem") |
13378 | 802 |
end; |
12704 | 803 |
|
18122 | 804 |
val exports = common_exports false; |
805 |
val goal_exports = common_exports true; |
|
7925 | 806 |
|
5819 | 807 |
|
15758
07e382399a96
binds/thms: do not store options, but delete from table;
wenzelm
parents:
15750
diff
changeset
|
808 |
|
5819 | 809 |
(** bindings **) |
810 |
||
15758
07e382399a96
binds/thms: do not store options, but delete from table;
wenzelm
parents:
15750
diff
changeset
|
811 |
(* delete_update_binds *) |
07e382399a96
binds/thms: do not store options, but delete from table;
wenzelm
parents:
15750
diff
changeset
|
812 |
|
07e382399a96
binds/thms: do not store options, but delete from table;
wenzelm
parents:
15750
diff
changeset
|
813 |
local |
5819 | 814 |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
815 |
val del_bind = map_binds o Vartab.delete_safe; |
7606 | 816 |
|
15758
07e382399a96
binds/thms: do not store options, but delete from table;
wenzelm
parents:
15750
diff
changeset
|
817 |
fun upd_bind ((x, i), t) = |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
818 |
let |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
819 |
val T = Term.fastype_of t; |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
820 |
val t' = |
19270 | 821 |
if null (hidden_polymorphism t T) then t |
8637 | 822 |
else Var ((x ^ "_has_extra_type_vars_on_rhs", i), T); |
18953
93903be7ff66
norm_term: Sign.const_expansion, Envir.expand_atom;
wenzelm
parents:
18928
diff
changeset
|
823 |
in declare_term t' #> map_binds (Vartab.update ((x, i), (T, t'))) end; |
5819 | 824 |
|
15758
07e382399a96
binds/thms: do not store options, but delete from table;
wenzelm
parents:
15750
diff
changeset
|
825 |
fun del_upd_bind (xi, NONE) = del_bind xi |
07e382399a96
binds/thms: do not store options, but delete from table;
wenzelm
parents:
15750
diff
changeset
|
826 |
| del_upd_bind (xi, SOME t) = upd_bind (xi, t); |
7606 | 827 |
|
15758
07e382399a96
binds/thms: do not store options, but delete from table;
wenzelm
parents:
15750
diff
changeset
|
828 |
in |
07e382399a96
binds/thms: do not store options, but delete from table;
wenzelm
parents:
15750
diff
changeset
|
829 |
|
07e382399a96
binds/thms: do not store options, but delete from table;
wenzelm
parents:
15750
diff
changeset
|
830 |
val delete_update_binds = fold del_upd_bind; |
07e382399a96
binds/thms: do not store options, but delete from table;
wenzelm
parents:
15750
diff
changeset
|
831 |
|
07e382399a96
binds/thms: do not store options, but delete from table;
wenzelm
parents:
15750
diff
changeset
|
832 |
end; |
7606 | 833 |
|
5819 | 834 |
|
8096 | 835 |
(* simult_matches *) |
836 |
||
19585 | 837 |
fun simult_matches _ (_, []) = [] |
838 |
| simult_matches ctxt (t, pats) = |
|
8096 | 839 |
let |
18678 | 840 |
fun fail () = error "Pattern match failed!"; |
10554 | 841 |
|
19585 | 842 |
val pairs = map (rpair t) pats; |
16031
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
843 |
val maxidx = fold (fn (t1, t2) => fn i => |
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
844 |
Int.max (Int.max (Term.maxidx_of_term t1, Term.maxidx_of_term t2), i)) pairs ~1; |
16458 | 845 |
val envs = Unify.smash_unifiers (theory_of ctxt, Envir.empty maxidx, |
10554 | 846 |
map swap pairs); (*prefer assignment of variables from patterns*) |
847 |
val env = |
|
8096 | 848 |
(case Seq.pull envs of |
15531 | 849 |
NONE => fail () |
850 |
| SOME (env, _) => env); (*ignore further results*) |
|
12309
03e9287be350
name space for local thms (export cond_extern, qualified);
wenzelm
parents:
12291
diff
changeset
|
851 |
val domain = |
03e9287be350
name space for local thms (export cond_extern, qualified);
wenzelm
parents:
12291
diff
changeset
|
852 |
filter_out Term.is_replaced_dummy_pattern (map #1 (Drule.vars_of_terms (map #1 pairs))); |
10554 | 853 |
val _ = (*may not assign variables from text*) |
12309
03e9287be350
name space for local thms (export cond_extern, qualified);
wenzelm
parents:
12291
diff
changeset
|
854 |
if null (map #1 (Envir.alist_of env) inter (map #1 (Drule.vars_of_terms (map #2 pairs)))) |
03e9287be350
name space for local thms (export cond_extern, qualified);
wenzelm
parents:
12291
diff
changeset
|
855 |
then () else fail (); |
16948 | 856 |
fun norm_bind (xi, (_, t)) = |
18310 | 857 |
if member (op =) domain xi then SOME (xi, Envir.norm_term env t) else NONE; |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19422
diff
changeset
|
858 |
in map_filter norm_bind (Envir.alist_of env) end; |
8096 | 859 |
|
860 |
||
861 |
(* add_binds(_i) *) |
|
5819 | 862 |
|
7925 | 863 |
local |
864 |
||
16031
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
865 |
fun gen_bind prep (xi as (x, _), raw_t) ctxt = |
15570 | 866 |
ctxt |> delete_update_binds [(xi, Option.map (prep ctxt) raw_t)]; |
5819 | 867 |
|
10810 | 868 |
in |
869 |
||
15531 | 870 |
fun drop_schematic (b as (xi, SOME t)) = if null (Term.term_vars t) then b else (xi, NONE) |
10554 | 871 |
| drop_schematic b = b; |
872 |
||
16031
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
873 |
val add_binds = fold (gen_bind read_term); |
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
874 |
val add_binds_i = fold (gen_bind cert_term); |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
875 |
|
16458 | 876 |
fun auto_bind f ts ctxt = ctxt |> add_binds_i (map drop_schematic (f (theory_of ctxt) ts)); |
12147 | 877 |
val auto_bind_goal = auto_bind AutoBind.goal; |
878 |
val auto_bind_facts = auto_bind AutoBind.facts; |
|
7925 | 879 |
|
880 |
end; |
|
5819 | 881 |
|
882 |
||
8096 | 883 |
(* match_bind(_i) *) |
5819 | 884 |
|
8096 | 885 |
local |
886 |
||
17860
b4cf247ea0d2
note_thmss, read/cert_vars etc.: natural argument order;
wenzelm
parents:
17756
diff
changeset
|
887 |
fun prep_bind prep_pats (raw_pats, t) ctxt = |
5819 | 888 |
let |
8096 | 889 |
val ctxt' = declare_term t ctxt; |
19585 | 890 |
val pats = prep_pats (Term.fastype_of t) ctxt' raw_pats; |
891 |
val binds = simult_matches ctxt' (t, pats); |
|
17860
b4cf247ea0d2
note_thmss, read/cert_vars etc.: natural argument order;
wenzelm
parents:
17756
diff
changeset
|
892 |
in (binds, ctxt') end; |
7670 | 893 |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
894 |
fun gen_binds prep_terms prep_pats gen raw_binds ctxt = |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
895 |
let |
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
896 |
val ts = prep_terms ctxt (map snd raw_binds); |
17860
b4cf247ea0d2
note_thmss, read/cert_vars etc.: natural argument order;
wenzelm
parents:
17756
diff
changeset
|
897 |
val (binds, ctxt') = |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19422
diff
changeset
|
898 |
apfst flat (fold_map (prep_bind prep_pats) (map fst raw_binds ~~ ts) ctxt); |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
899 |
val binds' = |
12550
32843ad8160a
generalize type variables properly: start with occurrences in objects
wenzelm
parents:
12530
diff
changeset
|
900 |
if gen then map #1 binds ~~ generalize ctxt' ctxt (map #2 binds) |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
901 |
else binds; |
15531 | 902 |
val binds'' = map (apsnd SOME) binds'; |
18310 | 903 |
val ctxt'' = |
904 |
warn_extra_tfrees ctxt |
|
905 |
(if gen then |
|
906 |
ctxt (*sic!*) |> fold declare_term (map #2 binds') |> add_binds_i binds'' |
|
907 |
else ctxt' |> add_binds_i binds''); |
|
908 |
in (ts, ctxt'') end; |
|
8096 | 909 |
|
910 |
in |
|
5935 | 911 |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
912 |
val match_bind = gen_binds read_terms read_term_pats; |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
913 |
val match_bind_i = gen_binds (map o cert_term) cert_term_pats; |
8096 | 914 |
|
915 |
end; |
|
5935 | 916 |
|
917 |
||
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
918 |
(* propositions with patterns *) |
5935 | 919 |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
920 |
local |
8096 | 921 |
|
10554 | 922 |
fun prep_propp schematic prep_props prep_pats (context, args) = |
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
923 |
let |
19585 | 924 |
fun prep (_, raw_pats) (ctxt, prop :: props) = |
925 |
let val ctxt' = declare_term prop ctxt |
|
926 |
in ((prop, prep_pats ctxt' raw_pats), (ctxt', props)) end |
|
17860
b4cf247ea0d2
note_thmss, read/cert_vars etc.: natural argument order;
wenzelm
parents:
17756
diff
changeset
|
927 |
| prep _ _ = sys_error "prep_propp"; |
b4cf247ea0d2
note_thmss, read/cert_vars etc.: natural argument order;
wenzelm
parents:
17756
diff
changeset
|
928 |
val (propp, (context', _)) = (fold_map o fold_map) prep args |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19422
diff
changeset
|
929 |
(context, prep_props schematic context (maps (map fst) args)); |
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
930 |
in (context', propp) end; |
5935 | 931 |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
932 |
fun gen_bind_propp prepp (ctxt, raw_args) = |
8096 | 933 |
let |
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
934 |
val (ctxt', args) = prepp (ctxt, raw_args); |
19585 | 935 |
val binds = flat (flat (map (map (simult_matches ctxt')) args)); |
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
936 |
val propss = map (map #1) args; |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
937 |
|
10554 | 938 |
(*generalize result: context evaluated now, binds added later*) |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
939 |
val gen = generalize ctxt' ctxt; |
15531 | 940 |
fun gen_binds c = c |> add_binds_i (map #1 binds ~~ map SOME (gen (map #2 binds))); |
941 |
in (ctxt' |> add_binds_i (map (apsnd SOME) binds), (propss, gen_binds)) end; |
|
8096 | 942 |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
943 |
in |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
944 |
|
11925 | 945 |
val read_propp = prep_propp false read_props read_prop_pats; |
946 |
val cert_propp = prep_propp false cert_props cert_prop_pats; |
|
10554 | 947 |
val read_propp_schematic = prep_propp true read_props read_prop_pats; |
948 |
val cert_propp_schematic = prep_propp true cert_props cert_prop_pats; |
|
949 |
||
11925 | 950 |
val bind_propp = gen_bind_propp read_propp; |
951 |
val bind_propp_i = gen_bind_propp cert_propp; |
|
952 |
val bind_propp_schematic = gen_bind_propp read_propp_schematic; |
|
10554 | 953 |
val bind_propp_schematic_i = gen_bind_propp cert_propp_schematic; |
6789 | 954 |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
955 |
end; |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
956 |
|
6789 | 957 |
|
5819 | 958 |
|
959 |
(** theorems **) |
|
960 |
||
18042 | 961 |
(* fact_tac *) |
962 |
||
18122 | 963 |
fun comp_incr_tac [] _ st = no_tac st |
964 |
| comp_incr_tac (th :: ths) i st = |
|
965 |
(Goal.compose_hhf_tac (Drule.incr_indexes st th) i APPEND comp_incr_tac ths i) st; |
|
18042 | 966 |
|
18122 | 967 |
fun fact_tac facts = Tactic.norm_hhf_tac THEN' comp_incr_tac facts; |
968 |
||
969 |
fun some_fact_tac ctxt = SUBGOAL (fn (goal, i) => |
|
18042 | 970 |
let |
19001 | 971 |
val index = fact_index_of ctxt; |
18042 | 972 |
val facts = FactIndex.could_unify index (Term.strip_all_body goal); |
973 |
in fact_tac facts i end); |
|
974 |
||
975 |
||
6091 | 976 |
(* get_thm(s) *) |
5819 | 977 |
|
18042 | 978 |
fun retrieve_thms _ pick ctxt (Fact s) = |
16501 | 979 |
let |
18042 | 980 |
val thy = theory_of ctxt; |
981 |
val th = Goal.prove thy [] [] (read_prop ctxt s) (K (ALLGOALS (some_fact_tac ctxt))) |
|
18678 | 982 |
handle ERROR msg => cat_error msg "Failed to retrieve literal fact."; |
18042 | 983 |
in pick "" [th] end |
984 |
| retrieve_thms from_thy pick ctxt xthmref = |
|
985 |
let |
|
986 |
val thy = theory_of ctxt; |
|
19001 | 987 |
val (space, tab) = #1 (thms_of ctxt); |
16501 | 988 |
val thmref = PureThy.map_name_of_thmref (NameSpace.intern space) xthmref; |
989 |
val name = PureThy.name_of_thmref thmref; |
|
990 |
in |
|
17412 | 991 |
(case Symtab.lookup tab name of |
16540 | 992 |
SOME ths => map (Thm.transfer thy) (PureThy.select_thm thmref ths) |
993 |
| NONE => from_thy thy xthmref) |> pick name |
|
18042 | 994 |
end; |
5819 | 995 |
|
9566 | 996 |
val get_thm = retrieve_thms PureThy.get_thms PureThy.single_thm; |
997 |
val get_thm_closure = retrieve_thms PureThy.get_thms_closure PureThy.single_thm; |
|
998 |
val get_thms = retrieve_thms PureThy.get_thms (K I); |
|
999 |
val get_thms_closure = retrieve_thms PureThy.get_thms_closure (K I); |
|
5819 | 1000 |
|
1001 |
||
16031
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
1002 |
(* valid_thms *) |
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
1003 |
|
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
1004 |
fun valid_thms ctxt (name, ths) = |
18678 | 1005 |
(case try (fn () => get_thms ctxt (Name name)) () of |
16031
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
1006 |
NONE => false |
16147 | 1007 |
| SOME ths' => Thm.eq_thms (ths, ths')); |
16031
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
1008 |
|
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
1009 |
|
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
1010 |
(* lthms_containing *) |
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
1011 |
|
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
1012 |
fun lthms_containing ctxt spec = |
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
1013 |
FactIndex.find (fact_index_of ctxt) spec |
18043 | 1014 |
|> map ((not o valid_thms ctxt) ? apfst (fn name => |
18042 | 1015 |
NameSpace.hidden (if name = "" then "unnamed" else name))); |
16031
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
1016 |
|
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
wenzelm
parents:
15979
diff
changeset
|
1017 |
|
13425
119ae829ad9b
support for split assumptions in cases (hyps vs. prems);
wenzelm
parents:
13415
diff
changeset
|
1018 |
(* name space operations *) |
12309
03e9287be350
name space for local thms (export cond_extern, qualified);
wenzelm
parents:
12291
diff
changeset
|
1019 |
|
19001 | 1020 |
val extern_thm = NameSpace.extern o #1 o #1 o thms_of; |
12309
03e9287be350
name space for local thms (export cond_extern, qualified);
wenzelm
parents:
12291
diff
changeset
|
1021 |
|
19062
0fd52e819c24
replaced qualified_force_prefix to sticky_prefix;
wenzelm
parents:
19033
diff
changeset
|
1022 |
val no_base_names = map_naming NameSpace.no_base_names; |
16147 | 1023 |
val qualified_names = map_naming NameSpace.qualified_names; |
19062
0fd52e819c24
replaced qualified_force_prefix to sticky_prefix;
wenzelm
parents:
19033
diff
changeset
|
1024 |
val sticky_prefix = map_naming o NameSpace.sticky_prefix; |
0fd52e819c24
replaced qualified_force_prefix to sticky_prefix;
wenzelm
parents:
19033
diff
changeset
|
1025 |
val restore_naming = map_naming o K o naming_of; |
12309
03e9287be350
name space for local thms (export cond_extern, qualified);
wenzelm
parents:
12291
diff
changeset
|
1026 |
|
19001 | 1027 |
fun hide_thms fully names = map_thms (fn ((space, tab), index) => |
1028 |
((fold (NameSpace.hide fully) names space, tab), index)); |
|
13425
119ae829ad9b
support for split assumptions in cases (hyps vs. prems);
wenzelm
parents:
13415
diff
changeset
|
1029 |
|
12309
03e9287be350
name space for local thms (export cond_extern, qualified);
wenzelm
parents:
12291
diff
changeset
|
1030 |
|
17360
fa1f262dbc4e
added add_view, export_view (supercedes adhoc view arguments);
wenzelm
parents:
17221
diff
changeset
|
1031 |
(* put_thms *) |
5819 | 1032 |
|
19079
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
1033 |
fun put_thms _ ("", NONE) ctxt = ctxt |
19143 | 1034 |
| put_thms do_index ("", SOME ths) ctxt = ctxt |> map_thms (fn (facts, index) => |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1035 |
let |
19143 | 1036 |
val index' = FactIndex.add_local do_index (is_known ctxt) ("", ths) index; |
19001 | 1037 |
in (facts, index') end) |
19079
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
1038 |
| put_thms _ (bname, NONE) ctxt = ctxt |> map_thms (fn ((space, tab), index) => |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1039 |
let |
19387 | 1040 |
val name = full_name ctxt bname; |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1041 |
val tab' = Symtab.delete_safe name tab; |
19001 | 1042 |
in ((space, tab'), index) end) |
19079
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
1043 |
| put_thms do_index (bname, SOME ths) ctxt = ctxt |> map_thms (fn ((space, tab), index) => |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1044 |
let |
19387 | 1045 |
val name = full_name ctxt bname; |
19001 | 1046 |
val space' = NameSpace.declare (naming_of ctxt) name space; |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1047 |
val tab' = Symtab.update (name, ths) tab; |
19143 | 1048 |
val index' = FactIndex.add_local do_index (is_known ctxt) (name, ths) index; |
19001 | 1049 |
in ((space', tab'), index') end); |
5819 | 1050 |
|
19079
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
1051 |
fun put_thms_internal thms ctxt = |
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
1052 |
ctxt |> map_naming (K local_naming) |> put_thms false thms |> restore_naming ctxt; |
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
1053 |
|
7606 | 1054 |
|
14564 | 1055 |
(* note_thmss *) |
5819 | 1056 |
|
12711 | 1057 |
local |
16147 | 1058 |
|
17860
b4cf247ea0d2
note_thmss, read/cert_vars etc.: natural argument order;
wenzelm
parents:
17756
diff
changeset
|
1059 |
fun gen_note_thmss get = fold_map (fn ((name, more_attrs), ths_attrs) => fn ctxt => |
5819 | 1060 |
let |
17360
fa1f262dbc4e
added add_view, export_view (supercedes adhoc view arguments);
wenzelm
parents:
17221
diff
changeset
|
1061 |
fun app (th, attrs) (ct, ths) = |
18728 | 1062 |
let val (ct', th') = foldl_map (Thm.proof_attributes (attrs @ more_attrs)) (ct, get ctxt th) |
12711 | 1063 |
in (ct', th' :: ths) end; |
17360
fa1f262dbc4e
added add_view, export_view (supercedes adhoc view arguments);
wenzelm
parents:
17221
diff
changeset
|
1064 |
val (ctxt', rev_thms) = fold app ths_attrs (ctxt, []); |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19422
diff
changeset
|
1065 |
val thms = flat (rev rev_thms); |
19079
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
1066 |
in ((name, thms), ctxt' |> put_thms true (name, SOME thms)) end); |
12711 | 1067 |
|
1068 |
in |
|
1069 |
||
16147 | 1070 |
val note_thmss = gen_note_thmss get_thms; |
1071 |
val note_thmss_i = gen_note_thmss (K I); |
|
15696 | 1072 |
|
1073 |
val note_thmss_accesses = gen_note_thmss get_thms; |
|
1074 |
val note_thmss_accesses_i = gen_note_thmss (K I); |
|
12711 | 1075 |
|
1076 |
end; |
|
9196 | 1077 |
|
5819 | 1078 |
|
1079 |
||
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1080 |
(** parameters **) |
17360
fa1f262dbc4e
added add_view, export_view (supercedes adhoc view arguments);
wenzelm
parents:
17221
diff
changeset
|
1081 |
|
8096 | 1082 |
(* variables *) |
1083 |
||
10381 | 1084 |
local |
1085 |
||
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1086 |
fun prep_vars prep_typ internal legacy = |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1087 |
fold_map (fn (raw_x, raw_T, raw_mx) => fn ctxt => |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1088 |
let |
19371 | 1089 |
val (x, mx) = Syntax.const_mixfix raw_x raw_mx; |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1090 |
val _ = |
18678 | 1091 |
if not legacy andalso not (Syntax.is_identifier (no_skolem internal x)) then |
1092 |
error ("Illegal variable name: " ^ quote x) |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1093 |
else (); |
12504 | 1094 |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1095 |
fun cond_tvars T = |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1096 |
if internal then T |
18678 | 1097 |
else Type.no_tvars T handle TYPE (msg, _, _) => error msg; |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1098 |
val opt_T = Option.map (cond_tvars o prep_typ ctxt) raw_T; |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1099 |
val var = (x, opt_T, mx); |
19001 | 1100 |
in (var, ctxt |> declare_var var |> #2) end); |
8096 | 1101 |
|
10381 | 1102 |
in |
1103 |
||
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1104 |
val read_vars = prep_vars read_typ false false; |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1105 |
val cert_vars = prep_vars cert_typ true false; |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1106 |
val read_vars_legacy = prep_vars read_typ true true; |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1107 |
val cert_vars_legacy = prep_vars cert_typ true true; |
8096 | 1108 |
|
10381 | 1109 |
end; |
1110 |
||
8096 | 1111 |
|
19663 | 1112 |
(* const syntax *) |
1113 |
||
1114 |
fun add_const_syntax prmode args ctxt = |
|
1115 |
ctxt |> map_syntax |
|
1116 |
(LocalSyntax.add_modesyntax (theory_of ctxt) prmode |
|
1117 |
(map (pair false o Consts.syntax (consts_of ctxt)) args)); |
|
1118 |
||
1119 |
||
19001 | 1120 |
(* abbreviations *) |
1121 |
||
19371 | 1122 |
val expand_abbrevs = map_consts o apsnd o Consts.expand_abbrevs; |
1123 |
||
19663 | 1124 |
fun add_abbrevs prmode = fold (fn (raw_c, raw_t, raw_mx) => fn ctxt => |
19001 | 1125 |
let |
19663 | 1126 |
val ([(c, _, mx)], _) = cert_vars [(raw_c, NONE, raw_mx)] ctxt; |
19387 | 1127 |
val (c', b) = Syntax.mixfix_const (full_name ctxt c) mx; |
19663 | 1128 |
val [t] = polymorphic ctxt [cert_term (ctxt |> expand_abbrevs false) raw_t]; |
19019 | 1129 |
val T = Term.fastype_of t; |
19001 | 1130 |
val _ = |
19270 | 1131 |
if null (hidden_polymorphism t T) then () |
19001 | 1132 |
else error ("Extra type variables on rhs of abbreviation: " ^ quote c); |
1133 |
in |
|
1134 |
ctxt |
|
1135 |
|> declare_term t |
|
19387 | 1136 |
|> map_consts |
19663 | 1137 |
(apsnd (Consts.abbreviate (pp ctxt) (tsig_of ctxt) (naming_of ctxt) (#1 prmode) ((c, t), b))) |
1138 |
|> map_syntax (LocalSyntax.add_modesyntax (theory_of ctxt) prmode [(false, (c', T, mx))]) |
|
19001 | 1139 |
end); |
1140 |
||
1141 |
||
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1142 |
(* fixes *) |
5819 | 1143 |
|
8096 | 1144 |
local |
1145 |
||
19001 | 1146 |
fun prep_mixfix (x, T, mx) = |
19019 | 1147 |
if mx <> NoSyn andalso mx <> Structure andalso |
1148 |
(can Syntax.dest_internal x orelse can Syntax.dest_skolem x) then |
|
19001 | 1149 |
error ("Illegal mixfix syntax for internal/skolem constant " ^ quote x) |
1150 |
else (true, (x, T, mx)); |
|
1151 |
||
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1152 |
fun no_dups _ [] = () |
18900 | 1153 |
| no_dups ctxt dups = error ("Duplicate fixed variable(s): " ^ commas_quote dups); |
11925 | 1154 |
|
18844 | 1155 |
fun gen_fixes prep raw_vars ctxt = |
8096 | 1156 |
let |
19371 | 1157 |
val thy = theory_of ctxt; |
1158 |
||
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1159 |
val (ys, zs) = split_list (fixes_of ctxt); |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1160 |
val (vars, ctxt') = prep raw_vars ctxt; |
12130
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
wenzelm
parents:
12123
diff
changeset
|
1161 |
val xs = map #1 vars; |
18971 | 1162 |
val _ = no_dups ctxt (duplicates (op =) xs); |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1163 |
val xs' = |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1164 |
if is_body ctxt then Term.variantlist (map Syntax.skolem xs, zs) |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1165 |
else (no_dups ctxt (xs inter_string ys); no_dups ctxt (xs inter_string zs); xs); |
8096 | 1166 |
in |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1167 |
ctxt' |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1168 |
|> map_fixes (fn (b, fixes) => (b, rev (xs ~~ xs') @ fixes)) |
19001 | 1169 |
|> fold_map declare_var (map2 (fn x' => fn (_, T, mx) => (x', T, mx)) xs' vars) |
19543 | 1170 |
|-> (map_syntax o LocalSyntax.add_syntax thy o map prep_mixfix) |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1171 |
|> pair xs' |
8096 | 1172 |
end; |
5819 | 1173 |
|
8096 | 1174 |
in |
7679 | 1175 |
|
18844 | 1176 |
val add_fixes = gen_fixes read_vars; |
1177 |
val add_fixes_i = gen_fixes cert_vars; |
|
1178 |
val add_fixes_legacy = gen_fixes cert_vars_legacy; |
|
8096 | 1179 |
|
1180 |
end; |
|
5819 | 1181 |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1182 |
|
18844 | 1183 |
(* invent fixes *) |
1184 |
||
1185 |
fun invent_fixes xs ctxt = |
|
1186 |
ctxt |
|
1187 |
|> set_body true |
|
1188 |
|> add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs) |
|
1189 |
||> restore_body ctxt; |
|
1190 |
||
1191 |
||
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1192 |
(* fixes vs. frees *) |
12016 | 1193 |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1194 |
fun fix_frees t ctxt = |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1195 |
let |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1196 |
fun add (Free (x, T)) = if is_fixed ctxt x then I else insert (op =) (x, SOME T, NoSyn) |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1197 |
| add _ = I; |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1198 |
val fixes = rev (fold_aterms add t []); |
18809 | 1199 |
in |
1200 |
ctxt |
|
1201 |
|> declare_term t |
|
1202 |
|> set_body false |
|
1203 |
|> (snd o add_fixes_i fixes) |
|
1204 |
|> restore_body ctxt |
|
1205 |
end; |
|
6895 | 1206 |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1207 |
fun auto_fixes (arg as (ctxt, (propss, x))) = |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1208 |
if is_body ctxt then arg |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1209 |
else ((fold o fold) fix_frees propss ctxt, (propss, x)); |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1210 |
|
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1211 |
fun bind_fixes xs ctxt = |
9291
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
1212 |
let |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1213 |
val (_, ctxt') = ctxt |> add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs); |
9291
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
1214 |
fun bind (t as Free (x, T)) = |
18340 | 1215 |
if member (op =) xs x then |
15531 | 1216 |
(case lookup_skolem ctxt' x of SOME x' => Free (x', T) | NONE => t) |
9291
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
1217 |
else t |
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
1218 |
| bind (t $ u) = bind t $ bind u |
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
1219 |
| bind (Abs (x, T, t)) = Abs (x, T, bind t) |
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
1220 |
| bind a = a; |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1221 |
in (bind, ctxt') end; |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1222 |
|
9291
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
1223 |
|
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
1224 |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1225 |
(** assumptions **) |
18187 | 1226 |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1227 |
(* generic assms *) |
18187 | 1228 |
|
1229 |
local |
|
1230 |
||
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1231 |
fun gen_assm ((name, attrs), props) ctxt = |
18187 | 1232 |
let |
1233 |
val cprops = map (Thm.cterm_of (theory_of ctxt)) props; |
|
1234 |
val asms = map (Goal.norm_hhf o Thm.assume) cprops; |
|
1235 |
||
1236 |
val ths = map (fn th => ([th], [])) asms; |
|
1237 |
val ([(_, thms)], ctxt') = |
|
1238 |
ctxt |
|
1239 |
|> auto_bind_facts props |
|
1240 |
|> note_thmss_i [((name, attrs), ths)]; |
|
1241 |
in ((cprops, (name, asms), (name, thms)), ctxt') end; |
|
1242 |
||
1243 |
fun gen_assms prepp exp args ctxt = |
|
1244 |
let |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1245 |
val (propss, ctxt1) = swap (prepp (ctxt, map snd args)); |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1246 |
val (results, ctxt2) = fold_map gen_assm (map fst args ~~ propss) ctxt1; |
18187 | 1247 |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19422
diff
changeset
|
1248 |
val new_asms = maps #1 results; |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1249 |
val new_prems = map #2 results; |
18809 | 1250 |
val ctxt3 = ctxt2 |
1251 |
|> map_assms (fn (asms, prems) => (asms @ [(new_asms, exp)], prems @ new_prems)) |
|
1252 |
val ctxt4 = ctxt3 |
|
19079
9a7678a0736d
added put_thms_internal: local_naming, no fact index;
wenzelm
parents:
19062
diff
changeset
|
1253 |
|> put_thms_internal (AutoBind.premsN, SOME (prems_of ctxt3)); |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1254 |
in (map #3 results, warn_extra_tfrees ctxt ctxt4) end; |
18187 | 1255 |
|
1256 |
in |
|
1257 |
||
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1258 |
val add_assms = gen_assms (apsnd #1 o bind_propp); |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1259 |
val add_assms_i = gen_assms (apsnd #1 o bind_propp_i); |
18187 | 1260 |
|
1261 |
end; |
|
1262 |
||
1263 |
||
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1264 |
(* basic assumptions *) |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1265 |
|
18879 | 1266 |
(* |
1267 |
[A] |
|
18971 | 1268 |
: |
18879 | 1269 |
B |
1270 |
-------- |
|
1271 |
#A ==> B |
|
1272 |
*) |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1273 |
fun assume_export true = Seq.single oo Drule.implies_intr_protected |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1274 |
| assume_export false = Seq.single oo Drule.implies_intr_list; |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1275 |
|
18879 | 1276 |
(* |
1277 |
[A] |
|
18971 | 1278 |
: |
18879 | 1279 |
B |
1280 |
------- |
|
1281 |
A ==> B |
|
1282 |
*) |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1283 |
fun presume_export _ = assume_export false; |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1284 |
|
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1285 |
|
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1286 |
(* additional views *) |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1287 |
|
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1288 |
fun add_view outer view = map_assms (fn (asms, prems) => |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1289 |
let |
19019 | 1290 |
val (asms1, asms2) = chop (length (assumptions_of outer)) asms; |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1291 |
val asms' = asms1 @ [(view, assume_export)] @ asms2; |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1292 |
in (asms', prems) end); |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1293 |
|
18785
5ae1f1c1b764
renamed export to export_standard (again!), because it includes Drule.local_standard';
wenzelm
parents:
18770
diff
changeset
|
1294 |
fun export_view view inner outer = export_standard (add_view outer view inner) outer; |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1295 |
|
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1296 |
|
5819 | 1297 |
|
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
1298 |
(** cases **) |
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
1299 |
|
16147 | 1300 |
local |
1301 |
||
16668 | 1302 |
fun rem_case name = remove (fn (x: string, (y, _)) => x = y) name; |
16147 | 1303 |
|
18476 | 1304 |
fun add_case _ ("", _) cases = cases |
1305 |
| add_case _ (name, NONE) cases = rem_case name cases |
|
1306 |
| add_case is_proper (name, SOME c) cases = (name, (c, is_proper)) :: rem_case name cases; |
|
16147 | 1307 |
|
18678 | 1308 |
fun prep_case name fxs c = |
18609 | 1309 |
let |
1310 |
fun replace (opt_x :: xs) ((y, T) :: ys) = (the_default y opt_x, T) :: replace xs ys |
|
1311 |
| replace [] ys = ys |
|
18678 | 1312 |
| replace (_ :: _) [] = error ("Too many parameters for case " ^ quote name); |
18609 | 1313 |
val RuleCases.Case {fixes, assumes, binds, cases} = c; |
1314 |
val fixes' = replace fxs fixes; |
|
1315 |
val binds' = map drop_schematic binds; |
|
1316 |
in |
|
1317 |
if null (fold (Term.add_tvarsT o snd) fixes []) andalso |
|
1318 |
null (fold (fold Term.add_vars o snd) assumes []) then |
|
1319 |
RuleCases.Case {fixes = fixes', assumes = assumes, binds = binds', cases = cases} |
|
18678 | 1320 |
else error ("Illegal schematic variable(s) in case " ^ quote name) |
18609 | 1321 |
end; |
1322 |
||
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1323 |
fun fix (x, T) ctxt = |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1324 |
let |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1325 |
val (bind, ctxt') = bind_fixes [x] ctxt; |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1326 |
val t = bind (Free (x, T)); |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1327 |
in (t, ctxt' |> declare_syntax t) end; |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1328 |
|
16147 | 1329 |
in |
1330 |
||
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1331 |
fun add_cases is_proper = map_cases o fold (add_case is_proper); |
18609 | 1332 |
|
1333 |
fun case_result c ctxt = |
|
1334 |
let |
|
1335 |
val RuleCases.Case {fixes, ...} = c; |
|
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1336 |
val (ts, ctxt') = ctxt |> fold_map fix fixes; |
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1337 |
val RuleCases.Case {assumes, binds, cases, ...} = RuleCases.apply ts c; |
18609 | 1338 |
in |
1339 |
ctxt' |
|
18699
f3bfe81b6e58
case_result: drop_schematic, i.e. be permissive about illegal binds;
wenzelm
parents:
18678
diff
changeset
|
1340 |
|> add_binds_i (map drop_schematic binds) |
18609 | 1341 |
|> add_cases true (map (apsnd SOME) cases) |
1342 |
|> pair (assumes, (binds, cases)) |
|
1343 |
end; |
|
1344 |
||
1345 |
val apply_case = apfst fst oo case_result; |
|
1346 |
||
16540 | 1347 |
fun get_case ctxt name xs = |
17184 | 1348 |
(case AList.lookup (op =) (cases_of ctxt) name of |
18678 | 1349 |
NONE => error ("Unknown case: " ^ quote name) |
1350 |
| SOME (c, _) => prep_case name xs c); |
|
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
1351 |
|
16147 | 1352 |
end; |
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
1353 |
|
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
1354 |
|
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
1355 |
|
10810 | 1356 |
(** print context information **) |
1357 |
||
1358 |
val verbose = ref false; |
|
1359 |
fun verb f x = if ! verbose then f (x ()) else []; |
|
1360 |
||
1361 |
fun setmp_verbose f x = Library.setmp verbose true f x; |
|
1362 |
||
1363 |
fun pretty_items prt name items = |
|
1364 |
let |
|
1365 |
fun prt_itms (name, [x]) = Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt x] |
|
1366 |
| prt_itms (name, xs) = Pretty.big_list (name ^ ":") (map prt xs); |
|
1367 |
in |
|
1368 |
if null items andalso not (! verbose) then [] |
|
1369 |
else [Pretty.big_list name (map prt_itms items)] |
|
1370 |
end; |
|
1371 |
||
1372 |
||
12072
4281198fb8cd
local syntax: add_syntax, proper read/pretty functions;
wenzelm
parents:
12066
diff
changeset
|
1373 |
(* local syntax *) |
4281198fb8cd
local syntax: add_syntax, proper read/pretty functions;
wenzelm
parents:
12066
diff
changeset
|
1374 |
|
12093 | 1375 |
val print_syntax = Syntax.print_syntax o syn_of; |
12072
4281198fb8cd
local syntax: add_syntax, proper read/pretty functions;
wenzelm
parents:
12066
diff
changeset
|
1376 |
|
4281198fb8cd
local syntax: add_syntax, proper read/pretty functions;
wenzelm
parents:
12066
diff
changeset
|
1377 |
|
18971 | 1378 |
(* local consts *) |
1379 |
||
1380 |
fun pretty_consts ctxt = |
|
1381 |
let |
|
19033
24e251657e56
consts: maintain thy version for efficient transfer;
wenzelm
parents:
19019
diff
changeset
|
1382 |
val ((_, globals), (space, consts)) = |
24e251657e56
consts: maintain thy version for efficient transfer;
wenzelm
parents:
19019
diff
changeset
|
1383 |
pairself (#constants o Consts.dest) (#consts (rep_context ctxt)); |
18971 | 1384 |
fun local_abbrev (_, (_, NONE)) = I |
1385 |
| local_abbrev (c, (T, SOME t)) = |
|
1386 |
if Symtab.defined globals c then I else cons (c, Logic.mk_equals (Const (c, T), t)); |
|
1387 |
val abbrevs = NameSpace.extern_table (space, Symtab.make (Symtab.fold local_abbrev consts [])); |
|
1388 |
in |
|
1389 |
if null abbrevs andalso not (! verbose) then [] |
|
19310 | 1390 |
else [Pretty.big_list "abbreviations:" (map (pretty_term_no_abbrevs ctxt o #2) abbrevs)] |
18971 | 1391 |
end; |
1392 |
||
1393 |
||
10810 | 1394 |
(* term bindings *) |
1395 |
||
16540 | 1396 |
fun pretty_binds ctxt = |
10810 | 1397 |
let |
16540 | 1398 |
val binds = binds_of ctxt; |
19310 | 1399 |
fun prt_bind (xi, (T, t)) = pretty_term_no_abbrevs ctxt (Logic.mk_equals (Var (xi, T), t)); |
10810 | 1400 |
in |
15758
07e382399a96
binds/thms: do not store options, but delete from table;
wenzelm
parents:
15750
diff
changeset
|
1401 |
if Vartab.is_empty binds andalso not (! verbose) then [] |
07e382399a96
binds/thms: do not store options, but delete from table;
wenzelm
parents:
15750
diff
changeset
|
1402 |
else [Pretty.big_list "term bindings:" (map prt_bind (Vartab.dest binds))] |
10810 | 1403 |
end; |
1404 |
||
1405 |
val print_binds = Pretty.writeln o Pretty.chunks o pretty_binds; |
|
1406 |
||
1407 |
||
1408 |
(* local theorems *) |
|
1409 |
||
16540 | 1410 |
fun pretty_lthms ctxt = |
19001 | 1411 |
pretty_items (pretty_thm ctxt) "facts:" (NameSpace.extern_table (#1 (thms_of ctxt))); |
10810 | 1412 |
|
12057 | 1413 |
val print_lthms = Pretty.writeln o Pretty.chunks o pretty_lthms; |
10810 | 1414 |
|
1415 |
||
1416 |
(* local contexts *) |
|
1417 |
||
16540 | 1418 |
fun pretty_cases ctxt = |
10810 | 1419 |
let |
12057 | 1420 |
val prt_term = pretty_term ctxt; |
1421 |
||
10810 | 1422 |
fun prt_let (xi, t) = Pretty.block |
10818 | 1423 |
[Pretty.quote (prt_term (Var (xi, Term.fastype_of t))), Pretty.str " =", Pretty.brk 1, |
10810 | 1424 |
Pretty.quote (prt_term t)]; |
1425 |
||
13425
119ae829ad9b
support for split assumptions in cases (hyps vs. prems);
wenzelm
parents:
13415
diff
changeset
|
1426 |
fun prt_asm (a, ts) = Pretty.block (Pretty.breaks |
119ae829ad9b
support for split assumptions in cases (hyps vs. prems);
wenzelm
parents:
13415
diff
changeset
|
1427 |
((if a = "" then [] else [Pretty.str (a ^ ":")]) @ map (Pretty.quote o prt_term) ts)); |
119ae829ad9b
support for split assumptions in cases (hyps vs. prems);
wenzelm
parents:
13415
diff
changeset
|
1428 |
|
10810 | 1429 |
fun prt_sect _ _ _ [] = [] |
1430 |
| prt_sect s sep prt xs = [Pretty.block (Pretty.breaks (Pretty.str s :: |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19422
diff
changeset
|
1431 |
flat (Library.separate sep (map (Library.single o prt) xs))))]; |
10810 | 1432 |
|
18609 | 1433 |
fun prt_case (name, (fixes, (asms, (lets, cs)))) = Pretty.block (Pretty.fbreaks |
10810 | 1434 |
(Pretty.str (name ^ ":") :: |
11915 | 1435 |
prt_sect "fix" [] (Pretty.str o fst) fixes @ |
10810 | 1436 |
prt_sect "let" [Pretty.str "and"] prt_let |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19422
diff
changeset
|
1437 |
(map_filter (fn (xi, SOME t) => SOME (xi, t) | _ => NONE) lets) @ |
13425
119ae829ad9b
support for split assumptions in cases (hyps vs. prems);
wenzelm
parents:
13415
diff
changeset
|
1438 |
(if forall (null o #2) asms then [] |
18609 | 1439 |
else prt_sect "assume" [Pretty.str "and"] prt_asm asms) @ |
1440 |
prt_sect "subcases:" [] (Pretty.str o fst) cs)); |
|
16540 | 1441 |
|
18476 | 1442 |
fun add_case (_, (_, false)) = I |
18609 | 1443 |
| add_case (name, (c as RuleCases.Case {fixes, ...}, true)) = |
1444 |
cons (name, (fixes, #1 (case_result c ctxt))); |
|
18476 | 1445 |
val cases = fold add_case (cases_of ctxt) []; |
10810 | 1446 |
in |
1447 |
if null cases andalso not (! verbose) then [] |
|
18476 | 1448 |
else [Pretty.big_list "cases:" (map prt_case cases)] |
10810 | 1449 |
end; |
1450 |
||
1451 |
val print_cases = Pretty.writeln o Pretty.chunks o pretty_cases; |
|
1452 |
||
1453 |
||
12057 | 1454 |
(* core context *) |
10810 | 1455 |
|
1456 |
val prems_limit = ref 10; |
|
1457 |
||
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1458 |
fun pretty_ctxt ctxt = |
10810 | 1459 |
let |
12057 | 1460 |
val prt_term = pretty_term ctxt; |
1461 |
||
12093 | 1462 |
(*structures*) |
19001 | 1463 |
val structs = LocalSyntax.structs_of (syntax_of ctxt); |
12093 | 1464 |
val prt_structs = if null structs then [] |
1465 |
else [Pretty.block (Pretty.str "structures:" :: Pretty.brk 1 :: |
|
1466 |
Pretty.commas (map Pretty.str structs))]; |
|
1467 |
||
12057 | 1468 |
(*fixes*) |
1469 |
fun prt_fix (x, x') = |
|
1470 |
if x = x' then Pretty.str x |
|
1471 |
else Pretty.block [Pretty.str x, Pretty.str " =", Pretty.brk 1, prt_term (Syntax.free x')]; |
|
18340 | 1472 |
val fixes = |
1473 |
rev (filter_out ((can Syntax.dest_internal orf member (op =) structs) o #1) (fixes_of ctxt)); |
|
12093 | 1474 |
val prt_fixes = if null fixes then [] |
1475 |
else [Pretty.block (Pretty.str "fixed variables:" :: Pretty.brk 1 :: |
|
1476 |
Pretty.commas (map prt_fix fixes))]; |
|
12057 | 1477 |
|
1478 |
(*prems*) |
|
10810 | 1479 |
val limit = ! prems_limit; |
1480 |
val prems = prems_of ctxt; |
|
1481 |
val len = length prems; |
|
12093 | 1482 |
val prt_prems = if null prems then [] |
1483 |
else [Pretty.big_list "prems:" ((if len <= limit then [] else [Pretty.str "..."]) @ |
|
1484 |
map (pretty_thm ctxt) (Library.drop (len - limit, prems)))]; |
|
1485 |
||
1486 |
in prt_structs @ prt_fixes @ prt_prems end; |
|
10810 | 1487 |
|
1488 |
||
1489 |
(* main context *) |
|
1490 |
||
16540 | 1491 |
fun pretty_context ctxt = |
10810 | 1492 |
let |
12057 | 1493 |
val prt_term = pretty_term ctxt; |
1494 |
val prt_typ = pretty_typ ctxt; |
|
1495 |
val prt_sort = pretty_sort ctxt; |
|
10810 | 1496 |
|
1497 |
(*theory*) |
|
12057 | 1498 |
val pretty_thy = Pretty.block |
17384 | 1499 |
[Pretty.str "theory:", Pretty.brk 1, Context.pretty_thy (theory_of ctxt)]; |
10810 | 1500 |
|
1501 |
(*defaults*) |
|
1502 |
fun prt_atom prt prtT (x, X) = Pretty.block |
|
1503 |
[prt x, Pretty.str " ::", Pretty.brk 1, prtT X]; |
|
1504 |
||
1505 |
fun prt_var (x, ~1) = prt_term (Syntax.free x) |
|
1506 |
| prt_var xi = prt_term (Syntax.var xi); |
|
1507 |
||
1508 |
fun prt_varT (x, ~1) = prt_typ (TFree (x, [])) |
|
1509 |
| prt_varT xi = prt_typ (TVar (xi, [])); |
|
1510 |
||
1511 |
val prt_defT = prt_atom prt_var prt_typ; |
|
1512 |
val prt_defS = prt_atom prt_varT prt_sort; |
|
16540 | 1513 |
|
1514 |
val (types, sorts, used, _) = defaults_of ctxt; |
|
10810 | 1515 |
in |
18609 | 1516 |
verb single (K pretty_thy) @ |
18672
ac1a048ca7dd
uniform handling of fixes: read/cert_vars, add_fixes(_i), body flag;
wenzelm
parents:
18619
diff
changeset
|
1517 |
pretty_ctxt ctxt @ |
18971 | 1518 |
verb pretty_consts (K ctxt) @ |
10810 | 1519 |
verb pretty_binds (K ctxt) @ |
12057 | 1520 |
verb pretty_lthms (K ctxt) @ |
10810 | 1521 |
verb pretty_cases (K ctxt) @ |
18609 | 1522 |
verb single (fn () => Pretty.big_list "type constraints:" (map prt_defT (Vartab.dest types))) @ |
1523 |
verb single (fn () => Pretty.big_list "default sorts:" (map prt_defS (Vartab.dest sorts))) @ |
|
18809 | 1524 |
verb single (fn () => Pretty.strs ("used type variable names:" :: rev used)) |
10810 | 1525 |
end; |
1526 |
||
18809 | 1527 |
|
1528 |
(* toplevel pretty printing *) |
|
1529 |
||
1530 |
val debug = ref false; |
|
1531 |
||
1532 |
fun pprint_context ctxt = Pretty.pprint |
|
1533 |
(if ! debug then Pretty.quote (Pretty.big_list "proof context:" (pretty_context ctxt)) |
|
1534 |
else Pretty.str "<context>"); |
|
1535 |
||
5819 | 1536 |
end; |