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