535 Pretty.breaks (Pretty.str prfx :: Args.pretty_attribs ctxt atts); |
537 Pretty.breaks (Pretty.str prfx :: Args.pretty_attribs ctxt atts); |
536 val prt_thm = Pretty.quote o ProofContext.pretty_thm ctxt; |
538 val prt_thm = Pretty.quote o ProofContext.pretty_thm ctxt; |
537 fun prt_thms thms = |
539 fun prt_thms thms = |
538 Pretty.enclose "[" "]" (Pretty.breaks (map prt_thm thms)); |
540 Pretty.enclose "[" "]" (Pretty.breaks (map prt_thm thms)); |
539 fun prt_reg (ts, (("", []), thms)) = |
541 fun prt_reg (ts, (("", []), thms)) = |
540 if ! show_wits |
542 if show_wits |
541 then Pretty.block [prt_inst ts, Pretty.fbrk, prt_thms thms] |
543 then Pretty.block [prt_inst ts, Pretty.fbrk, prt_thms thms] |
542 else prt_inst ts |
544 else prt_inst ts |
543 | prt_reg (ts, (attn, thms)) = |
545 | prt_reg (ts, (attn, thms)) = |
544 if ! show_wits |
546 if show_wits |
545 then Pretty.block ((prt_attn attn @ |
547 then Pretty.block ((prt_attn attn @ |
546 [Pretty.str ":", Pretty.brk 1, prt_inst ts, Pretty.fbrk, |
548 [Pretty.str ":", Pretty.brk 1, prt_inst ts, Pretty.fbrk, |
547 prt_thms thms])) |
549 prt_thms thms])) |
548 else Pretty.block ((prt_attn attn @ |
550 else Pretty.block ((prt_attn attn @ |
549 [Pretty.str ":", Pretty.brk 1, prt_inst ts])); |
551 [Pretty.str ":", Pretty.brk 1, prt_inst ts])); |
1729 |>> ProofContext.restore_naming ctxt; |
1727 |>> ProofContext.restore_naming ctxt; |
1730 |
1728 |
1731 end; |
1729 end; |
1732 |
1730 |
1733 |
1731 |
|
1732 (* collect witness theorems for global registration; |
|
1733 requires parameters and flattened list of (assumed!) identifiers |
|
1734 instead of recomputing it from the target *) |
|
1735 |
|
1736 fun collect_global_witnesses thy parms ids vts = let |
|
1737 val ts = map Logic.unvarify vts; |
|
1738 val (parms, parmTs) = split_list parms; |
|
1739 val parmvTs = map Type.varifyT parmTs; |
|
1740 val vtinst = fold (Sign.typ_match thy) (parmvTs ~~ map Term.fastype_of ts) Vartab.empty; |
|
1741 val tinst = Vartab.dest vtinst |> map (fn ((x, 0), (_, T)) => (x, T)) |
|
1742 |> Symtab.make; |
|
1743 (* replace parameter names in ids by instantiations *) |
|
1744 val vinst = Symtab.make (parms ~~ vts); |
|
1745 fun vinst_names ps = map (fn p => Symtab.lookup (vinst, p) |> valOf) ps; |
|
1746 val inst = Symtab.make (parms ~~ ts); |
|
1747 val ids' = map (apsnd vinst_names) ids; |
|
1748 val wits = List.concat (map (snd o valOf o get_global_registration thy) ids'); |
|
1749 in ((inst, tinst), wits) end; |
|
1750 |
|
1751 |
1734 (* store instantiations of args for all registered interpretations |
1752 (* store instantiations of args for all registered interpretations |
1735 of the theory *) |
1753 of the theory *) |
1736 |
1754 |
1737 fun note_thmss_registrations kind target args thy = |
1755 fun note_thmss_registrations kind target args thy = |
1738 let |
1756 let |
1739 val (parms, parmTs) = |
1757 val parms = the_locale thy target |> #params |> fst |> map fst; |
1740 the_locale thy target |> #params |> fst |> map fst |> split_list; |
|
1741 val parmvTs = map Type.varifyT parmTs; |
|
1742 val ids = flatten (ProofContext.init thy, intern_expr thy) |
1758 val ids = flatten (ProofContext.init thy, intern_expr thy) |
1743 (([], Symtab.empty), Expr (Locale target)) |> fst |> fst |
1759 (([], Symtab.empty), Expr (Locale target)) |> fst |> fst |
1744 |> List.mapPartial (fn (id, (_, Assumed _)) => SOME id | _ => NONE) |
1760 |> List.mapPartial (fn (id, (_, Assumed _)) => SOME id | _ => NONE) |
1745 |
1761 |
1746 val regs = get_global_registrations thy target; |
1762 val regs = get_global_registrations thy target; |
1747 |
1763 |
1748 (* add args to thy for all registrations *) |
1764 (* add args to thy for all registrations *) |
1749 |
1765 |
1750 fun activate (thy, (vts, ((prfx, atts2), _))) = |
1766 fun activate (thy, (vts, ((prfx, atts2), _))) = |
1751 let |
1767 let |
1752 val ts = map Logic.unvarify vts; |
1768 val ((inst, tinst), prems) = collect_global_witnesses thy parms ids vts; |
1753 (* type instantiation *) |
|
1754 val vtinst = fold (Sign.typ_match thy) (parmvTs ~~ map Term.fastype_of ts) Vartab.empty; |
|
1755 val tinst = Vartab.dest vtinst |> map (fn ((x, 0), (_, T)) => (x, T)) |
|
1756 |> Symtab.make; |
|
1757 (* replace parameter names in ids by instantiations *) |
|
1758 val vinst = Symtab.make (parms ~~ vts); |
|
1759 fun vinst_names ps = map (fn p => Symtab.lookup (vinst, p) |> valOf) ps; |
|
1760 val inst = Symtab.make (parms ~~ ts); |
|
1761 val ids' = map (apsnd vinst_names) ids; |
|
1762 val prems = List.concat (map (snd o valOf o get_global_registration thy) ids'); |
|
1763 val args' = map (fn ((n, atts), [(ths, [])]) => |
1769 val args' = map (fn ((n, atts), [(ths, [])]) => |
1764 ((NameSpace.qualified prfx n, |
1770 ((NameSpace.qualified prfx n, |
1765 map (Attrib.global_attribute_i thy) (atts @ atts2)), |
1771 map (Attrib.global_attribute_i thy) |
1766 [(map (Drule.standard o Drule.satisfy_hyps prems o inst_tab_thm thy (inst, tinst)) ths, [])])) |
1772 (inst_tab_atts thy (inst, tinst) atts @ atts2)), |
|
1773 [(map (Drule.standard o Drule.satisfy_hyps prems o |
|
1774 inst_tab_thm thy (inst, tinst)) ths, [])])) |
1767 args; |
1775 args; |
1768 in global_note_accesses_i (Drule.kind kind) prfx args' thy |> fst end; |
1776 in global_note_accesses_i (Drule.kind kind) prfx args' thy |> fst end; |
1769 in Library.foldl activate (thy, regs) end; |
1777 in Library.foldl activate (thy, regs) end; |
1770 |
1778 |
1771 |
1779 |
2025 |
2033 |
2026 local |
2034 local |
2027 |
2035 |
2028 (* extract proof obligations (assms and defs) from elements *) |
2036 (* extract proof obligations (assms and defs) from elements *) |
2029 |
2037 |
2030 fun extract_asms_elem (ts, Fixes _) = ts |
2038 fun extract_asms_elem (Fixes _) ts = ts |
2031 | extract_asms_elem (ts, Constrains _) = ts |
2039 | extract_asms_elem (Constrains _) ts = ts |
2032 | extract_asms_elem (ts, Assumes asms) = |
2040 | extract_asms_elem (Assumes asms) ts = |
2033 ts @ List.concat (map (fn (_, ams) => map (fn (t, _) => t) ams) asms) |
2041 ts @ List.concat (map (fn (_, ams) => map (fn (t, _) => t) ams) asms) |
2034 | extract_asms_elem (ts, Defines defs) = |
2042 | extract_asms_elem (Defines defs) ts = |
2035 ts @ map (fn (_, (def, _)) => def) defs |
2043 ts @ map (fn (_, (def, _)) => def) defs |
2036 | extract_asms_elem (ts, Notes _) = ts; |
2044 | extract_asms_elem (Notes _) ts = ts; |
2037 |
2045 |
2038 fun extract_asms_elems ((id, Assumed _), elems) = |
2046 fun extract_asms_elems ((id, Assumed _), elems) = |
2039 SOME (id, Library.foldl extract_asms_elem ([], elems)) |
2047 (id, fold extract_asms_elem elems []) |
2040 | extract_asms_elems ((_, Derived _), _) = NONE; |
2048 | extract_asms_elems ((id, Derived _), _) = (id, []); |
2041 |
2049 |
2042 fun extract_asms_elemss elemss = |
2050 fun extract_asms_elemss elemss = map extract_asms_elems elemss; |
2043 List.mapPartial extract_asms_elems elemss; |
|
2044 |
2051 |
2045 (* activate instantiated facts in theory or context *) |
2052 (* activate instantiated facts in theory or context *) |
2046 |
2053 |
2047 fun gen_activate_facts_elemss get_reg note_thmss attrib std put_reg add_wit |
2054 fun gen_activate_facts_elemss get_reg note attrib std put_reg add_wit |
2048 attn all_elemss new_elemss propss result thy_ctxt = |
2055 attn all_elemss new_elemss propss result thy_ctxt = |
2049 let |
2056 let |
2050 fun activate_elem disch (prfx, atts) (Notes facts) thy_ctxt = |
2057 fun activate_elem disch (prfx, atts) (Notes facts) thy_ctxt = |
2051 let |
2058 let |
2052 val facts' = facts |
2059 val facts' = facts |
2217 |
2224 |
2218 fun prep_registration_in_locale target expr thy = |
2225 fun prep_registration_in_locale target expr thy = |
2219 (* target already in internal form *) |
2226 (* target already in internal form *) |
2220 let |
2227 let |
2221 val ctxt = ProofContext.init thy; |
2228 val ctxt = ProofContext.init thy; |
2222 val ((target_ids, target_syn), target_elemss) = flatten (ctxt, I) |
2229 val ((raw_target_ids, target_syn), _) = flatten (ctxt, I) |
2223 (([], Symtab.empty), Expr (Locale target)); |
2230 (([], Symtab.empty), Expr (Locale target)); |
2224 val fixed = the_locale thy target |> #params |> #1 |> map #1; |
2231 val fixed = the_locale thy target |> #params |> #1 |> map #1; |
2225 val ((all_ids, syn), raw_elemss) = flatten (ctxt, intern_expr thy) |
2232 val ((all_ids, syn), raw_elemss) = flatten (ctxt, intern_expr thy) |
2226 ((target_ids, target_syn), Expr expr); |
2233 ((raw_target_ids, target_syn), Expr expr); |
2227 val ids = Library.drop (length target_ids, all_ids); |
2234 val (target_ids, ids) = splitAt (length raw_target_ids, all_ids); |
2228 val ((parms, elemss, _), _) = |
2235 val ((parms, elemss, _), _) = read_elemss false ctxt fixed raw_elemss []; |
2229 read_elemss false ctxt fixed raw_elemss []; |
|
2230 |
2236 |
2231 (** compute proof obligations **) |
2237 (** compute proof obligations **) |
2232 |
2238 |
2233 (* restore "small" ids, with mode *) |
2239 (* restore "small" ids, with mode *) |
2234 val ids' = map (apsnd snd) ids; |
2240 val ids' = map (apsnd snd) ids; |
2235 (* remove Int markers *) |
2241 (* remove Int markers *) |
2236 val elemss' = map (fn (_, es) => |
2242 val elemss' = map (fn (_, es) => |
2237 map (fn Int e => e) es) elemss |
2243 map (fn Int e => e) es) elemss |
2238 (* extract assumptions and defs *) |
2244 (* extract assumptions and defs *) |
2239 val propss = extract_asms_elemss (ids' ~~ elemss'); |
2245 val ids_elemss = ids' ~~ elemss'; |
2240 |
2246 val propss = extract_asms_elemss ids_elemss; |
2241 (** activation function: add registrations **) |
2247 |
|
2248 (** activation function: |
|
2249 - add registrations to the target locale |
|
2250 - add induced registrations for all global registrations of |
|
2251 the target, unless already present |
|
2252 - add facts of induced registrations to theory **) |
|
2253 |
|
2254 val t_ids = List.mapPartial |
|
2255 (fn (id, (_, Assumed _)) => SOME id | _ => NONE) target_ids; |
|
2256 |
2242 fun activate locale_results thy = let |
2257 fun activate locale_results thy = let |
2243 val thmss = unflat (map snd propss) locale_results; |
2258 val ids_elemss_thmss = ids_elemss ~~ |
2244 fun activate_id (id, thms) thy = |
2259 unflat (map snd propss) locale_results; |
|
2260 val regs = get_global_registrations thy target; |
|
2261 |
|
2262 fun activate_id (((id, Assumed _), _), thms) thy = |
2245 thy |> put_registration_in_locale target id |
2263 thy |> put_registration_in_locale target id |
2246 |> fold (add_witness_in_locale target id) thms; |
2264 |> fold (add_witness_in_locale target id) thms |
|
2265 | activate_id _ thy = thy; |
|
2266 |
|
2267 fun activate_reg (vts, ((prfx, atts2), _)) thy = let |
|
2268 val ((inst, tinst), wits) = |
|
2269 collect_global_witnesses thy fixed t_ids vts; |
|
2270 fun inst_parms ps = map (fn p => |
|
2271 valOf (assoc (map #1 fixed ~~ vts, p))) ps; |
|
2272 val disch = Drule.fconv_rule (Thm.beta_conversion true) o |
|
2273 Drule.satisfy_hyps wits; |
|
2274 val new_elemss = List.filter (fn (((name, ps), _), _) => |
|
2275 not (test_global_registration thy (name, inst_parms ps))) (ids_elemss); |
|
2276 fun activate_assumed_id (((_, Derived _), _), _) thy = thy |
|
2277 | activate_assumed_id ((((name, ps), Assumed _), _), thms) thy = let |
|
2278 val ps' = inst_parms ps; |
|
2279 in |
|
2280 if test_global_registration thy (name, ps') |
|
2281 then thy |
|
2282 else thy |
|
2283 |> put_global_registration (name, ps') (prfx, atts2) |
|
2284 |> fold (fn thm => fn thy => add_global_witness (name, ps') |
|
2285 ((disch o inst_tab_thm thy (inst, tinst)) thm) thy) thms |
|
2286 end; |
|
2287 |
|
2288 fun activate_derived_id ((_, Assumed _), _) thy = thy |
|
2289 | activate_derived_id (((name, ps), Derived ths), _) thy = let |
|
2290 val ps' = inst_parms ps; |
|
2291 in |
|
2292 if test_global_registration thy (name, ps') |
|
2293 then thy |
|
2294 else thy |
|
2295 |> put_global_registration (name, ps') (prfx, atts2) |
|
2296 |> fold (fn thm => fn thy => add_global_witness (name, ps') |
|
2297 ((disch o inst_tab_thm thy (inst, tinst) o Drule.satisfy_hyps locale_results) thm) thy) ths |
|
2298 end; |
|
2299 |
|
2300 fun activate_elem (Notes facts) thy = |
|
2301 let |
|
2302 val facts' = facts |
|
2303 |> Attrib.map_facts (Attrib.global_attribute_i thy o |
|
2304 Args.map_values I (tinst_tab_type tinst) |
|
2305 (inst_tab_term (inst, tinst)) |
|
2306 (disch o inst_tab_thm thy (inst, tinst) o |
|
2307 Drule.satisfy_hyps locale_results)) |
|
2308 |> map (apfst (apsnd (fn a => a @ map (Attrib.global_attribute thy) atts2))) |
|
2309 |> map (apsnd (map (apfst (map (disch o inst_tab_thm thy (inst, tinst) o Drule.satisfy_hyps locale_results))))) |
|
2310 |> map (apfst (apfst (NameSpace.qualified prfx))) |
|
2311 in |
|
2312 fst (global_note_accesses_i (Drule.kind "") prfx facts' thy) |
|
2313 end |
|
2314 | activate_elem _ thy = thy; |
|
2315 |
|
2316 fun activate_elems (_, elems) thy = fold activate_elem elems thy; |
|
2317 |
|
2318 in thy |> fold activate_assumed_id ids_elemss_thmss |
|
2319 |> fold activate_derived_id ids_elemss |
|
2320 |> fold activate_elems new_elemss end; |
2247 in |
2321 in |
2248 fold activate_id (map fst propss ~~ thmss) thy |
2322 thy |> fold activate_id ids_elemss_thmss |
|
2323 |> fold activate_reg regs |
2249 end; |
2324 end; |
2250 |
2325 |
2251 in (propss, activate) end; |
2326 in (propss, activate) end; |
2252 |
2327 |
2253 end; (* local *) |
2328 end; (* local *) |