author | nipkow |
Tue, 17 Jun 2025 14:11:40 +0200 | |
changeset 82733 | 8b537e1af2ec |
parent 81594 | 7e1b66416b7f |
permissions | -rw-r--r-- |
42240
5a4d30cd47a7
moved Isar/local_syntax.ML to Syntax/local_syntax.ML;
wenzelm
parents:
35429
diff
changeset
|
1 |
(* Title: Pure/Syntax/local_syntax.ML |
18997 | 2 |
Author: Makarius |
3 |
||
51655 | 4 |
Local syntax depending on theory syntax, with special support for |
5 |
implicit structure references. |
|
18997 | 6 |
*) |
7 |
||
8 |
signature LOCAL_SYNTAX = |
|
9 |
sig |
|
10 |
type T |
|
80074
951c371c1cd9
clarified names: discontinue odd convention from 3 decades ago;
wenzelm
parents:
62765
diff
changeset
|
11 |
val syntax_of: T -> Syntax.syntax |
18997 | 12 |
val init: theory -> T |
80897
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80074
diff
changeset
|
13 |
val rebuild: Proof.context -> T -> T |
35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset
|
14 |
datatype kind = Type | Const | Fixed |
59152
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
52144
diff
changeset
|
15 |
val add_syntax: Proof.context -> (kind * (string * typ * mixfix)) list -> |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
52144
diff
changeset
|
16 |
T -> {structs: string list, fixes: string list} option * T |
81594
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
17 |
val syntax_deps: Proof.context -> (string * string list) list -> T -> T |
81591
d570d215e380
syntax translations now work in a local theory context;
wenzelm
parents:
81149
diff
changeset
|
18 |
val translations: Proof.context -> bool -> Ast.ast Syntax.trrule list -> T -> T |
20785 | 19 |
val set_mode: Syntax.mode -> T -> T |
19541 | 20 |
val restore_mode: T -> T -> T |
59152
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
52144
diff
changeset
|
21 |
val update_modesyntax: Proof.context -> bool -> Syntax.mode -> |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
52144
diff
changeset
|
22 |
(kind * (string * typ * mixfix)) list -> |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
52144
diff
changeset
|
23 |
T -> {structs: string list, fixes: string list} option * T |
18997 | 24 |
end; |
25 |
||
33387 | 26 |
structure Local_Syntax: LOCAL_SYNTAX = |
18997 | 27 |
struct |
28 |
||
29 |
(* datatype T *) |
|
30 |
||
19386 | 31 |
type local_mixfix = |
81149 | 32 |
{name: string, is_fixed: bool} * |
33 |
({is_type: bool, add: bool, mode: Syntax.mode} * (string * typ * mixfix)); |
|
19386 | 34 |
|
18997 | 35 |
datatype T = Syntax of |
36 |
{thy_syntax: Syntax.syntax, |
|
37 |
local_syntax: Syntax.syntax, |
|
20785 | 38 |
mode: Syntax.mode, |
81591
d570d215e380
syntax translations now work in a local theory context;
wenzelm
parents:
81149
diff
changeset
|
39 |
mixfixes: local_mixfix list, |
81594
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
40 |
consts: (string * string list) list, |
81591
d570d215e380
syntax translations now work in a local theory context;
wenzelm
parents:
81149
diff
changeset
|
41 |
translations: (bool * Ast.ast Syntax.trrule list) list}; |
18997 | 42 |
|
81594
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
43 |
fun make_syntax (thy_syntax, local_syntax, mode, mixfixes, consts, translations) = |
81591
d570d215e380
syntax translations now work in a local theory context;
wenzelm
parents:
81149
diff
changeset
|
44 |
Syntax {thy_syntax = thy_syntax, local_syntax = local_syntax, mode = mode, |
81594
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
45 |
mixfixes = mixfixes, consts = consts, translations = translations}; |
18997 | 46 |
|
81594
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
47 |
fun map_syntax f (Syntax {thy_syntax, local_syntax, mode, mixfixes, consts, translations}) = |
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
48 |
make_syntax (f (thy_syntax, local_syntax, mode, mixfixes, consts, translations)); |
18997 | 49 |
|
80897
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80074
diff
changeset
|
50 |
fun is_consistent ctxt (Syntax {thy_syntax, ...}) = |
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80074
diff
changeset
|
51 |
Syntax.eq_syntax (Sign.syntax_of (Proof_Context.theory_of ctxt), thy_syntax); |
18997 | 52 |
|
80074
951c371c1cd9
clarified names: discontinue odd convention from 3 decades ago;
wenzelm
parents:
62765
diff
changeset
|
53 |
fun syntax_of (Syntax {local_syntax, ...}) = local_syntax; |
18997 | 54 |
|
55 |
||
56 |
(* build syntax *) |
|
57 |
||
81594
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
58 |
fun build_syntax ctxt mode mixfixes consts translations = |
18997 | 59 |
let |
80897
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80074
diff
changeset
|
60 |
val thy = Proof_Context.theory_of ctxt; |
80074
951c371c1cd9
clarified names: discontinue odd convention from 3 decades ago;
wenzelm
parents:
62765
diff
changeset
|
61 |
val thy_syntax = Sign.syntax_of thy; |
81149 | 62 |
fun update_gram ({is_type = true, add, mode}, decls) = |
63 |
Syntax.update_type_gram ctxt add mode decls |
|
64 |
| update_gram ({is_type = false, add, mode}, decls) = |
|
65 |
Syntax.update_const_gram ctxt add (Sign.logical_types thy) mode decls; |
|
35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset
|
66 |
|
18997 | 67 |
val local_syntax = thy_syntax |
81591
d570d215e380
syntax translations now work in a local theory context;
wenzelm
parents:
81149
diff
changeset
|
68 |
|> fold update_gram (AList.coalesce (op =) (rev (map snd mixfixes))) |
81594
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
69 |
|> Syntax.update_consts ctxt consts |
81591
d570d215e380
syntax translations now work in a local theory context;
wenzelm
parents:
81149
diff
changeset
|
70 |
|> fold_rev (uncurry (Syntax.update_trrules ctxt)) translations; |
81594
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
71 |
in make_syntax (thy_syntax, local_syntax, mode, mixfixes, consts, translations) end; |
19541 | 72 |
|
59152
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
52144
diff
changeset
|
73 |
fun init thy = |
80074
951c371c1cd9
clarified names: discontinue odd convention from 3 decades ago;
wenzelm
parents:
62765
diff
changeset
|
74 |
let val thy_syntax = Sign.syntax_of thy |
81594
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
75 |
in make_syntax (thy_syntax, thy_syntax, Syntax.mode_default, [], [], []) end; |
18997 | 76 |
|
81594
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
77 |
fun rebuild ctxt (syntax as Syntax {mode, mixfixes, consts, translations, ...}) = |
80897
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80074
diff
changeset
|
78 |
if is_consistent ctxt syntax then syntax |
81594
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
79 |
else build_syntax ctxt mode mixfixes consts translations; |
18997 | 80 |
|
19541 | 81 |
|
18997 | 82 |
(* mixfix declarations *) |
83 |
||
35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset
|
84 |
datatype kind = Type | Const | Fixed; |
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset
|
85 |
|
18997 | 86 |
local |
87 |
||
81149 | 88 |
fun prep_mixfix _ _ (_, (_, _, Structure _)) = (NONE: local_mixfix option) |
89 |
| prep_mixfix add mode (Type, decl as (x, _, _)) = |
|
90 |
SOME ({name = x, is_fixed = false}, ({is_type = true, add = add, mode = mode}, decl)) |
|
91 |
| prep_mixfix add mode (Const, decl as (x, _, _)) = |
|
92 |
SOME ({name = x, is_fixed = false}, ({is_type = false, add = add, mode = mode}, decl)) |
|
35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset
|
93 |
| prep_mixfix add mode (Fixed, (x, T, mx)) = |
81149 | 94 |
SOME (({name = x, is_fixed = true}, |
95 |
({is_type = false, add = add, mode = mode}, (Lexicon.mark_fixed x, T, mx)))); |
|
19016 | 96 |
|
62752 | 97 |
fun prep_struct (Fixed, (c, _, Structure _)) = SOME c |
62765
5b95a12b7b19
tuned messages -- position is usually missing here;
wenzelm
parents:
62752
diff
changeset
|
98 |
| prep_struct (_, (c, _, Structure _)) = error ("Bad structure mixfix declaration for " ^ quote c) |
19016 | 99 |
| prep_struct _ = NONE; |
18997 | 100 |
|
101 |
in |
|
102 |
||
81594
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
103 |
fun update_syntax ctxt add raw_decls (syntax as (Syntax {mode, mixfixes, consts, translations, ...})) = |
62752 | 104 |
(case filter_out (Mixfix.is_empty o #3 o #2) raw_decls of |
59152
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
52144
diff
changeset
|
105 |
[] => (NONE, syntax) |
19016 | 106 |
| decls => |
107 |
let |
|
35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
33387
diff
changeset
|
108 |
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
|
109 |
val new_structs = map_filter prep_struct decls; |
0664f10a4094
update_modesyntax: may delete 'structure' notation as well;
wenzelm
parents:
24951
diff
changeset
|
110 |
val mixfixes' = rev new_mixfixes @ mixfixes; |
18997 | 111 |
|
59152
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
52144
diff
changeset
|
112 |
val idents = Syntax_Trans.get_idents ctxt; |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
52144
diff
changeset
|
113 |
val idents' = |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
52144
diff
changeset
|
114 |
{structs = |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
52144
diff
changeset
|
115 |
if add then #structs idents @ new_structs |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
52144
diff
changeset
|
116 |
else subtract (op =) new_structs (#structs idents), |
81149 | 117 |
fixes = fold (fn ({name = x, is_fixed = true}, _) => cons x | _ => I) mixfixes' []}; |
59152
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
52144
diff
changeset
|
118 |
|
81594
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
119 |
val syntax' = build_syntax ctxt mode mixfixes' consts translations; |
59152
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
52144
diff
changeset
|
120 |
in (if idents = idents' then NONE else SOME idents', syntax') end); |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
52144
diff
changeset
|
121 |
|
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
52144
diff
changeset
|
122 |
fun add_syntax ctxt = update_syntax ctxt true; |
24951
834b8c2b9553
replaced add_modesyntax by general update_modesyntax (add or del);
wenzelm
parents:
20785
diff
changeset
|
123 |
|
18997 | 124 |
end; |
125 |
||
126 |
||
81591
d570d215e380
syntax translations now work in a local theory context;
wenzelm
parents:
81149
diff
changeset
|
127 |
(* translations *) |
d570d215e380
syntax translations now work in a local theory context;
wenzelm
parents:
81149
diff
changeset
|
128 |
|
81594
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
129 |
fun syntax_deps ctxt args (Syntax {mode, mixfixes, consts, translations, ...}) = |
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
130 |
build_syntax ctxt mode mixfixes (args @ consts) translations; |
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
131 |
|
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
132 |
fun translations ctxt add args (Syntax {mode, mixfixes, consts, translations, ...}) = |
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
133 |
(Sign.check_translations ctxt args; |
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
134 |
build_syntax ctxt mode mixfixes consts ((add, args) :: translations)); |
81591
d570d215e380
syntax translations now work in a local theory context;
wenzelm
parents:
81149
diff
changeset
|
135 |
|
d570d215e380
syntax translations now work in a local theory context;
wenzelm
parents:
81149
diff
changeset
|
136 |
|
19660 | 137 |
(* syntax mode *) |
138 |
||
81594
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
139 |
fun set_mode mode = map_syntax (fn (thy_syntax, local_syntax, _, mixfixes, consts, translations) => |
7e1b66416b7f
commands 'syntax_types' and 'syntax_consts' now work in a local theory context;
wenzelm
parents:
81591
diff
changeset
|
140 |
(thy_syntax, local_syntax, mode, mixfixes, consts, translations)); |
19660 | 141 |
|
142 |
fun restore_mode (Syntax {mode, ...}) = set_mode mode; |
|
143 |
||
59152
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
52144
diff
changeset
|
144 |
fun update_modesyntax ctxt add mode args syntax = |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
52144
diff
changeset
|
145 |
syntax |> set_mode mode |> update_syntax ctxt add args ||> restore_mode syntax; |
19660 | 146 |
|
18997 | 147 |
end; |