src/Pure/Syntax/local_syntax.ML
author nipkow
Tue, 17 Jun 2025 14:11:40 +0200
changeset 82733 8b537e1af2ec
parent 81594 7e1b66416b7f
permissions -rw-r--r--
reinstated intersection of lists as inter_list_set
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
     3
51655
28d6eb23522c tuned comment;
wenzelm
parents: 42290
diff changeset
     4
Local syntax depending on theory syntax, with special support for
28d6eb23522c tuned comment;
wenzelm
parents: 42290
diff changeset
     5
implicit structure references.
18997
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
     6
*)
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
     7
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
     8
signature LOCAL_SYNTAX =
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
     9
sig
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    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
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    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
d60f81c56fd4 Syntax.mode;
wenzelm
parents: 19660
diff changeset
    19
  val set_mode: Syntax.mode -> T -> T
19541
1bb8e26a26ee maintain implicit syntax mode;
wenzelm
parents: 19482
diff changeset
    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
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    24
end;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    25
33387
acea2f336721 modernized structure Local_Syntax;
wenzelm
parents: 32738
diff changeset
    26
structure Local_Syntax: LOCAL_SYNTAX =
18997
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    27
struct
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    28
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    29
(* datatype T *)
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    30
19386
38d83ffd6217 add_syntax: actually observe print mode;
wenzelm
parents: 19369
diff changeset
    31
type local_mixfix =
81149
0e506128c14a clarified signature;
wenzelm
parents: 80897
diff changeset
    32
  {name: string, is_fixed: bool} *
0e506128c14a clarified signature;
wenzelm
parents: 80897
diff changeset
    33
  ({is_type: bool, add: bool, mode: Syntax.mode} * (string * typ * mixfix));
19386
38d83ffd6217 add_syntax: actually observe print mode;
wenzelm
parents: 19369
diff changeset
    34
18997
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    35
datatype T = Syntax of
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    36
 {thy_syntax: Syntax.syntax,
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    37
  local_syntax: Syntax.syntax,
20785
d60f81c56fd4 Syntax.mode;
wenzelm
parents: 19660
diff changeset
    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
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    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
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    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
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    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
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    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
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    54
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    55
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    56
(* build syntax *)
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    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
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    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
0e506128c14a clarified signature;
wenzelm
parents: 80897
diff changeset
    62
    fun update_gram ({is_type = true, add, mode}, decls) =
0e506128c14a clarified signature;
wenzelm
parents: 80897
diff changeset
    63
          Syntax.update_type_gram ctxt add mode decls
0e506128c14a clarified signature;
wenzelm
parents: 80897
diff changeset
    64
      | update_gram ({is_type = false, add, mode}, decls) =
0e506128c14a clarified signature;
wenzelm
parents: 80897
diff changeset
    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
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    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
1bb8e26a26ee maintain implicit syntax mode;
wenzelm
parents: 19482
diff changeset
    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
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    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
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    80
19541
1bb8e26a26ee maintain implicit syntax mode;
wenzelm
parents: 19482
diff changeset
    81
18997
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    82
(* mixfix declarations *)
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    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
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    86
local
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    87
81149
0e506128c14a clarified signature;
wenzelm
parents: 80897
diff changeset
    88
fun prep_mixfix _ _ (_, (_, _, Structure _)) = (NONE: local_mixfix option)
0e506128c14a clarified signature;
wenzelm
parents: 80897
diff changeset
    89
  | prep_mixfix add mode (Type, decl as (x, _, _)) =
0e506128c14a clarified signature;
wenzelm
parents: 80897
diff changeset
    90
      SOME ({name = x, is_fixed = false}, ({is_type = true, add = add, mode = mode}, decl))
0e506128c14a clarified signature;
wenzelm
parents: 80897
diff changeset
    91
  | prep_mixfix add mode (Const, decl as (x, _, _)) =
0e506128c14a clarified signature;
wenzelm
parents: 80897
diff changeset
    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
0e506128c14a clarified signature;
wenzelm
parents: 80897
diff changeset
    94
      SOME (({name = x, is_fixed = true},
0e506128c14a clarified signature;
wenzelm
parents: 80897
diff changeset
    95
        ({is_type = false, add = add, mode = mode}, (Lexicon.mark_fixed x, T, mx))));
19016
f26377a4605a tuned mixfixes, mixfix_conflict;
wenzelm
parents: 18997
diff changeset
    96
62752
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    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
f26377a4605a tuned mixfixes, mixfix_conflict;
wenzelm
parents: 18997
diff changeset
    99
  | prep_struct _ = NONE;
18997
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   100
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   101
in
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   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
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
   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
f26377a4605a tuned mixfixes, mixfix_conflict;
wenzelm
parents: 18997
diff changeset
   106
  | decls =>
f26377a4605a tuned mixfixes, mixfix_conflict;
wenzelm
parents: 18997
diff changeset
   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
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   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
0e506128c14a clarified signature;
wenzelm
parents: 80897
diff changeset
   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
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   124
end;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   125
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   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
e3ec6839c631 added add_modesyntax;
wenzelm
parents: 19618
diff changeset
   137
(* syntax mode *)
e3ec6839c631 added add_modesyntax;
wenzelm
parents: 19618
diff changeset
   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
e3ec6839c631 added add_modesyntax;
wenzelm
parents: 19618
diff changeset
   141
e3ec6839c631 added add_modesyntax;
wenzelm
parents: 19618
diff changeset
   142
fun restore_mode (Syntax {mode, ...}) = set_mode mode;
e3ec6839c631 added add_modesyntax;
wenzelm
parents: 19618
diff changeset
   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
e3ec6839c631 added add_modesyntax;
wenzelm
parents: 19618
diff changeset
   146
18997
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   147
end;