5244
|
1 |
(* Title: Pure/locale.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Florian Kammueller, University of Cambridge
|
|
4 |
|
|
5 |
Locales. (* FIXME description *)
|
|
6 |
|
|
7 |
TODO:
|
|
8 |
- cleanup this TODO list;
|
|
9 |
- read_cterm, pretty_cterm etc. wrt. current scope: collect defaults;
|
|
10 |
- inner locale name space for rules / defs (?!);
|
|
11 |
- attributes for assumptions (??!);
|
|
12 |
- logical aspects of locales:
|
|
13 |
. unfold / fold defs for in/out (use simplifier);
|
|
14 |
. discharge assumptions and eliminate defs for result;
|
|
15 |
- improve error msgs: more checks, at very beginning;
|
|
16 |
*)
|
|
17 |
|
|
18 |
signature BASIC_LOCALE =
|
|
19 |
sig
|
|
20 |
val print_locales: theory -> unit
|
|
21 |
val print_goals: int -> thm -> unit
|
|
22 |
end;
|
|
23 |
|
|
24 |
signature LOCALE =
|
|
25 |
sig
|
|
26 |
include BASIC_LOCALE
|
|
27 |
val get_thm: theory -> xstring -> thm
|
|
28 |
val get_thms: theory -> xstring -> thm list
|
|
29 |
type locale
|
|
30 |
val add_locale: bstring -> (string * string * mixfix) list ->
|
|
31 |
(string * string) list -> (string * string) list -> theory -> theory
|
|
32 |
val add_locale_i: bstring -> (string * typ * mixfix) list ->
|
|
33 |
(string * term) list -> (string * term) list -> theory -> theory
|
|
34 |
val open_locale: xstring -> theory -> theory
|
|
35 |
val close_locale: theory -> theory
|
|
36 |
val in_locale: term list -> Sign.sg -> bool
|
|
37 |
val is_open_loc_sg: Sign.sg -> bool
|
|
38 |
val is_open_loc: theory -> bool
|
|
39 |
val read_cterm: Sign.sg -> string * typ -> cterm
|
|
40 |
val get_scope: theory -> (string * locale) list
|
|
41 |
val get_scope_sg: Sign.sg -> (string * locale) list
|
|
42 |
val collect_consts: Sign.sg -> string list
|
|
43 |
val setup: (theory -> theory) list
|
|
44 |
end;
|
|
45 |
|
|
46 |
structure Locale: LOCALE =
|
|
47 |
struct
|
|
48 |
|
|
49 |
|
|
50 |
(** type locale **)
|
|
51 |
|
|
52 |
type locale =
|
|
53 |
{consts: (string * typ) list,
|
|
54 |
nosyn: string list,
|
|
55 |
rules: (string * term) list,
|
|
56 |
defs: (string * term) list,
|
|
57 |
thms: (string * thm) list,
|
|
58 |
defaults: (string * sort) list * (string * typ) list * string list};
|
|
59 |
|
|
60 |
fun make_locale consts nosyn rules defs thms defaults =
|
|
61 |
{consts = consts, nosyn = nosyn, rules = rules, defs = defs,
|
|
62 |
thms = thms, defaults = defaults}: locale;
|
|
63 |
|
|
64 |
fun pretty_locale sg (name, {consts, rules, defs, nosyn = _, thms = _, defaults = _}) =
|
|
65 |
let
|
|
66 |
val prt_typ = Pretty.quote o Sign.pretty_typ sg;
|
|
67 |
val prt_term = Pretty.quote o Sign.pretty_term sg;
|
|
68 |
|
|
69 |
fun pretty_const (c, T) = Pretty.block
|
|
70 |
[Pretty.str (c ^ " ::"), Pretty.brk 1, prt_typ T];
|
|
71 |
|
|
72 |
fun pretty_axiom (a, t) = Pretty.block
|
|
73 |
[Pretty.str (a ^ ":"), Pretty.brk 1, prt_term t];
|
|
74 |
in
|
|
75 |
Pretty.big_list (name ^ " =")
|
|
76 |
[Pretty.big_list "consts:" (map pretty_const consts),
|
|
77 |
Pretty.big_list "rules:" (map pretty_axiom rules),
|
|
78 |
Pretty.big_list "defs:" (map pretty_axiom defs)]
|
|
79 |
end;
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
(** theory data **)
|
|
84 |
|
|
85 |
(* data kind 'Pure/locales' *)
|
|
86 |
|
|
87 |
type locale_data =
|
|
88 |
{space: NameSpace.T,
|
|
89 |
locales: locale Symtab.table,
|
|
90 |
scope: (string * locale) list ref};
|
|
91 |
|
|
92 |
fun make_locale_data space locales scope =
|
|
93 |
{space = space, locales = locales, scope = scope}: locale_data;
|
|
94 |
|
|
95 |
structure LocalesArgs =
|
|
96 |
struct
|
|
97 |
val name = "Pure/locales";
|
|
98 |
type T = locale_data;
|
|
99 |
|
|
100 |
val empty = make_locale_data NameSpace.empty Symtab.empty (ref []);
|
|
101 |
fun prep_ext {space, locales, scope = _} = make_locale_data space locales (ref []);
|
|
102 |
fun merge ({space = space1, locales = locales1, scope = _},
|
|
103 |
{space = space2, locales = locales2, scope = _}) =
|
|
104 |
make_locale_data (NameSpace.merge (space1, space2))
|
|
105 |
(Symtab.merge (K true) (locales1, locales2))
|
|
106 |
(ref []);
|
|
107 |
|
|
108 |
fun print sg {space, locales, scope} =
|
|
109 |
let
|
|
110 |
fun extrn name =
|
|
111 |
if ! long_names then name else NameSpace.extern space name;
|
|
112 |
val locs = map (apfst extrn) (Symtab.dest locales);
|
|
113 |
val scope_names = rev (map (extrn o fst) (! scope));
|
|
114 |
in
|
|
115 |
Pretty.writeln (Display.pretty_name_space ("locale name space", space));
|
|
116 |
Pretty.writeln (Pretty.big_list "locales:" (map (pretty_locale sg) locs));
|
|
117 |
Pretty.writeln (Pretty.strs ("current scope:" :: scope_names))
|
|
118 |
end;
|
|
119 |
end;
|
|
120 |
|
|
121 |
|
|
122 |
structure LocalesData = TheoryDataFun(LocalesArgs);
|
|
123 |
val print_locales = LocalesData.print;
|
|
124 |
|
|
125 |
|
|
126 |
(* access locales *)
|
|
127 |
|
|
128 |
fun get_locale_sg sg name = Symtab.lookup (#locales (LocalesData.get_sg sg), name);
|
|
129 |
|
|
130 |
val get_locale = get_locale_sg o Theory.sign_of;
|
|
131 |
|
|
132 |
fun put_locale (name, locale) thy =
|
|
133 |
let
|
|
134 |
val {space, locales, scope} = LocalesData.get thy;
|
|
135 |
val space' = NameSpace.extend (space, [name]);
|
|
136 |
val locales' = Symtab.update ((name, locale), locales);
|
|
137 |
in thy |> LocalesData.put (make_locale_data space' locales' scope) end;
|
|
138 |
|
|
139 |
fun lookup_locale thy xname =
|
|
140 |
let
|
|
141 |
val {space, locales, ...} = LocalesData.get thy;
|
|
142 |
val name = NameSpace.intern space xname;
|
|
143 |
in apsome (pair name) (get_locale thy name) end;
|
|
144 |
|
|
145 |
|
|
146 |
(* access scope *)
|
|
147 |
|
|
148 |
val get_scope_sg = ! o #scope o LocalesData.get_sg;
|
|
149 |
|
|
150 |
val get_scope = get_scope_sg o Theory.sign_of;
|
|
151 |
|
|
152 |
fun change_scope f thy =
|
|
153 |
let val {scope, ...} = LocalesData.get thy
|
|
154 |
in scope := f (! scope) end;
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
(** scope operations **)
|
|
159 |
|
|
160 |
(* change scope *)
|
|
161 |
|
|
162 |
fun the_locale thy xname =
|
|
163 |
(case lookup_locale thy xname of
|
|
164 |
Some loc => loc
|
|
165 |
| None => error ("Unknown locale " ^ quote xname));
|
|
166 |
|
|
167 |
fun open_locale xname thy =
|
|
168 |
(change_scope (cons (the_locale thy xname)) thy; thy);
|
|
169 |
|
|
170 |
fun pop_locale [] = error "Currently no open locales"
|
|
171 |
| pop_locale (_ :: locs) = locs;
|
|
172 |
|
|
173 |
fun close_locale thy = (change_scope pop_locale thy; thy);
|
|
174 |
|
|
175 |
|
|
176 |
(** functions for goals.ML **)
|
|
177 |
|
|
178 |
(* in_locale: check if hyps (: term list) of a proof are contained in the
|
|
179 |
(current) scope. This function is needed in prepare_proof. It needs to
|
|
180 |
refer to the signature, because theory is not available in prepare_proof. *)
|
|
181 |
|
|
182 |
fun in_locale hyps sg =
|
|
183 |
let val cur_sc = get_scope_sg sg;
|
|
184 |
val rule_lists = map (#rules o snd) cur_sc;
|
|
185 |
val def_lists = map (#defs o snd) cur_sc;
|
|
186 |
val rules = map snd (foldr (op union) (rule_lists, []));
|
|
187 |
val defs = map snd (foldr (op union) (def_lists, []));
|
|
188 |
val defnrules = rules @ defs;
|
|
189 |
in
|
|
190 |
hyps subset defnrules
|
|
191 |
end;
|
|
192 |
|
|
193 |
|
|
194 |
(* is_open_loc: check if any locale is open, i.e. in the scope of the current thy *)
|
|
195 |
fun is_open_loc_sg sign =
|
|
196 |
let val cur_sc = get_scope_sg sign
|
|
197 |
in not(null(cur_sc)) end;
|
|
198 |
|
|
199 |
val is_open_loc = is_open_loc_sg o Theory.sign_of;
|
|
200 |
|
|
201 |
|
|
202 |
(* get theorems *)
|
|
203 |
|
|
204 |
fun get_thm_locale name ((_, {thms, ...}: locale)) = assoc (thms, name);
|
|
205 |
|
|
206 |
fun get_thmx f get thy name =
|
|
207 |
(case get_first (get_thm_locale name) (get_scope thy) of
|
|
208 |
Some thm => f thm
|
|
209 |
| None => get thy name);
|
|
210 |
|
|
211 |
val get_thm = get_thmx I PureThy.get_thm;
|
|
212 |
|
|
213 |
val get_thms = get_thmx (fn x => [x]) PureThy.get_thms;
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
(** define locales **)
|
|
218 |
|
|
219 |
(* prepare types *)
|
|
220 |
|
|
221 |
fun read_typ sg (envT, s) =
|
|
222 |
let
|
|
223 |
fun def_sort (x, ~1) = assoc (envT, x)
|
|
224 |
| def_sort _ = None;
|
|
225 |
val T = Type.no_tvars (Sign.read_typ (sg, def_sort) s) handle TYPE (msg, _, _) => error msg;
|
|
226 |
in (Term.add_typ_tfrees (T, envT), T) end;
|
|
227 |
|
|
228 |
fun cert_typ sg (envT, raw_T) =
|
|
229 |
let val T = Type.no_tvars (Sign.certify_typ sg raw_T) handle TYPE (msg, _, _) => error msg
|
|
230 |
in (Term.add_typ_tfrees (T, envT), T) end;
|
|
231 |
|
|
232 |
|
|
233 |
(* prepare props *)
|
|
234 |
|
|
235 |
val add_frees = foldl_aterms (fn (vs, Free v) => v ins vs | (vs, _) => vs);
|
|
236 |
|
|
237 |
fun enter_term t (envS, envT, used) =
|
|
238 |
(Term.add_term_tfrees (t, envS), add_frees (envT, t), Term.add_term_tfree_names (t, used));
|
|
239 |
|
|
240 |
fun read_axm sg ((envS, envT, used), (name, s)) =
|
|
241 |
let
|
|
242 |
fun def_sort (x, ~1) = assoc (envS, x)
|
|
243 |
| def_sort _ = None;
|
|
244 |
fun def_type (x, ~1) = assoc (envT, x)
|
|
245 |
| def_type _ = None;
|
|
246 |
val (_, t) = Theory.read_def_axm (sg, def_type, def_sort) used (name, s);
|
|
247 |
in
|
|
248 |
(enter_term t (envS, envT, used), t)
|
|
249 |
end;
|
|
250 |
|
|
251 |
|
|
252 |
fun cert_axm sg ((envS, envT, used), (name, raw_t)) =
|
|
253 |
let val (_, t) = Theory.cert_axm sg (name, raw_t)
|
|
254 |
in (enter_term t (envS, envT, used), t) end;
|
|
255 |
|
|
256 |
|
|
257 |
(* Locale.read_cterm: read in a string as a certified term, and respect the bindings
|
|
258 |
that already exist for subterms. If no locale is open, this function is equal to
|
|
259 |
Thm.read_cterm *)
|
|
260 |
|
|
261 |
fun read_cterm sign =
|
|
262 |
let val cur_sc = get_scope_sg sign;
|
|
263 |
val defaults = map (#defaults o snd) cur_sc;
|
|
264 |
val envS = flat (map #1 defaults);
|
|
265 |
val envT = flat (map #2 defaults);
|
|
266 |
val used = flat (map #3 defaults);
|
|
267 |
fun def_sort (x, ~1) = assoc (envS, x)
|
|
268 |
| def_sort _ = None;
|
|
269 |
fun def_type (x, ~1) = assoc (envT, x)
|
|
270 |
| def_type _ = None;
|
|
271 |
in (if (is_open_loc_sg sign)
|
|
272 |
then (#1 o read_def_cterm (sign, def_type, def_sort) used true)
|
|
273 |
else Thm.read_cterm sign)
|
|
274 |
end;
|
|
275 |
|
|
276 |
(* basic functions needed for definitions and display *)
|
|
277 |
(* collect all locale constants of a scope, i.e. a list of locales *)
|
|
278 |
fun collect_consts sg =
|
|
279 |
let val cur_sc = get_scope_sg sg;
|
|
280 |
val locale_list = map snd cur_sc;
|
|
281 |
val const_list = flat (map #consts locale_list)
|
|
282 |
in map fst const_list end;
|
|
283 |
|
|
284 |
(* filter out the Free's in a term *)
|
|
285 |
fun list_frees t =
|
|
286 |
case t of Const(c,T) => []
|
|
287 |
| Var(v,T) => []
|
|
288 |
| Free(v,T)=> [Free(v,T)]
|
|
289 |
| Bound x => []
|
|
290 |
| Abs(a,T,u) => list_frees u
|
|
291 |
| t1 $ t2 => (list_frees t1) @ (list_frees t2);
|
|
292 |
|
|
293 |
(* filter out all Free's in a term that are not contained
|
|
294 |
in a list of strings. Used to prepare definitions. The list of strings
|
|
295 |
will be the consts of the scope. We filter out the "free" Free's to be
|
|
296 |
able to bind them *)
|
|
297 |
fun difflist term clist =
|
|
298 |
let val flist = list_frees term;
|
|
299 |
fun builddiff [] sl = []
|
|
300 |
| builddiff (t :: tl) sl =
|
|
301 |
let val Free(v,T) = t
|
|
302 |
in
|
|
303 |
if (v mem sl)
|
|
304 |
then builddiff tl sl
|
|
305 |
else t :: (builddiff tl sl)
|
|
306 |
end;
|
|
307 |
in distinct(builddiff flist clist) end;
|
|
308 |
|
|
309 |
(* Bind a term with !! over a list of "free" Free's *)
|
|
310 |
fun abs_over_free clist term =
|
|
311 |
let val diffl = rev(difflist term clist);
|
|
312 |
fun abs_o (t, (x as Free(v,T))) = all(T) $ Abs(v, T, abstract_over (x,t))
|
|
313 |
| abs_o (_ , _) = error ("Can't be: abs_over_free");
|
|
314 |
in foldl abs_o (term, diffl) end;
|
|
315 |
|
|
316 |
(* assume a definition, i.e assume the cterm of a definiton term and then eliminate
|
|
317 |
the binding !!, so that the def can be applied as rewrite. The meta hyp will still contain !! *)
|
|
318 |
fun prep_def clist sg = forall_elim_vars(0) o assume o (cterm_of sg);
|
|
319 |
|
|
320 |
|
|
321 |
(* concrete syntax *)
|
|
322 |
|
|
323 |
fun mark_syn c = "\\<^locale>" ^ c;
|
|
324 |
|
|
325 |
fun mk_loc_tr c ts = list_comb (Free (c, dummyT), ts);
|
|
326 |
|
|
327 |
|
|
328 |
(* add_locale *)
|
|
329 |
|
|
330 |
fun gen_add_locale prep_typ prep_term bname raw_consts raw_rules raw_defs thy =
|
|
331 |
let val sign = Theory.sign_of thy;
|
|
332 |
|
|
333 |
val name = Sign.full_name sign bname;
|
|
334 |
|
|
335 |
|
|
336 |
(* prepare locale consts *)
|
|
337 |
|
|
338 |
fun prep_const (envS, (raw_c, raw_T, raw_mx)) =
|
|
339 |
let
|
|
340 |
val c = Syntax.const_name raw_c raw_mx;
|
|
341 |
val c_syn = mark_syn c;
|
|
342 |
val mx = Syntax.fix_mixfix raw_c raw_mx;
|
|
343 |
val (envS', T) = prep_typ sign (envS, raw_T) handle ERROR =>
|
|
344 |
error ("The error(s) above occured in locale constant " ^ quote c);
|
|
345 |
val trfun = if mx = Syntax.NoSyn then None else Some (c_syn, mk_loc_tr c);
|
|
346 |
in (envS', ((c, T), (c_syn, T, mx), trfun)) end;
|
|
347 |
|
|
348 |
val (envS0, loc_consts_syn) = foldl_map prep_const ([], raw_consts);
|
|
349 |
val loc_consts = map #1 loc_consts_syn;
|
|
350 |
val loc_syn = map #2 loc_consts_syn;
|
|
351 |
val nosyn = map (#1 o #1) (filter (fn x => (#3(#2 x)) = NoSyn) loc_consts_syn);
|
|
352 |
val loc_trfuns = mapfilter #3 loc_consts_syn;
|
|
353 |
|
|
354 |
|
|
355 |
(* 1st stage: syntax_thy *)
|
|
356 |
|
|
357 |
val syntax_thy =
|
|
358 |
thy
|
|
359 |
|> Theory.add_modesyntax_i ("", true) loc_syn
|
|
360 |
|> Theory.add_trfuns ([], loc_trfuns, [], []);
|
|
361 |
|
|
362 |
val syntax_sign = Theory.sign_of syntax_thy;
|
|
363 |
|
|
364 |
|
|
365 |
(* prepare rules and defs *)
|
|
366 |
|
|
367 |
fun prep_axiom (env, (a, raw_t)) =
|
|
368 |
let
|
|
369 |
val (env', t) = prep_term syntax_sign (env, (a, raw_t)) handle ERROR =>
|
|
370 |
error ("The error(s) above occured in locale rule / definition " ^ quote a);
|
|
371 |
in (env', (a, t)) end;
|
|
372 |
|
|
373 |
val ((envS1, envT1, used1), loc_rules) =
|
|
374 |
foldl_map prep_axiom ((envS0, loc_consts, map fst envS0), raw_rules);
|
|
375 |
val (defaults, loc_defs) = foldl_map prep_axiom ((envS1, envT1, used1), raw_defs);
|
|
376 |
|
|
377 |
val old_loc_consts = collect_consts syntax_sign;
|
|
378 |
val new_loc_consts = (map #1 loc_consts);
|
|
379 |
val all_loc_consts = old_loc_consts @ new_loc_consts;
|
|
380 |
|
|
381 |
val loc_defs_terms = map (apsnd (abs_over_free (all_loc_consts))) loc_defs;
|
|
382 |
val loc_defs_thms = map (apsnd (prep_def (map #1 loc_consts) syntax_sign)) loc_defs_terms;
|
|
383 |
val loc_thms = (map (apsnd (Thm.assume o Thm.cterm_of syntax_sign)) (loc_rules)) @ loc_defs_thms;
|
|
384 |
|
|
385 |
|
|
386 |
(* error messages *) (* FIXME improve *)
|
|
387 |
|
|
388 |
val err_dup_locale =
|
|
389 |
if is_none (get_locale thy name) then []
|
|
390 |
else ["Duplicate definition of locale " ^ quote name];
|
|
391 |
|
|
392 |
val errs = err_dup_locale;
|
|
393 |
in
|
|
394 |
if null errs then ()
|
|
395 |
else error (cat_lines errs);
|
|
396 |
|
|
397 |
syntax_thy
|
|
398 |
|> put_locale (name, make_locale loc_consts nosyn loc_rules loc_defs_terms loc_thms defaults)
|
|
399 |
end;
|
|
400 |
|
|
401 |
|
|
402 |
val add_locale = gen_add_locale read_typ read_axm;
|
|
403 |
val add_locale_i = gen_add_locale cert_typ cert_axm;
|
|
404 |
|
|
405 |
(** print functions **)
|
|
406 |
(* idea: substitute all locale contants (Free's) that are syntactical by their
|
|
407 |
"real" constant representation (i.e. \\<^locale>constname).
|
|
408 |
- function const_ssubst does this substitution
|
|
409 |
- function Locale.pretty_term:
|
|
410 |
if locale is open then do this substitution & then call Sign.pretty_term
|
|
411 |
else call Sign.pretty_term
|
|
412 |
*)
|
|
413 |
(* substitutes all Free variables s in t by Const's s *)
|
|
414 |
fun const_ssubst t s =
|
|
415 |
case t of
|
|
416 |
Free(v,T) => if v = s then Const("\\<^locale>" ^ s,T) else Free(v,T)
|
|
417 |
| Const(c,T) => Const(c,T)
|
|
418 |
| Var(v,T) => Var(v,T)
|
|
419 |
| Bound x => Bound x
|
|
420 |
| Abs(a,T,u) => Abs(a,T, const_ssubst u s)
|
|
421 |
| t1 $ t2 => const_ssubst t1 s $ const_ssubst t2 s;
|
|
422 |
|
|
423 |
(* FIXME: improve: can be expressed with foldl *)
|
|
424 |
fun const_ssubst_list [] t = t
|
|
425 |
| const_ssubst_list (s :: l) t = const_ssubst_list l (const_ssubst t s);
|
|
426 |
|
|
427 |
(* Locale.pretty_term *)
|
|
428 |
fun pretty_term sign =
|
|
429 |
if (is_open_loc_sg sign) then
|
|
430 |
let val locale_list = map snd(get_scope_sg sign);
|
|
431 |
val nosyn = flat (map #nosyn locale_list);
|
|
432 |
val str_list = (collect_consts sign) \\ nosyn
|
|
433 |
in Sign.pretty_term sign o (const_ssubst_list str_list)
|
|
434 |
end
|
|
435 |
else Sign.pretty_term sign;
|
|
436 |
|
|
437 |
|
|
438 |
|
|
439 |
(** print_goals **)
|
|
440 |
|
|
441 |
(*print thm A1,...,An/B in "goal style" -- premises as numbered subgoals*)
|
|
442 |
|
|
443 |
local
|
|
444 |
|
|
445 |
(* utils *)
|
|
446 |
|
|
447 |
fun ins_entry (x, y) [] = [(x, [y])]
|
|
448 |
| ins_entry (x, y) ((pair as (x', ys')) :: pairs) =
|
|
449 |
if x = x' then (x', y ins ys') :: pairs
|
|
450 |
else pair :: ins_entry (x, y) pairs;
|
|
451 |
|
|
452 |
fun add_consts (Const (c, T), env) = ins_entry (T, (c, T)) env
|
|
453 |
| add_consts (t $ u, env) = add_consts (u, add_consts (t, env))
|
|
454 |
| add_consts (Abs (_, _, t), env) = add_consts (t, env)
|
|
455 |
| add_consts (_, env) = env;
|
|
456 |
|
|
457 |
fun add_vars (Free (x, T), env) = ins_entry (T, (x, ~1)) env
|
|
458 |
| add_vars (Var (xi, T), env) = ins_entry (T, xi) env
|
|
459 |
| add_vars (Abs (_, _, t), env) = add_vars (t, env)
|
|
460 |
| add_vars (t $ u, env) = add_vars (u, add_vars (t, env))
|
|
461 |
| add_vars (_, env) = env;
|
|
462 |
|
|
463 |
fun add_varsT (Type (_, Ts), env) = foldr add_varsT (Ts, env)
|
|
464 |
| add_varsT (TFree (x, S), env) = ins_entry (S, (x, ~1)) env
|
|
465 |
| add_varsT (TVar (xi, S), env) = ins_entry (S, xi) env;
|
|
466 |
|
|
467 |
fun sort_idxs vs = map (apsnd (sort (prod_ord string_ord int_ord))) vs;
|
|
468 |
fun sort_cnsts cs = map (apsnd (sort_wrt fst)) cs;
|
|
469 |
|
|
470 |
|
|
471 |
(* prepare atoms *)
|
|
472 |
|
|
473 |
fun consts_of t = sort_cnsts (add_consts (t, []));
|
|
474 |
fun vars_of t = sort_idxs (add_vars (t, []));
|
|
475 |
fun varsT_of t = rev (sort_idxs (it_term_types add_varsT (t, [])));
|
|
476 |
|
|
477 |
in
|
|
478 |
|
|
479 |
fun print_goals maxgoals state =
|
|
480 |
let
|
|
481 |
val {sign, ...} = rep_thm state;
|
|
482 |
|
|
483 |
val prt_term = pretty_term sign;
|
|
484 |
val prt_typ = Sign.pretty_typ sign;
|
|
485 |
val prt_sort = Sign.pretty_sort sign;
|
|
486 |
|
|
487 |
fun prt_atoms prt prtT (X, xs) = Pretty.block
|
|
488 |
[Pretty.block (Pretty.commas (map prt xs)), Pretty.str " ::",
|
|
489 |
Pretty.brk 1, prtT X];
|
|
490 |
|
|
491 |
fun prt_var (x, ~1) = prt_term (Syntax.free x)
|
|
492 |
| prt_var xi = prt_term (Syntax.var xi);
|
|
493 |
|
|
494 |
fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
|
|
495 |
| prt_varT xi = prt_typ (TVar (xi, []));
|
|
496 |
|
|
497 |
val prt_consts = prt_atoms (prt_term o Const) prt_typ;
|
|
498 |
val prt_vars = prt_atoms prt_var prt_typ;
|
|
499 |
val prt_varsT = prt_atoms prt_varT prt_sort;
|
|
500 |
|
|
501 |
|
|
502 |
fun print_list _ _ [] = ()
|
|
503 |
| print_list name prt lst = (writeln "";
|
|
504 |
Pretty.writeln (Pretty.big_list name (map prt lst)));
|
|
505 |
|
|
506 |
fun print_subgoals (_, []) = ()
|
|
507 |
| print_subgoals (n, A :: As) = (Pretty.writeln (Pretty.blk (0,
|
|
508 |
[Pretty.str (" " ^ string_of_int n ^ ". "), prt_term A]));
|
|
509 |
print_subgoals (n + 1, As));
|
|
510 |
|
|
511 |
val print_ffpairs =
|
|
512 |
print_list "Flex-flex pairs:" (prt_term o Logic.mk_flexpair);
|
|
513 |
|
|
514 |
val print_consts = print_list "Constants:" prt_consts o consts_of;
|
|
515 |
val print_vars = print_list "Variables:" prt_vars o vars_of;
|
|
516 |
val print_varsT = print_list "Type variables:" prt_varsT o varsT_of;
|
|
517 |
|
|
518 |
|
|
519 |
val {prop, ...} = rep_thm state;
|
|
520 |
val (tpairs, As, B) = Logic.strip_horn prop;
|
|
521 |
val ngoals = length As;
|
|
522 |
|
|
523 |
fun print_gs (types, sorts) =
|
|
524 |
(Pretty.writeln (prt_term B);
|
|
525 |
if ngoals = 0 then writeln "No subgoals!"
|
|
526 |
else if ngoals > maxgoals then
|
|
527 |
(print_subgoals (1, take (maxgoals, As));
|
|
528 |
writeln ("A total of " ^ string_of_int ngoals ^ " subgoals..."))
|
|
529 |
else print_subgoals (1, As);
|
|
530 |
|
|
531 |
print_ffpairs tpairs;
|
|
532 |
|
|
533 |
if types andalso ! show_consts then print_consts prop else ();
|
|
534 |
if types then print_vars prop else ();
|
|
535 |
if sorts then print_varsT prop else ());
|
|
536 |
in
|
|
537 |
setmp show_no_free_types true
|
|
538 |
(setmp show_types (! show_types orelse ! show_sorts)
|
|
539 |
(setmp show_sorts false print_gs))
|
|
540 |
(! show_types orelse ! show_sorts, ! show_sorts)
|
|
541 |
end;
|
|
542 |
|
|
543 |
end;
|
|
544 |
|
|
545 |
|
|
546 |
|
|
547 |
(** locale theory setup **)
|
|
548 |
|
|
549 |
val setup =
|
|
550 |
[LocalesData.init];
|
|
551 |
|
|
552 |
end;
|
|
553 |
|
|
554 |
structure BasicLocale: BASIC_LOCALE = Locale;
|
|
555 |
open BasicLocale;
|