src/Pure/Syntax/mixfix.ML
author wenzelm
Sun, 22 Sep 2024 15:46:19 +0200
changeset 80920 bbe2c56fe255
parent 80911 8ad5e6df050b
permissions -rw-r--r--
more uniform treatment of Markup.notation and Markup.expression: manage kinds via context;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Syntax/mixfix.ML
551
4c139c37dbaf minor cleanings;
wenzelm
parents: 472
diff changeset
     2
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
     3
18719
dca3ae4f6dd6 moved pure syntax to Syntax/syntax.ML and pure_thy.ML;
wenzelm
parents: 18673
diff changeset
     4
Mixfix declarations, infixes, binders.
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
     5
*)
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
     6
42287
d98eb048a2e4 discontinued special treatment of structure Mixfix;
wenzelm
parents: 42284
diff changeset
     7
signature BASIC_MIXFIX =
2199
bcb360f80dac added Infixl/rName: specify infix name independently from syntax;
wenzelm
parents: 1952
diff changeset
     8
sig
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
     9
  datatype mixfix =
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
    10
    NoSyn |
62752
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    11
    Mixfix of Input.source * int list * int * Position.range |
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    12
    Infix of Input.source * int * Position.range |
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    13
    Infixl of Input.source * int * Position.range |
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    14
    Infixr of Input.source * int * Position.range |
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    15
    Binder of Input.source * int * int * Position.range |
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    16
    Structure of Position.range
2199
bcb360f80dac added Infixl/rName: specify infix name independently from syntax;
wenzelm
parents: 1952
diff changeset
    17
end;
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
    18
42287
d98eb048a2e4 discontinued special treatment of structure Mixfix;
wenzelm
parents: 42284
diff changeset
    19
signature MIXFIX =
2199
bcb360f80dac added Infixl/rName: specify infix name independently from syntax;
wenzelm
parents: 1952
diff changeset
    20
sig
42287
d98eb048a2e4 discontinued special treatment of structure Mixfix;
wenzelm
parents: 42284
diff changeset
    21
  include BASIC_MIXFIX
62761
5c672b22dcc2 clarified simple mixfix;
wenzelm
parents: 62760
diff changeset
    22
  val mixfix: string -> mixfix
62752
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    23
  val is_empty: mixfix -> bool
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    24
  val equal: mixfix * mixfix -> bool
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    25
  val range_of: mixfix -> Position.range
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    26
  val pos_of: mixfix -> Position.T
62759
d16b2ec535ba more operations;
wenzelm
parents: 62753
diff changeset
    27
  val reset_pos: mixfix -> mixfix
19271
967e6c2578f2 turned string_of_mixfix into pretty_mixfix;
wenzelm
parents: 19020
diff changeset
    28
  val pretty_mixfix: mixfix -> Pretty.T
80897
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
    29
  val mixfix_args: Proof.context -> mixfix -> int
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
    30
  val default_constraint: Proof.context -> mixfix -> typ
35412
b8dead547d9e more uniform treatment of syntax for types vs. consts;
wenzelm
parents: 35390
diff changeset
    31
  val make_type: int -> typ
42287
d98eb048a2e4 discontinued special treatment of structure Mixfix;
wenzelm
parents: 42284
diff changeset
    32
  val binder_name: string -> string
80897
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
    33
  val syn_ext_types: Proof.context -> (string * typ * mixfix) list -> Syntax_Ext.syn_ext
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
    34
  val syn_ext_consts: Proof.context -> string list -> (string * typ * mixfix) list ->
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
    35
    Syntax_Ext.syn_ext
2199
bcb360f80dac added Infixl/rName: specify infix name independently from syntax;
wenzelm
parents: 1952
diff changeset
    36
end;
bcb360f80dac added Infixl/rName: specify infix name independently from syntax;
wenzelm
parents: 1952
diff changeset
    37
15751
65e4790c7914 identify binder translations only once (admits remove);
wenzelm
parents: 15570
diff changeset
    38
structure Mixfix: MIXFIX =
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
    39
struct
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
    40
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
    41
(** mixfix declarations **)
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
    42
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
    43
datatype mixfix =
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
    44
  NoSyn |
62752
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    45
  Mixfix of Input.source * int list * int * Position.range |
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    46
  Infix of Input.source * int * Position.range |
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    47
  Infixl of Input.source * int * Position.range |
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    48
  Infixr of Input.source * int * Position.range |
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    49
  Binder of Input.source * int * int * Position.range |
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    50
  Structure of Position.range;
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    51
62761
5c672b22dcc2 clarified simple mixfix;
wenzelm
parents: 62760
diff changeset
    52
fun mixfix s = Mixfix (Input.string s, [], 1000, Position.no_range);
5c672b22dcc2 clarified simple mixfix;
wenzelm
parents: 62760
diff changeset
    53
62752
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    54
fun is_empty NoSyn = true
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    55
  | is_empty _ = false;
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    56
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    57
fun equal (NoSyn, NoSyn) = true
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    58
  | equal (Mixfix (sy, ps, p, _), Mixfix (sy', ps', p', _)) =
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    59
      Input.equal_content (sy, sy') andalso ps = ps' andalso p = p'
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    60
  | equal (Infix (sy, p, _), Infix (sy', p', _)) = Input.equal_content (sy, sy') andalso p = p'
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    61
  | equal (Infixl (sy, p, _), Infixl (sy', p', _)) = Input.equal_content (sy, sy') andalso p = p'
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    62
  | equal (Infixr (sy, p, _), Infixr (sy', p', _)) = Input.equal_content (sy, sy') andalso p = p'
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    63
  | equal (Binder (sy, p, q, _), Binder (sy', p', q', _)) =
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    64
      Input.equal_content (sy, sy') andalso p = p' andalso q = q'
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    65
  | equal (Structure _, Structure _) = true
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    66
  | equal _ = false;
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    67
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    68
fun range_of NoSyn = Position.no_range
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    69
  | range_of (Mixfix (_, _, _, range)) = range
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    70
  | range_of (Infix (_, _, range)) = range
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    71
  | range_of (Infixl (_, _, range)) = range
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    72
  | range_of (Infixr (_, _, range)) = range
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    73
  | range_of (Binder (_, _, _, range)) = range
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    74
  | range_of (Structure range) = range;
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
    75
62797
e08c44eed27f tuned signature;
wenzelm
parents: 62796
diff changeset
    76
val pos_of = Position.range_position o range_of;
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
    77
62759
d16b2ec535ba more operations;
wenzelm
parents: 62753
diff changeset
    78
fun reset_pos NoSyn = NoSyn
d16b2ec535ba more operations;
wenzelm
parents: 62753
diff changeset
    79
  | reset_pos (Mixfix (sy, ps, p, _)) = Mixfix (Input.reset_pos sy, ps, p, Position.no_range)
d16b2ec535ba more operations;
wenzelm
parents: 62753
diff changeset
    80
  | reset_pos (Infix (sy, p, _)) = Infix (Input.reset_pos sy, p, Position.no_range)
d16b2ec535ba more operations;
wenzelm
parents: 62753
diff changeset
    81
  | reset_pos (Infixl (sy, p, _)) = Infixl (Input.reset_pos sy, p, Position.no_range)
d16b2ec535ba more operations;
wenzelm
parents: 62753
diff changeset
    82
  | reset_pos (Infixr (sy, p, _)) = Infixr (Input.reset_pos sy, p, Position.no_range)
d16b2ec535ba more operations;
wenzelm
parents: 62753
diff changeset
    83
  | reset_pos (Binder (sy, p, q, _)) = Binder (Input.reset_pos sy, p, q, Position.no_range)
d16b2ec535ba more operations;
wenzelm
parents: 62753
diff changeset
    84
  | reset_pos (Structure _) = Structure Position.no_range;
d16b2ec535ba more operations;
wenzelm
parents: 62753
diff changeset
    85
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
    86
19271
967e6c2578f2 turned string_of_mixfix into pretty_mixfix;
wenzelm
parents: 19020
diff changeset
    87
(* pretty_mixfix *)
967e6c2578f2 turned string_of_mixfix into pretty_mixfix;
wenzelm
parents: 19020
diff changeset
    88
967e6c2578f2 turned string_of_mixfix into pretty_mixfix;
wenzelm
parents: 19020
diff changeset
    89
local
11920
6833cadb4062 added string_of_mixfix;
wenzelm
parents: 11651
diff changeset
    90
69583
b0568a9dd160 tuned output;
wenzelm
parents: 69582
diff changeset
    91
val template = Pretty.cartouche o Pretty.str o #1 o Input.source_content;
55763
4b3907cb5654 tuned signature;
wenzelm
parents: 52143
diff changeset
    92
val keyword = Pretty.keyword2;
19271
967e6c2578f2 turned string_of_mixfix into pretty_mixfix;
wenzelm
parents: 19020
diff changeset
    93
val parens = Pretty.enclose "(" ")";
967e6c2578f2 turned string_of_mixfix into pretty_mixfix;
wenzelm
parents: 19020
diff changeset
    94
val brackets = Pretty.enclose "[" "]";
967e6c2578f2 turned string_of_mixfix into pretty_mixfix;
wenzelm
parents: 19020
diff changeset
    95
val int = Pretty.str o string_of_int;
967e6c2578f2 turned string_of_mixfix into pretty_mixfix;
wenzelm
parents: 19020
diff changeset
    96
967e6c2578f2 turned string_of_mixfix into pretty_mixfix;
wenzelm
parents: 19020
diff changeset
    97
in
11920
6833cadb4062 added string_of_mixfix;
wenzelm
parents: 11651
diff changeset
    98
19271
967e6c2578f2 turned string_of_mixfix into pretty_mixfix;
wenzelm
parents: 19020
diff changeset
    99
fun pretty_mixfix NoSyn = Pretty.str ""
62752
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
   100
  | pretty_mixfix (Mixfix (s, ps, p, _)) =
68273
53788963c4dc pretty-print according to defaults of input syntax;
wenzelm
parents: 68271
diff changeset
   101
      parens
53788963c4dc pretty-print according to defaults of input syntax;
wenzelm
parents: 68271
diff changeset
   102
        (Pretty.breaks
69582
wenzelm
parents: 69349
diff changeset
   103
          (template s ::
68273
53788963c4dc pretty-print according to defaults of input syntax;
wenzelm
parents: 68271
diff changeset
   104
            (if null ps then [] else [brackets (Pretty.commas (map int ps))]) @
53788963c4dc pretty-print according to defaults of input syntax;
wenzelm
parents: 68271
diff changeset
   105
            (if p = 1000 then [] else [int p])))
69582
wenzelm
parents: 69349
diff changeset
   106
  | pretty_mixfix (Infix (s, p, _)) = parens (Pretty.breaks [keyword "infix", template s, int p])
wenzelm
parents: 69349
diff changeset
   107
  | pretty_mixfix (Infixl (s, p, _)) = parens (Pretty.breaks [keyword "infixl", template s, int p])
wenzelm
parents: 69349
diff changeset
   108
  | pretty_mixfix (Infixr (s, p, _)) = parens (Pretty.breaks [keyword "infixr", template s, int p])
62752
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
   109
  | pretty_mixfix (Binder (s, p1, p2, _)) =
68273
53788963c4dc pretty-print according to defaults of input syntax;
wenzelm
parents: 68271
diff changeset
   110
      parens
53788963c4dc pretty-print according to defaults of input syntax;
wenzelm
parents: 68271
diff changeset
   111
        (Pretty.breaks
69582
wenzelm
parents: 69349
diff changeset
   112
          ([keyword "binder", template s] @
wenzelm
parents: 69349
diff changeset
   113
            (if p1 = p2 then [] else [brackets [int p1]]) @ [int p2]))
62752
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
   114
  | pretty_mixfix (Structure _) = parens [keyword "structure"];
19271
967e6c2578f2 turned string_of_mixfix into pretty_mixfix;
wenzelm
parents: 19020
diff changeset
   115
967e6c2578f2 turned string_of_mixfix into pretty_mixfix;
wenzelm
parents: 19020
diff changeset
   116
end;
11920
6833cadb4062 added string_of_mixfix;
wenzelm
parents: 11651
diff changeset
   117
6833cadb4062 added string_of_mixfix;
wenzelm
parents: 11651
diff changeset
   118
12531
adc39b100e9a improved mixfix_args;
wenzelm
parents: 12512
diff changeset
   119
(* syntax specifications *)
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
   120
80897
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
   121
fun mixfix_args ctxt =
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
   122
  fn NoSyn => 0
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
   123
   | Mixfix (sy, _, _, _) => Syntax_Ext.mixfix_args ctxt sy
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
   124
   | Infix (sy, _, _) => 2 + Syntax_Ext.mixfix_args ctxt sy
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
   125
   | Infixl (sy, _, _) => 2 + Syntax_Ext.mixfix_args ctxt sy
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
   126
   | Infixr (sy, _, _) => 2 + Syntax_Ext.mixfix_args ctxt sy
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
   127
   | Binder _ => 1
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
   128
   | Structure _ => 0;
4053
c88d0d5ae806 added mixfix_args;
wenzelm
parents: 3829
diff changeset
   129
c88d0d5ae806 added mixfix_args;
wenzelm
parents: 3829
diff changeset
   130
62959
19c2a58623ed back to static Mixfix.default_constraint without any special tricks (reverting e6443edaebff);
wenzelm
parents: 62797
diff changeset
   131
(* default type constraint *)
19c2a58623ed back to static Mixfix.default_constraint without any special tricks (reverting e6443edaebff);
wenzelm
parents: 62797
diff changeset
   132
80897
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
   133
fun default_constraint _ (Binder _) = (dummyT --> dummyT) --> dummyT
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
   134
  | default_constraint ctxt mx = replicate (mixfix_args ctxt mx) dummyT ---> dummyT;
62959
19c2a58623ed back to static Mixfix.default_constraint without any special tricks (reverting e6443edaebff);
wenzelm
parents: 62797
diff changeset
   135
19c2a58623ed back to static Mixfix.default_constraint without any special tricks (reverting e6443edaebff);
wenzelm
parents: 62797
diff changeset
   136
69586
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   137
(* mixfix template *)
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   138
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   139
fun mixfix_template (Mixfix (sy, _, _, _)) = SOME sy
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   140
  | mixfix_template (Infix (sy, _, _)) = SOME sy
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   141
  | mixfix_template (Infixl (sy, _, _)) = SOME sy
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   142
  | mixfix_template (Infixr (sy, _, _)) = SOME sy
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   143
  | mixfix_template (Binder (sy, _, _, _)) = SOME sy
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   144
  | mixfix_template _ = NONE;
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   145
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   146
fun mixfix_template_reports mx =
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   147
  if Options.default_bool "update_mixfix_cartouches" then
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   148
    (case mixfix_template mx of
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   149
      SOME sy => [((Input.pos_of sy, Markup.update), cartouche (#1 (Input.source_content sy)))]
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   150
    | NONE => [])
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   151
  else [];
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   152
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   153
80911
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   154
(* infix notation *)
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   155
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   156
val prefix_bg = Symbol_Pos.explode0 "'(";
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   157
val prefix_en = Symbol_Pos.explode0 "')";
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   158
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   159
val infix_bg = Symbol_Pos.explode0 "(";
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   160
val infix_arg1 = Symbol_Pos.explode0 "_ ";
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   161
val infix_arg2 = Symbol_Pos.explode0 "/ _)";
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   162
80920
bbe2c56fe255 more uniform treatment of Markup.notation and Markup.expression: manage kinds via context;
wenzelm
parents: 80911
diff changeset
   163
fun infix_block ctxt =
bbe2c56fe255 more uniform treatment of Markup.notation and Markup.expression: manage kinds via context;
wenzelm
parents: 80911
diff changeset
   164
  Syntax_Ext.mfix_name ctxt #>
bbe2c56fe255 more uniform treatment of Markup.notation and Markup.expression: manage kinds via context;
wenzelm
parents: 80911
diff changeset
   165
  Syntax_Ext.block_annotation 0 Markup_Kind.markup_infix #>
bbe2c56fe255 more uniform treatment of Markup.notation and Markup.expression: manage kinds via context;
wenzelm
parents: 80911
diff changeset
   166
  Symbol_Pos.explode0 #>
bbe2c56fe255 more uniform treatment of Markup.notation and Markup.expression: manage kinds via context;
wenzelm
parents: 80911
diff changeset
   167
  append infix_bg;
80911
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   168
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   169
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   170
(* binder notation *)
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   171
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   172
val binder_stamp = stamp ();
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   173
val binder_name = suffix "_binder";
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   174
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   175
val binder_bg = Symbol_Pos.explode0 "(";
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   176
val binder_en = Symbol_Pos.explode0 "_./ _)";
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   177
80920
bbe2c56fe255 more uniform treatment of Markup.notation and Markup.expression: manage kinds via context;
wenzelm
parents: 80911
diff changeset
   178
fun binder_block ctxt =
bbe2c56fe255 more uniform treatment of Markup.notation and Markup.expression: manage kinds via context;
wenzelm
parents: 80911
diff changeset
   179
  Syntax_Ext.mfix_name ctxt #>
bbe2c56fe255 more uniform treatment of Markup.notation and Markup.expression: manage kinds via context;
wenzelm
parents: 80911
diff changeset
   180
  Syntax_Ext.block_annotation 3 Markup_Kind.markup_binder #>
bbe2c56fe255 more uniform treatment of Markup.notation and Markup.expression: manage kinds via context;
wenzelm
parents: 80911
diff changeset
   181
  Symbol_Pos.explode0 #>
bbe2c56fe255 more uniform treatment of Markup.notation and Markup.expression: manage kinds via context;
wenzelm
parents: 80911
diff changeset
   182
  append binder_bg;
80911
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   183
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   184
fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   185
      [Type ("idts", []), ty2] ---> ty3
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   186
  | binder_typ c _ = error ("Bad type of binder: " ^ quote c);
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   187
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   188
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
   189
(* syn_ext_types *)
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
   190
42293
6cca0343ea48 renamed sprop "prop#" to "prop'" -- proper identifier;
wenzelm
parents: 42288
diff changeset
   191
val typeT = Type ("type", []);
6cca0343ea48 renamed sprop "prop#" to "prop'" -- proper identifier;
wenzelm
parents: 42288
diff changeset
   192
fun make_type n = replicate n typeT ---> typeT;
35412
b8dead547d9e more uniform treatment of syntax for types vs. consts;
wenzelm
parents: 35390
diff changeset
   193
80897
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
   194
fun syn_ext_types ctxt type_decls =
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
   195
  let
62796
wenzelm
parents: 62773
diff changeset
   196
    fun mk_infix sy ty t p1 p2 p3 pos =
80911
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   197
      let
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   198
        val ss = Input.source_explode sy;
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   199
        val syms = infix_block ctxt ss @ infix_arg1 @ ss @ infix_arg2;
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   200
      in Syntax_Ext.Mfix (syms, ty, t, [p1, p2], p3, pos) end;
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
   201
80906
wenzelm
parents: 80899
diff changeset
   202
    fun mfix_of (t, ty, mx) =
wenzelm
parents: 80899
diff changeset
   203
      (case mx of
wenzelm
parents: 80899
diff changeset
   204
        NoSyn => NONE
wenzelm
parents: 80899
diff changeset
   205
      | Mixfix (sy, ps, p, _) =>
62796
wenzelm
parents: 62773
diff changeset
   206
          SOME (Syntax_Ext.Mfix (Input.source_explode sy, ty, t, ps, p, pos_of mx))
80906
wenzelm
parents: 80899
diff changeset
   207
      | Infix (sy, p, _) => SOME (mk_infix sy ty t (p + 1) (p + 1) p (pos_of mx))
wenzelm
parents: 80899
diff changeset
   208
      | Infixl (sy, p, _) => SOME (mk_infix sy ty t p (p + 1) p (pos_of mx))
wenzelm
parents: 80899
diff changeset
   209
      | Infixr (sy, p, _) => SOME (mk_infix sy ty t (p + 1) p p (pos_of mx))
wenzelm
parents: 80899
diff changeset
   210
      | _ => error ("Bad mixfix declaration for " ^ quote t ^ Position.here (pos_of mx)));
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
   211
62753
76b814ccce61 tuned messages -- more positions;
wenzelm
parents: 62752
diff changeset
   212
    fun check_args (_, ty, mx) (SOME (Syntax_Ext.Mfix (sy, _, _, _, _, _))) =
80897
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
   213
          if length (Term.binder_types ty) = Syntax_Ext.mfix_args ctxt sy then ()
62752
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
   214
          else
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
   215
            error ("Bad number of type constructor arguments in mixfix annotation" ^
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 59841
diff changeset
   216
              Position.here (pos_of mx))
35351
7425aece4ee3 allow general mixfix syntax for type constructors;
wenzelm
parents: 35130
diff changeset
   217
      | check_args _ NONE = ();
7425aece4ee3 allow general mixfix syntax for type constructors;
wenzelm
parents: 35130
diff changeset
   218
80899
51c338103975 proper Context_Position.report, following 5328d67ec647;
wenzelm
parents: 80897
diff changeset
   219
    val _ = Context_Position.reports_text ctxt (maps (mixfix_template_reports o #3) type_decls);
69586
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   220
35351
7425aece4ee3 allow general mixfix syntax for type constructors;
wenzelm
parents: 35130
diff changeset
   221
    val mfix = map mfix_of type_decls;
7425aece4ee3 allow general mixfix syntax for type constructors;
wenzelm
parents: 35130
diff changeset
   222
    val _ = map2 check_args type_decls mfix;
80748
43c4817375bf support for syntax const dependencies, with minimal integrity checks;
wenzelm
parents: 69586
diff changeset
   223
    val consts = map (fn (t, _, _) => (t, [])) type_decls;
80897
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
   224
  in Syntax_Ext.syn_ext ctxt [] (map_filter I mfix) consts ([], [], [], []) ([], []) end;
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
   225
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
   226
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
   227
(* syn_ext_consts *)
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
   228
80897
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
   229
fun syn_ext_consts ctxt logical_types const_decls =
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
   230
  let
62796
wenzelm
parents: 62773
diff changeset
   231
    fun mk_infix sy ty c p1 p2 p3 pos =
80911
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   232
      let
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   233
        val ss0 = Input.source_explode (Input.reset_pos sy);
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   234
        val ss = Input.source_explode sy;
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   235
        val syms = infix_block ctxt ss @ infix_arg1 @ ss @ infix_arg2;
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   236
      in
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   237
        [Syntax_Ext.Mfix (prefix_bg @ ss0 @ prefix_en, ty, c, [], 1000, Position.none),
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   238
         Syntax_Ext.Mfix (syms, ty, c, [p1, p2], p3, pos)]
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   239
      end;
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
   240
80906
wenzelm
parents: 80899
diff changeset
   241
    fun mfix_of (c, ty, mx) =
wenzelm
parents: 80899
diff changeset
   242
      (case mx of
wenzelm
parents: 80899
diff changeset
   243
        NoSyn => []
80911
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   244
      | Mixfix (sy, ps, p, _) => [Syntax_Ext.Mfix (Input.source_explode sy, ty, c, ps, p, pos_of mx)]
80906
wenzelm
parents: 80899
diff changeset
   245
      | Infix (sy, p, _) => mk_infix sy ty c (p + 1) (p + 1) p (pos_of mx)
wenzelm
parents: 80899
diff changeset
   246
      | Infixl (sy, p, _) => mk_infix sy ty c p (p + 1) p (pos_of mx)
wenzelm
parents: 80899
diff changeset
   247
      | Infixr (sy, p, _) => mk_infix sy ty c (p + 1) p p (pos_of mx)
wenzelm
parents: 80899
diff changeset
   248
      | Binder (sy, p, q, _) =>
80911
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   249
          let
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   250
            val ss = Input.source_explode sy;
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   251
            val syms = binder_block ctxt ss @ ss @ binder_en;
8ad5e6df050b block markup for specific notation, notably infix and binder;
wenzelm
parents: 80906
diff changeset
   252
          in [Syntax_Ext.Mfix (syms, binder_typ c ty, binder_name c, [0, p], q, pos_of mx)] end
80906
wenzelm
parents: 80899
diff changeset
   253
      | _ => error ("Bad mixfix declaration for " ^ quote c ^ Position.here (pos_of mx)));
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
   254
21534
68f805e9db0b Binder: syntax const is determined by binder_name, not its syntax;
wenzelm
parents: 20892
diff changeset
   255
    fun binder (c, _, Binder _) = SOME (binder_name c, c)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15421
diff changeset
   256
      | binder _ = NONE;
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
   257
80899
51c338103975 proper Context_Position.report, following 5328d67ec647;
wenzelm
parents: 80897
diff changeset
   258
    val _ = Context_Position.reports_text ctxt (maps (mixfix_template_reports o #3) const_decls);
69586
9171d1ce5a35 support for "isabelle update -u mixfix_cartouches";
wenzelm
parents: 69584
diff changeset
   259
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19467
diff changeset
   260
    val mfix = maps mfix_of const_decls;
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19467
diff changeset
   261
    val binders = map_filter binder const_decls;
35129
ed24ba6f69aa discontinued unnamed infix syntax;
wenzelm
parents: 32786
diff changeset
   262
    val binder_trs = binders
52143
36ffe23b25f8 syntax translations always depend on context;
wenzelm
parents: 42298
diff changeset
   263
      |> map (Syntax_Ext.stamp_trfun binder_stamp o Syntax_Trans.mk_binder_tr);
35129
ed24ba6f69aa discontinued unnamed infix syntax;
wenzelm
parents: 32786
diff changeset
   264
    val binder_trs' = binders
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42287
diff changeset
   265
      |> map (Syntax_Ext.stamp_trfun binder_stamp o
52143
36ffe23b25f8 syntax translations always depend on context;
wenzelm
parents: 42298
diff changeset
   266
          apsnd Syntax_Trans.non_typed_tr' o Syntax_Trans.mk_binder_tr' o swap);
42298
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   267
80748
43c4817375bf support for syntax const dependencies, with minimal integrity checks;
wenzelm
parents: 69586
diff changeset
   268
    val consts =
43c4817375bf support for syntax const dependencies, with minimal integrity checks;
wenzelm
parents: 69586
diff changeset
   269
      map (fn (c, b) => (c, [b])) binders @
43c4817375bf support for syntax const dependencies, with minimal integrity checks;
wenzelm
parents: 69586
diff changeset
   270
      map (fn (c, _, _) => (c, [])) const_decls;
80897
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
   271
  in
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
   272
    Syntax_Ext.syn_ext ctxt logical_types mfix consts ([], binder_trs, binder_trs', []) ([], [])
5328d67ec647 more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents: 80748
diff changeset
   273
  end;
384
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
   274
a8d204d8071d support for new style mixfix annotations;
wenzelm
parents:
diff changeset
   275
end;
42287
d98eb048a2e4 discontinued special treatment of structure Mixfix;
wenzelm
parents: 42284
diff changeset
   276
d98eb048a2e4 discontinued special treatment of structure Mixfix;
wenzelm
parents: 42284
diff changeset
   277
structure Basic_Mixfix: BASIC_MIXFIX = Mixfix;
d98eb048a2e4 discontinued special treatment of structure Mixfix;
wenzelm
parents: 42284
diff changeset
   278
open Basic_Mixfix;