author  wenzelm 
Mon, 01 Mar 2010 17:07:36 +0100  
changeset 35412  b8dead547d9e 
parent 33387  acea2f336721 
child 35429  afa8cf9e63d8 
permissions  rwrr 
18997  1 
(* Title: Pure/Isar/local_syntax.ML 
2 
Author: Makarius 

3 

4 
Local syntax depending on theory syntax. 

5 
*) 

6 

32738  7 
val show_structs = Unsynchronized.ref false; 
18997  8 

9 
signature LOCAL_SYNTAX = 

10 
sig 

11 
type T 

12 
val syn_of: T > Syntax.syntax 

13 
val structs_of: T > string list 

14 
val init: theory > T 

15 
val rebuild: theory > T > T 

35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

16 
datatype kind = Type  Const  Fixed 
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

17 
val add_syntax: theory > (kind * (string * typ * mixfix)) list > T > T 
20785  18 
val set_mode: Syntax.mode > T > T 
19541  19 
val restore_mode: T > T > T 
24951
834b8c2b9553
replaced add_modesyntax by general update_modesyntax (add or del);
wenzelm
parents:
20785
diff
changeset

20 
val update_modesyntax: theory > bool > Syntax.mode > 
35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

21 
(kind * (string * typ * mixfix)) list > T > T 
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset

22 
val extern_term: T > term > term 
18997  23 
end; 
24 

33387  25 
structure Local_Syntax: LOCAL_SYNTAX = 
18997  26 
struct 
27 

28 
(* datatype T *) 

29 

19386  30 
type local_mixfix = 
35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

31 
(string * bool) * (*name, fixed?*) 
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

32 
((bool * bool * Syntax.mode) * (string * typ * mixfix)); (*type?, add?, mode, declaration*) 
19386  33 

18997  34 
datatype T = Syntax of 
35 
{thy_syntax: Syntax.syntax, 

36 
local_syntax: Syntax.syntax, 

20785  37 
mode: Syntax.mode, 
19386  38 
mixfixes: local_mixfix list, 
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset

39 
idents: string list * string list}; 
18997  40 

19541  41 
fun make_syntax (thy_syntax, local_syntax, mode, mixfixes, idents) = 
18997  42 
Syntax {thy_syntax = thy_syntax, local_syntax = local_syntax, 
19541  43 
mode = mode, mixfixes = mixfixes, idents = idents}; 
18997  44 

19541  45 
fun map_syntax f (Syntax {thy_syntax, local_syntax, mode, mixfixes, idents}) = 
46 
make_syntax (f (thy_syntax, local_syntax, mode, mixfixes, idents)); 

18997  47 

19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset

48 
fun is_consistent thy (Syntax {thy_syntax, ...}) = 
18997  49 
Syntax.eq_syntax (Sign.syn_of thy, thy_syntax); 
50 

51 
fun syn_of (Syntax {local_syntax, ...}) = local_syntax; 

52 
fun idents_of (Syntax {idents, ...}) = idents; 

53 
val structs_of = #1 o idents_of; 

54 

55 

56 
(* build syntax *) 

57 

19541  58 
fun build_syntax thy mode mixfixes (idents as (structs, _)) = 
18997  59 
let 
60 
val thy_syntax = Sign.syn_of thy; 

35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

61 
fun update_gram ((true, add, m), decls) = Syntax.update_type_gram add m decls 
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

62 
 update_gram ((false, add, m), decls) = 
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

63 
Syntax.update_const_gram add (Sign.is_logtype thy) m decls; 
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

64 

18997  65 
val (atrs, trs, trs', atrs') = Syntax.struct_trfuns structs; 
66 
val local_syntax = thy_syntax 

25390
8bfa6566ac6b
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25385
diff
changeset

67 
> Syntax.update_trfuns 
19386  68 
(map Syntax.mk_trfun atrs, map Syntax.mk_trfun trs, 
69 
map Syntax.mk_trfun trs', map Syntax.mk_trfun atrs') 

35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

70 
> fold update_gram (AList.coalesce (op =) (rev (map snd mixfixes))); 
19541  71 
in make_syntax (thy_syntax, local_syntax, mode, mixfixes, idents) end; 
72 

24960  73 
fun init thy = build_syntax thy Syntax.mode_default [] ([], []); 
18997  74 

19541  75 
fun rebuild thy (syntax as Syntax {mode, mixfixes, idents, ...}) = 
76 
if is_consistent thy syntax then syntax 

77 
else build_syntax thy mode mixfixes idents; 

18997  78 

19541  79 

18997  80 
(* mixfix declarations *) 
81 

35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

82 
datatype kind = Type  Const  Fixed; 
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

83 

18997  84 
local 
85 

35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

86 
fun prep_mixfix _ _ (_, (_, _, Structure)) = NONE 
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

87 
 prep_mixfix add mode (Type, decl as (x, _, _)) = SOME ((x, false), ((true, add, mode), decl)) 
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

88 
 prep_mixfix add mode (Const, decl as (x, _, _)) = SOME ((x, false), ((false, add, mode), decl)) 
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

89 
 prep_mixfix add mode (Fixed, (x, T, mx)) = 
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

90 
SOME ((x, true), ((false, add, mode), (Syntax.mark_fixed x, T, mx))); 
19016  91 

35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

92 
fun prep_struct (Fixed, (c, _, Structure)) = SOME c 
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

93 
 prep_struct (_, (c, _, Structure)) = error ("Bad mixfix declaration for " ^ quote c) 
19016  94 
 prep_struct _ = NONE; 
18997  95 

96 
in 

97 

24951
834b8c2b9553
replaced add_modesyntax by general update_modesyntax (add or del);
wenzelm
parents:
20785
diff
changeset

98 
fun update_syntax add thy raw_decls 
834b8c2b9553
replaced add_modesyntax by general update_modesyntax (add or del);
wenzelm
parents:
20785
diff
changeset

99 
(syntax as (Syntax {mode, mixfixes, idents = (structs, _), ...})) = 
834b8c2b9553
replaced add_modesyntax by general update_modesyntax (add or del);
wenzelm
parents:
20785
diff
changeset

100 
(case filter_out (fn (_, (_, _, mx)) => mx = NoSyn) raw_decls of 
19016  101 
[] => syntax 
102 
 decls => 

103 
let 

35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

104 
val new_mixfixes = map_filter (prep_mixfix add mode) decls; 
24954
0664f10a4094
update_modesyntax: may delete 'structure' notation as well;
wenzelm
parents:
24951
diff
changeset

105 
val new_structs = map_filter prep_struct decls; 
0664f10a4094
update_modesyntax: may delete 'structure' notation as well;
wenzelm
parents:
24951
diff
changeset

106 
val mixfixes' = rev new_mixfixes @ mixfixes; 
0664f10a4094
update_modesyntax: may delete 'structure' notation as well;
wenzelm
parents:
24951
diff
changeset

107 
val structs' = 
0664f10a4094
update_modesyntax: may delete 'structure' notation as well;
wenzelm
parents:
24951
diff
changeset

108 
if add then structs @ new_structs 
0664f10a4094
update_modesyntax: may delete 'structure' notation as well;
wenzelm
parents:
24951
diff
changeset

109 
else subtract (op =) new_structs structs; 
20785  110 
val fixes' = fold (fn ((x, true), _) => cons x  _ => I) mixfixes' []; 
19541  111 
in build_syntax thy mode mixfixes' (structs', fixes') end); 
18997  112 

24951
834b8c2b9553
replaced add_modesyntax by general update_modesyntax (add or del);
wenzelm
parents:
20785
diff
changeset

113 
val add_syntax = update_syntax true; 
834b8c2b9553
replaced add_modesyntax by general update_modesyntax (add or del);
wenzelm
parents:
20785
diff
changeset

114 

18997  115 
end; 
116 

117 

19660  118 
(* syntax mode *) 
119 

120 
fun set_mode mode = map_syntax (fn (thy_syntax, local_syntax, _, mixfixes, idents) => 

121 
(thy_syntax, local_syntax, mode, mixfixes, idents)); 

122 

123 
fun restore_mode (Syntax {mode, ...}) = set_mode mode; 

124 

24951
834b8c2b9553
replaced add_modesyntax by general update_modesyntax (add or del);
wenzelm
parents:
20785
diff
changeset

125 
fun update_modesyntax thy add mode args syntax = 
834b8c2b9553
replaced add_modesyntax by general update_modesyntax (add or del);
wenzelm
parents:
20785
diff
changeset

126 
syntax > set_mode mode > update_syntax add thy args > restore_mode syntax; 
19660  127 

128 

18997  129 
(* extern_term *) 
130 

19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset

131 
fun extern_term syntax = 
18997  132 
let 
19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset

133 
val (structs, fixes) = idents_of syntax; 
18997  134 
fun map_free (t as Free (x, T)) = 
19618  135 
let val i = find_index (fn s => s = x) structs + 1 in 
18997  136 
if i = 0 andalso member (op =) fixes x then 
35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset

137 
Term.Const (Syntax.mark_fixed x, T) 
18997  138 
else if i = 1 andalso not (! show_structs) then 
139 
Syntax.const "_struct" $ Syntax.const "_indexdefault" 

140 
else t 

141 
end 

142 
 map_free t = t; 

19369
a4374b41c9bf
simplified handling of authentic syntax (cf. early externing in consts.ML);
wenzelm
parents:
19016
diff
changeset

143 
in Term.map_aterms map_free end; 
18997  144 

145 
end; 