author | wenzelm |
Sat, 08 Apr 2006 22:51:25 +0200 | |
changeset 19369 | a4374b41c9bf |
parent 19016 | f26377a4605a |
child 19386 | 38d83ffd6217 |
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 |
||
24 |
(* datatype T *) |
|
25 |
||
26 |
datatype T = Syntax of |
|
27 |
{thy_syntax: Syntax.syntax, |
|
28 |
local_syntax: Syntax.syntax, |
|
19016 | 29 |
mixfixes: (((string * bool) * string list list) * (string * typ * mixfix)) list, |
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
30 |
idents: string list * string list}; |
18997 | 31 |
|
32 |
fun make_syntax (thy_syntax, local_syntax, mixfixes, idents) = |
|
33 |
Syntax {thy_syntax = thy_syntax, local_syntax = local_syntax, |
|
34 |
mixfixes = mixfixes, idents = idents}; |
|
35 |
||
36 |
fun map_syntax f (Syntax {thy_syntax, local_syntax, mixfixes, idents}) = |
|
37 |
make_syntax (f (thy_syntax, local_syntax, mixfixes, idents)); |
|
38 |
||
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
39 |
fun is_consistent thy (Syntax {thy_syntax, ...}) = |
18997 | 40 |
Syntax.eq_syntax (Sign.syn_of thy, thy_syntax); |
41 |
||
42 |
fun syn_of (Syntax {local_syntax, ...}) = local_syntax; |
|
43 |
fun idents_of (Syntax {idents, ...}) = idents; |
|
44 |
val structs_of = #1 o idents_of; |
|
45 |
||
46 |
||
47 |
(* build syntax *) |
|
48 |
||
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
49 |
fun build_syntax thy (mixfixes, idents as (structs, _)) = |
18997 | 50 |
let |
51 |
val thy_syntax = Sign.syn_of thy; |
|
52 |
val is_logtype = Sign.is_logtype thy; |
|
53 |
val (atrs, trs, trs', atrs') = Syntax.struct_trfuns structs; |
|
54 |
val local_syntax = thy_syntax |
|
55 |
|> Syntax.extend_trfuns |
|
56 |
(map Syntax.mk_trfun atrs, map Syntax.mk_trfun trs, |
|
57 |
map Syntax.mk_trfun trs', map Syntax.mk_trfun atrs') |
|
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
58 |
|> Syntax.extend_const_gram is_logtype Syntax.default_mode (map snd mixfixes) |
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
59 |
in make_syntax (thy_syntax, local_syntax, mixfixes, idents) end; |
18997 | 60 |
|
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
61 |
fun init thy = build_syntax thy ([], ([], [])); |
18997 | 62 |
|
63 |
fun rebuild thy (syntax as Syntax {mixfixes, idents, ...}) = |
|
64 |
if is_consistent thy syntax then syntax |
|
65 |
else build_syntax thy (mixfixes, idents); |
|
66 |
||
67 |
||
68 |
(* mixfix declarations *) |
|
69 |
||
70 |
local |
|
71 |
||
19016 | 72 |
fun mixfix_nosyn (_, (_, _, mx)) = mx = NoSyn; |
73 |
fun mixfix_struct (_, (_, _, mx)) = mx = Structure; |
|
74 |
||
75 |
fun mixfix_conflict (content1: string list list, ((_, content2), _)) = |
|
76 |
exists (fn x => exists (fn y => x = y) content2) content1; |
|
18997 | 77 |
|
19016 | 78 |
fun add_mixfix (fixed, (x, T, mx)) = |
79 |
let |
|
80 |
val content = Syntax.mixfix_content mx; |
|
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
81 |
val c = if fixed then Syntax.fixedN ^ x else x; |
19016 | 82 |
in remove mixfix_conflict content #> cons (((x, fixed), content), (c, T, mx)) end; |
83 |
||
84 |
fun prep_struct (fixed, (c, _, Structure)) = |
|
85 |
if fixed then SOME c |
|
86 |
else error ("Bad mixfix declaration for const: " ^ quote c) |
|
87 |
| prep_struct _ = NONE; |
|
18997 | 88 |
|
89 |
in |
|
90 |
||
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
91 |
fun add_syntax thy prmode raw_decls (syntax as (Syntax {mixfixes, idents = (structs, _), ...})) = |
19016 | 92 |
(case filter_out mixfix_nosyn raw_decls of |
93 |
[] => syntax |
|
94 |
| decls => |
|
95 |
let |
|
96 |
val mixfixes' = mixfixes |> fold add_mixfix (filter_out mixfix_struct decls); |
|
97 |
val fixes' = fold (fn (((x, true), _), _) => cons x | _ => I) mixfixes' []; |
|
98 |
val structs' = structs @ List.mapPartial prep_struct decls; |
|
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
99 |
in build_syntax thy (mixfixes', (structs', fixes')) end); |
18997 | 100 |
|
101 |
end; |
|
102 |
||
103 |
||
104 |
(* extern_term *) |
|
105 |
||
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
106 |
fun extern_term syntax = |
18997 | 107 |
let |
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
108 |
val (structs, fixes) = idents_of syntax; |
18997 | 109 |
fun map_free (t as Free (x, T)) = |
110 |
let val i = Library.find_index_eq x structs + 1 in |
|
111 |
if i = 0 andalso member (op =) fixes x then |
|
112 |
Const (Syntax.fixedN ^ x, T) |
|
113 |
else if i = 1 andalso not (! show_structs) then |
|
114 |
Syntax.const "_struct" $ Syntax.const "_indexdefault" |
|
115 |
else t |
|
116 |
end |
|
117 |
| map_free t = t; |
|
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset
|
118 |
in Term.map_aterms map_free end; |
18997 | 119 |
|
120 |
end; |