author | wenzelm |
Fri, 21 Dec 2001 15:18:07 +0100 | |
changeset 12579 | f4e0ce28aa8e |
parent 12575 | 34985eee55b1 |
child 12680 | 1556a7637439 |
permissions | -rw-r--r-- |
12014 | 1 |
(* Title: Pure/Isar/locale.ML |
11896 | 2 |
ID: $Id$ |
12575 | 3 |
Author: Markus Wenzel, LMU München |
11896 | 4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
5 |
||
12058 | 6 |
Locales -- Isar proof contexts as meta-level predicates, with local |
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
7 |
syntax and implicit structures. |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
8 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
9 |
Draws some basic ideas from Florian Kammüller's original version of |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
10 |
locales, but uses the richer infrastructure of Isar instead of the raw |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
11 |
meta-logic. Furthermore, we provide structured import of contexts |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
12 |
(with merge and rename operations), as well as type-inference of the |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
13 |
signature parts. |
11896 | 14 |
*) |
15 |
||
16 |
signature LOCALE = |
|
17 |
sig |
|
12046 | 18 |
type context |
19 |
datatype ('typ, 'term, 'fact, 'att) elem = |
|
12058 | 20 |
Fixes of (string * 'typ option * mixfix option) list | |
12046 | 21 |
Assumes of ((string * 'att list) * ('term * ('term list * 'term list)) list) list | |
22 |
Defines of ((string * 'att list) * ('term * 'term list)) list | |
|
12273 | 23 |
Notes of ((string * 'att list) * ('fact * 'att list) list) list |
24 |
datatype expr = |
|
25 |
Locale of string | |
|
26 |
Rename of expr * string option list | |
|
27 |
Merge of expr list |
|
28 |
val empty: expr |
|
29 |
datatype ('typ, 'term, 'fact, 'att) elem_expr = |
|
30 |
Elem of ('typ, 'term, 'fact, 'att) elem | Expr of expr |
|
12046 | 31 |
type 'att element |
32 |
type 'att element_i |
|
33 |
type locale |
|
34 |
val intern: Sign.sg -> xstring -> string |
|
12014 | 35 |
val cond_extern: Sign.sg -> string -> xstring |
12502 | 36 |
val the_locale: theory -> string -> locale |
12273 | 37 |
val attribute: ('att -> context attribute) -> ('typ, 'term, 'thm, 'att) elem_expr |
38 |
-> ('typ, 'term, 'thm, context attribute) elem_expr |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
39 |
val read_context_statement: xstring option -> context attribute element list -> |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
40 |
(string * (string list * string list)) list list -> context -> |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
41 |
string option * context * context * (term * (term list * term list)) list list |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
42 |
val cert_context_statement: string option -> context attribute element_i list -> |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
43 |
(term * (term list * term list)) list list -> context -> |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
44 |
string option * context * context * (term * (term list * term list)) list list |
12273 | 45 |
val add_locale: bstring -> expr -> context attribute element list -> theory -> theory |
46 |
val add_locale_i: bstring -> expr -> context attribute element_i list -> theory -> theory |
|
12289 | 47 |
val print_locales: theory -> unit |
48 |
val print_locale: theory -> expr -> unit |
|
12273 | 49 |
val add_thmss: string -> ((string * thm list) * context attribute list) list -> theory -> theory |
11896 | 50 |
val setup: (theory -> theory) list |
51 |
end; |
|
52 |
||
12289 | 53 |
structure Locale: LOCALE = |
11896 | 54 |
struct |
55 |
||
12273 | 56 |
(** locale elements and expressions **) |
11896 | 57 |
|
12014 | 58 |
type context = ProofContext.context; |
11896 | 59 |
|
12046 | 60 |
datatype ('typ, 'term, 'fact, 'att) elem = |
12058 | 61 |
Fixes of (string * 'typ option * mixfix option) list | |
12046 | 62 |
Assumes of ((string * 'att list) * ('term * ('term list * 'term list)) list) list | |
63 |
Defines of ((string * 'att list) * ('term * 'term list)) list | |
|
12273 | 64 |
Notes of ((string * 'att list) * ('fact * 'att list) list) list; |
65 |
||
66 |
datatype expr = |
|
67 |
Locale of string | |
|
68 |
Rename of expr * string option list | |
|
69 |
Merge of expr list; |
|
11896 | 70 |
|
12273 | 71 |
val empty = Merge []; |
72 |
||
73 |
datatype ('typ, 'term, 'fact, 'att) elem_expr = |
|
74 |
Elem of ('typ, 'term, 'fact, 'att) elem | Expr of expr; |
|
75 |
||
76 |
type 'att element = (string, string, string, 'att) elem_expr; |
|
77 |
type 'att element_i = (typ, term, thm list, 'att) elem_expr; |
|
12070 | 78 |
|
79 |
type locale = |
|
12289 | 80 |
{import: expr, (*dynamic import*) |
81 |
elems: ((typ, term, thm list, context attribute) elem * stamp) list, (*static content*) |
|
12502 | 82 |
params: (string * typ option) list * string list, (*all vs. local params*) |
12289 | 83 |
text: (string * typ) list * term list} (*logical representation*) |
12063 | 84 |
|
12289 | 85 |
fun make_locale import elems params text = |
86 |
{import = import, elems = elems, params = params, text = text}: locale; |
|
12063 | 87 |
|
11896 | 88 |
|
89 |
||
90 |
(** theory data **) |
|
91 |
||
92 |
structure LocalesArgs = |
|
93 |
struct |
|
12014 | 94 |
val name = "Isar/locales"; |
12063 | 95 |
type T = NameSpace.T * locale Symtab.table; |
11896 | 96 |
|
12063 | 97 |
val empty = (NameSpace.empty, Symtab.empty); |
98 |
val copy = I; |
|
12118 | 99 |
val prep_ext = I; |
12289 | 100 |
|
101 |
(*joining of locale elements: only facts may be added later!*) |
|
102 |
fun join ({import, elems, params, text}: locale, {elems = elems', ...}: locale) = |
|
103 |
Some (make_locale import (gen_merge_lists eq_snd elems elems') params text); |
|
12273 | 104 |
fun merge ((space1, locs1), (space2, locs2)) = |
12289 | 105 |
(NameSpace.merge (space1, space2), Symtab.join join (locs1, locs2)); |
106 |
||
12273 | 107 |
fun print _ (space, locs) = |
108 |
Pretty.strs ("locales:" :: map (NameSpace.cond_extern space o #1) (Symtab.dest locs)) |
|
12014 | 109 |
|> Pretty.writeln; |
11896 | 110 |
end; |
111 |
||
112 |
structure LocalesData = TheoryDataFun(LocalesArgs); |
|
113 |
val print_locales = LocalesData.print; |
|
114 |
||
12289 | 115 |
val intern = NameSpace.intern o #1 o LocalesData.get_sg; |
116 |
val cond_extern = NameSpace.cond_extern o #1 o LocalesData.get_sg; |
|
117 |
||
12277 | 118 |
|
119 |
(* access locales *) |
|
120 |
||
12063 | 121 |
fun declare_locale name = |
122 |
LocalesData.map (apfst (fn space => (NameSpace.extend (space, [name])))); |
|
11896 | 123 |
|
12273 | 124 |
fun put_locale name loc = LocalesData.map (apsnd (fn locs => Symtab.update ((name, loc), locs))); |
12063 | 125 |
fun get_locale thy name = Symtab.lookup (#2 (LocalesData.get thy), name); |
11896 | 126 |
|
12014 | 127 |
fun the_locale thy name = |
128 |
(case get_locale thy name of |
|
129 |
Some loc => loc |
|
130 |
| None => error ("Unknown locale " ^ quote name)); |
|
11896 | 131 |
|
12046 | 132 |
|
12277 | 133 |
(* diagnostics *) |
12273 | 134 |
|
12277 | 135 |
fun err_in_locale ctxt msg ids = |
136 |
let |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
137 |
val sign = ProofContext.sign_of ctxt; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
138 |
fun prt_id (name, parms) = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
139 |
[Pretty.block (Pretty.breaks (map Pretty.str (cond_extern sign name :: parms)))]; |
12289 | 140 |
val prt_ids = flat (separate [Pretty.str " +", Pretty.brk 1] (map prt_id ids)); |
12502 | 141 |
val err_msg = |
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
142 |
if forall (equal "" o #1) ids then msg |
12502 | 143 |
else msg ^ "\n" ^ Pretty.string_of (Pretty.block |
144 |
(Pretty.str "The error(s) above occurred in locale:" :: Pretty.brk 1 :: prt_ids)); |
|
145 |
in raise ProofContext.CONTEXT (err_msg, ctxt) end; |
|
12063 | 146 |
|
12277 | 147 |
|
148 |
||
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
149 |
(** primitives **) |
12046 | 150 |
|
12277 | 151 |
(* renaming *) |
12263 | 152 |
|
153 |
fun rename ren x = if_none (assoc_string (ren, x)) x; |
|
154 |
||
155 |
fun rename_term ren (Free (x, T)) = Free (rename ren x, T) |
|
156 |
| rename_term ren (t $ u) = rename_term ren t $ rename_term ren u |
|
157 |
| rename_term ren (Abs (x, T, t)) = Abs (x, T, rename_term ren t) |
|
158 |
| rename_term _ a = a; |
|
159 |
||
160 |
fun rename_thm ren th = |
|
161 |
let |
|
162 |
val {sign, hyps, prop, maxidx, ...} = Thm.rep_thm th; |
|
163 |
val cert = Thm.cterm_of sign; |
|
12502 | 164 |
val (xs, Ts) = Library.split_list (foldl Term.add_frees ([], prop :: hyps)); |
12263 | 165 |
val xs' = map (rename ren) xs; |
166 |
fun cert_frees names = map (cert o Free) (names ~~ Ts); |
|
167 |
fun cert_vars names = map (cert o Var o apfst (rpair (maxidx + 1))) (names ~~ Ts); |
|
168 |
in |
|
169 |
if xs = xs' then th |
|
170 |
else |
|
171 |
th |
|
172 |
|> Drule.implies_intr_list (map cert hyps) |
|
173 |
|> Drule.forall_intr_list (cert_frees xs) |
|
174 |
|> Drule.forall_elim_list (cert_vars xs) |
|
175 |
|> Thm.instantiate ([], cert_vars xs ~~ cert_frees xs') |
|
176 |
|> (fn th' => Drule.implies_elim_list th' (map (Thm.assume o cert o rename_term ren) hyps)) |
|
177 |
end; |
|
178 |
||
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
179 |
fun rename_elem ren (Fixes fixes) = Fixes (fixes |> map (fn (x, T, mx) => |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
180 |
let val x' = rename ren x in |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
181 |
if x = x' then (x, T, mx) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
182 |
else (x', T, if mx = None then mx else Some Syntax.NoSyn) (*drop syntax*) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
183 |
end)) |
12263 | 184 |
| rename_elem ren (Assumes asms) = Assumes (map (apsnd (map (fn (t, (ps, qs)) => |
185 |
(rename_term ren t, (map (rename_term ren) ps, map (rename_term ren) qs))))) asms) |
|
186 |
| rename_elem ren (Defines defs) = Defines (map (apsnd (fn (t, ps) => |
|
187 |
(rename_term ren t, map (rename_term ren) ps))) defs) |
|
12273 | 188 |
| rename_elem ren (Notes facts) = Notes (map (apsnd (map (apfst (map (rename_thm ren))))) facts); |
12263 | 189 |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
190 |
fun rename_facts prfx elem = |
12307 | 191 |
let |
12323 | 192 |
fun qualify (arg as ((name, atts), x)) = |
193 |
if name = "" then arg |
|
194 |
else ((NameSpace.pack (filter_out (equal "") (prfx @ [name])), atts), x); |
|
12307 | 195 |
in |
196 |
(case elem of |
|
197 |
Fixes fixes => Fixes fixes |
|
198 |
| Assumes asms => Assumes (map qualify asms) |
|
199 |
| Defines defs => Defines (map qualify defs) |
|
200 |
| Notes facts => Notes (map qualify facts)) |
|
201 |
end; |
|
202 |
||
12263 | 203 |
|
12502 | 204 |
(* type instantiation *) |
205 |
||
206 |
fun inst_type [] T = T |
|
207 |
| inst_type env T = Term.map_type_tfree (fn v => if_none (assoc (env, v)) (TFree v)) T; |
|
208 |
||
209 |
fun inst_term [] t = t |
|
210 |
| inst_term env t = Term.map_term_types (inst_type env) t; |
|
211 |
||
212 |
fun inst_thm [] th = th |
|
213 |
| inst_thm env th = |
|
214 |
let |
|
215 |
val {sign, hyps, prop, maxidx, ...} = Thm.rep_thm th; |
|
12575 | 216 |
val cert = Thm.cterm_of sign; |
217 |
val certT = Thm.ctyp_of sign; |
|
12579 | 218 |
val tfrees = foldr Term.add_term_tfree_names (prop :: hyps, []); |
219 |
val env' = filter (fn ((a, _), _) => a mem_string tfrees) env; |
|
12502 | 220 |
in |
221 |
if null env' then th |
|
222 |
else |
|
223 |
th |
|
224 |
|> Drule.implies_intr_list (map cert hyps) |
|
12575 | 225 |
|> Drule.tvars_intr_list (map (#1 o #1) env') |
12502 | 226 |
|> (fn (th', al) => th' |> |
227 |
Thm.instantiate ((map (fn ((a, _), T) => (the (assoc (al, a)), certT T)) env'), [])) |
|
228 |
|> (fn th'' => Drule.implies_elim_list th'' |
|
229 |
(map (Thm.assume o cert o inst_term env') hyps)) |
|
230 |
end; |
|
231 |
||
232 |
fun inst_elem env (Fixes fixes) = |
|
233 |
Fixes (map (fn (x, T, mx) => (x, apsome (inst_type env) T, mx)) fixes) |
|
234 |
| inst_elem env (Assumes asms) = Assumes (map (apsnd (map (fn (t, (ps, qs)) => |
|
235 |
(inst_term env t, (map (inst_term env) ps, map (inst_term env) qs))))) asms) |
|
236 |
| inst_elem env (Defines defs) = Defines (map (apsnd (fn (t, ps) => |
|
237 |
(inst_term env t, map (inst_term env) ps))) defs) |
|
238 |
| inst_elem env (Notes facts) = Notes (map (apsnd (map (apfst (map (inst_thm env))))) facts); |
|
239 |
||
240 |
||
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
241 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
242 |
(** structured contexts: rename + merge + implicit type instantiation **) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
243 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
244 |
(* parameter types *) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
245 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
246 |
fun frozen_tvars ctxt Ts = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
247 |
let |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
248 |
val tvars = rev (foldl Term.add_tvarsT ([], Ts)); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
249 |
val tfrees = map TFree |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
250 |
(Term.invent_type_names (ProofContext.used_types ctxt) (length tvars) ~~ map #2 tvars); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
251 |
in map #1 tvars ~~ tfrees end; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
252 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
253 |
fun unify_frozen ctxt maxidx Ts Us = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
254 |
let |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
255 |
val tsig = Sign.tsig_of (ProofContext.sign_of ctxt); |
12546 | 256 |
fun unify (env, (Some T, Some U)) = (Type.unify tsig env (U, T) |
257 |
handle Type.TUNIFY => raise TYPE ("unify_frozen: failed to unify types", [U, T], [])) |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
258 |
| unify (env, _) = env; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
259 |
fun paramify (i, None) = (i, None) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
260 |
| paramify (i, Some T) = apsnd Some (Type.paramify_dummies (i, T)); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
261 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
262 |
val (maxidx', Ts') = foldl_map paramify (maxidx, Ts); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
263 |
val (maxidx'', Us') = foldl_map paramify (maxidx, Us); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
264 |
val (unifier, _) = foldl unify ((Vartab.empty, maxidx''), Ts' ~~ Us'); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
265 |
val Vs = map (apsome (Envir.norm_type unifier)) Us'; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
266 |
val unifier' = Vartab.extend (unifier, frozen_tvars ctxt (mapfilter I Vs)); |
12532 | 267 |
in map (apsome (Envir.norm_type unifier')) Vs end; |
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
268 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
269 |
fun params_of elemss = flat (map (snd o fst) elemss); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
270 |
fun param_types ps = mapfilter (fn (_, None) => None | (x, Some T) => Some (x, T)) ps; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
271 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
272 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
273 |
(* flatten expressions *) |
11896 | 274 |
|
12510 | 275 |
local |
12502 | 276 |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
277 |
fun unique_parms ctxt elemss = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
278 |
let |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
279 |
val param_decls = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
280 |
flat (map (fn ((name, (ps, qs)), _) => map (rpair (name, ps)) qs) elemss) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
281 |
|> Symtab.make_multi |> Symtab.dest; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
282 |
in |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
283 |
(case find_first (fn (_, ids) => length ids > 1) param_decls of |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
284 |
Some (q, ids) => err_in_locale ctxt ("Multiple declaration of parameter " ^ quote q) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
285 |
(map (apsnd (map fst)) ids) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
286 |
| None => map (apfst (apsnd #1)) elemss) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
287 |
end; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
288 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
289 |
fun unify_parms ctxt fixed_parms raw_parmss = |
12502 | 290 |
let |
291 |
val tsig = Sign.tsig_of (ProofContext.sign_of ctxt); |
|
292 |
val maxidx = length raw_parmss; |
|
293 |
val idx_parmss = (0 upto maxidx - 1) ~~ raw_parmss; |
|
294 |
||
295 |
fun varify i = Term.map_type_tfree (fn (a, S) => TVar ((a, i), S)); |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
296 |
fun varify_parms (i, ps) = map (apsnd (varify i)) (param_types ps); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
297 |
val parms = fixed_parms @ flat (map varify_parms idx_parmss); |
12502 | 298 |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
299 |
fun unify T ((env, maxidx), U) = Type.unify tsig (env, maxidx) (U, T) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
300 |
handle Type.TUNIFY => raise TYPE ("unify_parms: failed to unify types", [U, T], []); |
12502 | 301 |
fun unify_list (envir, T :: Us) = foldl (unify T) (envir, Us) |
302 |
| unify_list (envir, []) = envir; |
|
303 |
val (unifier, _) = foldl unify_list |
|
304 |
((Vartab.empty, maxidx), map #2 (Symtab.dest (Symtab.make_multi parms))); |
|
305 |
||
306 |
val parms' = map (apsnd (Envir.norm_type unifier)) (gen_distinct eq_fst parms); |
|
307 |
val unifier' = Vartab.extend (unifier, frozen_tvars ctxt (map #2 parms')); |
|
308 |
||
309 |
fun inst_parms (i, ps) = |
|
310 |
foldr Term.add_typ_tfrees (mapfilter snd ps, []) |
|
311 |
|> mapfilter (fn (a, S) => |
|
312 |
let val T = Envir.norm_type unifier' (TVar ((a, i), S)) |
|
313 |
in if T = TFree (a, S) then None else Some ((a, S), T) end); |
|
314 |
in map inst_parms idx_parmss end; |
|
315 |
||
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
316 |
in |
12502 | 317 |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
318 |
fun unify_elemss _ _ [] = [] |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
319 |
| unify_elemss _ [] [elems] = [elems] |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
320 |
| unify_elemss ctxt fixed_parms elemss = |
12502 | 321 |
let |
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
322 |
val envs = unify_parms ctxt fixed_parms (map (#2 o #1) elemss); |
12502 | 323 |
fun inst (((name, ps), elems), env) = |
324 |
((name, map (apsnd (apsome (inst_type env))) ps), map (inst_elem env) elems); |
|
325 |
in map inst (elemss ~~ envs) end; |
|
326 |
||
12575 | 327 |
fun flatten_expr ctxt (prev_idents, expr) = |
12014 | 328 |
let |
329 |
val thy = ProofContext.theory_of ctxt; |
|
12263 | 330 |
|
12289 | 331 |
fun renaming (Some x :: xs) (y :: ys) = (y, x) :: renaming xs ys |
332 |
| renaming (None :: xs) (y :: ys) = renaming xs ys |
|
12273 | 333 |
| renaming [] _ = [] |
12289 | 334 |
| renaming xs [] = raise ERROR_MESSAGE ("Too many arguments in renaming: " ^ |
335 |
commas (map (fn None => "_" | Some x => quote x) xs)); |
|
336 |
||
337 |
fun rename_parms ren (name, ps) = |
|
338 |
let val ps' = map (rename ren) ps in |
|
339 |
(case duplicates ps' of [] => (name, ps') |
|
340 |
| dups => err_in_locale ctxt ("Duplicate parameters: " ^ commas_quote dups) [(name, ps')]) |
|
341 |
end; |
|
12263 | 342 |
|
12273 | 343 |
fun identify ((ids, parms), Locale name) = |
12289 | 344 |
let |
345 |
val {import, params, ...} = the_locale thy name; |
|
346 |
val ps = map #1 (#1 params); |
|
347 |
in |
|
12273 | 348 |
if (name, ps) mem ids then (ids, parms) |
12277 | 349 |
else |
12289 | 350 |
let val (ids', parms') = identify ((ids, parms), import); (*acyclic dependencies!*) |
351 |
in (ids' @ [(name, ps)], merge_lists parms' ps) end |
|
12273 | 352 |
end |
353 |
| identify ((ids, parms), Rename (e, xs)) = |
|
354 |
let |
|
355 |
val (ids', parms') = identify (([], []), e); |
|
12289 | 356 |
val ren = renaming xs parms' handle ERROR_MESSAGE msg => err_in_locale ctxt msg ids'; |
357 |
val ids'' = distinct (map (rename_parms ren) ids'); |
|
358 |
val parms'' = distinct (flat (map #2 ids'')); |
|
359 |
in (merge_lists ids ids'', merge_lists parms parms'') end |
|
12273 | 360 |
| identify (arg, Merge es) = foldl identify (arg, es); |
12014 | 361 |
|
12307 | 362 |
fun eval (name, xs) = |
12273 | 363 |
let |
12502 | 364 |
val {params = (ps, qs), elems, ...} = the_locale thy name; |
12307 | 365 |
val ren = filter_out (op =) (map #1 ps ~~ xs); |
12502 | 366 |
val (params', elems') = |
367 |
if null ren then ((ps, qs), map #1 elems) |
|
368 |
else ((map (apfst (rename ren)) ps, map (rename ren) qs), |
|
369 |
map (rename_elem ren o #1) elems); |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
370 |
val elems'' = map (rename_facts [NameSpace.base name, space_implode "_" xs]) elems'; |
12502 | 371 |
in ((name, params'), elems'') end; |
12307 | 372 |
|
12575 | 373 |
val idents = gen_rems (op =) (#1 (identify (([], []), expr)), prev_idents); |
374 |
val raw_elemss = unique_parms ctxt (map eval idents); |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
375 |
val elemss = unify_elemss ctxt [] raw_elemss; |
12575 | 376 |
in (prev_idents @ idents, elemss) end; |
12046 | 377 |
|
12510 | 378 |
end; |
379 |
||
12070 | 380 |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
381 |
(* activate elements *) |
12273 | 382 |
|
12510 | 383 |
local |
384 |
||
12575 | 385 |
fun activate_elem (Fixes fixes) = ProofContext.add_fixes fixes |
12273 | 386 |
| activate_elem (Assumes asms) = |
387 |
#1 o ProofContext.assume_i ProofContext.export_assume asms o |
|
388 |
ProofContext.fix_frees (flat (map (map #1 o #2) asms)) |
|
389 |
| activate_elem (Defines defs) = (fn ctxt => #1 (ProofContext.assume_i ProofContext.export_def |
|
390 |
(map (fn ((name, atts), (t, ps)) => |
|
391 |
let val (c, t') = ProofContext.cert_def ctxt t |
|
392 |
in ((if name = "" then Thm.def_name c else name, atts), [(t', (ps, []))]) end) defs) ctxt)) |
|
393 |
| activate_elem (Notes facts) = #1 o ProofContext.have_thmss facts; |
|
12263 | 394 |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
395 |
fun activate_elems ((name, ps), elems) = ProofContext.qualified (fn ctxt => |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
396 |
foldl (fn (c, e) => activate_elem e c) (ctxt, elems) handle ProofContext.CONTEXT (msg, ctxt) => |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
397 |
err_in_locale ctxt msg [(name, map fst ps)]); |
12502 | 398 |
|
12546 | 399 |
in |
400 |
||
401 |
fun activate_facts prep_facts (ctxt, ((name, ps), raw_elems)) = |
|
402 |
let |
|
403 |
val elems = map (prep_facts ctxt) raw_elems; |
|
404 |
val res = ((name, ps), elems); |
|
405 |
in (ctxt |> activate_elems res, res) end; |
|
406 |
||
12510 | 407 |
end; |
408 |
||
12307 | 409 |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
410 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
411 |
(** prepare context elements **) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
412 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
413 |
(* expressions *) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
414 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
415 |
fun intern_expr sg (Locale xname) = Locale (intern sg xname) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
416 |
| intern_expr sg (Merge exprs) = Merge (map (intern_expr sg) exprs) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
417 |
| intern_expr sg (Rename (expr, xs)) = Rename (intern_expr sg expr, xs); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
418 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
419 |
|
12546 | 420 |
(* attributes *) |
421 |
||
422 |
local fun read_att attrib (x, srcs) = (x, map attrib srcs) in |
|
423 |
||
424 |
fun attribute _ (Elem (Fixes fixes)) = Elem (Fixes fixes) |
|
425 |
| attribute attrib (Elem (Assumes asms)) = Elem (Assumes (map (apfst (read_att attrib)) asms)) |
|
426 |
| attribute attrib (Elem (Defines defs)) = Elem (Defines (map (apfst (read_att attrib)) defs)) |
|
427 |
| attribute attrib (Elem (Notes facts)) = |
|
428 |
Elem (Notes (map (apfst (read_att attrib) o apsnd (map (read_att attrib))) facts)) |
|
429 |
| attribute _ (Expr expr) = Expr expr; |
|
430 |
||
431 |
end; |
|
432 |
||
433 |
||
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
434 |
(* parameters *) |
12502 | 435 |
|
436 |
local |
|
437 |
||
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
438 |
fun prep_fixes prep_vars ctxt fixes = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
439 |
let val vars = snd (foldl_map prep_vars (ctxt, map (fn (x, T, _) => ([x], T)) fixes)) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
440 |
in map (fn (([x'], T'), (_, _, mx)) => (x', T', mx)) (vars ~~ fixes) end; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
441 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
442 |
in |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
443 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
444 |
fun read_fixes x = prep_fixes ProofContext.read_vars x; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
445 |
fun cert_fixes x = prep_fixes ProofContext.cert_vars x; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
446 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
447 |
end; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
448 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
449 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
450 |
(* propositions and bindings *) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
451 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
452 |
datatype ('a, 'b) int_ext = Int of 'a | Ext of 'b; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
453 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
454 |
local |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
455 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
456 |
fun declare_int_elem i (ctxt, Fixes fixes) = |
12575 | 457 |
(ctxt |> ProofContext.add_fixes (map (fn (x, T, mx) => |
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
458 |
(x, apsome (Term.map_type_tfree (Type.param i)) T, mx)) fixes), []) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
459 |
| declare_int_elem _ (ctxt, _) = (ctxt, []); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
460 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
461 |
fun declare_ext_elem prep_fixes (ctxt, Fixes fixes) = |
12575 | 462 |
(ctxt |> ProofContext.add_fixes (prep_fixes ctxt fixes), []) |
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
463 |
| declare_ext_elem _ (ctxt, Assumes asms) = (ctxt, map #2 asms) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
464 |
| declare_ext_elem _ (ctxt, Defines defs) = (ctxt, map (fn (_, (t, ps)) => [(t, (ps, []))]) defs) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
465 |
| declare_ext_elem _ (ctxt, Notes facts) = (ctxt, []); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
466 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
467 |
fun declare_elems prep_fixes ((ctxt, i), ((name, ps), elems)) = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
468 |
let val (ctxt', propps) = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
469 |
(case elems of |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
470 |
Int es => foldl_map (declare_int_elem i) (ctxt, es) |
12546 | 471 |
| Ext e => foldl_map (declare_ext_elem prep_fixes) (ctxt, [e])) |
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
472 |
handle ProofContext.CONTEXT (msg, ctxt) => err_in_locale ctxt msg [(name, map fst ps)] |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
473 |
in ((ctxt', i + 1), propps) end; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
474 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
475 |
|
12502 | 476 |
fun close_frees ctxt t = |
477 |
let val frees = rev (filter_out (ProofContext.is_fixed ctxt o #1) (Term.add_frees ([], t))) |
|
478 |
in Term.list_all_free (frees, t) end; |
|
479 |
||
12575 | 480 |
fun no_binds _ [] = [] |
481 |
| no_binds ctxt _ = raise ProofContext.CONTEXT ("Illegal term bindings in locale element", ctxt); |
|
482 |
||
12502 | 483 |
fun closeup ctxt (Assumes asms) = Assumes (asms |> map (fn (a, propps) => |
12575 | 484 |
(a, map (fn (t, (ps, qs)) => (close_frees ctxt t, |
485 |
(no_binds ctxt ps, no_binds ctxt qs))) propps))) |
|
486 |
| closeup ctxt (Defines defs) = Defines (defs |> map (fn (a, (t, ps)) => |
|
487 |
(a, (close_frees ctxt (#2 (ProofContext.cert_def ctxt t)), no_binds ctxt ps)))) |
|
12502 | 488 |
| closeup ctxt elem = elem; |
489 |
||
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
490 |
fun finish_elem parms _ (Fixes fixes, _) = Ext (Fixes (map (fn (x, _, mx) => |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
491 |
(x, assoc_string (parms, x), mx)) fixes)) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
492 |
| finish_elem _ close (Assumes asms, propp) = Ext (close (Assumes (map #1 asms ~~ propp))) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
493 |
| finish_elem _ close (Defines defs, propp) = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
494 |
Ext (close (Defines (map #1 defs ~~ map (fn [(t, (ps, []))] => (t, ps)) propp))) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
495 |
| finish_elem _ _ (Notes facts, _) = Ext (Notes facts); |
12510 | 496 |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
497 |
fun finish_elems ctxt parms close (((name, ps), elems), propps) = |
12502 | 498 |
let |
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
499 |
val elems' = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
500 |
(case elems of |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
501 |
Int es => map Int (#2 (hd (unify_elemss ctxt parms [((name, ps), es)]))) |
12546 | 502 |
| Ext e => [finish_elem parms close (e, hd propps)]); |
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
503 |
val ps' = map (fn (x, _) => (x, assoc (parms, x))) ps; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
504 |
in ((name, ps'), elems') end; |
12510 | 505 |
|
12277 | 506 |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
507 |
fun prep_elemss prep_fixes prepp do_close context fixed_params raw_elemss raw_concl = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
508 |
let |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
509 |
val ((raw_ctxt, maxidx), raw_proppss) = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
510 |
foldl_map (declare_elems prep_fixes) ((context, 0), raw_elemss); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
511 |
val raw_propps = map flat raw_proppss; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
512 |
val raw_propp = flat raw_propps; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
513 |
val (ctxt, all_propp) = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
514 |
prepp (ProofContext.declare_terms (map Free fixed_params) raw_ctxt, raw_concl @ raw_propp); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
515 |
val ctxt = ProofContext.declare_terms (flat (map (map fst) all_propp)) ctxt; |
12502 | 516 |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
517 |
val all_propp' = map2 (op ~~) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
518 |
(#1 (#2 (ProofContext.bind_propp_schematic_i (ctxt, all_propp))), map (map snd) all_propp); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
519 |
val n = length raw_concl; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
520 |
val concl = take (n, all_propp'); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
521 |
val propp = drop (n, all_propp'); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
522 |
val propps = unflat raw_propps propp; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
523 |
val proppss = map2 (uncurry unflat) (raw_proppss, propps); |
12502 | 524 |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
525 |
val xs = map #1 (params_of raw_elemss); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
526 |
val typing = unify_frozen ctxt maxidx |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
527 |
(map (ProofContext.default_type raw_ctxt) xs) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
528 |
(map (ProofContext.default_type ctxt) xs); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
529 |
val parms = param_types (xs ~~ typing); |
12273 | 530 |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
531 |
val close = if do_close then closeup ctxt else I; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
532 |
val elemss = map2 (finish_elems ctxt parms close) (raw_elemss, proppss); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
533 |
in (parms, elemss, concl) end; |
12502 | 534 |
|
535 |
in |
|
536 |
||
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
537 |
fun read_elemss x = prep_elemss read_fixes ProofContext.read_propp_schematic x; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
538 |
fun cert_elemss x = prep_elemss cert_fixes ProofContext.cert_propp_schematic x; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
539 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
540 |
end; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
541 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
542 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
543 |
(* facts *) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
544 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
545 |
local |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
546 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
547 |
fun prep_name ctxt (name, atts) = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
548 |
if NameSpace.is_qualified name then |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
549 |
raise ProofContext.CONTEXT ("Illegal qualified name: " ^ quote name, ctxt) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
550 |
else (name, atts); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
551 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
552 |
fun prep_facts _ _ (Int elem) = elem |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
553 |
| prep_facts _ _ (Ext (Fixes fixes)) = Fixes fixes |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
554 |
| prep_facts _ ctxt (Ext (Assumes asms)) = Assumes (map (apfst (prep_name ctxt)) asms) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
555 |
| prep_facts _ ctxt (Ext (Defines defs)) = Defines (map (apfst (prep_name ctxt)) defs) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
556 |
| prep_facts get ctxt (Ext (Notes facts)) = Notes (facts |> map (fn (a, bs) => |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
557 |
(prep_name ctxt a, map (apfst (get ctxt)) bs))); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
558 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
559 |
in |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
560 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
561 |
fun get_facts x = prep_facts ProofContext.get_thms x; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
562 |
fun get_facts_i x = prep_facts (K I) x; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
563 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
564 |
end; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
565 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
566 |
|
12546 | 567 |
(* full context statements: import + elements + conclusion *) |
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
568 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
569 |
local |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
570 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
571 |
fun prep_context_statement prep_expr prep_elemss prep_facts |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
572 |
close fixed_params import elements raw_concl context = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
573 |
let |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
574 |
val sign = ProofContext.sign_of context; |
12575 | 575 |
fun flatten (ids, Elem (Fixes fixes)) = |
576 |
(ids, [(("", map (rpair None o #1) fixes), Ext (Fixes fixes))]) |
|
577 |
| flatten (ids, Elem elem) = (ids, [(("", []), Ext elem)]) |
|
578 |
| flatten (ids, Expr expr) = |
|
579 |
let val (ids', elemss) = flatten_expr context (ids, prep_expr sign expr) |
|
580 |
in (ids', map (apsnd Int) elemss) end |
|
12546 | 581 |
val activate = activate_facts prep_facts; |
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
582 |
|
12575 | 583 |
val (import_ids, raw_import_elemss) = flatten ([], Expr import); |
584 |
val raw_elemss = flat (#2 ((foldl_map flatten (import_ids, elements)))); |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
585 |
val (parms, all_elemss, concl) = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
586 |
prep_elemss close context fixed_params (raw_import_elemss @ raw_elemss) raw_concl; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
587 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
588 |
val n = length raw_import_elemss; |
12546 | 589 |
val (import_ctxt, import_elemss) = foldl_map activate (context, take (n, all_elemss)); |
590 |
val (ctxt, elemss) = foldl_map activate (import_ctxt, drop (n, all_elemss)); |
|
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
591 |
in (((import_ctxt, import_elemss), (ctxt, elemss)), concl) end; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
592 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
593 |
val gen_context = prep_context_statement intern_expr read_elemss get_facts; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
594 |
val gen_context_i = prep_context_statement (K I) cert_elemss get_facts_i; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
595 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
596 |
fun gen_statement prep_locale prep_ctxt raw_locale elems concl ctxt = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
597 |
let |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
598 |
val thy = ProofContext.theory_of ctxt; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
599 |
val locale = apsome (prep_locale (Theory.sign_of thy)) raw_locale; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
600 |
val (fixed_params, import) = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
601 |
(case locale of None => ([], empty) |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
602 |
| Some name => (param_types (#1 (#params (the_locale thy name))), Locale name)); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
603 |
val (((locale_ctxt, _), (elems_ctxt, _)), concl') = |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
604 |
prep_ctxt false fixed_params import elems concl ctxt; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
605 |
in (locale, locale_ctxt, elems_ctxt, concl') end; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
606 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
607 |
in |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
608 |
|
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
609 |
fun read_context x y z = #1 (gen_context true [] x y [] z); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
610 |
fun cert_context x y z = #1 (gen_context_i true [] x y [] z); |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
611 |
val read_context_statement = gen_statement intern gen_context; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
612 |
val cert_context_statement = gen_statement (K I) gen_context_i; |
12502 | 613 |
|
614 |
end; |
|
11896 | 615 |
|
616 |
||
617 |
||
12070 | 618 |
(** print locale **) |
619 |
||
12307 | 620 |
fun print_locale thy raw_expr = |
12070 | 621 |
let |
622 |
val sg = Theory.sign_of thy; |
|
12289 | 623 |
val thy_ctxt = ProofContext.init thy; |
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
624 |
val (ctxt, elemss) = #1 (read_context raw_expr [] thy_ctxt); |
12070 | 625 |
|
12307 | 626 |
val prt_typ = Pretty.quote o ProofContext.pretty_typ ctxt; |
627 |
val prt_term = Pretty.quote o ProofContext.pretty_term ctxt; |
|
628 |
val prt_thm = Pretty.quote o ProofContext.pretty_thm ctxt; |
|
12070 | 629 |
|
630 |
fun prt_syn syn = |
|
631 |
let val s = (case syn of None => "(structure)" | Some mx => Syntax.string_of_mixfix mx) |
|
12575 | 632 |
in if s = "" then [] else [Pretty.brk 2, Pretty.str s] end; |
12070 | 633 |
fun prt_fix (x, Some T, syn) = Pretty.block (Pretty.str (x ^ " ::") :: Pretty.brk 1 :: |
634 |
prt_typ T :: Pretty.brk 1 :: prt_syn syn) |
|
635 |
| prt_fix (x, None, syn) = Pretty.block (Pretty.str x :: Pretty.brk 1 :: prt_syn syn); |
|
636 |
||
12307 | 637 |
fun prt_name "" = [Pretty.brk 1] |
638 |
| prt_name name = [Pretty.str (ProofContext.cond_extern ctxt name ^ ":"), Pretty.brk 1]; |
|
639 |
fun prt_asm ((a, _), ts) = Pretty.block (prt_name a @ Pretty.breaks (map (prt_term o fst) ts)); |
|
640 |
fun prt_def ((a, _), (t, _)) = Pretty.block (prt_name a @ [prt_term t]); |
|
641 |
fun prt_fact ((a, _), ths) = Pretty.block |
|
642 |
(prt_name a @ Pretty.breaks (map prt_thm (flat (map fst ths)))); |
|
12070 | 643 |
|
12289 | 644 |
fun items _ [] = [] |
645 |
| items prfx (x :: xs) = Pretty.block [Pretty.str prfx, Pretty.brk 1, x] :: items " and" xs; |
|
646 |
fun prt_elem (Fixes fixes) = items "fixes" (map prt_fix fixes) |
|
647 |
| prt_elem (Assumes asms) = items "assumes" (map prt_asm asms) |
|
648 |
| prt_elem (Defines defs) = items "defines" (map prt_def defs) |
|
649 |
| prt_elem (Notes facts) = items "notes" (map prt_fact facts); |
|
12277 | 650 |
in |
12502 | 651 |
Pretty.big_list "locale elements:" (map (Pretty.chunks o prt_elem) (flat (map #2 elemss))) |
12307 | 652 |
|> Pretty.writeln |
12277 | 653 |
end; |
12070 | 654 |
|
655 |
||
656 |
||
11896 | 657 |
(** define locales **) |
658 |
||
12063 | 659 |
(* add_locale(_i) *) |
11896 | 660 |
|
12502 | 661 |
local |
662 |
||
663 |
fun gen_add_locale prep_context prep_expr bname raw_import raw_body thy = |
|
12063 | 664 |
let |
665 |
val sign = Theory.sign_of thy; |
|
11896 | 666 |
val name = Sign.full_name sign bname; |
12502 | 667 |
val _ = conditional (is_some (get_locale thy name)) (fn () => |
668 |
error ("Duplicate definition of locale " ^ quote name)); |
|
11896 | 669 |
|
12273 | 670 |
val thy_ctxt = ProofContext.init thy; |
12510 | 671 |
val ((import_ctxt, import_elemss), (body_ctxt, body_elemss)) = |
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
672 |
prep_context raw_import raw_body thy_ctxt; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
673 |
val import_parms = params_of import_elemss; |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
674 |
val import = (prep_expr sign raw_import); |
12273 | 675 |
|
12510 | 676 |
val elems = flat (map snd body_elemss); |
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
677 |
val body_parms = params_of body_elemss; |
12273 | 678 |
val text = ([], []); (* FIXME *) |
12063 | 679 |
in |
680 |
thy |
|
681 |
|> declare_locale name |
|
12289 | 682 |
|> put_locale name (make_locale import (map (fn e => (e, stamp ())) elems) |
12502 | 683 |
(import_parms @ body_parms, map #1 body_parms) text) |
12063 | 684 |
end; |
685 |
||
12502 | 686 |
in |
687 |
||
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
688 |
val add_locale = gen_add_locale read_context intern_expr; |
12502 | 689 |
val add_locale_i = gen_add_locale cert_context (K I); |
690 |
||
691 |
end; |
|
12063 | 692 |
|
11896 | 693 |
|
12063 | 694 |
|
12070 | 695 |
(** store results **) |
11896 | 696 |
|
12143 | 697 |
fun add_thmss name args thy = |
12063 | 698 |
let |
12289 | 699 |
val {import, params, elems, text} = the_locale thy name; |
12143 | 700 |
val note = Notes (map (fn ((a, ths), atts) => |
701 |
((a, atts), [(map (curry Thm.name_thm a) ths, [])])) args); |
|
12063 | 702 |
in |
12529
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
703 |
thy |> ProofContext.init |
d99716a53f59
simultaneous type-inference of complete context/statement specifications;
wenzelm
parents:
12514
diff
changeset
|
704 |
|> cert_context_statement (Some name) [Elem note] []; (*test attributes!*) |
12289 | 705 |
thy |> put_locale name (make_locale import (elems @ [(note, stamp ())]) params text) |
12063 | 706 |
end; |
11896 | 707 |
|
12063 | 708 |
|
11896 | 709 |
|
710 |
(** locale theory setup **) |
|
12063 | 711 |
|
11896 | 712 |
val setup = |
713 |
[LocalesData.init]; |
|
714 |
||
715 |
end; |