| author | wenzelm | 
| Wed, 29 Jun 2005 15:13:38 +0200 | |
| changeset 16608 | 4f8d7b83c7e2 | 
| parent 16540 | e3d61eff7c12 | 
| child 16668 | fdb4992cf1d2 | 
| permissions | -rw-r--r-- | 
| 5819 | 1 | (* Title: Pure/Isar/proof_context.ML | 
| 2 | ID: $Id$ | |
| 3 | Author: Markus Wenzel, TU Muenchen | |
| 4 | ||
| 15758 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 5 | The key concept of Isar proof contexts. | 
| 5819 | 6 | *) | 
| 7 | ||
| 14707 | 8 | val show_structs = ref false; | 
| 9 | ||
| 5819 | 10 | signature PROOF_CONTEXT = | 
| 11 | sig | |
| 12 | type context | |
| 9470 | 13 | type exporter | 
| 5819 | 14 | exception CONTEXT of string * context | 
| 15 | val theory_of: context -> theory | |
| 16458 | 16 | val sign_of: context -> theory (*obsolete*) | 
| 14828 | 17 | val is_fixed: context -> string -> bool | 
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 18 | val is_known: context -> string -> bool | 
| 12057 | 19 | val fixed_names_of: context -> string list | 
| 20 | val assumptions_of: context -> (cterm list * exporter) list | |
| 7557 | 21 | val prems_of: context -> thm list | 
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 22 | val fact_index_of: context -> FactIndex.T | 
| 5874 | 23 | val init: theory -> context | 
| 14828 | 24 | val pretty_term: context -> term -> Pretty.T | 
| 25 | val pretty_typ: context -> typ -> Pretty.T | |
| 26 | val pretty_sort: context -> sort -> Pretty.T | |
| 27 | val pp: context -> Pretty.pp | |
| 28 | val pretty_thm: context -> thm -> Pretty.T | |
| 29 | val pretty_thms: context -> thm list -> Pretty.T | |
| 30 | val pretty_fact: context -> string * thm list -> Pretty.T | |
| 31 | val string_of_term: context -> term -> string | |
| 12414 | 32 | val default_type: context -> string -> typ option | 
| 12504 | 33 | val used_types: context -> string list | 
| 5819 | 34 | val read_typ: context -> string -> typ | 
| 16348 | 35 | val read_typ_syntax: context -> string -> typ | 
| 36 | val read_typ_abbrev: context -> string -> typ | |
| 5819 | 37 | val cert_typ: context -> typ -> typ | 
| 16348 | 38 | val cert_typ_syntax: context -> typ -> typ | 
| 39 | val cert_typ_abbrev: context -> typ -> typ | |
| 10583 | 40 | val get_skolem: context -> string -> string | 
| 9133 | 41 | val extern_skolem: context -> term -> term | 
| 14720 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 42 | val read_termTs: context -> (string -> bool) -> (indexname -> typ option) | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 43 | -> (indexname -> sort option) -> string list -> (string * typ) list | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 44 | -> term list * (indexname * typ) list | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 45 | val read_termTs_schematic: context -> (string -> bool) -> (indexname -> typ option) | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 46 | -> (indexname -> sort option) -> string list -> (string * typ) list | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 47 | -> term list * (indexname * typ) list | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 48 | val read_term_liberal: context -> string -> term | 
| 5819 | 49 | val read_term: context -> string -> term | 
| 50 | val read_prop: context -> string -> term | |
| 11925 | 51 | val read_prop_schematic: context -> string -> term | 
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 52 | val read_terms: context -> string list -> term list | 
| 8096 | 53 | val read_term_pats: typ -> context -> string list -> term list | 
| 54 | val read_prop_pats: context -> string list -> term list | |
| 5819 | 55 | val cert_term: context -> term -> term | 
| 56 | val cert_prop: context -> term -> term | |
| 8096 | 57 | val cert_term_pats: typ -> context -> term list -> term list | 
| 58 | val cert_prop_pats: context -> term list -> term list | |
| 5819 | 59 | val declare_term: term -> context -> context | 
| 15703 | 60 | val read_tyname: context -> string -> typ | 
| 61 | val read_const: context -> string -> term | |
| 7925 | 62 | val warn_extra_tfrees: context -> context -> context | 
| 12550 
32843ad8160a
generalize type variables properly: start with occurrences in objects
 wenzelm parents: 
12530diff
changeset | 63 | val generalize: context -> context -> term list -> term list | 
| 9470 | 64 | val find_free: term -> string -> term option | 
| 12008 | 65 | val export: bool -> context -> context -> thm -> thm Seq.seq | 
| 13415 | 66 | val export_standard: cterm list -> context -> context -> thm -> thm | 
| 15696 | 67 | val export_plain: cterm list -> context -> context -> thm -> thm | 
| 10810 | 68 | val drop_schematic: indexname * term option -> indexname * term option | 
| 69 | val add_binds: (indexname * string option) list -> context -> context | |
| 70 | val add_binds_i: (indexname * term option) list -> context -> context | |
| 12147 | 71 | val auto_bind_goal: term list -> context -> context | 
| 72 | val auto_bind_facts: term list -> context -> context | |
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 73 | val match_bind: bool -> (string list * string) list -> context -> context | 
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 74 | val match_bind_i: bool -> (term list * term) list -> context -> context | 
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 75 | val read_propp: context * (string * (string list * string list)) list list | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 76 | -> context * (term * (term list * term list)) list list | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 77 | val cert_propp: context * (term * (term list * term list)) list list | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 78 | -> context * (term * (term list * term list)) list list | 
| 10554 | 79 | val read_propp_schematic: context * (string * (string list * string list)) list list | 
| 80 | -> context * (term * (term list * term list)) list list | |
| 81 | val cert_propp_schematic: context * (term * (term list * term list)) list list | |
| 82 | -> context * (term * (term list * term list)) list list | |
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 83 | val bind_propp: context * (string * (string list * string list)) list list | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 84 | -> context * (term list list * (context -> context)) | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 85 | val bind_propp_i: context * (term * (term list * term list)) list list | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 86 | -> context * (term list list * (context -> context)) | 
| 10554 | 87 | val bind_propp_schematic: context * (string * (string list * string list)) list list | 
| 88 | -> context * (term list list * (context -> context)) | |
| 89 | val bind_propp_schematic_i: context * (term * (term list * term list)) list list | |
| 90 | -> context * (term list list * (context -> context)) | |
| 15456 
956d6acacf89
Specific theorems in a named list of theorems can now be referred to
 berghofe parents: 
15452diff
changeset | 91 | val get_thm: context -> thmref -> thm | 
| 
956d6acacf89
Specific theorems in a named list of theorems can now be referred to
 berghofe parents: 
15452diff
changeset | 92 | val get_thm_closure: context -> thmref -> thm | 
| 
956d6acacf89
Specific theorems in a named list of theorems can now be referred to
 berghofe parents: 
15452diff
changeset | 93 | val get_thms: context -> thmref -> thm list | 
| 
956d6acacf89
Specific theorems in a named list of theorems can now be referred to
 berghofe parents: 
15452diff
changeset | 94 | val get_thms_closure: context -> thmref -> thm list | 
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 95 | val valid_thms: context -> string * thm list -> bool | 
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 96 | val lthms_containing: context -> FactIndex.spec -> (string * thm list) list | 
| 16348 | 97 | val extern_thm: context -> string -> xstring | 
| 16147 | 98 | val qualified_names: context -> context | 
| 99 | val no_base_names: context -> context | |
| 100 | val custom_accesses: (string list -> string list list) -> context -> context | |
| 101 | val restore_naming: context -> context -> context | |
| 13425 
119ae829ad9b
support for split assumptions in cases (hyps vs. prems);
 wenzelm parents: 
13415diff
changeset | 102 | val hide_thms: bool -> string list -> context -> context | 
| 6091 | 103 | val put_thm: string * thm -> context -> context | 
| 104 | val put_thms: string * thm list -> context -> context | |
| 105 | val put_thmss: (string * thm list) list -> context -> context | |
| 7606 | 106 | val reset_thms: string -> context -> context | 
| 14564 | 107 | val note_thmss: | 
| 16147 | 108 | ((bstring * context attribute list) * (thmref * context attribute list) list) list -> | 
| 109 | context -> context * (bstring * thm list) list | |
| 14564 | 110 | val note_thmss_i: | 
| 16147 | 111 | ((bstring * context attribute list) * (thm list * context attribute list) list) list -> | 
| 112 | context -> context * (bstring * thm list) list | |
| 11918 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 113 | val export_assume: exporter | 
| 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 114 | val export_presume: exporter | 
| 12086 | 115 | val cert_def: context -> term -> string * term | 
| 11918 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 116 | val export_def: exporter | 
| 9470 | 117 | val assume: exporter | 
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 118 | -> ((string * context attribute list) * (string * (string list * string list)) list) list | 
| 12835 | 119 | -> context -> context * (bstring * thm list) list | 
| 9470 | 120 | val assume_i: exporter | 
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 121 | -> ((string * context attribute list) * (term * (term list * term list)) list) list | 
| 12835 | 122 | -> context -> context * (bstring * thm list) list | 
| 14720 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 123 | val read_vars: context * (string list * string option) | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 124 | -> context * (string list * typ option) | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 125 | val cert_vars: context * (string list * typ option) | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 126 | -> context * (string list * typ option) | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 127 | val read_vars_liberal: context * (string list * string option) | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 128 | -> context * (string list * typ option) | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 129 | val cert_vars_liberal: context * (string list * typ option) | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 130 | -> context * (string list * typ option) | 
| 7411 | 131 | val fix: (string list * string option) list -> context -> context | 
| 7663 | 132 | val fix_i: (string list * typ option) list -> context -> context | 
| 14643 | 133 | val add_fixes: (string * typ option * Syntax.mixfix option) list -> context -> context | 
| 14720 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 134 | val add_fixes_liberal: (string * typ option * Syntax.mixfix option) list -> context -> context | 
| 12016 | 135 | val fix_frees: term list -> context -> context | 
| 9291 
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
 wenzelm parents: 
9274diff
changeset | 136 | val bind_skolem: context -> string list -> term -> term | 
| 13425 
119ae829ad9b
support for split assumptions in cases (hyps vs. prems);
 wenzelm parents: 
13415diff
changeset | 137 | val apply_case: RuleCases.T -> context | 
| 
119ae829ad9b
support for split assumptions in cases (hyps vs. prems);
 wenzelm parents: 
13415diff
changeset | 138 | -> context * ((indexname * term option) list * (string * term list) list) | 
| 16147 | 139 | val get_case: context -> string -> string option list -> RuleCases.T | 
| 140 | val add_cases: (string * RuleCases.T option) list -> context -> context | |
| 10810 | 141 | val verbose: bool ref | 
| 142 |   val setmp_verbose: ('a -> 'b) -> 'a -> 'b
 | |
| 12072 
4281198fb8cd
local syntax: add_syntax, proper read/pretty functions;
 wenzelm parents: 
12066diff
changeset | 143 | val print_syntax: context -> unit | 
| 10810 | 144 | val print_binds: context -> unit | 
| 12057 | 145 | val print_lthms: context -> unit | 
| 10810 | 146 | val print_cases: context -> unit | 
| 147 | val prems_limit: int ref | |
| 12057 | 148 | val pretty_asms: context -> Pretty.T list | 
| 10810 | 149 | val pretty_context: context -> Pretty.T list | 
| 5819 | 150 | end; | 
| 151 | ||
| 16540 | 152 | structure ProofContext: PROOF_CONTEXT = | 
| 5819 | 153 | struct | 
| 154 | ||
| 16540 | 155 | (** generic proof contexts **) | 
| 9470 | 156 | |
| 16540 | 157 | type context = Context.proof; | 
| 5819 | 158 | exception CONTEXT of string * context; | 
| 159 | ||
| 16540 | 160 | val theory_of = Context.theory_of_proof; | 
| 16458 | 161 | val sign_of = theory_of; | 
| 5819 | 162 | |
| 16540 | 163 | val init = Context.init_proof; | 
| 12057 | 164 | |
| 7270 | 165 | |
| 5819 | 166 | |
| 16540 | 167 | (** Isar proof context information **) | 
| 5819 | 168 | |
| 16540 | 169 | type exporter = bool -> cterm list -> thm -> thm Seq.seq; | 
| 5819 | 170 | |
| 16540 | 171 | datatype ctxt = | 
| 172 | Ctxt of | |
| 173 |    {syntax: Syntax.syntax * string list * string list,  (*syntax with structs and mixfixed*)
 | |
| 174 | asms: | |
| 175 | ((cterm list * exporter) list * (*assumes: A ==> _*) | |
| 176 | (string * thm list) list) * (*prems: A |- A *) | |
| 177 | (string * string) list, (*fixes: !!x. _*) | |
| 178 | binds: (term * typ) Vartab.table, (*term bindings*) | |
| 179 | thms: NameSpace.naming * (*local thms*) | |
| 180 | thm list NameSpace.table * FactIndex.T, | |
| 181 | cases: (string * RuleCases.T) list, (*local contexts*) | |
| 182 | defs: | |
| 183 | typ Vartab.table * (*type constraints*) | |
| 184 | sort Vartab.table * (*default sorts*) | |
| 185 | string list * (*used type variables*) | |
| 186 | term list Symtab.table}; (*type variable occurrences*) | |
| 5819 | 187 | |
| 16540 | 188 | fun make_ctxt (syntax, asms, binds, thms, cases, defs) = | 
| 189 |   Ctxt {syntax = syntax, asms = asms, binds = binds, thms = thms, cases = cases, defs = defs};
 | |
| 5819 | 190 | |
| 16540 | 191 | structure ContextData = ProofDataFun | 
| 16458 | 192 | (struct | 
| 16540 | 193 | val name = "Isar/context"; | 
| 194 | type T = ctxt; | |
| 195 | fun init thy = | |
| 196 | make_ctxt ((Sign.syn_of thy, [], []), (([], []), []), Vartab.empty, | |
| 197 | (NameSpace.default_naming, NameSpace.empty_table, FactIndex.empty), [], | |
| 198 | (Vartab.empty, Vartab.empty, [], Symtab.empty)); | |
| 199 | fun print _ _ = (); | |
| 16458 | 200 | end); | 
| 5819 | 201 | |
| 16540 | 202 | val _ = Context.add_setup [ContextData.init]; | 
| 5819 | 203 | |
| 16540 | 204 | fun rep_context ctxt = ContextData.get ctxt |> (fn Ctxt args => args); | 
| 5819 | 205 | |
| 16540 | 206 | fun map_context f = ContextData.map (fn Ctxt {syntax, asms, binds, thms, cases, defs} =>
 | 
| 207 | make_ctxt (f (syntax, asms, binds, thms, cases, defs))); | |
| 5819 | 208 | |
| 16540 | 209 | val syntax_of = #syntax o rep_context; | 
| 5819 | 210 | |
| 16540 | 211 | val assumptions_of = #1 o #1 o #asms o rep_context; | 
| 212 | val prems_of = List.concat o map #2 o #2 o #1 o #asms o rep_context; | |
| 213 | val fixes_of = #2 o #asms o rep_context; | |
| 214 | val fixed_names_of = map #2 o fixes_of; | |
| 5819 | 215 | |
| 16540 | 216 | val binds_of = #binds o rep_context; | 
| 5819 | 217 | |
| 16540 | 218 | val thms_of = #thms o rep_context; | 
| 219 | val fact_index_of = #3 o thms_of; | |
| 220 | ||
| 221 | val cases_of = #cases o rep_context; | |
| 5819 | 222 | |
| 16540 | 223 | val defaults_of = #defs o rep_context; | 
| 224 | val type_occs_of = #4 o defaults_of; | |
| 5819 | 225 | |
| 16540 | 226 | fun is_fixed ctxt x = exists (equal x o #2) (fixes_of ctxt); | 
| 227 | fun is_known ctxt x = | |
| 228 | is_some (Vartab.lookup (#1 (defaults_of ctxt), (x, ~1))) orelse is_fixed ctxt x; | |
| 5819 | 229 | |
| 230 | ||
| 8096 | 231 | |
| 12093 | 232 | (** local syntax **) | 
| 233 | ||
| 234 | val fixedN = "\\<^fixed>"; | |
| 235 | val structN = "\\<^struct>"; | |
| 236 | ||
| 12100 | 237 | |
| 14697 | 238 | (* translation functions *) | 
| 14665 
d2e5df3d1201
index syntax: support for general expressions (input only);
 wenzelm parents: 
14643diff
changeset | 239 | |
| 
d2e5df3d1201
index syntax: support for general expressions (input only);
 wenzelm parents: 
14643diff
changeset | 240 | fun fixed_tr x = (fixedN ^ x, curry Term.list_comb (Syntax.free x)); | 
| 
d2e5df3d1201
index syntax: support for general expressions (input only);
 wenzelm parents: 
14643diff
changeset | 241 | |
| 12100 | 242 | fun context_tr' ctxt = | 
| 243 | let | |
| 244 | val (_, structs, mixfixed) = syntax_of ctxt; | |
| 245 | ||
| 246 | fun tr' (t $ u) = tr' t $ tr' u | |
| 247 | | tr' (Abs (x, T, t)) = Abs (x, T, tr' t) | |
| 248 | | tr' (t as Free (x, T)) = | |
| 14697 | 249 | let val i = Library.find_index_eq x structs + 1 in | 
| 250 | if i = 0 andalso x mem_string mixfixed then Const (fixedN ^ x, T) | |
| 14707 | 251 | else if i = 1 andalso not (! show_structs) then | 
| 252 | Syntax.const "_struct" $ Syntax.const "_indexdefault" | |
| 12100 | 253 | else t | 
| 254 | end | |
| 255 | | tr' a = a; | |
| 256 | in tr' end; | |
| 257 | ||
| 258 | ||
| 12576 
9fd10052c3f7
added add_fixes: derives mssing type scheme from mixfix;
 wenzelm parents: 
12550diff
changeset | 259 | (* add syntax *) | 
| 
9fd10052c3f7
added add_fixes: derives mssing type scheme from mixfix;
 wenzelm parents: 
12550diff
changeset | 260 | |
| 
9fd10052c3f7
added add_fixes: derives mssing type scheme from mixfix;
 wenzelm parents: 
12550diff
changeset | 261 | fun mixfix_type mx = replicate (Syntax.mixfix_args mx) TypeInfer.logicT ---> TypeInfer.logicT; | 
| 12093 | 262 | |
| 263 | local | |
| 264 | ||
| 15531 | 265 | fun mixfix x NONE mx = (fixedN ^ x, mixfix_type mx, Syntax.fix_mixfix x mx) | 
| 266 | | mixfix x (SOME T) mx = (fixedN ^ x, T, Syntax.fix_mixfix x mx); | |
| 12093 | 267 | |
| 15531 | 268 | fun prep_mixfix (_, _, NONE) = NONE | 
| 269 | | prep_mixfix (x, opt_T, SOME mx) = SOME (mixfix x opt_T mx); | |
| 12093 | 270 | |
| 15531 | 271 | fun prep_mixfix' (_, _, NONE) = NONE | 
| 272 | | prep_mixfix' (x, _, SOME Syntax.NoSyn) = NONE | |
| 273 | | prep_mixfix' (x, opt_T, _) = SOME (x, mixfix x opt_T (Syntax.literal x)); | |
| 12093 | 274 | |
| 15531 | 275 | fun prep_struct (x, _, NONE) = SOME x | 
| 276 | | prep_struct _ = NONE; | |
| 12093 | 277 | |
| 15750 | 278 | fun mk trs = map Syntax.mk_trfun trs; | 
| 279 | ||
| 12093 | 280 | in | 
| 281 | ||
| 16540 | 282 | fun add_syntax decls ctxt = ctxt |> | 
| 283 | map_context (fn ((syn, structs, mixfixed), asms, binds, thms, cases, defs) => | |
| 12093 | 284 | let | 
| 16540 | 285 | val is_logtype = Sign.is_logtype (theory_of ctxt); | 
| 15570 | 286 | val structs' = structs @ List.mapPartial prep_struct decls; | 
| 287 | val mxs = List.mapPartial prep_mixfix decls; | |
| 288 | val (fixed, mxs_output) = Library.split_list (List.mapPartial prep_mixfix' decls); | |
| 12093 | 289 | val trs = map fixed_tr fixed; | 
| 290 | val syn' = syn | |
| 14901 | 291 |         |> Syntax.extend_const_gram is_logtype ("", false) mxs_output
 | 
| 292 |         |> Syntax.extend_const_gram is_logtype ("", true) mxs
 | |
| 15750 | 293 | |> Syntax.extend_trfuns ([], mk trs, [], []); | 
| 16540 | 294 | in ((syn', structs', fixed @ mixfixed), asms, binds, thms, cases, defs) end); | 
| 12093 | 295 | |
| 16540 | 296 | fun syn_of ctxt = | 
| 297 | let | |
| 298 | val (syn, structs, _) = syntax_of ctxt; | |
| 299 | val (atrs, trs, trs', atrs') = Syntax.struct_trfuns structs; | |
| 15750 | 300 | in syn |> Syntax.extend_trfuns (mk atrs, mk trs, mk trs', mk atrs') end; | 
| 14697 | 301 | |
| 12093 | 302 | end; | 
| 303 | ||
| 304 | ||
| 305 | ||
| 14828 | 306 | (** pretty printing **) | 
| 307 | ||
| 16458 | 308 | fun pretty_term ctxt t = Sign.pretty_term' (syn_of ctxt) (theory_of ctxt) (context_tr' ctxt t); | 
| 309 | fun pretty_typ ctxt T = Sign.pretty_typ (theory_of ctxt) T; | |
| 310 | fun pretty_sort ctxt S = Sign.pretty_sort (theory_of ctxt) S; | |
| 311 | fun pretty_classrel ctxt cs = Sign.pretty_classrel (theory_of ctxt) cs; | |
| 312 | fun pretty_arity ctxt ar = Sign.pretty_arity (theory_of ctxt) ar; | |
| 14828 | 313 | |
| 14974 
b1ecb7859c99
avoid premature evaluation of syn_of (wastes time in conjunction with pp);
 wenzelm parents: 
14901diff
changeset | 314 | 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: 
14901diff
changeset | 315 | pretty_classrel ctxt, pretty_arity ctxt); | 
| 14828 | 316 | |
| 317 | val string_of_term = Pretty.string_of oo pretty_term; | |
| 318 | ||
| 319 | fun pretty_thm ctxt thm = | |
| 320 | if ! Display.show_hyps then | |
| 14876 | 321 | Display.pretty_thm_aux (pp ctxt) false thm | 
| 14828 | 322 | else pretty_term ctxt (Thm.prop_of thm); | 
| 323 | ||
| 324 | fun pretty_thms ctxt [th] = pretty_thm ctxt th | |
| 325 | | pretty_thms ctxt ths = Pretty.blk (0, Pretty.fbreaks (map (pretty_thm ctxt) ths)); | |
| 326 | ||
| 327 | fun pretty_fact ctxt ("", ths) = pretty_thms ctxt ths
 | |
| 328 | | pretty_fact ctxt (a, [th]) = | |
| 329 | Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1, pretty_thm ctxt th] | |
| 330 | | pretty_fact ctxt (a, ths) = | |
| 331 | Pretty.block (Pretty.fbreaks (Pretty.str (a ^ ":") :: map (pretty_thm ctxt) ths)); | |
| 332 | ||
| 333 | ||
| 334 | ||
| 7663 | 335 | (** default sorts and types **) | 
| 336 | ||
| 16540 | 337 | fun def_sort ctxt xi = Vartab.lookup (#2 (defaults_of ctxt), xi); | 
| 7663 | 338 | |
| 16540 | 339 | fun def_type ctxt pattern xi = | 
| 340 |   let val {binds, defs = (types, _, _, _), ...} = rep_context ctxt in
 | |
| 341 | (case Vartab.lookup (types, xi) of | |
| 342 | NONE => | |
| 343 | if pattern then NONE | |
| 344 | else Vartab.lookup (binds, xi) |> Option.map (TypeInfer.polymorphicT o #2) | |
| 345 | | some => some) | |
| 346 | end; | |
| 7663 | 347 | |
| 16540 | 348 | fun default_type ctxt x = Vartab.lookup (#1 (defaults_of ctxt), (x, ~1)); | 
| 349 | val used_types = #3 o defaults_of; | |
| 12414 | 350 | |
| 7663 | 351 | |
| 5819 | 352 | |
| 353 | (** prepare types **) | |
| 354 | ||
| 9504 | 355 | local | 
| 356 | ||
| 357 | fun read_typ_aux read ctxt s = | |
| 16458 | 358 | transform_error (read (syn_of ctxt) (theory_of ctxt, def_sort ctxt)) s | 
| 7663 | 359 | handle ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt); | 
| 5819 | 360 | |
| 10554 | 361 | fun cert_typ_aux cert ctxt raw_T = | 
| 16458 | 362 | cert (theory_of ctxt) raw_T | 
| 10554 | 363 | handle TYPE (msg, _, _) => raise CONTEXT (msg, ctxt); | 
| 9504 | 364 | |
| 365 | in | |
| 366 | ||
| 16348 | 367 | val read_typ = read_typ_aux Sign.read_typ'; | 
| 368 | val read_typ_syntax = read_typ_aux Sign.read_typ_syntax'; | |
| 369 | val read_typ_abbrev = read_typ_aux Sign.read_typ_abbrev'; | |
| 370 | val cert_typ = cert_typ_aux Sign.certify_typ; | |
| 371 | val cert_typ_syntax = cert_typ_aux Sign.certify_typ_syntax; | |
| 372 | val cert_typ_abbrev = cert_typ_aux Sign.certify_typ_abbrev; | |
| 9504 | 373 | |
| 374 | end; | |
| 375 | ||
| 5819 | 376 | |
| 7679 | 377 | (* internalize Skolem constants *) | 
| 378 | ||
| 10583 | 379 | fun lookup_skolem ctxt x = assoc (fixes_of ctxt, x); | 
| 15974 | 380 | fun get_skolem ctxt x = if_none (lookup_skolem ctxt x) x; | 
| 7679 | 381 | |
| 12504 | 382 | fun no_skolem internal ctxt x = | 
| 9291 
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
 wenzelm parents: 
9274diff
changeset | 383 | if can Syntax.dest_skolem x then | 
| 7679 | 384 |     raise CONTEXT ("Illegal reference to internal Skolem constant: " ^ quote x, ctxt)
 | 
| 12504 | 385 | else if not internal andalso can Syntax.dest_internal x then | 
| 9291 
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
 wenzelm parents: 
9274diff
changeset | 386 |     raise CONTEXT ("Illegal reference to internal variable: " ^ quote x, ctxt)
 | 
| 7679 | 387 | else x; | 
| 388 | ||
| 14720 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 389 | fun intern_skolem ctxt internal = | 
| 7679 | 390 | let | 
| 391 | fun intern (t as Free (x, T)) = | |
| 14720 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 392 | if internal x then t | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 393 | else | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 394 | (case lookup_skolem ctxt (no_skolem false ctxt x) of | 
| 15531 | 395 | SOME x' => Free (x', T) | 
| 396 | | NONE => t) | |
| 7679 | 397 | | intern (t $ u) = intern t $ intern u | 
| 398 | | intern (Abs (x, T, t)) = Abs (x, T, intern t) | |
| 399 | | intern a = a; | |
| 400 | in intern end; | |
| 401 | ||
| 402 | ||
| 9133 | 403 | (* externalize Skolem constants -- for printing purposes only *) | 
| 404 | ||
| 405 | fun extern_skolem ctxt = | |
| 406 | let | |
| 407 | val rev_fixes = map Library.swap (fixes_of ctxt); | |
| 408 | ||
| 409 | fun extern (t as Free (x, T)) = | |
| 410 | (case assoc (rev_fixes, x) of | |
| 15531 | 411 | SOME x' => Free (if lookup_skolem ctxt x' = SOME x then x' else NameSpace.hidden x', T) | 
| 412 | | NONE => t) | |
| 9133 | 413 | | extern (t $ u) = extern t $ extern u | 
| 414 | | extern (Abs (x, T, t)) = Abs (x, T, extern t) | |
| 415 | | extern a = a; | |
| 416 | in extern end | |
| 417 | ||
| 8096 | 418 | |
| 5819 | 419 | (** prepare terms and propositions **) | 
| 420 | ||
| 421 | (* | |
| 16501 | 422 | (1) read / certify wrt. theory of context | 
| 5819 | 423 | (2) intern Skolem constants | 
| 424 | (3) expand term bindings | |
| 425 | *) | |
| 426 | ||
| 427 | ||
| 16501 | 428 | (* read wrt. theory *) (*exception ERROR*) | 
| 5819 | 429 | |
| 16458 | 430 | fun read_def_termTs freeze pp syn thy (types, sorts, used) sTs = | 
| 431 | Sign.read_def_terms' pp (Sign.is_logtype thy) syn (thy, types, sorts) used freeze sTs; | |
| 5874 | 432 | |
| 16458 | 433 | fun read_def_termT freeze pp syn thy defs sT = | 
| 434 | apfst hd (read_def_termTs freeze pp syn thy defs [sT]); | |
| 14828 | 435 | |
| 16458 | 436 | fun read_term_thy freeze pp syn thy defs s = | 
| 437 | #1 (read_def_termT freeze pp syn thy defs (s, TypeInfer.logicT)); | |
| 5874 | 438 | |
| 16458 | 439 | fun read_prop_thy freeze pp syn thy defs s = | 
| 440 | #1 (read_def_termT freeze pp syn thy defs (s, propT)); | |
| 12072 
4281198fb8cd
local syntax: add_syntax, proper read/pretty functions;
 wenzelm parents: 
12066diff
changeset | 441 | |
| 16458 | 442 | fun read_terms_thy freeze pp syn thy defs = | 
| 443 | #1 o read_def_termTs freeze pp syn thy defs o map (rpair TypeInfer.logicT); | |
| 12072 
4281198fb8cd
local syntax: add_syntax, proper read/pretty functions;
 wenzelm parents: 
12066diff
changeset | 444 | |
| 16458 | 445 | fun read_props_thy freeze pp syn thy defs = | 
| 446 | #1 o read_def_termTs freeze pp syn thy defs o map (rpair propT); | |
| 5819 | 447 | |
| 448 | ||
| 449 | (* norm_term *) | |
| 450 | ||
| 451 | (*beta normal form for terms (not eta normal form), chase variables in | |
| 452 | bindings environment (code taken from Pure/envir.ML)*) | |
| 453 | ||
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 454 | fun unifyT ctxt (T, U) = | 
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 455 | let val maxidx = Int.max (Term.maxidx_of_typ T, Term.maxidx_of_typ U) | 
| 16458 | 456 | in #1 (Type.unify (Sign.tsig_of (theory_of ctxt)) (Vartab.empty, maxidx) (T, U)) end; | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 457 | |
| 16540 | 458 | fun norm_term ctxt schematic = | 
| 5819 | 459 | let | 
| 460 | (*raised when norm has no effect on a term, to do sharing instead of copying*) | |
| 461 | exception SAME; | |
| 462 | ||
| 16540 | 463 | val binds = binds_of ctxt; | 
| 5819 | 464 | fun norm (t as Var (xi, T)) = | 
| 465 | (case Vartab.lookup (binds, xi) of | |
| 15758 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 466 | SOME (u, U) => | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 467 | let | 
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 468 | val env = unifyT ctxt (T, U) handle Type.TUNIFY => | 
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13629diff
changeset | 469 |                   raise TYPE ("norm_term: ill-typed variable assignment", [T, U], [t, u]);
 | 
| 15798 
016f3be5a5ec
Adapted to new interface of instantiation and unification / matching functions.
 berghofe parents: 
15758diff
changeset | 470 | val u' = Envir.subst_TVars env u; | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 471 | in norm u' handle SAME => u' end | 
| 15758 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 472 | | NONE => | 
| 10554 | 473 | if schematic then raise SAME | 
| 474 |             else raise CONTEXT ("Unbound schematic variable: " ^ Syntax.string_of_vname xi, ctxt))
 | |
| 5819 | 475 | | norm (Abs (a, T, body)) = Abs (a, T, norm body) | 
| 476 | | norm (Abs (_, _, body) $ t) = normh (subst_bound (t, body)) | |
| 477 | | norm (f $ t) = | |
| 478 | ((case norm f of | |
| 479 | Abs (_, _, body) => normh (subst_bound (t, body)) | |
| 480 | | nf => nf $ (norm t handle SAME => t)) handle SAME => f $ norm t) | |
| 481 | | norm _ = raise SAME | |
| 482 | and normh t = norm t handle SAME => t | |
| 483 | in normh end; | |
| 484 | ||
| 485 | ||
| 6550 | 486 | (* dummy patterns *) | 
| 487 | ||
| 9540 | 488 | fun prepare_dummies t = #2 (Term.replace_dummy_patterns (1, t)); | 
| 6762 | 489 | |
| 9540 | 490 | fun reject_dummies ctxt t = Term.no_dummy_patterns t | 
| 491 |   handle TERM _ => raise CONTEXT ("Illegal dummy pattern(s) in term", ctxt);
 | |
| 6550 | 492 | |
| 493 | ||
| 5819 | 494 | (* read terms *) | 
| 495 | ||
| 10554 | 496 | local | 
| 497 | ||
| 15531 | 498 | 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: 
14707diff
changeset | 499 | |
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 500 | fun gen_read' read app pattern schematic | 
| 14720 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 501 | ctxt internal more_types more_sorts more_used s = | 
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13629diff
changeset | 502 | let | 
| 14720 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 503 | val types = append_env (def_type ctxt pattern) more_types; | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 504 | val sorts = append_env (def_sort ctxt) more_sorts; | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 505 | val used = used_types ctxt @ more_used; | 
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13629diff
changeset | 506 | in | 
| 16458 | 507 | (transform_error (read (pp ctxt) (syn_of ctxt) (theory_of ctxt) (types, sorts, used)) s | 
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13629diff
changeset | 508 | handle TERM (msg, _) => raise CONTEXT (msg, ctxt) | 
| 14720 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 509 | | ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt)) | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 510 | |> app (intern_skolem ctxt internal) | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 511 | |> app (if pattern then I else norm_term ctxt schematic) | 
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 512 | |> app (if pattern then prepare_dummies else reject_dummies ctxt) | 
| 14720 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 513 | end; | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 514 | |
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 515 | fun gen_read read app pattern schematic ctxt = | 
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 516 | gen_read' read app pattern schematic ctxt (K false) (K NONE) (K NONE) []; | 
| 14720 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 517 | |
| 10554 | 518 | in | 
| 519 | ||
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 520 | 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: 
15979diff
changeset | 521 | val read_termTs_schematic = gen_read' (read_def_termTs false) (apfst o map) false true; | 
| 8096 | 522 | |
| 14720 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 523 | fun read_term_pats T ctxt = | 
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 524 | #1 o gen_read (read_def_termTs false) (apfst o map) true false ctxt o map (rpair T); | 
| 8096 | 525 | val read_prop_pats = read_term_pats propT; | 
| 526 | ||
| 14720 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 527 | fun read_term_liberal ctxt = | 
| 16458 | 528 | 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: 
14707diff
changeset | 529 | |
| 16458 | 530 | val read_term = gen_read (read_term_thy true) I false false; | 
| 531 | val read_prop = gen_read (read_prop_thy true) I false false; | |
| 532 | val read_prop_schematic = gen_read (read_prop_thy true) I false true; | |
| 533 | val read_terms = gen_read (read_terms_thy true) map false false; | |
| 534 | fun read_props schematic = gen_read (read_props_thy true) map false schematic; | |
| 5819 | 535 | |
| 10554 | 536 | end; | 
| 537 | ||
| 5819 | 538 | |
| 539 | (* certify terms *) | |
| 540 | ||
| 10554 | 541 | local | 
| 542 | ||
| 14720 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 543 | fun gen_cert cert pattern schematic ctxt t = t | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 544 | |> (if pattern then I else norm_term ctxt schematic) | 
| 16458 | 545 | |> (fn t' => cert (pp ctxt) (theory_of ctxt) t' | 
| 16501 | 546 | handle TYPE (msg, _, _) => raise CONTEXT (msg, ctxt) | 
| 547 | | TERM (msg, _) => raise CONTEXT (msg, ctxt)); | |
| 548 | ||
| 549 | val certify_term = #1 ooo Sign.certify_term; | |
| 550 | val certify_prop = #1 ooo Sign.certify_prop; | |
| 5819 | 551 | |
| 10554 | 552 | in | 
| 8096 | 553 | |
| 16501 | 554 | val cert_term = gen_cert certify_term false false; | 
| 555 | val cert_prop = gen_cert certify_prop false false; | |
| 556 | val cert_props = map oo gen_cert certify_prop false; | |
| 10554 | 557 | |
| 16501 | 558 | fun cert_term_pats _ = map o gen_cert certify_term true false; | 
| 559 | val cert_prop_pats = map o gen_cert certify_prop true false; | |
| 10554 | 560 | |
| 561 | end; | |
| 5819 | 562 | |
| 563 | ||
| 564 | (* declare terms *) | |
| 565 | ||
| 10381 | 566 | local | 
| 567 | ||
| 5819 | 568 | val ins_types = foldl_aterms | 
| 569 | (fn (types, Free (x, T)) => Vartab.update (((x, ~1), T), types) | |
| 570 | | (types, Var v) => Vartab.update (v, types) | |
| 571 | | (types, _) => types); | |
| 572 | ||
| 573 | val ins_sorts = foldl_types (foldl_atyps | |
| 574 | (fn (sorts, TFree (x, S)) => Vartab.update (((x, ~1), S), sorts) | |
| 575 | | (sorts, TVar v) => Vartab.update (v, sorts) | |
| 576 | | (sorts, _) => sorts)); | |
| 577 | ||
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 578 | val ins_used = foldl_term_types (fn t => foldl_atyps | 
| 12504 | 579 | (fn (used, TFree (x, _)) => x ins_string used | 
| 580 | | (used, _) => used)); | |
| 12291 | 581 | |
| 582 | val ins_occs = foldl_term_types (fn t => foldl_atyps | |
| 583 | (fn (tab, TFree (x, _)) => Symtab.update_multi ((x, t), tab) | (tab, _) => tab)); | |
| 5819 | 584 | |
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 585 | fun ins_skolem def_ty = foldr | 
| 5994 | 586 | (fn ((x, x'), types) => | 
| 7663 | 587 | (case def_ty x' of | 
| 15531 | 588 | SOME T => Vartab.update (((x, ~1), T), types) | 
| 589 | | NONE => types)); | |
| 5994 | 590 | |
| 16540 | 591 | fun map_defaults f = map_context (fn (syntax, asms, binds, thms, cases, defs) => | 
| 592 | (syntax, asms, binds, thms, cases, f defs)); | |
| 5819 | 593 | |
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 594 | in | 
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 595 | |
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 596 | fun declare_term_syntax t ctxt = | 
| 5819 | 597 | ctxt | 
| 12291 | 598 | |> map_defaults (fn (types, sorts, used, occ) => (ins_types (types, t), sorts, used, occ)) | 
| 599 | |> map_defaults (fn (types, sorts, used, occ) => (types, ins_sorts (sorts, t), used, occ)) | |
| 600 | |> map_defaults (fn (types, sorts, used, occ) => (types, sorts, ins_used (used, t), occ)); | |
| 10381 | 601 | |
| 16540 | 602 | fun declare_term t ctxt = | 
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 603 | ctxt | 
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 604 | |> declare_term_syntax t | 
| 12291 | 605 | |> map_defaults (fn (types, sorts, used, occ) => (types, sorts, used, ins_occs (occ, t))) | 
| 606 | |> map_defaults (fn (types, sorts, used, occ) => | |
| 16540 | 607 | (ins_skolem (fn x => | 
| 608 | Vartab.lookup (types, (x, ~1))) types (fixes_of ctxt), sorts, used, occ)); | |
| 5819 | 609 | |
| 10381 | 610 | end; | 
| 5819 | 611 | |
| 612 | ||
| 15703 | 613 | (* type and constant names *) | 
| 614 | ||
| 615 | fun read_tyname ctxt c = | |
| 616 | if c mem_string used_types ctxt then | |
| 16458 | 617 | TFree (c, if_none (def_sort ctxt (c, ~1)) (Sign.defaultS (theory_of ctxt))) | 
| 618 | else Sign.read_tyname (theory_of ctxt) c; | |
| 15703 | 619 | |
| 620 | fun read_const ctxt c = | |
| 621 | (case lookup_skolem ctxt c of | |
| 622 | SOME c' => Free (c', dummyT) | |
| 16458 | 623 | | NONE => Sign.read_const (theory_of ctxt) c); | 
| 15703 | 624 | |
| 625 | ||
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 626 | |
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 627 | (** Hindley-Milner polymorphism **) | 
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 628 | |
| 7925 | 629 | (* warn_extra_tfrees *) | 
| 630 | ||
| 16540 | 631 | fun warn_extra_tfrees ctxt1 ctxt2 = | 
| 12130 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 632 | let | 
| 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 633 | fun known_tfree a (Type (_, Ts)) = exists (known_tfree a) Ts | 
| 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 634 | | known_tfree a (TFree (a', _)) = a = a' | 
| 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 635 | | known_tfree _ _ = false; | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 636 | |
| 12130 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 637 | val extras = | 
| 16540 | 638 | Library.gen_rems Library.eq_fst (pairself (Symtab.dest o type_occs_of) (ctxt1, ctxt2)) | 
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 639 | |> map (fn (a, ts) => map (pair a) (List.mapPartial (try (#1 o Term.dest_Free)) ts)) | 
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 640 | |> List.concat | 
| 15570 | 641 | |> List.mapPartial (fn (a, x) => | 
| 15531 | 642 | (case def_type ctxt1 false (x, ~1) of NONE => SOME (a, x) | 
| 643 | | SOME T => if known_tfree a T then NONE else SOME (a, x))); | |
| 12130 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 644 | val tfrees = map #1 extras |> Library.sort_strings |> Library.unique_strings; | 
| 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 645 | val frees = map #2 extras |> Library.sort_strings |> Library.unique_strings; | 
| 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 646 | in | 
| 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 647 | if null extras then () | 
| 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 648 |     else warning ("Just introduced free type variable(s): " ^ commas tfrees ^ " in " ^
 | 
| 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 649 | space_implode " or " frees); | 
| 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 650 | ctxt2 | 
| 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 651 | end; | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 652 | |
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 653 | |
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 654 | (* generalize type variables *) | 
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 655 | |
| 12550 
32843ad8160a
generalize type variables properly: start with occurrences in objects
 wenzelm parents: 
12530diff
changeset | 656 | fun generalize_tfrees inner outer = | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 657 | let | 
| 12057 | 658 | val extra_fixes = fixed_names_of inner \\ fixed_names_of outer; | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 659 | fun still_fixed (Free (x, _)) = not (x mem_string extra_fixes) | 
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 660 | | still_fixed _ = false; | 
| 16540 | 661 | val occs_inner = type_occs_of inner; | 
| 662 | val occs_outer = type_occs_of outer; | |
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 663 | fun add a gen = | 
| 15974 | 664 | if is_some (Symtab.lookup (occs_outer, a)) orelse | 
| 12550 
32843ad8160a
generalize type variables properly: start with occurrences in objects
 wenzelm parents: 
12530diff
changeset | 665 | exists still_fixed (Symtab.lookup_multi (occs_inner, a)) | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 666 | then gen else a :: gen; | 
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 667 | in fn tfrees => fold add tfrees [] end; | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 668 | |
| 12550 
32843ad8160a
generalize type variables properly: start with occurrences in objects
 wenzelm parents: 
12530diff
changeset | 669 | fun generalize inner outer ts = | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 670 | let | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 671 | val tfrees = generalize_tfrees inner outer (foldr Term.add_term_tfree_names [] ts); | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 672 | fun gen (x, S) = if x mem_string tfrees then TVar ((x, 0), S) else TFree (x, S); | 
| 12550 
32843ad8160a
generalize type variables properly: start with occurrences in objects
 wenzelm parents: 
12530diff
changeset | 673 | 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: 
8462diff
changeset | 674 | |
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 675 | |
| 9553 | 676 | |
| 677 | (** export theorems **) | |
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 678 | |
| 15531 | 679 | fun get_free x (NONE, t as Free (y, _)) = if x = y then SOME t else NONE | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 680 | | get_free _ (opt, _) = opt; | 
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 681 | |
| 15531 | 682 | fun find_free t x = foldl_aterms (get_free x) (NONE, t); | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 683 | |
| 13415 | 684 | fun export_view view is_goal inner outer = | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 685 | let | 
| 12550 
32843ad8160a
generalize type variables properly: start with occurrences in objects
 wenzelm parents: 
12530diff
changeset | 686 | val gen = generalize_tfrees inner outer; | 
| 12057 | 687 | val fixes = fixed_names_of inner \\ fixed_names_of outer; | 
| 688 | val asms = Library.drop (length (assumptions_of outer), assumptions_of inner); | |
| 11816 | 689 | val exp_asms = map (fn (cprops, exp) => exp is_goal cprops) asms; | 
| 12550 
32843ad8160a
generalize type variables properly: start with occurrences in objects
 wenzelm parents: 
12530diff
changeset | 690 | in fn thm => thm | 
| 12804 | 691 | |> Tactic.norm_hhf_rule | 
| 11816 | 692 | |> Seq.EVERY (rev exp_asms) | 
| 13415 | 693 | |> Seq.map (Drule.implies_intr_list view) | 
| 11816 | 694 | |> Seq.map (fn rule => | 
| 695 | let | |
| 16458 | 696 |         val {thy, prop, ...} = Thm.rep_thm rule;
 | 
| 697 | val frees = map (Thm.cterm_of thy) (List.mapPartial (find_free prop) fixes); | |
| 12550 
32843ad8160a
generalize type variables properly: start with occurrences in objects
 wenzelm parents: 
12530diff
changeset | 698 | val tfrees = gen (Term.add_term_tfree_names (prop, [])); | 
| 11816 | 699 | in | 
| 700 | rule | |
| 701 | |> Drule.forall_intr_list frees | |
| 12804 | 702 | |> Tactic.norm_hhf_rule | 
| 12504 | 703 | |> (#1 o Drule.tvars_intr_list tfrees) | 
| 11816 | 704 | end) | 
| 705 | end; | |
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 706 | |
| 15758 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 707 | (*without varification*) | 
| 15696 | 708 | fun export_view' view is_goal inner outer = | 
| 709 | let | |
| 710 | val asms = Library.drop (length (assumptions_of outer), assumptions_of inner); | |
| 711 | val exp_asms = map (fn (cprops, exp) => exp is_goal cprops) asms; | |
| 712 | in fn thm =>thm | |
| 713 | |> Tactic.norm_hhf_plain | |
| 714 | |> Seq.EVERY (rev exp_asms) | |
| 715 | |> Seq.map (Drule.implies_intr_list view) | |
| 716 | |> Seq.map Tactic.norm_hhf_plain | |
| 717 | end; | |
| 718 | ||
| 13415 | 719 | val export = export_view []; | 
| 13399 
c136276dc847
support locale ``views'' (for cumulative predicates);
 wenzelm parents: 
13378diff
changeset | 720 | |
| 15696 | 721 | fun gen_export_std exp_view view inner outer = | 
| 722 | let val exp = exp_view view false inner outer in | |
| 13378 | 723 | fn th => | 
| 724 | (case Seq.pull (exp th) of | |
| 15531 | 725 | SOME (th', _) => th' |> Drule.local_standard | 
| 726 |       | NONE => raise CONTEXT ("Internal failure while exporting theorem", inner))
 | |
| 13378 | 727 | end; | 
| 12704 | 728 | |
| 15696 | 729 | val export_standard = gen_export_std export_view; | 
| 730 | val export_plain = gen_export_std export_view'; | |
| 7925 | 731 | |
| 5819 | 732 | |
| 15758 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 733 | |
| 5819 | 734 | (** bindings **) | 
| 735 | ||
| 15758 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 736 | (* delete_update_binds *) | 
| 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 737 | |
| 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 738 | local | 
| 5819 | 739 | |
| 16540 | 740 | fun del_bind xi = map_context (fn (syntax, asms, binds, thms, cases, defs) => | 
| 741 | (syntax, asms, Vartab.delete_safe xi binds, thms, cases, defs)); | |
| 7606 | 742 | |
| 15758 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 743 | fun upd_bind ((x, i), t) = | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 744 | let | 
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 745 | val T = Term.fastype_of t; | 
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 746 | val t' = | 
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 747 | if null (Term.term_tvars t \\ Term.typ_tvars T) then t | 
| 8637 | 748 | else Var ((x ^ "_has_extra_type_vars_on_rhs", i), T); | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 749 | in | 
| 16540 | 750 | map_context (fn (syntax, asms, binds, thms, cases, defs) => | 
| 751 | (syntax, asms, Vartab.update (((x, i), (t', T)), binds), thms, cases, defs)) | |
| 15758 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 752 | o declare_term t' | 
| 5819 | 753 | end; | 
| 754 | ||
| 15758 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 755 | fun del_upd_bind (xi, NONE) = del_bind xi | 
| 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 756 | | del_upd_bind (xi, SOME t) = upd_bind (xi, t); | 
| 7606 | 757 | |
| 15758 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 758 | in | 
| 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 759 | |
| 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 760 | val delete_update_binds = fold del_upd_bind; | 
| 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 761 | |
| 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 762 | end; | 
| 7606 | 763 | |
| 5819 | 764 | |
| 8096 | 765 | (* simult_matches *) | 
| 766 | ||
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 767 | fun simult_matches ctxt [] = [] | 
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 768 | | simult_matches ctxt pairs = | 
| 8096 | 769 | let | 
| 10554 | 770 |         fun fail () = raise CONTEXT ("Pattern match failed!", ctxt);
 | 
| 771 | ||
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 772 | val maxidx = fold (fn (t1, t2) => fn i => | 
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 773 | Int.max (Int.max (Term.maxidx_of_term t1, Term.maxidx_of_term t2), i)) pairs ~1; | 
| 16458 | 774 | val envs = Unify.smash_unifiers (theory_of ctxt, Envir.empty maxidx, | 
| 10554 | 775 | map swap pairs); (*prefer assignment of variables from patterns*) | 
| 776 | val env = | |
| 8096 | 777 | (case Seq.pull envs of | 
| 15531 | 778 | NONE => fail () | 
| 779 | | SOME (env, _) => env); (*ignore further results*) | |
| 12309 
03e9287be350
name space for local thms (export cond_extern, qualified);
 wenzelm parents: 
12291diff
changeset | 780 | val domain = | 
| 
03e9287be350
name space for local thms (export cond_extern, qualified);
 wenzelm parents: 
12291diff
changeset | 781 | filter_out Term.is_replaced_dummy_pattern (map #1 (Drule.vars_of_terms (map #1 pairs))); | 
| 10554 | 782 | val _ = (*may not assign variables from text*) | 
| 12309 
03e9287be350
name space for local thms (export cond_extern, qualified);
 wenzelm parents: 
12291diff
changeset | 783 | 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: 
12291diff
changeset | 784 | then () else fail (); | 
| 15798 
016f3be5a5ec
Adapted to new interface of instantiation and unification / matching functions.
 berghofe parents: 
15758diff
changeset | 785 | fun norm_bind (xi, (_, t)) = if xi mem domain then SOME (xi, Envir.norm_term env t) else NONE; | 
| 15570 | 786 | in List.mapPartial norm_bind (Envir.alist_of env) end; | 
| 8096 | 787 | |
| 788 | ||
| 789 | (* add_binds(_i) *) | |
| 5819 | 790 | |
| 7925 | 791 | local | 
| 792 | ||
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 793 | fun gen_bind prep (xi as (x, _), raw_t) ctxt = | 
| 15570 | 794 | ctxt |> delete_update_binds [(xi, Option.map (prep ctxt) raw_t)]; | 
| 5819 | 795 | |
| 10810 | 796 | in | 
| 797 | ||
| 15531 | 798 | fun drop_schematic (b as (xi, SOME t)) = if null (Term.term_vars t) then b else (xi, NONE) | 
| 10554 | 799 | | drop_schematic b = b; | 
| 800 | ||
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 801 | val add_binds = fold (gen_bind read_term); | 
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 802 | val add_binds_i = fold (gen_bind cert_term); | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 803 | |
| 16458 | 804 | fun auto_bind f ts ctxt = ctxt |> add_binds_i (map drop_schematic (f (theory_of ctxt) ts)); | 
| 12147 | 805 | val auto_bind_goal = auto_bind AutoBind.goal; | 
| 806 | val auto_bind_facts = auto_bind AutoBind.facts; | |
| 7925 | 807 | |
| 808 | end; | |
| 5819 | 809 | |
| 810 | ||
| 8096 | 811 | (* match_bind(_i) *) | 
| 5819 | 812 | |
| 8096 | 813 | local | 
| 814 | ||
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 815 | fun prep_bind prep_pats (ctxt, (raw_pats, t)) = | 
| 5819 | 816 | let | 
| 8096 | 817 | val ctxt' = declare_term t ctxt; | 
| 818 | val pats = prep_pats (fastype_of t) ctxt' raw_pats; | |
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 819 | val binds = simult_matches ctxt' (map (rpair t) pats); | 
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 820 | in (ctxt', binds) end; | 
| 7670 | 821 | |
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 822 | fun gen_binds prep_terms prep_pats gen raw_binds ctxt = | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 823 | let | 
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 824 | val ts = prep_terms ctxt (map snd raw_binds); | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 825 | val (ctxt', binds) = | 
| 15570 | 826 | apsnd List.concat (foldl_map (prep_bind prep_pats) (ctxt, map fst raw_binds ~~ ts)); | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 827 | val binds' = | 
| 12550 
32843ad8160a
generalize type variables properly: start with occurrences in objects
 wenzelm parents: 
12530diff
changeset | 828 | if gen then map #1 binds ~~ generalize ctxt' ctxt (map #2 binds) | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 829 | else binds; | 
| 15531 | 830 | val binds'' = map (apsnd SOME) binds'; | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 831 | in | 
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 832 | warn_extra_tfrees ctxt | 
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 833 | (if gen then | 
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 834 | ctxt (*sic!*) |> fold declare_term (map #2 binds') |> add_binds_i binds'' | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 835 | else ctxt' |> add_binds_i binds'') | 
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 836 | end; | 
| 8096 | 837 | |
| 838 | in | |
| 5935 | 839 | |
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 840 | val match_bind = gen_binds read_terms read_term_pats; | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 841 | val match_bind_i = gen_binds (map o cert_term) cert_term_pats; | 
| 8096 | 842 | |
| 843 | end; | |
| 5935 | 844 | |
| 845 | ||
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 846 | (* propositions with patterns *) | 
| 5935 | 847 | |
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 848 | local | 
| 8096 | 849 | |
| 10554 | 850 | fun prep_propp schematic prep_props prep_pats (context, args) = | 
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 851 | let | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 852 | fun prep ((ctxt, prop :: props), (_, (raw_pats1, raw_pats2))) = | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 853 | let | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 854 | val ctxt' = declare_term prop ctxt; | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 855 | val pats = prep_pats ctxt' (raw_pats1 @ raw_pats2); (*simultaneous type inference!*) | 
| 13629 | 856 | in ((ctxt', props), (prop, splitAt(length raw_pats1, pats))) end | 
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 857 | | prep _ = sys_error "prep_propp"; | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 858 | val ((context', _), propp) = foldl_map (foldl_map prep) | 
| 15570 | 859 | ((context, prep_props schematic context (List.concat (map (map fst) args))), args); | 
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 860 | in (context', propp) end; | 
| 5935 | 861 | |
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 862 | fun matches ctxt (prop, (pats1, pats2)) = | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 863 | simult_matches ctxt (map (rpair prop) pats1 @ map (rpair (Logic.strip_imp_concl prop)) pats2); | 
| 8096 | 864 | |
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 865 | fun gen_bind_propp prepp (ctxt, raw_args) = | 
| 8096 | 866 | let | 
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 867 | val (ctxt', args) = prepp (ctxt, raw_args); | 
| 15570 | 868 | val binds = List.concat (List.concat (map (map (matches ctxt')) args)); | 
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 869 | val propss = map (map #1) args; | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 870 | |
| 10554 | 871 | (*generalize result: context evaluated now, binds added later*) | 
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 872 | val gen = generalize ctxt' ctxt; | 
| 15531 | 873 | fun gen_binds c = c |> add_binds_i (map #1 binds ~~ map SOME (gen (map #2 binds))); | 
| 874 | in (ctxt' |> add_binds_i (map (apsnd SOME) binds), (propss, gen_binds)) end; | |
| 8096 | 875 | |
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 876 | in | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 877 | |
| 11925 | 878 | val read_propp = prep_propp false read_props read_prop_pats; | 
| 879 | val cert_propp = prep_propp false cert_props cert_prop_pats; | |
| 10554 | 880 | val read_propp_schematic = prep_propp true read_props read_prop_pats; | 
| 881 | val cert_propp_schematic = prep_propp true cert_props cert_prop_pats; | |
| 882 | ||
| 11925 | 883 | val bind_propp = gen_bind_propp read_propp; | 
| 884 | val bind_propp_i = gen_bind_propp cert_propp; | |
| 885 | val bind_propp_schematic = gen_bind_propp read_propp_schematic; | |
| 10554 | 886 | val bind_propp_schematic_i = gen_bind_propp cert_propp_schematic; | 
| 6789 | 887 | |
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 888 | end; | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 889 | |
| 6789 | 890 | |
| 5819 | 891 | |
| 892 | (** theorems **) | |
| 893 | ||
| 6091 | 894 | (* get_thm(s) *) | 
| 5819 | 895 | |
| 9566 | 896 | (*beware of proper order of evaluation!*) | 
| 16540 | 897 | fun retrieve_thms from_thy pick ctxt = | 
| 898 | let | |
| 899 | val thy_ref = Theory.self_ref (theory_of ctxt); | |
| 900 | val (_, (space, tab), _) = thms_of ctxt; | |
| 901 | in | |
| 16501 | 902 | fn xthmref => | 
| 903 | let | |
| 16540 | 904 | val thy = Theory.deref thy_ref; | 
| 16501 | 905 | val thmref = PureThy.map_name_of_thmref (NameSpace.intern space) xthmref; | 
| 906 | val name = PureThy.name_of_thmref thmref; | |
| 907 | in | |
| 908 | (case Symtab.lookup (tab, name) of | |
| 16540 | 909 | SOME ths => map (Thm.transfer thy) (PureThy.select_thm thmref ths) | 
| 910 | | NONE => from_thy thy xthmref) |> pick name | |
| 16501 | 911 | end | 
| 9566 | 912 | end; | 
| 5819 | 913 | |
| 9566 | 914 | val get_thm = retrieve_thms PureThy.get_thms PureThy.single_thm; | 
| 915 | val get_thm_closure = retrieve_thms PureThy.get_thms_closure PureThy.single_thm; | |
| 916 | val get_thms = retrieve_thms PureThy.get_thms (K I); | |
| 917 | val get_thms_closure = retrieve_thms PureThy.get_thms_closure (K I); | |
| 5819 | 918 | |
| 919 | ||
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 920 | (* valid_thms *) | 
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 921 | |
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 922 | fun valid_thms ctxt (name, ths) = | 
| 16501 | 923 | (case try (transform_error (fn () => get_thms ctxt (Name name))) () of | 
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 924 | NONE => false | 
| 16147 | 925 | | SOME ths' => Thm.eq_thms (ths, ths')); | 
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 926 | |
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 927 | |
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 928 | (* lthms_containing *) | 
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 929 | |
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 930 | fun lthms_containing ctxt spec = | 
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 931 | FactIndex.find (fact_index_of ctxt) spec | 
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 932 | |> List.filter (valid_thms ctxt) | 
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 933 | |> gen_distinct eq_fst; | 
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 934 | |
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 935 | |
| 13425 
119ae829ad9b
support for split assumptions in cases (hyps vs. prems);
 wenzelm parents: 
13415diff
changeset | 936 | (* name space operations *) | 
| 12309 
03e9287be350
name space for local thms (export cond_extern, qualified);
 wenzelm parents: 
12291diff
changeset | 937 | |
| 16540 | 938 | val extern_thm = NameSpace.extern o #1 o #2 o thms_of; | 
| 12309 
03e9287be350
name space for local thms (export cond_extern, qualified);
 wenzelm parents: 
12291diff
changeset | 939 | |
| 16540 | 940 | fun map_naming f = map_context (fn (syntax, asms, binds, | 
| 16348 | 941 | (naming, table, index), cases, defs) => | 
| 16540 | 942 | (syntax, asms, binds, (f naming, table, index), cases, defs)); | 
| 12309 
03e9287be350
name space for local thms (export cond_extern, qualified);
 wenzelm parents: 
12291diff
changeset | 943 | |
| 16147 | 944 | val qualified_names = map_naming NameSpace.qualified_names; | 
| 945 | val no_base_names = map_naming NameSpace.no_base_names; | |
| 946 | val custom_accesses = map_naming o NameSpace.custom_accesses; | |
| 16540 | 947 | val restore_naming = map_naming o K o #1 o thms_of; | 
| 12309 
03e9287be350
name space for local thms (export cond_extern, qualified);
 wenzelm parents: 
12291diff
changeset | 948 | |
| 16348 | 949 | fun hide_thms fully names = map_context | 
| 16540 | 950 | (fn (syntax, asms, binds, (naming, (space, tab), index), cases, defs) => | 
| 951 | (syntax, asms, binds, | |
| 16348 | 952 | (naming, (fold (NameSpace.hide fully) names space, tab), index), cases, defs)); | 
| 13425 
119ae829ad9b
support for split assumptions in cases (hyps vs. prems);
 wenzelm parents: 
13415diff
changeset | 953 | |
| 12309 
03e9287be350
name space for local thms (export cond_extern, qualified);
 wenzelm parents: 
12291diff
changeset | 954 | |
| 6091 | 955 | (* put_thm(s) *) | 
| 5819 | 956 | |
| 16147 | 957 | fun put_thms ("", _) ctxt = ctxt
 | 
| 958 | | put_thms (bname, ths) ctxt = ctxt |> map_context | |
| 16540 | 959 | (fn (syntax, asms, binds, (naming, (space, tab), index), cases, defs) => | 
| 16147 | 960 | let | 
| 961 | val name = NameSpace.full naming bname; | |
| 962 | val space' = NameSpace.declare naming name space; | |
| 963 | val tab' = Symtab.update ((name, ths), tab); | |
| 964 | val index' = FactIndex.add (is_known ctxt) (name, ths) index; | |
| 16540 | 965 | in (syntax, asms, binds, (naming, (space', tab'), index'), cases, defs) end); | 
| 5819 | 966 | |
| 16147 | 967 | fun put_thm (name, th) = put_thms (name, [th]); | 
| 968 | val put_thmss = fold put_thms; | |
| 5819 | 969 | |
| 15758 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 970 | |
| 7606 | 971 | (* reset_thms *) | 
| 972 | ||
| 16540 | 973 | fun reset_thms name = (* FIXME NameSpace.hide!? *) | 
| 974 | map_context (fn (syntax, asms, binds, (q, (space, tab), index), cases, defs) => | |
| 975 | (syntax, asms, binds, (q, (space, Symtab.delete_safe name tab), index), cases, defs)); | |
| 7606 | 976 | |
| 977 | ||
| 14564 | 978 | (* note_thmss *) | 
| 5819 | 979 | |
| 12711 | 980 | local | 
| 16147 | 981 | |
| 982 | fun gen_note_thss get (ctxt, ((name, more_attrs), ths_attrs)) = | |
| 5819 | 983 | let | 
| 984 | fun app ((ct, ths), (th, attrs)) = | |
| 12711 | 985 | let val (ct', th') = Thm.applys_attributes ((ct, get ctxt th), attrs @ more_attrs) | 
| 986 | in (ct', th' :: ths) end; | |
| 15570 | 987 | val (ctxt', rev_thms) = Library.foldl app ((ctxt, []), ths_attrs); | 
| 988 | val thms = List.concat (rev rev_thms); | |
| 16147 | 989 | in (ctxt' |> put_thms (name, thms), (name, thms)) end; | 
| 5819 | 990 | |
| 16147 | 991 | fun gen_note_thmss get args ctxt = | 
| 992 | foldl_map (gen_note_thss get) (ctxt, args); | |
| 12711 | 993 | |
| 994 | in | |
| 995 | ||
| 16147 | 996 | val note_thmss = gen_note_thmss get_thms; | 
| 997 | val note_thmss_i = gen_note_thmss (K I); | |
| 15696 | 998 | |
| 999 | val note_thmss_accesses = gen_note_thmss get_thms; | |
| 1000 | val note_thmss_accesses_i = gen_note_thmss (K I); | |
| 12711 | 1001 | |
| 1002 | end; | |
| 9196 | 1003 | |
| 5819 | 1004 | |
| 1005 | ||
| 1006 | (** assumptions **) | |
| 1007 | ||
| 11918 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 1008 | (* basic exporters *) | 
| 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 1009 | |
| 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 1010 | fun export_assume true = Seq.single oo Drule.implies_intr_goals | 
| 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 1011 | | export_assume false = Seq.single oo Drule.implies_intr_list; | 
| 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 1012 | |
| 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 1013 | fun export_presume _ = export_assume false; | 
| 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 1014 | |
| 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 1015 | |
| 12057 | 1016 | (* defs *) | 
| 1017 | ||
| 12066 | 1018 | fun cert_def ctxt eq = | 
| 12016 | 1019 | let | 
| 12057 | 1020 | fun err msg = raise CONTEXT (msg ^ | 
| 1021 | "\nThe error(s) above occurred in local definition: " ^ string_of_term ctxt eq, ctxt); | |
| 1022 | val (lhs, rhs) = Logic.dest_equals (Term.strip_all_body eq) | |
| 1023 | handle TERM _ => err "Not a meta-equality (==)"; | |
| 12086 | 1024 | val (f, xs) = Term.strip_comb lhs; | 
| 1025 | val (c, _) = Term.dest_Free f handle TERM _ => | |
| 13430 | 1026 | err "Head of lhs must be a free/fixed variable"; | 
| 12057 | 1027 | |
| 12086 | 1028 | fun is_free (Free (x, _)) = not (is_fixed ctxt x) | 
| 1029 | | is_free _ = false; | |
| 15570 | 1030 | val extra_frees = List.filter is_free (term_frees rhs) \\ xs; | 
| 12016 | 1031 | in | 
| 12086 | 1032 | conditional (not (forall (is_Bound orf is_free) xs andalso null (duplicates xs))) (fn () => | 
| 13430 | 1033 | err "Arguments of lhs must be distinct free/bound variables"); | 
| 12086 | 1034 | conditional (f mem Term.term_frees rhs) (fn () => | 
| 1035 | err "Element to be defined occurs on rhs"); | |
| 1036 | conditional (not (null extra_frees)) (fn () => | |
| 1037 |       err ("Extra free variables on rhs: " ^ commas_quote (map (#1 o dest_Free) extra_frees)));
 | |
| 15570 | 1038 | (c, Term.list_all_free (List.mapPartial (try Term.dest_Free) xs, eq)) | 
| 12057 | 1039 | end; | 
| 1040 | ||
| 1041 | fun head_of_def cprop = | |
| 1042 | #1 (Term.strip_comb (#1 (Logic.dest_equals (Term.strip_all_body (Thm.term_of cprop))))) | |
| 16458 | 1043 | |> Thm.cterm_of (Thm.theory_of_cterm cprop); | 
| 11918 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 1044 | |
| 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 1045 | fun export_def _ cprops thm = | 
| 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 1046 | thm | 
| 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 1047 | |> Drule.implies_intr_list cprops | 
| 12057 | 1048 | |> Drule.forall_intr_list (map head_of_def cprops) | 
| 11918 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 1049 | |> Drule.forall_elim_vars 0 | 
| 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 1050 | |> RANGE (replicate (length cprops) (Tactic.rtac Drule.reflexive_thm)) 1; | 
| 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 1051 | |
| 
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
 wenzelm parents: 
11915diff
changeset | 1052 | |
| 5819 | 1053 | (* assume *) | 
| 1054 | ||
| 7270 | 1055 | local | 
| 6797 | 1056 | |
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 1057 | fun add_assm (ctxt, ((name, attrs), props)) = | 
| 5819 | 1058 | let | 
| 16458 | 1059 | val cprops = map (Thm.cterm_of (theory_of ctxt)) props; | 
| 12804 | 1060 | val asms = map (Tactic.norm_hhf_rule o Thm.assume) cprops; | 
| 5919 | 1061 | |
| 1062 | val ths = map (fn th => ([th], [])) asms; | |
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 1063 | val (ctxt', [(_, thms)]) = | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 1064 | ctxt | 
| 12147 | 1065 | |> auto_bind_facts props | 
| 14564 | 1066 | |> note_thmss_i [((name, attrs), ths)]; | 
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 1067 | in (ctxt', (cprops, (name, asms), (name, thms))) end; | 
| 5819 | 1068 | |
| 9470 | 1069 | fun gen_assms prepp exp args ctxt = | 
| 1070 | let | |
| 10465 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 1071 | val (ctxt1, propss) = prepp (ctxt, map snd args); | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 1072 | val (ctxt2, results) = foldl_map add_assm (ctxt1, map fst args ~~ propss); | 
| 
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
 wenzelm parents: 
10381diff
changeset | 1073 | |
| 15570 | 1074 | val cprops = List.concat (map #1 results); | 
| 9470 | 1075 | val asmss = map #2 results; | 
| 1076 | val thmss = map #3 results; | |
| 12072 
4281198fb8cd
local syntax: add_syntax, proper read/pretty functions;
 wenzelm parents: 
12066diff
changeset | 1077 | val ctxt3 = ctxt2 |> map_context | 
| 16540 | 1078 | (fn (syntax, ((asms_ct, asms_th), fixes), binds, thms, cases, defs) => | 
| 1079 | (syntax, ((asms_ct @ [(cprops, exp)], asms_th @ asmss), fixes), binds, thms, | |
| 15735 | 1080 | cases, defs)); | 
| 11925 | 1081 |     val ctxt4 = ctxt3 |> put_thms ("prems", prems_of ctxt3);
 | 
| 1082 | in (warn_extra_tfrees ctxt ctxt4, thmss) end; | |
| 5819 | 1083 | |
| 7270 | 1084 | in | 
| 1085 | ||
| 8616 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 1086 | val assume = gen_assms (apsnd #1 o bind_propp); | 
| 
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8462diff
changeset | 1087 | val assume_i = gen_assms (apsnd #1 o bind_propp_i); | 
| 7270 | 1088 | |
| 1089 | end; | |
| 5819 | 1090 | |
| 1091 | ||
| 8096 | 1092 | (* variables *) | 
| 1093 | ||
| 10381 | 1094 | local | 
| 1095 | ||
| 14720 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 1096 | fun prep_vars prep_typ internal liberal (ctxt, (xs, raw_T)) = | 
| 8096 | 1097 | let | 
| 12504 | 1098 | fun cond_tvars T = | 
| 1099 | if internal then T | |
| 1100 | else Type.no_tvars T handle TYPE (msg, _, _) => raise CONTEXT (msg, ctxt); | |
| 1101 | ||
| 14720 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 1102 | val _ = if liberal then () else | 
| 15570 | 1103 | (case List.filter (not o Syntax.is_identifier) (map (no_skolem internal ctxt) xs) of | 
| 8096 | 1104 |       [] => () | bads => raise CONTEXT ("Bad variable name(s): " ^ commas_quote bads, ctxt));
 | 
| 1105 | ||
| 15570 | 1106 | val opt_T = Option.map (cond_tvars o prep_typ ctxt) raw_T; | 
| 15974 | 1107 | val T = if_none opt_T TypeInfer.logicT; | 
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 1108 | val ctxt' = ctxt |> fold declare_term_syntax (map (fn x => Free (x, T)) xs); | 
| 8096 | 1109 | in (ctxt', (xs, opt_T)) end; | 
| 1110 | ||
| 10381 | 1111 | in | 
| 1112 | ||
| 14720 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 1113 | val read_vars = prep_vars read_typ false false; | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 1114 | val cert_vars = prep_vars cert_typ true false; | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 1115 | val read_vars_liberal = prep_vars read_typ false true; | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 1116 | val cert_vars_liberal = prep_vars cert_typ true true; | 
| 8096 | 1117 | |
| 10381 | 1118 | end; | 
| 1119 | ||
| 8096 | 1120 | |
| 5819 | 1121 | (* fix *) | 
| 1122 | ||
| 8096 | 1123 | local | 
| 1124 | ||
| 12309 
03e9287be350
name space for local thms (export cond_extern, qualified);
 wenzelm parents: 
12291diff
changeset | 1125 | fun map_fixes f = | 
| 16540 | 1126 | map_context (fn (syntax, (assumes, fixes), binds, thms, cases, defs) => | 
| 1127 | (syntax, (assumes, f fixes), binds, thms, cases, defs)); | |
| 5819 | 1128 | |
| 11925 | 1129 | fun err_dups ctxt xs = raise CONTEXT ("Duplicate variable(s): " ^ commas_quote xs, ctxt);
 | 
| 1130 | ||
| 12130 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 1131 | val declare = | 
| 16031 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 1132 | fold declare_term_syntax o | 
| 
fbf3471214d6
moved everything related to thms_containing to find_theorems.ML;
 wenzelm parents: 
15979diff
changeset | 1133 | List.mapPartial (fn (_, NONE) => NONE | (x, SOME T) => SOME (Free (x, T))); | 
| 12130 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 1134 | |
| 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 1135 | fun add_vars xs Ts ctxt = | 
| 12309 
03e9287be350
name space for local thms (export cond_extern, qualified);
 wenzelm parents: 
12291diff
changeset | 1136 | let val xs' = Term.variantlist (map Syntax.skolem xs, map #2 (fixes_of ctxt)) in | 
| 
03e9287be350
name space for local thms (export cond_extern, qualified);
 wenzelm parents: 
12291diff
changeset | 1137 | ctxt | 
| 
03e9287be350
name space for local thms (export cond_extern, qualified);
 wenzelm parents: 
12291diff
changeset | 1138 | |> declare (xs' ~~ Ts) | 
| 
03e9287be350
name space for local thms (export cond_extern, qualified);
 wenzelm parents: 
12291diff
changeset | 1139 | |> map_fixes (fn fixes => (xs ~~ xs') @ fixes) | 
| 12130 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 1140 | end; | 
| 11925 | 1141 | |
| 12130 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 1142 | fun add_vars_direct xs Ts ctxt = | 
| 12309 
03e9287be350
name space for local thms (export cond_extern, qualified);
 wenzelm parents: 
12291diff
changeset | 1143 | ctxt | 
| 
03e9287be350
name space for local thms (export cond_extern, qualified);
 wenzelm parents: 
12291diff
changeset | 1144 | |> declare (xs ~~ Ts) | 
| 
03e9287be350
name space for local thms (export cond_extern, qualified);
 wenzelm parents: 
12291diff
changeset | 1145 | |> map_fixes (fn fixes => | 
| 12130 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 1146 | (case xs inter_string map #1 fixes of | 
| 12309 
03e9287be350
name space for local thms (export cond_extern, qualified);
 wenzelm parents: 
12291diff
changeset | 1147 | [] => (xs ~~ xs) @ fixes | 
| 12130 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 1148 | | dups => err_dups ctxt dups)); | 
| 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 1149 | |
| 11925 | 1150 | |
| 1151 | fun gen_fix prep add raw_vars ctxt = | |
| 8096 | 1152 | let | 
| 1153 | val (ctxt', varss) = foldl_map prep (ctxt, raw_vars); | |
| 15570 | 1154 | val vars = rev (List.concat (map (fn (xs, T) => map (rpair T) xs) varss)); | 
| 12130 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 1155 | val xs = map #1 vars; | 
| 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 1156 | val Ts = map #2 vars; | 
| 8096 | 1157 | in | 
| 11925 | 1158 | (case Library.duplicates xs of [] => () | dups => err_dups ctxt dups); | 
| 12130 
30d9143aff7e
syntactic declaration of external *and* internal versions of fixes;
 wenzelm parents: 
12123diff
changeset | 1159 | ctxt' |> add xs Ts | 
| 8096 | 1160 | end; | 
| 5819 | 1161 | |
| 15531 | 1162 | fun prep_type (x, NONE, SOME mx) = ([x], SOME (mixfix_type mx)) | 
| 12576 
9fd10052c3f7
added add_fixes: derives mssing type scheme from mixfix;
 wenzelm parents: 
12550diff
changeset | 1163 | | prep_type (x, opt_T, _) = ([x], opt_T); | 
| 
9fd10052c3f7
added add_fixes: derives mssing type scheme from mixfix;
 wenzelm parents: 
12550diff
changeset | 1164 | |
| 8096 | 1165 | in | 
| 7679 | 1166 | |
| 11925 | 1167 | val fix = gen_fix read_vars add_vars; | 
| 1168 | val fix_i = gen_fix cert_vars add_vars; | |
| 14720 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 1169 | |
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 1170 | fun fix_direct liberal = | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 1171 | gen_fix (if liberal then cert_vars_liberal else cert_vars) add_vars_direct; | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 1172 | |
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 1173 | fun add_fixes decls = add_syntax decls o fix_direct false (map prep_type decls); | 
| 
ceff6d4fb836
cleanup up read functions, include liberal versions;
 wenzelm parents: 
14707diff
changeset | 1174 | fun add_fixes_liberal decls = add_syntax decls o fix_direct true (map prep_type decls); | 
| 8096 | 1175 | |
| 1176 | end; | |
| 5819 | 1177 | |
| 12048 | 1178 | fun fix_frees ts ctxt = | 
| 1179 | let | |
| 15570 | 1180 | val frees = Library.foldl Term.add_frees ([], ts); | 
| 15531 | 1181 | fun new (x, T) = if is_fixed ctxt x then NONE else SOME ([x], SOME T); | 
| 15570 | 1182 | in fix_direct false (rev (List.mapPartial new frees)) ctxt end; | 
| 12016 | 1183 | |
| 6895 | 1184 | |
| 9291 
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
 wenzelm parents: 
9274diff
changeset | 1185 | (*Note: improper use may result in variable capture / dynamic scoping!*) | 
| 
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
 wenzelm parents: 
9274diff
changeset | 1186 | fun bind_skolem ctxt xs = | 
| 
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
 wenzelm parents: 
9274diff
changeset | 1187 | let | 
| 15531 | 1188 | val ctxt' = ctxt |> fix_i [(xs, NONE)]; | 
| 9291 
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
 wenzelm parents: 
9274diff
changeset | 1189 | fun bind (t as Free (x, T)) = | 
| 
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
 wenzelm parents: 
9274diff
changeset | 1190 | if x mem_string xs then | 
| 15531 | 1191 | (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: 
9274diff
changeset | 1192 | else t | 
| 
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
 wenzelm parents: 
9274diff
changeset | 1193 | | bind (t $ u) = bind t $ bind u | 
| 
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
 wenzelm parents: 
9274diff
changeset | 1194 | | bind (Abs (x, T, t)) = Abs (x, T, bind t) | 
| 
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
 wenzelm parents: 
9274diff
changeset | 1195 | | bind a = a; | 
| 
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
 wenzelm parents: 
9274diff
changeset | 1196 | in bind end; | 
| 
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
 wenzelm parents: 
9274diff
changeset | 1197 | |
| 
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
 wenzelm parents: 
9274diff
changeset | 1198 | |
| 5819 | 1199 | |
| 8373 
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
 wenzelm parents: 
8186diff
changeset | 1200 | (** cases **) | 
| 
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
 wenzelm parents: 
8186diff
changeset | 1201 | |
| 16147 | 1202 | fun apply_case ({fixes, assumes, binds}: RuleCases.T) ctxt =
 | 
| 1203 | let | |
| 1204 | fun bind (c, (x, T)) = (c |> fix_i [([x], SOME T)], bind_skolem c [x] (Free (x, T))); | |
| 1205 | val (ctxt', xs) = foldl_map bind (ctxt, fixes); | |
| 1206 | fun app t = Library.foldl Term.betapply (t, xs); | |
| 1207 | in (ctxt', (map (apsnd (Option.map app)) binds, map (apsnd (map app)) assumes)) end; | |
| 1208 | ||
| 1209 | local | |
| 1210 | ||
| 11793 
5f0ab6f5c280
support impromptu terminology of cases parameters;
 wenzelm parents: 
11764diff
changeset | 1211 | fun prep_case ctxt name xs {fixes, assumes, binds} =
 | 
| 
5f0ab6f5c280
support impromptu terminology of cases parameters;
 wenzelm parents: 
11764diff
changeset | 1212 | let | 
| 15974 | 1213 | fun replace (opt_x :: xs) ((y, T) :: ys) = (if_none opt_x y, T) :: replace xs ys | 
| 11793 
5f0ab6f5c280
support impromptu terminology of cases parameters;
 wenzelm parents: 
11764diff
changeset | 1214 | | replace [] ys = ys | 
| 
5f0ab6f5c280
support impromptu terminology of cases parameters;
 wenzelm parents: 
11764diff
changeset | 1215 |       | replace (_ :: _) [] = raise CONTEXT ("Too many parameters for case " ^ quote name, ctxt);
 | 
| 
5f0ab6f5c280
support impromptu terminology of cases parameters;
 wenzelm parents: 
11764diff
changeset | 1216 | in | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 1217 | if null (foldr Term.add_typ_tvars [] (map snd fixes)) andalso | 
| 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 1218 | null (foldr Term.add_term_vars [] (List.concat (map snd assumes))) then | 
| 11793 
5f0ab6f5c280
support impromptu terminology of cases parameters;
 wenzelm parents: 
11764diff
changeset | 1219 |         {fixes = replace xs fixes, assumes = assumes, binds = map drop_schematic binds}
 | 
| 
5f0ab6f5c280
support impromptu terminology of cases parameters;
 wenzelm parents: 
11764diff
changeset | 1220 |     else raise CONTEXT ("Illegal schematic variable(s) in case " ^ quote name, ctxt)
 | 
| 
5f0ab6f5c280
support impromptu terminology of cases parameters;
 wenzelm parents: 
11764diff
changeset | 1221 | end; | 
| 8403 | 1222 | |
| 16147 | 1223 | fun rem_case name = remove (fn (x, (y, _)) => x = y) name; | 
| 1224 | ||
| 1225 | fun add_case ("", _) cases = cases
 | |
| 1226 | | add_case (name, NONE) cases = rem_case name cases | |
| 1227 | | add_case (name, SOME c) cases = (name, c) :: rem_case name cases; | |
| 1228 | ||
| 1229 | in | |
| 1230 | ||
| 16540 | 1231 | fun get_case ctxt name xs = | 
| 1232 | (case assoc_string (cases_of ctxt, name) of | |
| 15531 | 1233 |     NONE => raise CONTEXT ("Unknown case: " ^ quote name, ctxt)
 | 
| 1234 | | SOME c => prep_case ctxt name xs c); | |
| 8373 
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
 wenzelm parents: 
8186diff
changeset | 1235 | |
| 16540 | 1236 | fun add_cases xs = map_context (fn (syntax, asms, binds, thms, cases, defs) => | 
| 1237 | (syntax, asms, binds, thms, fold add_case xs cases, defs)); | |
| 8384 | 1238 | |
| 16147 | 1239 | end; | 
| 8373 
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
 wenzelm parents: 
8186diff
changeset | 1240 | |
| 
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
 wenzelm parents: 
8186diff
changeset | 1241 | |
| 
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
 wenzelm parents: 
8186diff
changeset | 1242 | |
| 10810 | 1243 | (** print context information **) | 
| 1244 | ||
| 1245 | val verbose = ref false; | |
| 1246 | fun verb f x = if ! verbose then f (x ()) else []; | |
| 1247 | fun verb_single x = verb Library.single x; | |
| 1248 | ||
| 1249 | fun setmp_verbose f x = Library.setmp verbose true f x; | |
| 1250 | ||
| 1251 | fun pretty_items prt name items = | |
| 1252 | let | |
| 1253 | fun prt_itms (name, [x]) = Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt x] | |
| 1254 | | prt_itms (name, xs) = Pretty.big_list (name ^ ":") (map prt xs); | |
| 1255 | in | |
| 1256 | if null items andalso not (! verbose) then [] | |
| 1257 | else [Pretty.big_list name (map prt_itms items)] | |
| 1258 | end; | |
| 1259 | ||
| 1260 | ||
| 12072 
4281198fb8cd
local syntax: add_syntax, proper read/pretty functions;
 wenzelm parents: 
12066diff
changeset | 1261 | (* local syntax *) | 
| 
4281198fb8cd
local syntax: add_syntax, proper read/pretty functions;
 wenzelm parents: 
12066diff
changeset | 1262 | |
| 12093 | 1263 | val print_syntax = Syntax.print_syntax o syn_of; | 
| 12072 
4281198fb8cd
local syntax: add_syntax, proper read/pretty functions;
 wenzelm parents: 
12066diff
changeset | 1264 | |
| 
4281198fb8cd
local syntax: add_syntax, proper read/pretty functions;
 wenzelm parents: 
12066diff
changeset | 1265 | |
| 10810 | 1266 | (* term bindings *) | 
| 1267 | ||
| 16540 | 1268 | fun pretty_binds ctxt = | 
| 10810 | 1269 | let | 
| 16540 | 1270 | val binds = binds_of ctxt; | 
| 12057 | 1271 | fun prt_bind (xi, (t, T)) = pretty_term ctxt (Logic.mk_equals (Var (xi, T), t)); | 
| 10810 | 1272 | in | 
| 15758 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 1273 | if Vartab.is_empty binds andalso not (! verbose) then [] | 
| 
07e382399a96
binds/thms: do not store options, but delete from table;
 wenzelm parents: 
15750diff
changeset | 1274 | else [Pretty.big_list "term bindings:" (map prt_bind (Vartab.dest binds))] | 
| 10810 | 1275 | end; | 
| 1276 | ||
| 1277 | val print_binds = Pretty.writeln o Pretty.chunks o pretty_binds; | |
| 1278 | ||
| 1279 | ||
| 1280 | (* local theorems *) | |
| 1281 | ||
| 16540 | 1282 | fun pretty_lthms ctxt = | 
| 1283 | pretty_items (pretty_thm ctxt) "facts:" (NameSpace.extern_table (#2 (thms_of ctxt))); | |
| 10810 | 1284 | |
| 12057 | 1285 | val print_lthms = Pretty.writeln o Pretty.chunks o pretty_lthms; | 
| 10810 | 1286 | |
| 1287 | ||
| 1288 | (* local contexts *) | |
| 1289 | ||
| 16540 | 1290 | fun pretty_cases ctxt = | 
| 10810 | 1291 | let | 
| 12057 | 1292 | val prt_term = pretty_term ctxt; | 
| 1293 | ||
| 10810 | 1294 | fun prt_let (xi, t) = Pretty.block | 
| 10818 | 1295 | [Pretty.quote (prt_term (Var (xi, Term.fastype_of t))), Pretty.str " =", Pretty.brk 1, | 
| 10810 | 1296 | Pretty.quote (prt_term t)]; | 
| 1297 | ||
| 13425 
119ae829ad9b
support for split assumptions in cases (hyps vs. prems);
 wenzelm parents: 
13415diff
changeset | 1298 | fun prt_asm (a, ts) = Pretty.block (Pretty.breaks | 
| 
119ae829ad9b
support for split assumptions in cases (hyps vs. prems);
 wenzelm parents: 
13415diff
changeset | 1299 | ((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: 
13415diff
changeset | 1300 | |
| 10810 | 1301 | fun prt_sect _ _ _ [] = [] | 
| 1302 | | prt_sect s sep prt xs = [Pretty.block (Pretty.breaks (Pretty.str s :: | |
| 15570 | 1303 | List.concat (Library.separate sep (map (Library.single o prt) xs))))]; | 
| 10810 | 1304 | |
| 10830 | 1305 | fun prt_case (name, (fixes, (lets, asms))) = Pretty.block (Pretty.fbreaks | 
| 10810 | 1306 | (Pretty.str (name ^ ":") :: | 
| 11915 | 1307 | prt_sect "fix" [] (Pretty.str o fst) fixes @ | 
| 10810 | 1308 | prt_sect "let" [Pretty.str "and"] prt_let | 
| 15570 | 1309 | (List.mapPartial (fn (xi, SOME t) => SOME (xi, t) | _ => NONE) lets) @ | 
| 13425 
119ae829ad9b
support for split assumptions in cases (hyps vs. prems);
 wenzelm parents: 
13415diff
changeset | 1310 | (if forall (null o #2) asms then [] | 
| 
119ae829ad9b
support for split assumptions in cases (hyps vs. prems);
 wenzelm parents: 
13415diff
changeset | 1311 | else prt_sect "assume" [Pretty.str "and"] prt_asm asms))); | 
| 16540 | 1312 | |
| 1313 | val cases = cases_of ctxt; | |
| 10810 | 1314 | in | 
| 1315 | if null cases andalso not (! verbose) then [] | |
| 10830 | 1316 | else [Pretty.big_list "cases:" | 
| 16147 | 1317 | (map (prt_case o apsnd (fn c => (#fixes c, #2 (apply_case c ctxt)))) (rev cases))] | 
| 10810 | 1318 | end; | 
| 1319 | ||
| 1320 | val print_cases = Pretty.writeln o Pretty.chunks o pretty_cases; | |
| 1321 | ||
| 1322 | ||
| 12057 | 1323 | (* core context *) | 
| 10810 | 1324 | |
| 1325 | val prems_limit = ref 10; | |
| 1326 | ||
| 12057 | 1327 | fun pretty_asms ctxt = | 
| 10810 | 1328 | let | 
| 12057 | 1329 | val prt_term = pretty_term ctxt; | 
| 1330 | ||
| 12093 | 1331 | (*structures*) | 
| 1332 | val (_, structs, _) = syntax_of ctxt; | |
| 1333 | val prt_structs = if null structs then [] | |
| 1334 | else [Pretty.block (Pretty.str "structures:" :: Pretty.brk 1 :: | |
| 1335 | Pretty.commas (map Pretty.str structs))]; | |
| 1336 | ||
| 12057 | 1337 | (*fixes*) | 
| 1338 | fun prt_fix (x, x') = | |
| 1339 | if x = x' then Pretty.str x | |
| 1340 | else Pretty.block [Pretty.str x, Pretty.str " =", Pretty.brk 1, prt_term (Syntax.free x')]; | |
| 12093 | 1341 | val fixes = rev (filter_out | 
| 1342 | ((can Syntax.dest_internal o #1) orf (fn (_, x') => x' mem_string structs)) (fixes_of ctxt)); | |
| 1343 | val prt_fixes = if null fixes then [] | |
| 1344 | else [Pretty.block (Pretty.str "fixed variables:" :: Pretty.brk 1 :: | |
| 1345 | Pretty.commas (map prt_fix fixes))]; | |
| 12057 | 1346 | |
| 1347 | (*prems*) | |
| 10810 | 1348 | val limit = ! prems_limit; | 
| 1349 | val prems = prems_of ctxt; | |
| 1350 | val len = length prems; | |
| 12093 | 1351 | val prt_prems = if null prems then [] | 
| 1352 | else [Pretty.big_list "prems:" ((if len <= limit then [] else [Pretty.str "..."]) @ | |
| 1353 | map (pretty_thm ctxt) (Library.drop (len - limit, prems)))]; | |
| 1354 | ||
| 1355 | in prt_structs @ prt_fixes @ prt_prems end; | |
| 10810 | 1356 | |
| 1357 | ||
| 1358 | (* main context *) | |
| 1359 | ||
| 16540 | 1360 | fun pretty_context ctxt = | 
| 10810 | 1361 | let | 
| 12057 | 1362 | val prt_term = pretty_term ctxt; | 
| 1363 | val prt_typ = pretty_typ ctxt; | |
| 1364 | val prt_sort = pretty_sort ctxt; | |
| 10810 | 1365 | |
| 1366 | (*theory*) | |
| 12057 | 1367 | val pretty_thy = Pretty.block | 
| 16458 | 1368 | [Pretty.str "Theory:", Pretty.brk 1, Context.pretty_thy (theory_of ctxt)]; | 
| 10810 | 1369 | |
| 1370 | (*defaults*) | |
| 1371 | fun prt_atom prt prtT (x, X) = Pretty.block | |
| 1372 | [prt x, Pretty.str " ::", Pretty.brk 1, prtT X]; | |
| 1373 | ||
| 1374 | fun prt_var (x, ~1) = prt_term (Syntax.free x) | |
| 1375 | | prt_var xi = prt_term (Syntax.var xi); | |
| 1376 | ||
| 1377 | fun prt_varT (x, ~1) = prt_typ (TFree (x, [])) | |
| 1378 | | prt_varT xi = prt_typ (TVar (xi, [])); | |
| 1379 | ||
| 1380 | val prt_defT = prt_atom prt_var prt_typ; | |
| 1381 | val prt_defS = prt_atom prt_varT prt_sort; | |
| 16540 | 1382 | |
| 1383 | val (types, sorts, used, _) = defaults_of ctxt; | |
| 10810 | 1384 | in | 
| 1385 | verb_single (K pretty_thy) @ | |
| 12057 | 1386 | pretty_asms ctxt @ | 
| 10810 | 1387 | verb pretty_binds (K ctxt) @ | 
| 12057 | 1388 | verb pretty_lthms (K ctxt) @ | 
| 10810 | 1389 | verb pretty_cases (K ctxt) @ | 
| 1390 | verb_single (fn () => Pretty.big_list "type constraints:" (map prt_defT (Vartab.dest types))) @ | |
| 1391 | verb_single (fn () => Pretty.big_list "default sorts:" (map prt_defS (Vartab.dest sorts))) @ | |
| 1392 |     verb_single (fn () => Pretty.strs ("used type variable names:" :: used))
 | |
| 1393 | end; | |
| 1394 | ||
| 5819 | 1395 | end; |