| author | paulson |
| Tue, 02 May 2006 14:27:49 +0200 | |
| changeset 19532 | dae447f2b0b4 |
| parent 19482 | 9f11af8f7ef9 |
| child 19541 | 1bb8e26a26ee |
| permissions | -rw-r--r-- |
| 18997 | 1 |
(* Title: Pure/Isar/local_syntax.ML |
2 |
ID: $Id$ |
|
3 |
Author: Makarius |
|
4 |
||
5 |
Local syntax depending on theory syntax. |
|
6 |
*) |
|
7 |
||
8 |
val show_structs = ref false; |
|
9 |
||
10 |
signature LOCAL_SYNTAX = |
|
11 |
sig |
|
12 |
type T |
|
13 |
val syn_of: T -> Syntax.syntax |
|
14 |
val structs_of: T -> string list |
|
15 |
val init: theory -> T |
|
16 |
val rebuild: theory -> T -> T |
|
|
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
17 |
val add_syntax: theory -> string * bool -> (bool * (string * typ * mixfix)) list -> T -> T |
|
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
18 |
val extern_term: T -> term -> term |
| 18997 | 19 |
end; |
20 |
||
| 19016 | 21 |
structure LocalSyntax: LOCAL_SYNTAX = |
| 18997 | 22 |
struct |
23 |
||
| 19386 | 24 |
|
| 18997 | 25 |
(* datatype T *) |
26 |
||
| 19386 | 27 |
type local_mixfix = |
28 |
((string * bool) * string list list) * (*name, fixed?, mixfix content*) |
|
29 |
((string * bool) * (string * typ * mixfix)); (*mode, inout?, mixfix syntax*) |
|
30 |
||
| 18997 | 31 |
datatype T = Syntax of |
32 |
{thy_syntax: Syntax.syntax,
|
|
33 |
local_syntax: Syntax.syntax, |
|
| 19386 | 34 |
mixfixes: local_mixfix list, |
|
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
35 |
idents: string list * string list}; |
| 18997 | 36 |
|
37 |
fun make_syntax (thy_syntax, local_syntax, mixfixes, idents) = |
|
38 |
Syntax {thy_syntax = thy_syntax, local_syntax = local_syntax,
|
|
39 |
mixfixes = mixfixes, idents = idents}; |
|
40 |
||
41 |
fun map_syntax f (Syntax {thy_syntax, local_syntax, mixfixes, idents}) =
|
|
42 |
make_syntax (f (thy_syntax, local_syntax, mixfixes, idents)); |
|
43 |
||
|
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
44 |
fun is_consistent thy (Syntax {thy_syntax, ...}) =
|
| 18997 | 45 |
Syntax.eq_syntax (Sign.syn_of thy, thy_syntax); |
46 |
||
47 |
fun syn_of (Syntax {local_syntax, ...}) = local_syntax;
|
|
48 |
fun idents_of (Syntax {idents, ...}) = idents;
|
|
49 |
val structs_of = #1 o idents_of; |
|
50 |
||
51 |
||
52 |
(* build syntax *) |
|
53 |
||
|
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
54 |
fun build_syntax thy (mixfixes, idents as (structs, _)) = |
| 18997 | 55 |
let |
56 |
val thy_syntax = Sign.syn_of thy; |
|
57 |
val is_logtype = Sign.is_logtype thy; |
|
58 |
val (atrs, trs, trs', atrs') = Syntax.struct_trfuns structs; |
|
59 |
val local_syntax = thy_syntax |
|
60 |
|> Syntax.extend_trfuns |
|
| 19386 | 61 |
(map Syntax.mk_trfun atrs, map Syntax.mk_trfun trs, |
62 |
map Syntax.mk_trfun trs', map Syntax.mk_trfun atrs') |
|
63 |
|> fold (uncurry (Syntax.extend_const_gram is_logtype)) |
|
|
19454
46a7e133f802
moved coalesce to AList, added equality predicates to library
haftmann
parents:
19386
diff
changeset
|
64 |
(AList.coalesce (op =) (rev (map snd mixfixes))); |
|
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
65 |
in make_syntax (thy_syntax, local_syntax, mixfixes, idents) end; |
| 18997 | 66 |
|
|
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
67 |
fun init thy = build_syntax thy ([], ([], [])); |
| 18997 | 68 |
|
69 |
fun rebuild thy (syntax as Syntax {mixfixes, idents, ...}) =
|
|
70 |
if is_consistent thy syntax then syntax |
|
71 |
else build_syntax thy (mixfixes, idents); |
|
72 |
||
73 |
||
74 |
(* mixfix declarations *) |
|
75 |
||
76 |
local |
|
77 |
||
| 19016 | 78 |
fun mixfix_nosyn (_, (_, _, mx)) = mx = NoSyn; |
79 |
fun mixfix_struct (_, (_, _, mx)) = mx = Structure; |
|
80 |
||
| 19386 | 81 |
fun mixfix_conflict ((content1: string list list, inout1), ((_, content2), ((_, inout2), _))) = |
82 |
inout1 andalso inout2 andalso exists (fn x => exists (fn y => x = y) content2) content1; |
|
| 18997 | 83 |
|
| 19386 | 84 |
fun add_mixfix (mode, inout) (fixed, (x, T, mx)) = |
| 19016 | 85 |
let |
86 |
val content = Syntax.mixfix_content mx; |
|
|
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
87 |
val c = if fixed then Syntax.fixedN ^ x else x; |
| 19386 | 88 |
in |
89 |
remove mixfix_conflict (content, inout) #> |
|
90 |
cons (((x, fixed), content), ((mode, inout), (c, T, mx))) |
|
91 |
end; |
|
| 19016 | 92 |
|
93 |
fun prep_struct (fixed, (c, _, Structure)) = |
|
94 |
if fixed then SOME c |
|
95 |
else error ("Bad mixfix declaration for const: " ^ quote c)
|
|
96 |
| prep_struct _ = NONE; |
|
| 18997 | 97 |
|
98 |
in |
|
99 |
||
|
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
100 |
fun add_syntax thy prmode raw_decls (syntax as (Syntax {mixfixes, idents = (structs, _), ...})) =
|
| 19016 | 101 |
(case filter_out mixfix_nosyn raw_decls of |
102 |
[] => syntax |
|
103 |
| decls => |
|
104 |
let |
|
| 19386 | 105 |
val mixfixes' = mixfixes |> fold (add_mixfix prmode) (filter_out mixfix_struct decls); |
| 19016 | 106 |
val fixes' = fold (fn (((x, true), _), _) => cons x | _ => I) mixfixes' []; |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19454
diff
changeset
|
107 |
val structs' = structs @ map_filter prep_struct decls; |
|
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
108 |
in build_syntax thy (mixfixes', (structs', fixes')) end); |
| 18997 | 109 |
|
110 |
end; |
|
111 |
||
112 |
||
113 |
(* extern_term *) |
|
114 |
||
|
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
115 |
fun extern_term syntax = |
| 18997 | 116 |
let |
|
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
117 |
val (structs, fixes) = idents_of syntax; |
| 18997 | 118 |
fun map_free (t as Free (x, T)) = |
119 |
let val i = Library.find_index_eq x structs + 1 in |
|
120 |
if i = 0 andalso member (op =) fixes x then |
|
121 |
Const (Syntax.fixedN ^ x, T) |
|
122 |
else if i = 1 andalso not (! show_structs) then |
|
123 |
Syntax.const "_struct" $ Syntax.const "_indexdefault" |
|
124 |
else t |
|
125 |
end |
|
126 |
| map_free t = t; |
|
|
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
127 |
in Term.map_aterms map_free end; |
| 18997 | 128 |
|
129 |
end; |