author | wenzelm |
Mon, 06 Feb 2006 20:59:51 +0100 | |
changeset 18951 | 4f5f6c632127 |
parent 18876 | ddb6803da197 |
child 19017 | 5f06c37043e4 |
permissions | -rw-r--r-- |
18744 | 1 |
(* Title: Pure/Isar/local_theory.ML |
2 |
ID: $Id$ |
|
3 |
Author: Makarius |
|
4 |
||
5 |
Local theory operations, with optional target locale. |
|
6 |
*) |
|
7 |
||
18951 | 8 |
type local_theory = Proof.context; |
9 |
||
18744 | 10 |
signature LOCAL_THEORY = |
11 |
sig |
|
18951 | 12 |
val params_of: local_theory -> (string * typ) list |
13 |
val hyps_of: local_theory -> term list |
|
14 |
val standard: local_theory -> thm -> thm |
|
15 |
val pretty_consts: local_theory -> (string * typ -> bool) -> (string * typ) list -> Pretty.T |
|
16 |
val quiet_mode: bool ref |
|
17 |
val print_consts: local_theory -> (string * typ -> bool) -> (string * typ) list -> unit |
|
18 |
val theory: (theory -> theory) -> local_theory -> local_theory |
|
19 |
val theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory |
|
20 |
val init: xstring option -> theory -> local_theory |
|
21 |
val init_i: string option -> theory -> local_theory |
|
22 |
val exit: local_theory -> local_theory * theory |
|
23 |
val consts: ((bstring * typ) * mixfix) list -> local_theory -> term list * local_theory |
|
18876 | 24 |
val consts_restricted: (string * typ -> bool) -> |
18951 | 25 |
((bstring * typ) * mixfix) list -> local_theory -> term list * local_theory |
26 |
val axioms: ((bstring * Attrib.src list) * term list) list -> local_theory -> |
|
27 |
(bstring * thm list) list * local_theory |
|
28 |
val axioms_finish: (local_theory -> thm -> thm) -> |
|
29 |
((bstring * Attrib.src list) * term list) list -> local_theory -> |
|
30 |
(bstring * thm list) list * local_theory |
|
18767 | 31 |
val notes: string -> ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list -> |
18951 | 32 |
local_theory -> (bstring * thm list) list * local_theory |
33 |
val note: (bstring * Attrib.src list) * thm list -> local_theory -> |
|
34 |
(bstring * thm list) * local_theory |
|
18823 | 35 |
val def: (bstring * mixfix) * ((bstring * Attrib.src list) * term) -> |
18951 | 36 |
local_theory -> (term * (bstring * thm)) * local_theory |
37 |
val def_finish: (local_theory -> term -> thm -> thm) -> |
|
18823 | 38 |
(bstring * mixfix) * ((bstring * Attrib.src list) * term) -> |
18951 | 39 |
local_theory -> (term * (bstring * thm)) * local_theory |
18744 | 40 |
end; |
41 |
||
42 |
structure LocalTheory: LOCAL_THEORY = |
|
43 |
struct |
|
44 |
||
18767 | 45 |
(** local context data **) |
18744 | 46 |
|
47 |
structure Data = ProofDataFun |
|
48 |
( |
|
18876 | 49 |
val name = "Pure/local_theory"; |
18744 | 50 |
type T = |
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
51 |
{locale: (string * (cterm list * Proof.context)) option, |
18744 | 52 |
params: (string * typ) list, |
53 |
hyps: term list, |
|
54 |
exit: theory -> theory}; |
|
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
55 |
fun init thy = {locale = NONE, params = [], hyps = [], exit = I}; |
18744 | 56 |
fun print _ _ = (); |
57 |
); |
|
58 |
||
59 |
val _ = Context.add_setup Data.init; |
|
60 |
||
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
61 |
fun map_locale f = Data.map (fn {locale, params, hyps, exit} => |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
62 |
{locale = Option.map (fn (loc, (view, ctxt)) => (loc, (view, f ctxt))) locale, |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
63 |
params = params, hyps = hyps, exit = exit}); |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
64 |
|
18767 | 65 |
val locale_of = #locale o Data.get; |
66 |
val params_of = #params o Data.get; |
|
67 |
val hyps_of = #hyps o Data.get; |
|
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
68 |
|
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
69 |
fun standard ctxt = |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
70 |
(case #locale (Data.get ctxt) of |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
71 |
NONE => Drule.standard |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
72 |
| SOME (_, (_, loc_ctxt)) => ProofContext.export_standard ctxt loc_ctxt); |
18744 | 73 |
|
18767 | 74 |
|
18823 | 75 |
(* print consts *) |
18767 | 76 |
|
18951 | 77 |
val quiet_mode = ref false; |
78 |
||
18767 | 79 |
local |
80 |
||
81 |
fun pretty_var ctxt (x, T) = |
|
82 |
Pretty.block [Pretty.str x, Pretty.str " ::", Pretty.brk 1, |
|
83 |
Pretty.quote (ProofContext.pretty_typ ctxt T)]; |
|
18744 | 84 |
|
18767 | 85 |
fun pretty_vars ctxt kind vs = Pretty.big_list kind (map (pretty_var ctxt) vs); |
86 |
||
87 |
in |
|
88 |
||
18876 | 89 |
fun pretty_consts ctxt pred cs = |
90 |
(case filter pred (params_of ctxt) of |
|
18767 | 91 |
[] => pretty_vars ctxt "constants" cs |
92 |
| ps => Pretty.chunks [pretty_vars ctxt "parameters" ps, pretty_vars ctxt "constants" cs]); |
|
93 |
||
18876 | 94 |
fun print_consts _ _ [] = () |
18951 | 95 |
| print_consts ctxt pred cs = |
96 |
if ! quiet_mode then () else Pretty.writeln (pretty_consts ctxt pred cs); |
|
18823 | 97 |
|
18767 | 98 |
end; |
99 |
||
100 |
||
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
101 |
(* theory/locale substructures *) |
18781 | 102 |
|
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
103 |
fun transfer thy = |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
104 |
ProofContext.transfer thy #> map_locale (ProofContext.transfer thy); |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
105 |
|
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
106 |
fun theory f ctxt = transfer (f (ProofContext.theory_of ctxt)) ctxt; |
18781 | 107 |
|
108 |
fun theory_result f ctxt = |
|
109 |
let val (res, thy') = f (ProofContext.theory_of ctxt) |
|
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
110 |
in (res, transfer thy' ctxt) end; |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
111 |
|
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
112 |
fun locale_result f ctxt = |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
113 |
(case locale_of ctxt of |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
114 |
NONE => error "Local theory: no locale context" |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
115 |
| SOME (loc, (view, loc_ctxt)) => |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
116 |
let |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
117 |
val (res, loc_ctxt') = f loc_ctxt; |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
118 |
val thy' = ProofContext.theory_of loc_ctxt'; |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
119 |
in (res, ctxt |> map_locale (K loc_ctxt') |> transfer thy') end); |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
120 |
|
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
121 |
|
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
122 |
(* init -- exit *) |
18781 | 123 |
|
124 |
fun init_i NONE thy = ProofContext.init thy |
|
125 |
| init_i (SOME loc) thy = |
|
126 |
thy |
|
127 |
|> Locale.init loc |
|
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
128 |
||>> ProofContext.inferred_fixes |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
129 |
|> (fn ((view, params), ctxt) => Data.put |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
130 |
{locale = SOME (loc, (view, ctxt)), |
18781 | 131 |
params = params, |
132 |
hyps = ProofContext.assms_of ctxt, |
|
133 |
exit = Sign.restore_naming thy} ctxt) |
|
134 |
|> theory (Sign.add_path (Sign.base_name loc) #> Sign.no_base_names); |
|
135 |
||
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
136 |
fun init loc thy = init_i (Option.map (Locale.intern thy) loc) thy; |
18781 | 137 |
|
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
138 |
fun exit ctxt = (ctxt, #exit (Data.get ctxt) (ProofContext.theory_of ctxt)); |
18781 | 139 |
|
140 |
||
141 |
||
142 |
(** local theory **) |
|
143 |
||
18767 | 144 |
(* consts *) |
18744 | 145 |
|
18876 | 146 |
fun consts_restricted pred decls ctxt = |
18744 | 147 |
let |
148 |
val thy = ProofContext.theory_of ctxt; |
|
18876 | 149 |
val params = filter pred (params_of ctxt); |
18744 | 150 |
val ps = map Free params; |
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
151 |
val Ps = map #2 params; |
18744 | 152 |
in |
153 |
ctxt |
|
18781 | 154 |
|> theory (Sign.add_consts_i (decls |> map (fn ((c, T), mx) => (c, Ps ---> T, mx)))) |
18767 | 155 |
|> pair (decls |> map (fn ((c, T), _) => |
156 |
Term.list_comb (Const (Sign.full_name thy c, Ps ---> T), ps))) |
|
157 |
end; |
|
158 |
||
18876 | 159 |
val consts = consts_restricted (K true); |
160 |
||
18767 | 161 |
|
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
162 |
(* fact definitions *) |
18767 | 163 |
|
164 |
fun notes kind facts ctxt = |
|
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
165 |
let |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
166 |
val attrib = Attrib.attribute_i (ProofContext.theory_of ctxt); |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
167 |
val facts' = map (apsnd (map (apfst (map (standard ctxt))))) facts; |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
168 |
in |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
169 |
ctxt |> |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
170 |
(case locale_of ctxt of |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
171 |
NONE => theory_result (PureThy.note_thmss_i kind (Attrib.map_facts attrib facts')) |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
172 |
| SOME (loc, view_ctxt) => |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
173 |
locale_result (K (apfst #1 (Locale.add_thmss kind loc facts' view_ctxt)))) |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
174 |
||> (#2 o ProofContext.note_thmss_i (Attrib.map_facts attrib facts)) |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
175 |
end; |
18767 | 176 |
|
18781 | 177 |
fun note (a, ths) ctxt = |
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
178 |
ctxt |> notes PureThy.theoremK [(a, [(ths, [])])] |>> hd; |
18767 | 179 |
|
180 |
||
181 |
(* axioms *) |
|
182 |
||
18781 | 183 |
local |
184 |
||
185 |
fun add_axiom hyps (name, prop) thy = |
|
18767 | 186 |
let |
18781 | 187 |
val name' = if name = "" then "axiom_" ^ string_of_int (serial ()) else name; |
18767 | 188 |
val frees = rev ([] |> Term.add_frees prop |> fold Term.add_frees hyps); |
189 |
val prop' = Term.list_all_free (frees, Logic.list_implies (hyps, prop)); |
|
190 |
val thy' = thy |> Theory.add_axioms_i [(name', prop')]; |
|
18781 | 191 |
|
192 |
val cert = Thm.cterm_of thy'; |
|
193 |
fun all_polymorphic (x, T) th = |
|
194 |
let val var = cert (Var ((x, 0), #1 (Logic.dest_all (Thm.prop_of th)))) |
|
195 |
in ((var, cert (Free (x, T))), Thm.forall_elim var th) end; |
|
196 |
fun implies_polymorphic hyp th = Thm.assume (cert hyp) COMP th; |
|
18767 | 197 |
val th = |
198 |
Thm.get_axiom_i thy' (Sign.full_name thy' name') |
|
18781 | 199 |
|> fold_map all_polymorphic frees |-> Drule.cterm_instantiate |
200 |
|> fold implies_polymorphic hyps |
|
18767 | 201 |
in (th, thy') end; |
202 |
||
18781 | 203 |
in |
204 |
||
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
205 |
fun axioms_finish finish = fold_map (fn ((name, atts), props) => |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
206 |
fold ProofContext.fix_frees props |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
207 |
#> (fn ctxt => ctxt |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
208 |
|> theory_result (fold_map (add_axiom (hyps_of ctxt)) (PureThy.name_multi name props)) |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
209 |
|-> (fn ths => note ((name, atts), map (finish ctxt) ths)))); |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
210 |
|
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
211 |
val axioms = axioms_finish (K I); |
18744 | 212 |
|
213 |
end; |
|
18781 | 214 |
|
215 |
||
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
216 |
(* constant definitions *) |
18781 | 217 |
|
218 |
local |
|
219 |
||
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
220 |
fun add_def (name, prop) = |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
221 |
Theory.add_defs_i false [(name, prop)] #> (fn thy => |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
222 |
let |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
223 |
val th = Thm.get_axiom_i thy (Sign.full_name thy name); |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
224 |
val cert = Thm.cterm_of thy; |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
225 |
val subst = map2 (fn var => fn free => (cert (Var var), cert (Free free))) |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
226 |
(Term.add_vars (Thm.prop_of th) []) (Term.add_frees prop []); |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
227 |
in (Drule.cterm_instantiate subst th, thy) end); |
18781 | 228 |
|
229 |
in |
|
230 |
||
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
231 |
fun def_finish finish (var, spec) ctxt = |
18781 | 232 |
let |
233 |
val (x, mx) = var; |
|
234 |
val ((name, atts), rhs) = spec; |
|
235 |
val name' = if name = "" then Thm.def_name x else name; |
|
18876 | 236 |
val rhs_frees = Term.add_frees rhs []; |
18781 | 237 |
in |
238 |
ctxt |
|
18876 | 239 |
|> consts_restricted (member (op =) rhs_frees) [((x, Term.fastype_of rhs), mx)] |
18781 | 240 |
|-> (fn [lhs] => |
241 |
theory_result (add_def (name', Logic.mk_equals (lhs, rhs))) |
|
242 |
#-> (fn th => fn ctxt' => note ((name', atts), [finish ctxt' lhs th]) ctxt') |
|
243 |
#> apfst (fn (b, [th]) => (lhs, (b, th)))) |
|
244 |
end; |
|
245 |
||
18805
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
246 |
val def = def_finish (K (K I)); |
18863b33c831
improved 'notes', including proper treatment of locale results;
wenzelm
parents:
18781
diff
changeset
|
247 |
|
18781 | 248 |
end; |
249 |
||
250 |
end; |